]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
add LCursor & parameter to cursor moving functions
[lyx.git] / src / text2.C
index 5aeeb6fbc1ea3779dc3b9598cb44967fb54ad943..7f7fc1d498fdc6ddf379ed1e7cc3b3b5f70a9091 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(
 }
 
 
-InsetBase * 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()
-{
-       InsetBase * 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);
@@ -325,11 +291,9 @@ void LyXText::setLayout(string const & layout)
                bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
                bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
                InsetBase * inset = new InsetEnvironment(params, layout);
-               if (bv()->insertInset(inset)) {
-                       //inset->edit(bv());
-                       //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
-               } else
-                       delete inset;
+               insertInset(inset);
+               //inset->edit(bv());
+               //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
                return;
        }
 
@@ -477,34 +441,30 @@ void LyXText::setFont(LyXFont const & font, bool toggleall)
 // the cursor set functions have a special mechanism. When they
 // realize you left an empty paragraph, they will delete it.
 
-void LyXText::cursorHome()
+void LyXText::cursorHome(LCursor & cur)
 {
-       ParagraphList::iterator cpit = cursorPar();
-       setCursor(cpit, cpit->getRow(cursor().pos())->pos());
+       setCursor(cur.par(), cur.textRow().pos());
 }
 
 
-void LyXText::cursorEnd()
+void LyXText::cursorEnd(LCursor & cur)
 {
-       ParagraphList::iterator cpit = cursorPar();
-       pos_type end = cpit->getRow(cursor().pos())->endpos();
        // if not on the last row of the par, put the cursor before
        // the final space
-       setCursor(cpit, end == cpit->size() ? end : end - 1);
+       pos_type const end = cur.textRow().endpos();
+       setCursor(cur.par(), end == cur.lastpos() ? end : end - 1);
 }
 
 
-void LyXText::cursorTop()
+void LyXText::cursorTop(LCursor &)
 {
-       setCursor(paragraphs().begin(), 0);
+       setCursor(0, 0);
 }
 
 
-void LyXText::cursorBottom()
+void LyXText::cursorBottom(LCursor & cur)
 {
-       ParagraphList::iterator lastpit =
-               boost::prior(paragraphs().end());
-       setCursor(lastpit, lastpit->size());
+       setCursor(cur.lastpar(), boost::prior(paragraphs().end())->size());
 }
 
 
@@ -556,7 +516,7 @@ string LyXText::getStringToIndex()
        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;
@@ -915,7 +875,7 @@ void LyXText::insertInset(InsetBase * inset)
        // and fails if the cursor is behind the inset and getInset
        // does not return the inset!
        if (isHighlyEditableInset(inset))
-               cursorLeft(true);
+               cursorLeft(bv()->cursor(), true);
 
        unFreezeUndo();
 }
@@ -932,7 +892,7 @@ 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())
@@ -983,7 +943,7 @@ 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())
@@ -1034,7 +994,7 @@ void LyXText::pasteSelection(size_t sel_index)
 
        cur.clearSelection();
        cur.resetAnchor();
-       setCursor(ppp.first, ppp.second);
+       setCursor(parOffset(ppp.first), ppp.second);
        cur.setSelection();
        updateCounters();
 }
@@ -1048,7 +1008,7 @@ void LyXText::setSelectionRange(lyx::pos_type length)
        LCursor & cur = bv()->cursor();
        cur.resetAnchor();
        while (length--)
-               cursorRight(true);
+               cursorRight(cur, true);
        cur.setSelection();
 }
 
@@ -1088,8 +1048,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();
@@ -1097,7 +1056,7 @@ void LyXText::insertStringAsLines(string const & str)
 
        redoParagraphs(cursorPar(), endpit);
        cur.resetAnchor();
-       setCursor(pit, pos);
+       setCursor(parOffset(pit), pos);
        cur.setSelection();
 }
 
@@ -1130,12 +1089,6 @@ void LyXText::insertStringAsParagraphs(string const & str)
 }
 
 
-void LyXText::setCursor(ParagraphList::iterator pit, pos_type pos)
-{
-       setCursor(parOffset(pit), pos);
-}
-
-
 bool LyXText::setCursor(paroffset_type par, pos_type pos, bool setfont,
        bool boundary)
 {
@@ -1199,7 +1152,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 +1197,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 +1225,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 +1297,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,97 +1317,124 @@ 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);
+       }
 }
 
 
-bool LyXText::checkAndActivateInset(bool front)
+bool LyXText::checkAndActivateInset(LCursor & cur, bool front)
 {
-       if (cursor().pos() == cursorPar()->size())
+       if (cur.pos() == cur.lastpos())
                return false;
-       InsetBase * inset = cursorPar()->getInset(cursor().pos());
+       InsetBase * inset = cur.nextInset();
        if (!isHighlyEditableInset(inset))
                return false;
-       inset->edit(bv()->cursor(), front);
+       inset->edit(cur, front);
        return true;
 }
 
 
-DispatchResult LyXText::moveRight()
+DispatchResult LyXText::moveRight(LCursor & cur)
 {
-       if (cursorPar()->isRightToLeftPar(bv()->buffer()->params()))
-               return moveLeftIntern(false, true, false);
+       if (cur.paragraph().isRightToLeftPar(bv()->buffer()->params()))
+               return moveLeftIntern(cur, false, true, false);
        else
-               return moveRightIntern(true, true, false);
+               return moveRightIntern(cur, true, true, false);
 }
 
 
-DispatchResult LyXText::moveLeft()
+DispatchResult LyXText::moveLeft(LCursor & cur)
 {
-       if (cursorPar()->isRightToLeftPar(bv()->buffer()->params()))
-               return moveRightIntern(true, true, false);
+       if (cur.paragraph().isRightToLeftPar(bv()->buffer()->params()))
+               return moveRightIntern(cur, true, true, false);
        else
-               return moveLeftIntern(false, true, false);
+               return moveLeftIntern(cur, false, true, false);
 }
 
 
-DispatchResult LyXText::moveRightIntern(bool front, bool activate_inset, bool selecting)
+DispatchResult LyXText::moveRightIntern(LCursor & cur, 
+       bool front, bool activate_inset, bool selecting)
 {
-       ParagraphList::iterator c_par = cursorPar();
-       if (boost::next(c_par) == paragraphs().end()
-               && cursor().pos() >= c_par->size())
+       if (cur.par() == cur.lastpar() && cur.pos() == cur.lastpos())
                return DispatchResult(false, FINISHED_RIGHT);
-       if (activate_inset && checkAndActivateInset(front))
+       if (activate_inset && checkAndActivateInset(cur, front))
                return DispatchResult(true, true);
-       cursorRight(true);
+       cursorRight(cur, true);
        if (!selecting)
-               bv()->cursor().clearSelection();
+               cur.clearSelection();
        return DispatchResult(true);
 }
 
 
-DispatchResult LyXText::moveLeftIntern(bool front,
-                         bool activate_inset, bool selecting)
+DispatchResult LyXText::moveLeftIntern(LCursor & cur,
+       bool front, bool activate_inset, bool selecting)
 {
-       if (cursor().par() == 0 && cursor().pos() <= 0)
+       if (cur.par() == 0 && cur.pos() == 0)
                return DispatchResult(false, FINISHED);
-       cursorLeft(true);
+       cursorLeft(cur, true);
        if (!selecting)
-               bv()->cursor().clearSelection();
-       if (activate_inset && checkAndActivateInset(front))
+               cur.clearSelection();
+       if (activate_inset && checkAndActivateInset(cur, front))
                return DispatchResult(true, true);
        return DispatchResult(true);
 }
 
 
-DispatchResult LyXText::moveUp()
+DispatchResult LyXText::moveUp(LCursor & cur)
 {
-       if (cursorPar() == firstPar() && cursorRow() == firstRow())
+       if (cur.par() == 0 && cur.row() == 0)
                return DispatchResult(false, FINISHED_UP);
-       cursorUp(false);
-       bv()->cursor().clearSelection();
+       cursorUp(cur, false);
+       cur.clearSelection();
        return DispatchResult(true);
 }
 
 
-DispatchResult LyXText::moveDown()
+DispatchResult LyXText::moveDown(LCursor & cur)
 {
-       LCursor & cur = bv()->cursor();
-       if (cursorPar() == lastPar() && cursorRow() == lastRow())
+       if (cur.par() == cur.lastpar() && cur.textRow().endpos() == cur.lastpos())
                return DispatchResult(false, FINISHED_DOWN);
-       cursorDown(false);
+       cursorDown(cur, false);
        cur.clearSelection();
        return DispatchResult(true);
 }
 
 
-bool LyXText::cursorLeft(bool internal)
+bool LyXText::cursorLeft(LCursor & cur, bool internal)
 {
-       LCursor & cur = bv()->cursor();
-       if (cur.pos() > 0) {
+       if (cur.pos() != 0) {
                bool boundary = cur.boundary();
                setCursor(cur.par(), cur.pos() - 1, true, false);
                if (!internal && !boundary &&
@@ -1473,9 +1453,8 @@ bool LyXText::cursorLeft(bool internal)
 }
 
 
-bool LyXText::cursorRight(bool internal)
+bool LyXText::cursorRight(LCursor & cur, bool internal)
 {
-       LCursor & cur = bv()->cursor();
        if (!internal && cur.boundary()) {
                setCursor(cur.par(), cur.pos(), true, false);
                return true;
@@ -1489,7 +1468,7 @@ bool LyXText::cursorRight(bool internal)
                return true;
        }
 
-       if (cur.par() + 1 != int(paragraphs().size())) {
+       if (cur.par() != cur.lastpar()) {
                setCursor(cur.par() + 1, 0);
                return true;
        }
@@ -1498,59 +1477,51 @@ bool LyXText::cursorRight(bool internal)
 }
 
 
-void LyXText::cursorUp(bool selecting)
+void LyXText::cursorUp(LCursor & cur, 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();
-               InsetBase * 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);
        }
 }
 
 
-void LyXText::cursorDown(bool selecting)
+void LyXText::cursorDown(LCursor & cur, bool selecting)
 {
-       LCursor & cur = bv()->cursor();
-       Row const & row = *cursorRow();
-       int x = cur.x_target() - xo_;
-       int y = cursorY() - row.baseline() + row.height() + 1;
-       setCursorFromCoordinates(x, y);
+       Row const & row = cur.textRow();
+       int x = cur.x_target();
+       int y = cursorY(cur.current()) - row.baseline() + row.height() + 1;
+       setCursorFromCoordinates(cur.current(), x, y);
 
        if (!selecting) {
-               int y_abs = y + yo_ - bv()->top_y();
-               InsetBase * 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);
        }
 }
 
 
-void LyXText::cursorUpParagraph()
+void LyXText::cursorUpParagraph(LCursor & cur)
 {
-       ParagraphList::iterator cpit = cursorPar();
-       if (cursor().pos() > 0)
-               setCursor(cpit, 0);
-       else if (cpit != paragraphs().begin())
-               setCursor(boost::prior(cpit), 0);
+       if (cur.pos() > 0)
+               setCursor(cur.par(), 0);
+       else if (cur.par() != 0)
+               setCursor(cur.par() - 1, 0);
 }
 
 
-void LyXText::cursorDownParagraph()
+void LyXText::cursorDownParagraph(LCursor & cur)
 {
-       ParagraphList::iterator pit = cursorPar();
-       ParagraphList::iterator next_pit = boost::next(pit);
-
-       if (next_pit != paragraphs().end())
-               setCursor(next_pit, 0);
+       if (cur.par() != cur.lastpar())
+               setCursor(cur.par() + 1, 0);
        else
-               setCursor(pit, pit->size());
+               setCursor(cur.par(), cur.lastpos());
 }
 
 
@@ -1566,7 +1537,7 @@ void LyXText::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
        // if cursor position is after the place where the delete occured,
        // update it
        if (cur.pos() > where.pos())
-               cur.pos(cur.pos()-1);
+               --cur.pos();
 
        // check also if we don't want to set the cursor on a spot behind the
        // pagragraph because we erased the last character.
@@ -1715,13 +1686,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 +1702,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);