]> git.lyx.org Git - lyx.git/blob - src/textcursor.C
Bug: 1025
[lyx.git] / src / textcursor.C
1 /**
2  * \file textcursor.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "textcursor.h"
14 #include "paragraph.h"
15
16 #include <string>
17
18 using std::string;
19
20
21 void TextCursor::setSelection()
22 {
23         if (!selection.set()) {
24                 selection.start = selection.cursor;
25                 selection.end = selection.cursor;
26         }
27
28         selection.set(true);
29
30         // 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;
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         {
54                 selection.set(false);
55         }
56 }
57
58
59 void TextCursor::clearSelection()
60 {
61         selection.set(false);
62         selection.mark(false);
63         selection.end    = cursor;
64         selection.start  = cursor;
65         selection.cursor = cursor;
66 }
67
68
69 string const TextCursor::selectionAsString(Buffer const & buffer,
70                                         bool label) const
71 {
72         if (!selection.set())
73                 return string();
74
75         // should be const ...
76         ParagraphList::iterator startpit = selection.start.par();
77         ParagraphList::iterator endpit = selection.end.par();
78         size_t const startpos = selection.start.pos();
79         size_t const endpos = selection.end.pos();
80
81         if (startpit == endpit)
82                 return startpit->asString(buffer, startpos, endpos, label);
83
84         // First paragraph in selection
85         string result =
86                 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
87
88         // The paragraphs in between (if any)
89         ParagraphList::iterator pit = startpit;
90         for (++pit; pit != endpit; ++pit)
91                 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
92
93         // Last paragraph in selection
94         result += endpit->asString(buffer, 0, endpos, label);
95
96         return result;
97 }