]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
more cursor dispatch
[lyx.git] / src / text2.C
index 5aeeb6fbc1ea3779dc3b9598cb44967fb54ad943..e15775524e164ac99ef5fd351930390432dac074 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);
@@ -1088,8 +1054,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 +1164,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();
 }
@@ -1250,6 +1215,7 @@ void LyXText::setCurrentFont()
 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 +1237,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 +1309,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 +1329,37 @@ 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);
+       pos_type const pos = row.pos() + getColumnNearX(pit, row, x, 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)
+{
+       int xx = x; // is modified by getColumnNearX
+       ParagraphList::iterator pit;
+       Row const & row = *getRowNearY(y, pit);
+       bool bound = false;
+       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);
+       }
 }
 
 
@@ -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();
-               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);
        }
 }
 
@@ -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();
-               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);
        }
 }
 
@@ -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);