From 3390fc33d7482c5dce6af70b5863dd71aadb9c02 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Fri, 27 Jun 2003 13:13:04 +0000 Subject: [PATCH] move some selection related stuff over to textcursor.C git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7218 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Makefile.am | 2 + src/lyxtext.h | 2 - src/text2.C | 87 ++------------------------------------------ src/textcursor.C | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ src/textcursor.h | 7 ++++ 5 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 src/textcursor.C diff --git a/src/Makefile.am b/src/Makefile.am index cdafd15d4c..4b30a5c6e9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -231,6 +231,8 @@ lyx_SOURCES = \ text.C \ text2.C \ text3.C \ + textcursor.C \ + textcursor.h \ text_funcs.C \ text_funcs.h \ toc.C \ diff --git a/src/lyxtext.h b/src/lyxtext.h index 9395bb8490..fa5228e081 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -277,8 +277,6 @@ public: void setSelection(); /// void clearSelection(); - /// - string const selectionAsString(Buffer const *, bool label) const; /// select the word we need depending on word_location void getWord(LyXCursor & from, LyXCursor & to, diff --git a/src/text2.C b/src/text2.C index bbfcad9eca..1b805a5140 100644 --- a/src/text2.C +++ b/src/text2.C @@ -530,7 +530,6 @@ bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only) LyXCursor tmpcursor; setCursor(tmpcursor, start, 0); - //redoParagraphs(tmpcursor, &(*pastend)); redoParagraphs(tmpcursor, pastend); // We need to actually move the text->cursor. I don't @@ -757,98 +756,18 @@ void LyXText::partialRebreak() // need the selection cursor: void LyXText::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); + bool const lsel = TextCursor::setSelection(); if (inset_owner && (selection.set() || lsel)) inset_owner->setUpdateStatus(bv(), InsetText::SELECTION); } -string const LyXText::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(); - pos_type const startpos(selection.start.pos()); - pos_type const endpos(selection.end.pos()); - - if (startpit == endpit) { - return startpit->asString(buffer, startpos, endpos, label); - } - - string result; - - // First paragraph in selection - result += startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n"; - - // The paragraphs in between (if any) - ParagraphList::iterator pit = boost::next(startpit); - for (; 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; -} - void LyXText::clearSelection() { - selection.set(false); - selection.mark(false); - last_sel_cursor = selection.end = selection.start = selection.cursor = cursor; + TextCursor::clearSelection(); + // reset this in the bv_owner! if (bv_owner && bv_owner->text) bv_owner->text->xsel_cache.set(false); diff --git a/src/textcursor.C b/src/textcursor.C new file mode 100644 index 0000000000..38ae302bd2 --- /dev/null +++ b/src/textcursor.C @@ -0,0 +1,95 @@ + +#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; +} diff --git a/src/textcursor.h b/src/textcursor.h index dad6348cb9..26091d5a4b 100644 --- a/src/textcursor.h +++ b/src/textcursor.h @@ -56,6 +56,13 @@ private: }; struct TextCursor { + /// returns true if selection was set previously + bool setSelection(); + /// + void clearSelection(); + /// + string const selectionAsString(Buffer const * buffer, bool label) const; + // actual cursor position LyXCursor cursor; -- 2.39.2