]> git.lyx.org Git - lyx.git/blob - src/textcursor.C
remove toggleToggle
[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         last_sel_cursor = cursor;
18
19         // and now the whole selection
20
21         if (selection.cursor.par() == cursor.par())
22                 if (selection.cursor.pos() < cursor.pos()) {
23                         selection.end = cursor;
24                         selection.start = selection.cursor;
25                 } else {
26                         selection.end = selection.cursor;
27                         selection.start = cursor;
28                 }
29         else if (selection.cursor.y() < cursor.y() ||
30                  (selection.cursor.y() == cursor.y()
31                   && selection.cursor.x() < cursor.x())) {
32                 selection.end = cursor;
33                 selection.start = selection.cursor;
34         }
35         else {
36                 selection.end = selection.cursor;
37                 selection.start = cursor;
38         }
39
40         // a selection with no contents is not a selection
41         if (selection.start.par() == selection.end.par() &&
42             selection.start.pos() == selection.end.pos())
43                 selection.set(false);
44
45         return lsel;
46 }
47
48
49 void TextCursor::clearSelection()
50 {
51         selection.set(false);
52         selection.mark(false);
53         last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
54 }
55
56
57 string const TextCursor::selectionAsString(Buffer const * buffer,
58                                         bool label) const
59 {
60         if (!selection.set())
61                 return string();
62
63         // should be const ...
64         ParagraphList::iterator startpit = selection.start.par();
65         ParagraphList::iterator endpit = selection.end.par();
66         size_t const startpos = selection.start.pos();
67         size_t const endpos = selection.end.pos();
68
69         if (startpit == endpit)
70                 return startpit->asString(buffer, startpos, endpos, label);
71
72         // First paragraph in selection
73         string result =
74                 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
75
76         // The paragraphs in between (if any)
77         ParagraphList::iterator pit = startpit;
78         for (++pit; pit != endpit; ++pit)
79                 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
80
81         // Last paragraph in selection
82         result += endpit->asString(buffer, 0, endpos, label);
83
84         return result;
85 }