Lars Gullik BjÃnnes wrote: > [EMAIL PROTECTED] (Lars Gullik BjÃnnes) writes: > > | And yes, I found this better. > > And you should just apply this now...
Done, thanks. Next step. iterator.[Ch]: I need this lockPath stuff. It is needed to place a cursor given some position in the document tree. In the old scheme, inset were locked sequentially following the cursor movement. Implemented in ParIterator for simplicity, because we have random access to the stack (PosIterator has a real stack) Now we want to do all text operations without caring about locking and cursors, and then on a final step lock all the branch in the tree (and place the cursor). It is ugly, but temporary anyway (will die with the locking). It is a const method. PosIterator.[Ch]: A pair of stupid helpers (I promess to later study how to make PosIterator a forward_iterator and remove these) bufferview_funcs.[Ch]: a simple helper to put a selection in an arbitrary point in the tree (using lockPath) What do you think? Alfredo
? PosIterator.C-save ? PosIterator.h-save ? all.diff ? bfs.cpp ? files ? save ? frontends/screen.C-save Index: PosIterator.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/PosIterator.C,v retrieving revision 1.1 diff -u -p -u -r1.1 PosIterator.C --- PosIterator.C 2 Nov 2003 17:56:25 -0000 1.1 +++ PosIterator.C 2 Nov 2003 18:12:49 -0000 @@ -147,3 +147,21 @@ PosIterator::PosIterator(BufferView & bv operator=(par.asPosIterator(pos)); } + + +int distance(PosIterator const & cur, PosIterator const & end) +{ + PosIterator p = cur; + int count = 0; + for (; p != end; ++p, ++count); + return count; +} + + +void advance(PosIterator & cur, int howmuch) +{ + for (int i = 0; i < howmuch; ++i) + ++cur; + for (int i = 0; i > howmuch; --i) + --cur; +} Index: PosIterator.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/PosIterator.h,v retrieving revision 1.1 diff -u -p -u -r1.1 PosIterator.h --- PosIterator.h 2 Nov 2003 17:56:25 -0000 1.1 +++ PosIterator.h 2 Nov 2003 18:12:49 -0000 @@ -62,7 +62,10 @@ private: }; bool operator!=(PosIterator const &, PosIterator const &); +bool operator==(PosIterator const &, PosIterator const &); +int distance(PosIterator const &, PosIterator const &); +void advance(PosIterator &, int); #endif Index: bufferview_funcs.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferview_funcs.C,v retrieving revision 1.118 diff -u -p -u -r1.118 bufferview_funcs.C --- bufferview_funcs.C 24 Oct 2003 09:45:02 -0000 1.118 +++ bufferview_funcs.C 2 Nov 2003 18:12:50 -0000 @@ -26,6 +26,8 @@ #include "lyxrow.h" #include "paragraph.h" #include "ParagraphParameters.h" +#include "PosIterator.h" +#include "iterators.h" #include "frontends/Alert.h" #include "frontends/LyXView.h" @@ -421,5 +423,35 @@ void replaceSelection(LyXText * text) text->bv()->update(); } } + + +void put_selection_at(BufferView * bv, PosIterator const & cur, + int length, bool backwards) +{ + ParIterator par = bv->buffer()->par_iterator_begin(); + for (; par.pit() != cur.pit(); ++par) + ; + + bv->getLyXText()->clearSelection(); + + LyXText * text = par.text() ? par.text() : bv->text; + + par.lockPath(bv); + + text->setCursor(cur.pit(), cur.pos()); + + if (length) { + text->setSelectionRange(length); + text->setSelection(); + if (backwards) + text->cursor = text->selection.start; + } + + + bv->fitCursor(); + bv->update(); + +} + }; // namespace bv_funcs Index: bufferview_funcs.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferview_funcs.h,v retrieving revision 1.24 diff -u -p -u -r1.24 bufferview_funcs.h --- bufferview_funcs.h 6 Oct 2003 15:42:07 -0000 1.24 +++ bufferview_funcs.h 2 Nov 2003 18:12:50 -0000 @@ -20,6 +20,7 @@ class BufferView; class LyXFont; class LyXText; +class PosIterator; namespace bv_funcs { @@ -44,6 +45,11 @@ void update_and_apply_freefont(BufferVie */ void apply_freefont(BufferView * bv); + +void put_selection_at(BufferView * bv, PosIterator & cur, + int length, bool backwards); + + /// what type of depth change to make enum DEPTH_CHANGE { INC_DEPTH, @@ -88,6 +94,9 @@ extern void toggleAndShow(BufferView *, bool toggleall = true); /// replace selection with insertion extern void replaceSelection(LyXText * lt); + + + }; // namespace bv_funcs #endif Index: iterators.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/iterators.C,v retrieving revision 1.24 diff -u -p -u -r1.24 iterators.C --- iterators.C 2 Nov 2003 17:56:26 -0000 1.24 +++ iterators.C 2 Nov 2003 18:12:51 -0000 @@ -15,9 +15,15 @@ #include "paragraph.h" #include "PosIterator.h" #include "cursor.h" +#include "BufferView.h" +#include "funcrequest.h" +#include "dispatchresult.h" + #include "insets/inset.h" +#include "insets/updatableinset.h" +#include "insets/insettext.h" #include <boost/next_prior.hpp> #include <boost/optional.hpp> @@ -373,4 +379,26 @@ PosIterator ParIterator::asPosIterator(l p.stack_.push(PosIteratorItem(const_cast<ParagraphList *>(pp.plist), pp.pit, pos, 0)); return p; +} + + +void ParIterator::lockPath(BufferView * bv) const +{ + bv->insetUnlock(); + int last = size() - 1; + for (int i = 0; i < last; ++i) { + UpdatableInset * outer = dynamic_cast<UpdatableInset *>((*pimpl_->positions[i].it)->inset); + FuncRequest cmd(bv, LFUN_INSET_EDIT); + outer->dispatch(cmd); + LyXText * txt = outer->getText(*pimpl_->positions[i].index); + InsetText * inner = txt->inset_owner; + // deep vodoo magic: on a table, the edit call locks the first + // cell and further lock calls get lost there. + // We have to unlock it to then lock the correct one. + if (outer != inner) { + outer->insetUnlock(bv); + outer->lockInsetInInset(bv, inner); + inner->dispatch(FuncRequest(bv, LFUN_INSET_EDIT)); + } + } } Index: iterators.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/iterators.h,v retrieving revision 1.20 diff -u -p -u -r1.20 iterators.h --- iterators.h 2 Nov 2003 17:56:26 -0000 1.20 +++ iterators.h 2 Nov 2003 18:12:51 -0000 @@ -20,6 +20,7 @@ class LyXText; class InsetOld; class Cursor; +class BufferView; class PosIterator; @@ -59,6 +60,8 @@ public: /// friend bool operator==(ParIterator const & iter1, ParIterator const & iter2); + /// + void lockPath(BufferView *) const; /// PosIterator asPosIterator(lyx::pos_type) const;