]> git.lyx.org Git - features.git/commitdiff
* BufferView_pimpl.C:
authorAndré Pönitz <poenitz@gmx.net>
Mon, 13 Oct 2003 10:59:31 +0000 (10:59 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Mon, 13 Oct 2003 10:59:31 +0000 (10:59 +0000)
* lyxtext.h:
* text.C:
* text2.C:
* text3.C:
* undo_funcs.[Ch]: use paroffset_type instead of
  ParagraphList::iterators to prevent multiple conversion
  (and get a more robust interface)

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7909 a592a061-630c-0410-9148-cb99ea01b6c8

src/BufferView_pimpl.C
src/ChangeLog
src/insets/insettabular.C
src/insets/insettext.C
src/lyxtext.h
src/text.C
src/text2.C
src/text3.C
src/undo_funcs.C
src/undo_funcs.h

index b7eafa9d9663223a7f89083ecc79dc97c681f35f..939d590d0508edc9dcd4c4cebe2d1a5f025ef3c5 100644 (file)
@@ -1340,7 +1340,7 @@ bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
        }
 
        // not quite sure if we want this...
-       recordUndo(bv_, Undo::ATOMIC);
+       bv_->text->recUndo(bv_->text->cursor.par());
        freezeUndo();
 
        beforeChange(bv_->text);
index 8bd297542b368483b556cabbc0f538e498aef9a7..57be76bc3aca5ac0c917200c343557cb892a0919 100644 (file)
@@ -1,3 +1,15 @@
+
+2003-10-13  André Pönitz  <poenitz@gmx.net>
+
+       * BufferView_pimpl.C:
+       * lyxtext.h:
+       * text.C:
+       * text2.C:
+       * text3.C:
+       * undo_funcs.[Ch]: use paroffset_type instead of
+         ParagraphList::iterators to prevent multiple conversion
+         (and get a more robust interface)
+
 2003-10-13  Lars Gullik Bjønnes  <larsbj@gullik.net>
 
        * lyxfunc.C (dispatch): RESULT -> dispatch_result
index 232ada36a41988e15a875d0d91b44ac17de5a06c..c9f84033f97c0e60764bb89d2a24169a573957ee 100644 (file)
@@ -635,8 +635,12 @@ dispatch_result InsetTabular::localDispatch(FuncRequest const & cmd)
 
        if (cmd.action == LFUN_INSET_EDIT) {
 
+               lyxerr << "InsetTabular::edit: " << this << " args: '"
+                       << cmd.argument << "'  first cell: "
+                       << &tabular.cell_info[0][0].inset << endl;
+
                if (!bv->lockInset(this)) {
-                       lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
+                       lyxerr << "InsetTabular::Cannot lock inset" << endl;
                        return DISPATCHED;
                }
 
@@ -2538,6 +2542,11 @@ bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
        if (cell != -1)
                return tabular.getPWidth(cell).zero();
 
+       // this is a workaround for a crash (New, Insert->Tabular,
+       // Insert->FootNote)
+       if (!owner()) 
+               return false;
+
        // well we didn't obviously find it so maybe our owner knows more
        BOOST_ASSERT(owner());
        return owner()->forceDefaultParagraphs(in);
index 6224d2099aca6f6f5dedcc2b0a94e65110445fe9..6c8d37a8577c66cb8131b6c5069e50f80800d27d 100644 (file)
@@ -1336,7 +1336,7 @@ void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
 
 
        if (text_.selection.set())
-               recordUndo(bv, Undo::ATOMIC, text_.cursorPar());
+               text_.recUndo(text_.cursor.par());
 
        if (selectall) {
                text_.cursorTop();
index 9492ca92fff647bf28584edf9fd4a831c9552229..fd18da9abe48e2a44c813b057a70ca91ca6d1a71 100644 (file)
@@ -258,6 +258,10 @@ public:
                         lyx::pos_type pos,
                         LyXFont const & font) const;
 
+       ///
+       void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
+       ///
+       void recUndo(lyx::paroffset_type first) const;
        ///
        void setCursorFromCoordinates(int x, int y);
        ///
index 16db12f50ab5caaec20052cd0d20957e137ff2fb..cf321783b4e4ad82dd77f2e86689359f1a6fb772 100644 (file)
@@ -1206,7 +1206,7 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
           && layout->labeltype != LABEL_SENSITIVE)
                return;
 
-       recordUndo(bv(), Undo::ATOMIC, cursorPar());
+       recUndo(cursor.par());
 
        // Always break behind a space
        //
@@ -1284,7 +1284,7 @@ void LyXText::redoParagraph()
 // same Paragraph one to the right and make a rebreak
 void LyXText::insertChar(char c)
 {
-       recordUndo(bv(), Undo::INSERT, cursorPar());
+       recordUndo(bv(), Undo::INSERT, ownerParagraphs(), cursor.par(), cursor.par());
 
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
@@ -1568,7 +1568,7 @@ void LyXText::acceptChange()
        if (selection.start.par() == selection.end.par()) {
                LyXCursor & startc = selection.start;
                LyXCursor & endc = selection.end;
-               recordUndo(bv(), Undo::INSERT, getPar(startc));
+               recordUndo(bv(), Undo::INSERT, ownerParagraphs(), startc.par());
                getPar(startc)->acceptChange(startc.pos(), endc.pos());
                finishUndo();
                clearSelection();
@@ -1587,7 +1587,7 @@ void LyXText::rejectChange()
        if (selection.start.par() == selection.end.par()) {
                LyXCursor & startc = selection.start;
                LyXCursor & endc = selection.end;
-               recordUndo(bv(), Undo::INSERT, getPar(startc));
+               recordUndo(bv(), Undo::INSERT, ownerParagraphs(), startc.par());
                getPar(startc)->rejectChange(startc.pos(), endc.pos());
                finishUndo();
                clearSelection();
@@ -1797,7 +1797,7 @@ void LyXText::changeCase(LyXText::TextCase action)
                setCursor(to.par(), to.pos() + 1);
        }
 
-       recordUndo(bv(), Undo::ATOMIC, getPar(from), getPar(to));
+       recordUndo(bv(), Undo::ATOMIC, ownerParagraphs(), from.par(), to.par());
 
        pos_type pos = from.pos();
        int par = from.par();
@@ -1858,11 +1858,7 @@ void LyXText::Delete()
 
        // if you had success make a backspace
        if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
-               LyXCursor tmpcursor = cursor;
-               // to make sure undo gets the right cursor position
-               cursor = old_cursor;
-               recordUndo(bv(), Undo::DELETE, cursorPar());
-               cursor = tmpcursor;
+               recordUndo(bv(), Undo::DELETE, ownerParagraphs(), old_cursor.par());
                backspace();
        }
 }
@@ -1911,11 +1907,9 @@ void LyXText::backspace()
                        }
                }
 
-               if (cursorPar() != ownerParagraphs().begin()) {
-                       recordUndo(bv(), Undo::DELETE,
-                               boost::prior(cursorPar()),
-                               cursorPar());
-               }
+               if (cursor.par() != 0)
+                       recordUndo(bv(), Undo::DELETE, ownerParagraphs(),
+                               cursor.par() - 1, cursor.par());
 
                ParagraphList::iterator tmppit = cursorPar();
                // We used to do cursorLeftIntern() here, but it is
@@ -1954,7 +1948,7 @@ void LyXText::backspace()
        } else {
                // this is the code for a normal backspace, not pasting
                // any paragraphs
-               recordUndo(bv(), Undo::DELETE, cursorPar());
+               recordUndo(bv(), Undo::DELETE, ownerParagraphs(), cursor.par());
                // We used to do cursorLeftIntern() here, but it is
                // not a good idea since it triggers the auto-delete
                // mechanism. So we do a cursorLeftIntern()-lite,
@@ -1988,7 +1982,7 @@ ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
 ParagraphList::iterator LyXText::getPar(int par) const
 {
        BOOST_ASSERT(par >= 0);
-       BOOST_ASSERT(par < ownerParagraphs().size());
+       BOOST_ASSERT(par < int(ownerParagraphs().size()));
        ParagraphList::iterator pit = ownerParagraphs().begin();
        std::advance(pit, par);
        return pit;
index e3994ced4f73dbe98d570a5ca4f59b3e74a801bc..b3102acf6fa22cac9df8f03226b1e40478dd8f4f 100644 (file)
@@ -276,7 +276,7 @@ void LyXText::toggleInset()
 
        // do we want to keep this?? (JMarc)
        if (!isHighlyEditableInset(inset))
-               recordUndo(bv(), Undo::ATOMIC);
+               recUndo(cursor.par());
 
        if (inset->isOpen())
                inset->close(bv());
@@ -328,7 +328,7 @@ LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
                ++endpit;
        }
 
-       recordUndo(bv(), Undo::ATOMIC, getPar(sstart_cur), boost::prior(undoendpit));
+       recUndo(sstart_cur.par(), parOffset(undoendpit) - 1);
 
        // ok we have a selection. This is always between sstart_cur
        // and sel_end cursor
@@ -421,7 +421,7 @@ bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
        ParagraphList::iterator pastend = boost::next(end);
 
        if (!test_only)
-               recordUndo(bv(), Undo::ATOMIC, start, end);
+               recUndo(parOffset(start), parOffset(end));
 
        bool changed = false;
 
@@ -514,7 +514,7 @@ void LyXText::setFont(LyXFont const & font, bool toggleall)
        // ok we have a selection. This is always between sel_start_cursor
        // and sel_end cursor
 
-       recordUndo(bv(), Undo::ATOMIC, getPar(selection.start), getPar(selection.end));
+       recUndo(selection.start.par(), selection.end.par());
        freezeUndo();
        cursor = selection.start;
        while (cursor.par() != selection.end.par() ||
@@ -787,8 +787,7 @@ void LyXText::setParagraph(bool line_top, bool line_bottom,
                ++endpit;
        }
 
-       recordUndo(bv(), Undo::ATOMIC, getPar(selection.start),
-               boost::prior(undoendpit));
+       recUndo(selection.start.par(), parOffset(undoendpit) - 1);
 
 
        int tmppit = selection.end.par();
@@ -1125,7 +1124,7 @@ void LyXText::insertInset(InsetOld * inset)
 {
        if (!cursorPar()->insetAllowed(inset->lyxCode()))
                return;
-       recordUndo(bv(), Undo::ATOMIC, cursorPar());
+       recUndo(cursor.par());
        freezeUndo();
        cursorPar()->insertInset(cursor.pos(), inset);
        // Just to rebreak and refresh correctly.
@@ -1175,8 +1174,7 @@ void LyXText::cutSelection(bool doclear, bool realcut)
                ++endpit;
        }
 
-       recordUndo(bv(), Undo::DELETE, getPar(selection.start),
-                  boost::prior(undoendpit));
+       recUndo(selection.start.par(), parOffset(undoendpit) - 1);
 
        endpit = getPar(selection.end.par());
        int endpos = selection.end.pos();
@@ -1246,7 +1244,7 @@ void LyXText::pasteSelection(size_t sel_index)
        if (!CutAndPaste::checkPastePossible())
                return;
 
-       recordUndo(bv(), Undo::INSERT, cursorPar());
+       recUndo(cursor.par());
 
        ParagraphList::iterator endpit;
        PitPosPair ppp;
@@ -1289,7 +1287,7 @@ void LyXText::setSelectionRange(lyx::pos_type length)
 // simple replacing. The font of the first selected character is used
 void LyXText::replaceSelectionWithString(string const & str)
 {
-       recordUndo(bv(), Undo::ATOMIC);
+       recUndo(cursor.par());
        freezeUndo();
 
        if (!selection.set()) { // create a dummy selection
@@ -1325,7 +1323,7 @@ void LyXText::insertStringAsLines(string const & str)
        pos_type pos = cursor.pos();
        ParagraphList::iterator endpit = boost::next(cursorPar());
 
-       recordUndo(bv(), Undo::ATOMIC);
+       recUndo(cursor.par());
 
        // only to be sure, should not be neccessary
        clearSelection();
@@ -1905,7 +1903,7 @@ bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
                while (endpit != ownerParagraphs().end() && endpit->getDepth())
                        ++endpit;
 
-               recordUndo(bv(), Undo::DELETE, old_pit, boost::prior(endpit));
+               recUndo(parOffset(old_pit), parOffset(endpit) - 1);
                cursor = tmpcursor;
 
                // delete old par
@@ -1938,6 +1936,18 @@ ParagraphList & LyXText::ownerParagraphs() const
 }
 
 
+void LyXText::recUndo(paroffset_type first, paroffset_type last) const
+{
+       recordUndo(bv(), Undo::ATOMIC, ownerParagraphs(), first, last);
+}
+
+
+void LyXText::recUndo(lyx::paroffset_type par) const
+{
+       recordUndo(bv(), Undo::ATOMIC, ownerParagraphs(), par, par);
+}
+
+
 bool LyXText::isInInset() const
 {
        // Sub-level has non-null bv owner and non-null inset owner.
index 35380d1f2740ae243ce0789661b8b57ef5ab83ee..638ea44936f7d16848f16721ea465460a6e3c605 100644 (file)
@@ -412,14 +412,14 @@ dispatch_result LyXText::dispatch(FuncRequest const & cmd)
 
                for (; tmp != end; ++tmp) {
                        if (tmp->params().startOfAppendix()) {
-                               recordUndo(bv, Undo::ATOMIC, tmp);
+                               recUndo(parOffset(tmp));
                                tmp->params().startOfAppendix(false);
                                redoParagraph(tmp);
                                break;
                        }
                }
 
-               recordUndo(bv, Undo::ATOMIC, pit);
+               recUndo(parOffset(pit));
                pit->params().startOfAppendix(start);
 
                // we can set the refreshing parameters now
@@ -964,7 +964,7 @@ dispatch_result LyXText::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_TRANSPOSE_CHARS:
-               recordUndo(bv, Undo::ATOMIC, cursorPar());
+               recUndo(cursor.par());
                redoParagraph();
                bv->update();
                break;
@@ -1434,7 +1434,7 @@ dispatch_result LyXText::dispatch(FuncRequest const & cmd)
                        // ...or maybe the recordUndo()
                        // below isn't necessary at all anylonger?
                        if (inset_hit->lyxCode() == InsetOld::REF_CODE)
-                               recordUndo(bv, Undo::ATOMIC);
+                               recUndo(cursor.par());
 
                        bv->owner()->message(inset_hit->editMessage());
 
index b12a4a885fe5ea8e67748c22809e41f8fecaacba..b1de438dafb143faed63d3af2a7761702e9b0df0 100644 (file)
@@ -17,6 +17,7 @@
 #include "undo_funcs.h"
 
 #include "buffer.h"
+#include "debug.h"
 #include "BufferView.h"
 #include "funcrequest.h"
 #include "iterators.h"
 
 #include "insets/updatableinset.h"
 
+#include <iostream>
 
-/// The flag used by FinishUndo().
+using std::endl;
+using lyx::paroffset_type;
+
+
+/// The flag used by finishUndo().
 bool undo_finished;
 
 /// Whether actions are not added to the undo stacks.
@@ -36,18 +42,22 @@ namespace {
 
 
 void recordUndo(BufferView * bv, Undo::undo_kind kind,
-       ParagraphList::iterator first, ParagraphList::iterator last,
+       ParagraphList & plist, paroffset_type firstpar, paroffset_type lastpar,
        limited_stack<Undo> & stack)
 {
        Buffer * buf = bv->buffer();
 
+       ParagraphList::iterator first = plist.begin();
+       advance(first, firstpar);
+       ParagraphList::iterator last = plist.begin();
+       advance(last, lastpar);
+
        // First, record inset id, if cursor is in one
        UpdatableInset * inset = first->inInset();
        LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
        int const inset_id = inset ? inset->id() : -1;
 
        // We simply record the entire outer paragraphs
-       ParagraphList & plist = buf->paragraphs();
        ParIterator null = buf->par_iterator_end();
 
        // First, identify the outer paragraphs
@@ -59,8 +69,8 @@ void recordUndo(BufferView * bv, Undo::undo_kind kind,
        }
 
        // And calculate a stable reference to them
-       int const first_offset = std::distance(plist.begin(), first);
-       int const last_offset = std::distance(last, plist.end());
+       int const first_offset = firstpar;
+       int const last_offset = plist.size() - lastpar;
 
        // Undo::ATOMIC are always recorded (no overlapping there).
 
@@ -87,6 +97,7 @@ void recordUndo(BufferView * bv, Undo::undo_kind kind,
                cursor_offset, text->cursor.pos(),
                ParagraphList()));
 
+       lyxerr << "G" << endl;
        // Record the relevant paragraphs
        ParagraphList & undo_pars = stack.top().pars;
 
@@ -103,7 +114,7 @@ void recordUndo(BufferView * bv, Undo::undo_kind kind,
 
 
 // Returns false if no undo possible.
-bool performUndoOrRedo(BufferView * bv, Undo & undo)
+bool performUndoOrRedo(BufferView * bv, Undo const & undo)
 {
        Buffer * buf = bv->buffer();
        ParagraphList & plist = buf->paragraphs();
@@ -141,10 +152,17 @@ bool performUndoOrRedo(BufferView * bv, Undo & undo)
                LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
                text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
 
+               lyxerr << "undo, inset: " << inset << endl;
+
                if (inset) {
+                       lyxerr << "undo, inset owner: " << inset->owner() << endl;
+
                        // Magic needed to update inset internal state
                        FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
-                       inset->localDispatch(cmd);
+                       if (inset->owner())
+                               inset->owner()->localDispatch(cmd);
+                       else
+                               inset->localDispatch(cmd);
                }
 
                // set cursor again to force the position to be the right one
@@ -247,24 +265,23 @@ bool textRedo(BufferView * bv)
 
 
 void recordUndo(BufferView * bv, Undo::undo_kind kind,
-            ParagraphList::iterator first, ParagraphList::iterator last)
+       ParagraphList & plist, paroffset_type first, paroffset_type last)
 {
        if (!undo_frozen) {
-               recordUndo(bv, kind, first, last, bv->buffer()->undostack());
+               recordUndo(bv, kind, plist, first, last, bv->buffer()->undostack());
                bv->buffer()->redostack().clear();
        }
 }
 
 
 void recordUndo(BufferView * bv, Undo::undo_kind kind,
-            ParagraphList::iterator first)
+       ParagraphList & plist, paroffset_type par)
 {
-       recordUndo(bv, kind, first, first);
+       recordUndo(bv, kind, plist, par, par);
 }
 
 
 void recordUndo(BufferView * bv, Undo::undo_kind kind)
 {
-       recordUndo(bv, kind, bv->text->cursorPar());
+       recordUndo(bv, kind, bv->text->ownerParagraphs(), bv->text->cursor.par());
 }
-
index f34005dca6360d0af54212319a5c35472f929959..96efcb956481b76ac426a1cdc805997fd2120fc6 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "undo.h"
 #include "ParagraphList_fwd.h"
+#include "support/types.h"
 
 class BufferView;
 class Paragraph;
@@ -48,10 +49,11 @@ void unFreezeUndo();
  * will record the original information of the paragraphs in the undo stack.
  */
 void recordUndo(BufferView *, Undo::undo_kind kind,
-                   ParagraphList::iterator first, ParagraphList::iterator last);
+       ParagraphList & plist, lyx::paroffset_type first, lyx::paroffset_type last);
+
 /// Convienience: Prepare undo when change in a single paragraph.
 void recordUndo(BufferView *, Undo::undo_kind kind,
-                   ParagraphList::iterator first);
+       ParagraphList & plist, lyx::paroffset_type par);
 
 /// Convienience: Prepare undo for the paragraph that contains the cursor
 void recordUndo(BufferView *, Undo::undo_kind kind);