This moves most of 'setSelection', 'clearSelection' and 'selectionAsString' over to a new textcursor.C
Andre' -- Those who desire to give up Freedom in order to gain Security, will not have, nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
Index: Makefile.am =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/Makefile.am,v retrieving revision 1.177 diff -u -p -r1.177 Makefile.am --- Makefile.am 20 Jun 2003 12:46:22 -0000 1.177 +++ Makefile.am 27 Jun 2003 12:27:07 -0000 @@ -231,6 +231,8 @@ lyx_SOURCES = \ text.C \ text2.C \ text3.C \ + textcursor.C \ + textcursor.h \ text_funcs.C \ text_funcs.h \ toc.C \ Index: lyxtext.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtext.h,v retrieving revision 1.180 diff -u -p -r1.180 lyxtext.h --- lyxtext.h 27 Jun 2003 11:53:40 -0000 1.180 +++ lyxtext.h 27 Jun 2003 12:27:07 -0000 @@ -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, Index: text2.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text2.C,v retrieving revision 1.375 diff -u -p -r1.375 text2.C --- text2.C 27 Jun 2003 09:44:25 -0000 1.375 +++ text2.C 27 Jun 2003 12:27:07 -0000 @@ -530,7 +530,6 @@ bool LyXText::changeDepth(bv_funcs::DEPT 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); Index: textcursor.C =================================================================== RCS file: textcursor.C diff -N textcursor.C --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ textcursor.C 27 Jun 2003 12:27:07 -0000 @@ -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; +} Index: textcursor.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/textcursor.h,v retrieving revision 1.1 diff -u -p -r1.1 textcursor.h --- textcursor.h 27 Jun 2003 11:53:40 -0000 1.1 +++ textcursor.h 27 Jun 2003 12:27:07 -0000 @@ -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;