3 #include "textcursor.h"
5 bool TextCursor::setSelection()
7 bool const lsel = selection.set();
9 if (!selection.set()) {
10 last_sel_cursor = selection.cursor;
11 selection.start = selection.cursor;
12 selection.end = selection.cursor;
17 // first the toggling area
18 if (cursor.y() < last_sel_cursor.y()
19 || (cursor.y() == last_sel_cursor.y()
20 && cursor.x() < last_sel_cursor.x())) {
21 toggle_end_cursor = last_sel_cursor;
22 toggle_cursor = cursor;
24 toggle_end_cursor = cursor;
25 toggle_cursor = last_sel_cursor;
28 last_sel_cursor = cursor;
30 // and now the whole selection
32 if (selection.cursor.par() == cursor.par())
33 if (selection.cursor.pos() < cursor.pos()) {
34 selection.end = cursor;
35 selection.start = selection.cursor;
37 selection.end = selection.cursor;
38 selection.start = cursor;
40 else if (selection.cursor.y() < cursor.y() ||
41 (selection.cursor.y() == cursor.y()
42 && selection.cursor.x() < cursor.x())) {
43 selection.end = cursor;
44 selection.start = selection.cursor;
47 selection.end = selection.cursor;
48 selection.start = cursor;
51 // a selection with no contents is not a selection
52 if (selection.start.par() == selection.end.par() &&
53 selection.start.pos() == selection.end.pos())
60 void TextCursor::clearSelection()
63 selection.mark(false);
64 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
68 string const TextCursor::selectionAsString(Buffer const * buffer,
74 // should be const ...
75 ParagraphList::iterator startpit = selection.start.par();
76 ParagraphList::iterator endpit = selection.end.par();
77 size_t const startpos = selection.start.pos();
78 size_t const endpos = selection.end.pos();
80 if (startpit == endpit)
81 return startpit->asString(buffer, startpos, endpos, label);
83 // First paragraph in selection
85 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
87 // The paragraphs in between (if any)
88 ParagraphList::iterator pit = startpit;
89 for (++pit; pit != endpit; ++pit)
90 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
92 // Last paragraph in selection
93 result += endpit->asString(buffer, 0, endpos, label);