]> git.lyx.org Git - lyx.git/blob - src/textcursor.C
move some selection related stuff over to textcursor.C
[lyx.git] / src / textcursor.C
1
2 #include "textcursor.h"
3
4 bool TextCursor::setSelection()
5 {
6         bool const lsel = selection.set();
7
8         if (!selection.set()) {
9                 last_sel_cursor = selection.cursor;
10                 selection.start = selection.cursor;
11                 selection.end = selection.cursor;
12         }
13
14         selection.set(true);
15
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;
22         } else {
23                 toggle_end_cursor = cursor;
24                 toggle_cursor = last_sel_cursor;
25         }
26
27         last_sel_cursor = cursor;
28
29         // and now the whole selection
30
31         if (selection.cursor.par() == cursor.par())
32                 if (selection.cursor.pos() < cursor.pos()) {
33                         selection.end = cursor;
34                         selection.start = selection.cursor;
35                 } else {
36                         selection.end = selection.cursor;
37                         selection.start = cursor;
38                 }
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;
44         }
45         else {
46                 selection.end = selection.cursor;
47                 selection.start = cursor;
48         }
49
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())
53                 selection.set(false);
54         
55         return lsel;
56 }
57
58
59 void TextCursor::clearSelection()
60 {
61         selection.set(false);
62         selection.mark(false);
63         last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
64 }
65
66
67 string const TextCursor::selectionAsString(Buffer const * buffer,
68                                         bool label) const
69 {
70         if (!selection.set())
71                 return string();
72
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();
78
79         if (startpit == endpit)
80                 return startpit->asString(buffer, startpos, endpos, label);
81
82         // First paragraph in selection
83         string result =
84                 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
85
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";
90
91         // Last paragraph in selection
92         result += endpit->asString(buffer, 0, endpos, label);
93
94         return result;
95 }