2 #include "textcursor.h"
4 bool TextCursor::setSelection()
6 bool const lsel = selection.set();
8 if (!selection.set()) {
9 last_sel_cursor = selection.cursor;
10 selection.start = selection.cursor;
11 selection.end = selection.cursor;
16 // first the toggling area
17 if (cursor.y() < last_sel_cursor.y()
18 || (cursor.y() == last_sel_cursor.y()
19 && cursor.x() < last_sel_cursor.x())) {
20 toggle_end_cursor = last_sel_cursor;
21 toggle_cursor = cursor;
23 toggle_end_cursor = cursor;
24 toggle_cursor = last_sel_cursor;
27 last_sel_cursor = cursor;
29 // and now the whole selection
31 if (selection.cursor.par() == cursor.par())
32 if (selection.cursor.pos() < cursor.pos()) {
33 selection.end = cursor;
34 selection.start = selection.cursor;
36 selection.end = selection.cursor;
37 selection.start = cursor;
39 else if (selection.cursor.y() < cursor.y() ||
40 (selection.cursor.y() == cursor.y()
41 && selection.cursor.x() < cursor.x())) {
42 selection.end = cursor;
43 selection.start = selection.cursor;
46 selection.end = selection.cursor;
47 selection.start = cursor;
50 // a selection with no contents is not a selection
51 if (selection.start.par() == selection.end.par() &&
52 selection.start.pos() == selection.end.pos())
59 void TextCursor::clearSelection()
62 selection.mark(false);
63 last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
67 string const TextCursor::selectionAsString(Buffer const * buffer,
73 // should be const ...
74 ParagraphList::iterator startpit = selection.start.par();
75 ParagraphList::iterator endpit = selection.end.par();
76 size_t const startpos = selection.start.pos();
77 size_t const endpos = selection.end.pos();
79 if (startpit == endpit)
80 return startpit->asString(buffer, startpos, endpos, label);
82 // First paragraph in selection
84 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
86 // The paragraphs in between (if any)
87 ParagraphList::iterator pit = startpit;
88 for (++pit; pit != endpit; ++pit)
89 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
91 // Last paragraph in selection
92 result += endpit->asString(buffer, 0, endpos, label);