]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
bug + spped fixes + small stuff
[lyx.git] / src / text2.C
index 61ae776ac7febfbb477a6749f98b7fc31f8a8a89..6e4c06c7430308e8da540cb65a07390b29fb1b9d 100644 (file)
@@ -79,9 +79,9 @@ LyXText::LyXText(BufferView * bv, bool in_inset)
 {}
 
 
-void LyXText::init(BufferView * bview)
+void LyXText::init(BufferView * bv)
 {
-       bv_owner = bview;
+       bv_owner = bv;
 
        ParagraphList::iterator const beg = paragraphs().begin();
        ParagraphList::iterator const end = paragraphs().end();
@@ -94,8 +94,7 @@ void LyXText::init(BufferView * bview)
        current_font = getFont(beg, 0);
 
        redoParagraphs(beg, end);
-       setCursorIntern(0, 0);
-       bv()->cursor().resetAnchor();
+       bv->cursor().resetAnchor();
 
        updateCounters();
 }
@@ -214,38 +213,6 @@ void LyXText::setCharFont(
 }
 
 
-InsetOld * LyXText::getInset() const
-{
-       ParagraphList::iterator pit = cursorPar();
-       pos_type const pos = cursor().pos();
-
-       if (pos < pit->size() && pit->isInset(pos)) {
-               return pit->getInset(pos);
-       }
-       return 0;
-}
-
-
-bool LyXText::toggleInset()
-{
-       InsetOld * inset = getInset();
-       // is there an editable inset at cursor position?
-       if (!isEditableInset(inset))
-               return false;
-       //bv()->owner()->message(inset->editMessage());
-
-       // do we want to keep this?? (JMarc)
-       if (!isHighlyEditableInset(inset))
-               recUndo(cursor().par());
-
-       if (inset->isOpen())
-               inset->close();
-       else
-               inset->open();
-       return true;
-}
-
-
 // used in setLayout
 // Asger is not sure we want to do this...
 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
@@ -269,8 +236,7 @@ void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
 
 
 // return past-the-last paragraph influenced by a layout change on pit
-ParagraphList::iterator
-LyXText::undoSpan(ParagraphList::iterator pit)
+ParagraphList::iterator LyXText::undoSpan(ParagraphList::iterator pit)
 {
        ParagraphList::iterator end = paragraphs().end();
        ParagraphList::iterator nextpit = boost::next(pit);
@@ -324,17 +290,15 @@ void LyXText::setLayout(string const & layout)
                bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
                bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
                bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
-               InsetOld * inset = new InsetEnvironment(params, layout);
-               if (bv()->insertInset(inset)) {
-                       //inset->edit(bv());
-                       //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
-               } else
-                       delete inset;
+               InsetBase * inset = new InsetEnvironment(params, layout);
+               insertInset(inset);
+               //inset->edit(bv());
+               //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
                return;
        }
 
        ParagraphList::iterator start =
-               getPar(bv()->cursor().selStart().par());
+               getPar(bv()->cursor().selBegin().par());
        ParagraphList::iterator end =
                boost::next(getPar(bv()->cursor().selEnd().par()));
        ParagraphList::iterator endpit = setLayout(start, end, layout);
@@ -355,7 +319,7 @@ void getSelectionSpan(LyXText & text,
                beg = text.cursorPar();
                end = boost::next(beg);
        } else {
-               beg = text.getPar(text.bv()->cursor().selStart());
+               beg = text.getPar(text.bv()->cursor().selBegin());
                end = boost::next(text.getPar(text.bv()->cursor().selEnd()));
        }
 }
@@ -451,13 +415,13 @@ void LyXText::setFont(LyXFont const & font, bool toggleall)
        }
 
        // ok we have a selection.
-       recUndo(cur.selStart().par(), cur.selEnd().par());
+       recUndo(cur.selBegin().par(), cur.selEnd().par());
        freezeUndo();
 
-       ParagraphList::iterator beg = getPar(cur.selStart().par());
+       ParagraphList::iterator beg = getPar(cur.selBegin().par());
        ParagraphList::iterator end = getPar(cur.selEnd().par());
        
-       PosIterator pos(&paragraphs(), beg, cur.selStart().pos());
+       PosIterator pos(&paragraphs(), beg, cur.selBegin().pos());
        PosIterator posend(&paragraphs(), end, cur.selEnd().pos());
 
        BufferParams const & params = bv()->buffer()->params();
@@ -502,8 +466,7 @@ void LyXText::cursorTop()
 
 void LyXText::cursorBottom()
 {
-       ParagraphList::iterator lastpit =
-               boost::prior(paragraphs().end());
+       ParagraphList::iterator lastpit = boost::prior(paragraphs().end());
        setCursor(lastpit, lastpit->size());
 }
 
@@ -553,10 +516,10 @@ string LyXText::getStringToIndex()
        string idxstring;
        if (!cur.selection())
                bv()->owner()->message(_("Nothing to index!"));
-       else if (cur.selStart().par() != cur.selEnd().par())
+       else if (cur.selBegin().par() != cur.selEnd().par())
                bv()->owner()->message(_("Cannot index more than one paragraph!"));
        else
-               idxstring = selectionAsString(*bv()->buffer(), false);
+               idxstring = cur.selectionAsString(false);
 
        // Reset cursors to their original position.
        cursor() = reset_cursor;
@@ -582,10 +545,10 @@ void LyXText::setParagraph(Spacing const & spacing, LyXAlignment align,
        LCursor & cur = bv()->cursor();
        // make sure that the depth behind the selection are restored, too
        ParagraphList::iterator undopit = undoSpan(getPar(cur.selEnd()));
-       recUndo(cur.selStart().par(), parOffset(undopit) - 1);
+       recUndo(cur.selBegin().par(), parOffset(undopit) - 1);
 
        ParagraphList::reverse_iterator pit(getPar(cur.selEnd().par()));
-       ParagraphList::reverse_iterator beg(getPar(cur.selStart().par()));
+       ParagraphList::reverse_iterator beg(getPar(cur.selBegin().par()));
 
        for (--pit; pit != beg; ++pit) {
                ParagraphParameters & params = pit->params();
@@ -606,7 +569,7 @@ void LyXText::setParagraph(Spacing const & spacing, LyXAlignment align,
                params.noindent(noindent);
        }
 
-       redoParagraphs(getPar(cur.selStart()), undopit);
+       redoParagraphs(getPar(cur.selBegin()), undopit);
 }
 
 
@@ -819,14 +782,14 @@ void LyXText::setCounter(Buffer const & buf, ParagraphList::iterator pit)
                if (layout->labeltype == LABEL_SENSITIVE) {
                        ParagraphList::iterator end = paragraphs().end();
                        ParagraphList::iterator tmppit = pit;
-                       InsetOld * in = 0;
+                       InsetBase * in = 0;
                        bool isOK = false;
                        while (tmppit != end && tmppit->inInset()
                               // the single '=' is intended below
                               && (in = tmppit->inInset()->owner()))
                        {
-                               if (in->lyxCode() == InsetOld::FLOAT_CODE ||
-                                   in->lyxCode() == InsetOld::WRAP_CODE) {
+                               if (in->lyxCode() == InsetBase::FLOAT_CODE ||
+                                   in->lyxCode() == InsetBase::WRAP_CODE) {
                                        isOK = true;
                                        break;
                                } else {
@@ -841,9 +804,9 @@ void LyXText::setCounter(Buffer const & buf, ParagraphList::iterator pit)
                        if (isOK) {
                                string type;
 
-                               if (in->lyxCode() == InsetOld::FLOAT_CODE)
+                               if (in->lyxCode() == InsetBase::FLOAT_CODE)
                                        type = static_cast<InsetFloat*>(in)->params().type;
-                               else if (in->lyxCode() == InsetOld::WRAP_CODE)
+                               else if (in->lyxCode() == InsetBase::WRAP_CODE)
                                        type = static_cast<InsetWrap*>(in)->params().type;
                                else
                                        BOOST_ASSERT(false);
@@ -899,7 +862,7 @@ void LyXText::updateCounters()
 }
 
 
-void LyXText::insertInset(InsetOld * inset)
+void LyXText::insertInset(InsetBase * inset)
 {
        if (!cursorPar()->insetAllowed(inset->lyxCode()))
                return;
@@ -932,20 +895,20 @@ void LyXText::cutSelection(bool doclear, bool realcut)
        // finished. The solution used currently just works, to make it
        // faster we need to be more clever and probably also have more
        // calls to stuffClipboard. (Lgb)
-       bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
+       bv()->stuffClipboard(cur.selectionAsString(true));
 
        // This doesn't make sense, if there is no selection
        if (!cur.selection())
                return;
 
-       // OK, we have a selection. This is always between cur.selStart()
+       // OK, we have a selection. This is always between cur.selBegin()
        // and cur.selEnd()
 
        // make sure that the depth behind the selection are restored, too
-       ParagraphList::iterator begpit = getPar(cur.selStart().par());
+       ParagraphList::iterator begpit = getPar(cur.selBegin().par());
        ParagraphList::iterator endpit = getPar(cur.selEnd().par());
        ParagraphList::iterator undopit = undoSpan(endpit);
-       recUndo(cur.selStart().par(), parOffset(undopit) - 1);
+       recUndo(cur.selBegin().par(), parOffset(undopit) - 1);
 
        int endpos = cur.selEnd().pos();
 
@@ -954,13 +917,13 @@ void LyXText::cutSelection(bool doclear, bool realcut)
                CutAndPaste::cutSelection(bufparams,
                                          paragraphs(),
                                          begpit , endpit,
-                                         cur.selStart().pos(), endpos,
+                                         cur.selBegin().pos(), endpos,
                                          bufparams.textclass,
                                          doclear)
                : CutAndPaste::eraseSelection(bufparams,
                                              paragraphs(),
                                              begpit, endpit,
-                                             cur.selStart().pos(), endpos,
+                                             cur.selBegin().pos(), endpos,
                                              doclear);
        // sometimes necessary
        if (doclear)
@@ -983,25 +946,25 @@ void LyXText::copySelection()
 {
        LCursor & cur = bv()->cursor();
        // stuff the selection onto the X clipboard, from an explicit copy request
-       bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
+       bv()->stuffClipboard(cur.selectionAsString(true));
 
        // this doesnt make sense, if there is no selection
        if (!cur.selection())
                return;
 
-       // ok we have a selection. This is always between cur.selStart()
+       // ok we have a selection. This is always between cur.selBegin()
        // and sel_end cursor
 
        // copy behind a space if there is one
-       while (getPar(cur.selStart())->size() > cur.selStart().pos()
-              && getPar(cur.selStart())->isLineSeparator(cur.selStart().pos())
-              && (cur.selStart().par() != cur.selEnd().par()
-                  || cur.selStart().pos() < cur.selEnd().pos()))
-               cur.selStart().pos(cur.selStart().pos() + 1);
+       while (getPar(cur.selBegin())->size() > cur.selBegin().pos()
+              && getPar(cur.selBegin())->isLineSeparator(cur.selBegin().pos())
+              && (cur.selBegin().par() != cur.selEnd().par()
+                  || cur.selBegin().pos() < cur.selEnd().pos()))
+               cur.selBegin().pos(cur.selBegin().pos() + 1);
 
-       CutAndPaste::copySelection(getPar(cur.selStart().par()),
+       CutAndPaste::copySelection(getPar(cur.selBegin().par()),
                                   getPar(cur.selEnd().par()),
-                                  cur.selStart().pos(), 
+                                  cur.selBegin().pos(), 
                                   cur.selEnd().pos(),
                                   bv()->buffer()->params().textclass);
 }
@@ -1062,9 +1025,9 @@ void LyXText::replaceSelectionWithString(string const & str)
 
        // Get font setting before we cut
        pos_type pos = cur.selEnd().pos();
-       LyXFont const font = getPar(cur.selStart())
+       LyXFont const font = getPar(cur.selBegin())
                ->getFontSettings(bv()->buffer()->params(),
-                                 cur.selStart().pos());
+                                 cur.selBegin().pos());
 
        // Insert the new string
        string::const_iterator cit = str.begin();
@@ -1088,8 +1051,7 @@ void LyXText::insertStringAsLines(string const & str)
        ParagraphList::iterator pit = cursorPar();
        pos_type pos = cursor().pos();
        ParagraphList::iterator endpit = boost::next(cursorPar());
-
-       recUndo(cursor().par());
+       recordUndo(cur, Undo::ATOMIC);
 
        // only to be sure, should not be neccessary
        cur.clearSelection();
@@ -1199,7 +1161,7 @@ void LyXText::setCursorIntern(paroffset_type par,
                              pos_type pos, bool setfont, bool boundary)
 {
        setCursor(cursor(), par, pos, boundary);
-       bv()->cursor().x_target(cursorX() + xo_);
+       bv()->cursor().x_target() = cursorX(cursor());
        if (setfont)
                setCurrentFont();
 }
@@ -1244,12 +1206,13 @@ void LyXText::setCurrentFont()
        }
 }
 
-
+// x is an absolute screen coord
 // returns the column near the specified x-coordinate of the row
 // x is set to the real beginning of this column
 pos_type LyXText::getColumnNearX(ParagraphList::iterator pit,
        Row const & row, int & x, bool & boundary) const
 {
+       x -= xo_;
        double tmpx             = row.x();
        double fill_separator   = row.fill_separator();
        double fill_hfill       = row.fill_hfill();
@@ -1271,7 +1234,7 @@ pos_type LyXText::getColumnNearX(ParagraphList::iterator pit,
 
        // check for empty row
        if (vc == end) {
-               x = int(tmpx);
+               x = int(tmpx) + xo_;
                return 0;
        }
 
@@ -1343,9 +1306,8 @@ pos_type LyXText::getColumnNearX(ParagraphList::iterator pit,
                c = end - 1;
        }
 
-       c -= row.pos();
-       x = int(tmpx);
-       return c;
+       x = int(tmpx) + xo_;
+       return c - row.pos();
 }
 
 
@@ -1364,10 +1326,40 @@ void LyXText::setCursorFromCoordinates(CursorSlice & cur, int x, int y)
        ParagraphList::iterator pit;
        Row const & row = *getRowNearY(y, pit);
        bool bound = false;
-       pos_type const column = getColumnNearX(pit, row, x, bound);
-       cur.par(parOffset(pit));
-       cur.pos(row.pos() + column);
-       cur.boundary(bound);
+       int xx = x + xo_; // getRowNearX get absolute x coords
+       pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
+       cur.par() = parOffset(pit);
+       cur.pos() = pos;
+       cur.boundary() = bound;
+}
+
+
+// x,y are absolute screen coordinates
+void LyXText::edit(LCursor & cur, int x, int y)
+{
+       ParagraphList::iterator pit;
+       Row const & row = *getRowNearY(y - yo_, pit);
+       bool bound = false;
+
+       int xx = x; // is modified by getColumnNearX
+       pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
+       cur.par() = parOffset(pit);
+       cur.pos() = pos;
+       cur.boundary() = bound;
+
+       // try to descend into nested insets
+       InsetBase * inset = checkInsetHit(x, y);
+       if (inset) {
+               // This should be just before or just behind the
+               // cursor position set above.
+               BOOST_ASSERT((pos != 0 && inset == pit->getInset(pos - 1))
+                            || inset == pit->getInset(pos));
+               // Make sure the cursor points to the position before
+               // this inset.
+               if (inset == pit->getInset(pos - 1))
+                       --cur.pos();
+               inset->edit(cur, x, y);
+       }
 }
 
 
@@ -1375,7 +1367,7 @@ bool LyXText::checkAndActivateInset(bool front)
 {
        if (cursor().pos() == cursorPar()->size())
                return false;
-       InsetOld * inset = cursorPar()->getInset(cursor().pos());
+       InsetBase * inset = cursorPar()->getInset(cursor().pos());
        if (!isHighlyEditableInset(inset))
                return false;
        inset->edit(bv()->cursor(), front);
@@ -1432,10 +1424,11 @@ DispatchResult LyXText::moveLeftIntern(bool front,
 
 DispatchResult LyXText::moveUp()
 {
-       if (cursorPar() == firstPar() && cursorRow() == firstRow())
+       LCursor & cur = bv()->cursor();
+       if (cur.par() == 0 && cursorRow() == firstRow())
                return DispatchResult(false, FINISHED_UP);
        cursorUp(false);
-       bv()->cursor().clearSelection();
+       cur.clearSelection();
        return DispatchResult(true);
 }
 
@@ -1443,7 +1436,7 @@ DispatchResult LyXText::moveUp()
 DispatchResult LyXText::moveDown()
 {
        LCursor & cur = bv()->cursor();
-       if (cursorPar() == lastPar() && cursorRow() == lastRow())
+       if (cur.par() == cur.lastpar() && cursorRow() == lastRow())
                return DispatchResult(false, FINISHED_DOWN);
        cursorDown(false);
        cur.clearSelection();
@@ -1502,15 +1495,14 @@ void LyXText::cursorUp(bool selecting)
 {
        LCursor & cur = bv()->cursor();
        Row const & row = *cursorRow();
-       int x = cur.x_target() - xo_;
-       int y = cursorY() - row.baseline() - 1;
+       int x = cur.x_target();
+       int y = cursorY(cur.current()) - row.baseline() - 1;
        setCursorFromCoordinates(x, y);
 
        if (!selecting) {
-               int y_abs = y + yo_ - bv()->top_y();
-               InsetOld * inset_hit = checkInsetHit(cur.x_target(), y_abs);
+               InsetBase * inset_hit = checkInsetHit(cur.x_target(), y);
                if (inset_hit && isHighlyEditableInset(inset_hit))
-                       inset_hit->edit(cur, cur.x_target(), y_abs);
+                       inset_hit->edit(cur, cur.x_target(), y);
        }
 }
 
@@ -1519,15 +1511,14 @@ void LyXText::cursorDown(bool selecting)
 {
        LCursor & cur = bv()->cursor();
        Row const & row = *cursorRow();
-       int x = cur.x_target() - xo_;
-       int y = cursorY() - row.baseline() + row.height() + 1;
+       int x = cur.x_target();
+       int y = cursorY(cur.current()) - row.baseline() + row.height() + 1;
        setCursorFromCoordinates(x, y);
 
        if (!selecting) {
-               int y_abs = y + yo_ - bv()->top_y();
-               InsetOld * inset_hit = checkInsetHit(cur.x_target(), y_abs);
+               InsetBase * inset_hit = checkInsetHit(cur.x_target(), y);
                if (inset_hit && isHighlyEditableInset(inset_hit))
-                       inset_hit->edit(cur, cur.x_target(), y_abs);
+                       inset_hit->edit(cur, cur.x_target(), y);
        }
 }
 
@@ -1715,13 +1706,13 @@ ParagraphList & LyXText::paragraphs() const
 
 void LyXText::recUndo(paroffset_type first, paroffset_type last) const
 {
-       recordUndo(Undo::ATOMIC, this, first, last);
+       recordUndo(bv()->cursor(), Undo::ATOMIC, first, last);
 }
 
 
 void LyXText::recUndo(lyx::paroffset_type par) const
 {
-       recordUndo(Undo::ATOMIC, this, par, par);
+       recordUndo(bv()->cursor(), Undo::ATOMIC, par, par);
 }
 
 
@@ -1731,6 +1722,26 @@ bool LyXText::isInInset() const
 }
 
 
+bool LyXText::toggleInset()
+{
+       InsetBase * inset = bv()->cursor().nextInset();
+       // is there an editable inset at cursor position?
+       if (!isEditableInset(inset))
+               return false;
+       //bv()->owner()->message(inset->editMessage());
+
+       // do we want to keep this?? (JMarc)
+       if (!isHighlyEditableInset(inset))
+               recUndo(cursor().par());
+
+       if (inset->isOpen())
+               inset->close();
+       else
+               inset->open();
+       return true;
+}
+
+
 int defaultRowHeight()
 {
        return int(font_metrics::maxHeight(LyXFont(LyXFont::ALL_SANE)) *  1.2);