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