]> git.lyx.org Git - lyx.git/blob - src/textcursor.C
split LyXText::rowlist_ into individual Paragraph::rows_ chunks
[lyx.git] / src / textcursor.C
1 #include <config.h>
2
3 #include "textcursor.h"
4
5 void TextCursor::setSelection()
6 {
7         if (!selection.set()) {
8                 selection.start = selection.cursor;
9                 selection.end = selection.cursor;
10         }
11
12         selection.set(true);
13
14         // and now the whole selection
15         if (selection.cursor.par() == cursor.par())
16                 if (selection.cursor.pos() < cursor.pos()) {
17                         selection.end = cursor;
18                         selection.start = selection.cursor;
19                 } else {
20                         selection.end = selection.cursor;
21                         selection.start = cursor;
22                 }
23         else if (selection.cursor.y() < cursor.y() ||
24                  (selection.cursor.y() == cursor.y()
25                   && selection.cursor.x() < cursor.x())) {
26                 selection.end = cursor;
27                 selection.start = selection.cursor;
28         }
29         else {
30                 selection.end = selection.cursor;
31                 selection.start = cursor;
32         }
33
34         // a selection with no contents is not a selection
35         if (selection.start.par() == selection.end.par() &&
36             selection.start.pos() == selection.end.pos())
37         {
38                 selection.set(false);
39         }
40 }
41
42
43 void TextCursor::clearSelection()
44 {
45         selection.set(false);
46         selection.mark(false);
47         selection.end    = cursor;
48         selection.start  = cursor;
49         selection.cursor = cursor;
50 }
51
52
53 string const TextCursor::selectionAsString(Buffer const * buffer,
54                                         bool label) const
55 {
56         if (!selection.set())
57                 return string();
58
59         // should be const ...
60         ParagraphList::iterator startpit = selection.start.par();
61         ParagraphList::iterator endpit = selection.end.par();
62         size_t const startpos = selection.start.pos();
63         size_t const endpos = selection.end.pos();
64
65         if (startpit == endpit)
66                 return startpit->asString(buffer, startpos, endpos, label);
67
68         // First paragraph in selection
69         string result =
70                 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
71
72         // The paragraphs in between (if any)
73         ParagraphList::iterator pit = startpit;
74         for (++pit; pit != endpit; ++pit)
75                 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
76
77         // Last paragraph in selection
78         result += endpit->asString(buffer, 0, endpos, label);
79
80         return result;
81 }