3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "textcursor.h"
14 #include "paragraph.h"
17 void TextCursor::setSelection()
19 if (!selection.set()) {
20 selection.start = selection.cursor;
21 selection.end = selection.cursor;
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;
32 selection.end = selection.cursor;
33 selection.start = cursor;
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;
42 selection.end = selection.cursor;
43 selection.start = cursor;
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())
55 void TextCursor::clearSelection()
58 selection.mark(false);
59 selection.end = cursor;
60 selection.start = cursor;
61 selection.cursor = cursor;
65 string const TextCursor::selectionAsString(Buffer const & buffer,
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();
77 if (startpit == endpit)
78 return startpit->asString(buffer, startpos, endpos, label);
80 // First paragraph in selection
82 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
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";
89 // Last paragraph in selection
90 result += endpit->asString(buffer, 0, endpos, label);