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