mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-13 14:32:04 +00:00
3390fc33d7
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7218 a592a061-630c-0410-9148-cb99ea01b6c8
96 lines
2.3 KiB
C
96 lines
2.3 KiB
C
|
|
#include "textcursor.h"
|
|
|
|
bool TextCursor::setSelection()
|
|
{
|
|
bool const lsel = selection.set();
|
|
|
|
if (!selection.set()) {
|
|
last_sel_cursor = selection.cursor;
|
|
selection.start = selection.cursor;
|
|
selection.end = selection.cursor;
|
|
}
|
|
|
|
selection.set(true);
|
|
|
|
// first the toggling area
|
|
if (cursor.y() < last_sel_cursor.y()
|
|
|| (cursor.y() == last_sel_cursor.y()
|
|
&& cursor.x() < last_sel_cursor.x())) {
|
|
toggle_end_cursor = last_sel_cursor;
|
|
toggle_cursor = cursor;
|
|
} else {
|
|
toggle_end_cursor = cursor;
|
|
toggle_cursor = last_sel_cursor;
|
|
}
|
|
|
|
last_sel_cursor = cursor;
|
|
|
|
// and now the whole selection
|
|
|
|
if (selection.cursor.par() == cursor.par())
|
|
if (selection.cursor.pos() < cursor.pos()) {
|
|
selection.end = cursor;
|
|
selection.start = selection.cursor;
|
|
} else {
|
|
selection.end = selection.cursor;
|
|
selection.start = cursor;
|
|
}
|
|
else if (selection.cursor.y() < cursor.y() ||
|
|
(selection.cursor.y() == cursor.y()
|
|
&& selection.cursor.x() < cursor.x())) {
|
|
selection.end = cursor;
|
|
selection.start = selection.cursor;
|
|
}
|
|
else {
|
|
selection.end = selection.cursor;
|
|
selection.start = cursor;
|
|
}
|
|
|
|
// a selection with no contents is not a selection
|
|
if (selection.start.par() == selection.end.par() &&
|
|
selection.start.pos() == selection.end.pos())
|
|
selection.set(false);
|
|
|
|
return lsel;
|
|
}
|
|
|
|
|
|
void TextCursor::clearSelection()
|
|
{
|
|
selection.set(false);
|
|
selection.mark(false);
|
|
last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
|
|
}
|
|
|
|
|
|
string const TextCursor::selectionAsString(Buffer const * buffer,
|
|
bool label) const
|
|
{
|
|
if (!selection.set())
|
|
return string();
|
|
|
|
// should be const ...
|
|
ParagraphList::iterator startpit = selection.start.par();
|
|
ParagraphList::iterator endpit = selection.end.par();
|
|
size_t const startpos = selection.start.pos();
|
|
size_t const endpos = selection.end.pos();
|
|
|
|
if (startpit == endpit)
|
|
return startpit->asString(buffer, startpos, endpos, label);
|
|
|
|
// First paragraph in selection
|
|
string result =
|
|
startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
|
|
|
|
// The paragraphs in between (if any)
|
|
ParagraphList::iterator pit = startpit;
|
|
for (++pit; pit != endpit; ++pit)
|
|
result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
|
|
|
|
// Last paragraph in selection
|
|
result += endpit->asString(buffer, 0, endpos, label);
|
|
|
|
return result;
|
|
}
|