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