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"
21 void TextCursor::setSelection()
23 if (!selection.set()) {
24 selection.start = selection.cursor;
25 selection.end = selection.cursor;
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;
36 selection.end = selection.cursor;
37 selection.start = cursor;
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;
46 selection.end = selection.cursor;
47 selection.start = cursor;
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())
59 void TextCursor::clearSelection()
62 selection.mark(false);
63 selection.end = cursor;
64 selection.start = cursor;
65 selection.cursor = cursor;
69 string const TextCursor::selectionAsString(Buffer const & buffer,
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();
81 if (startpit == endpit)
82 return startpit->asString(buffer, startpos, endpos, label);
84 // First paragraph in selection
86 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
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";
93 // Last paragraph in selection
94 result += endpit->asString(buffer, 0, endpos, label);