]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
more cursor dispatch
[lyx.git] / src / text.C
index 5f477a7605f2f3e43538dc906b83150b518c6f54..5f53767f140a1236abce8e724d44b91e79631c70 100644 (file)
@@ -58,6 +58,8 @@ using lyx::support::contains;
 using lyx::support::lowercase;
 using lyx::support::uppercase;
 
+using std::advance;
+using std::distance;
 using std::max;
 using std::endl;
 using std::string;
@@ -344,7 +346,7 @@ int LyXText::rightMargin(Paragraph const & par) const
 {
        LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
 
-       return 
+       return
                RIGHT_MARGIN
                + font_metrics::signedWidth(tclass.rightmargin(),
                                       tclass.defaultfont())
@@ -833,7 +835,7 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
 // convenience function
 void LyXText::redoParagraph()
 {
-       bv()->clearSelection();
+       bv()->cursor().clearSelection();
        redoParagraph(cursorPar());
        setCursorIntern(cursor().par(), cursor().pos());
 }
@@ -843,7 +845,7 @@ void LyXText::redoParagraph()
 // same Paragraph one to the right and make a rebreak
 void LyXText::insertChar(char c)
 {
-       recordUndo(Undo::INSERT, this, cursor().par(), cursor().par());
+       recordUndo(bv()->cursor(), Undo::INSERT);
 
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
@@ -1027,7 +1029,7 @@ void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
                                int const ns = numberOfSeparators(*pit, row);
                                bool disp_inset = false;
                                if (row.endpos() < pit->size()) {
-                                       InsetOld * in = pit->getInset(row.endpos());
+                                       InsetBase * in = pit->getInset(row.endpos());
                                        if (in)
                                                disp_inset = in->display();
                                }
@@ -1104,9 +1106,9 @@ void LyXText::selectWord(word_location loc)
                setCursor(from.par(), from.pos());
        if (to == from)
                return;
-       bv()->resetAnchor();
+       bv()->cursor().resetAnchor();
        setCursor(to.par(), to.pos());
-       bv()->setSelection();
+       bv()->cursor().setSelection();
 }
 
 
@@ -1114,26 +1116,26 @@ void LyXText::selectWord(word_location loc)
 // selection is currently set
 bool LyXText::selectWordWhenUnderCursor(word_location loc)
 {
-       if (!bv()->selection().set()) {
-               selectWord(loc);
-               return bv()->selection().set();
-       }
-       return false;
+       if (bv()->cursor().selection())
+               return false;
+       selectWord(loc);
+       return bv()->cursor().selection();
 }
 
 
 void LyXText::acceptChange()
 {
-       if (!bv()->selection().set() && cursorPar()->size())
+       LCursor & cur = bv()->cursor();
+       if (!cur.selection() && cursorPar()->size())
                return;
 
-       if (bv()->selStart().par() == bv()->selEnd().par()) {
-               CursorSlice const & startc = bv()->selStart();
-               CursorSlice const & endc = bv()->selEnd();
-               recordUndo(Undo::INSERT, this, startc.par());
+       if (cur.selBegin().par() == cur.par()) {
+               CursorSlice const & startc = cur.selBegin();
+               CursorSlice const & endc = cur.selEnd();
+               recordUndo(cur, Undo::INSERT, cur.anchor().par());
                getPar(startc)->acceptChange(startc.pos(), endc.pos());
                finishUndo();
-               bv()->clearSelection();
+               cur.clearSelection();
                redoParagraph(getPar(startc));
                setCursorIntern(startc.par(), 0);
        }
@@ -1143,16 +1145,17 @@ void LyXText::acceptChange()
 
 void LyXText::rejectChange()
 {
-       if (!bv()->selection().set() && cursorPar()->size())
+       LCursor & cur = bv()->cursor();
+       if (!cur.selection() && cursorPar()->size())
                return;
 
-       if (bv()->selStart().par() == bv()->selEnd().par()) {
-               CursorSlice const & startc = bv()->selStart();
-               CursorSlice const & endc = bv()->selEnd();
-               recordUndo(Undo::INSERT, this, startc.par());
+       if (cur.selBegin().par() == cur.selEnd().par()) {
+               CursorSlice const & startc = cur.selBegin();
+               CursorSlice const & endc = cur.selEnd();
+               recordUndo(cur, Undo::INSERT, cur.anchor().par());
                getPar(startc)->rejectChange(startc.pos(), endc.pos());
                finishUndo();
-               bv()->clearSelection();
+               cur.clearSelection();
                redoParagraph(getPar(startc));
                setCursorIntern(startc.par(), 0);
        }
@@ -1163,16 +1166,17 @@ void LyXText::rejectChange()
 // Delete from cursor up to the end of the current or next word.
 void LyXText::deleteWordForward()
 {
+       LCursor & cur = bv()->cursor();
        if (cursorPar()->empty())
-               cursorRight(bv());
+               cursorRight(true);
        else {
                CursorSlice tmpcursor = cursor();
-               bv()->selection().set(true); // to avoid deletion
+               cur.selection() = true; // to avoid deletion
                cursorRightOneWord();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
-               bv()->resetAnchor();
+               cur.resetAnchor();
                cursor() = tmpcursor;
-               bv()->setSelection();
+               cur.setSelection();
                cutSelection(true, false);
        }
 }
@@ -1181,16 +1185,17 @@ void LyXText::deleteWordForward()
 // Delete from cursor to start of current or prior word.
 void LyXText::deleteWordBackward()
 {
+       LCursor & cur = bv()->cursor();
        if (cursorPar()->empty())
-               cursorLeft(bv());
+               cursorLeft(true);
        else {
                CursorSlice tmpcursor = cursor();
-               bv()->selection().set(true); // to avoid deletion
+               cur.selection() = true; // to avoid deletion
                cursorLeftOneWord();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
-               bv()->resetAnchor();
+               cur.resetAnchor();
                cursor() = tmpcursor;
-               bv()->setSelection();
+               cur.setSelection();
                cutSelection(true, false);
        }
 }
@@ -1199,19 +1204,20 @@ void LyXText::deleteWordBackward()
 // Kill to end of line.
 void LyXText::deleteLineForward()
 {
+       LCursor & cur = bv()->cursor();
        if (cursorPar()->empty()) {
                // Paragraph is empty, so we just go to the right
-               cursorRight(bv());
+               cursorRight(true);
        } else {
                CursorSlice tmpcursor = cursor();
-               bv()->selection().set(true); // to avoid deletion
+               cur.selection() = true; // to avoid deletion
                cursorEnd();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
-               bv()->resetAnchor();
+               cur.resetAnchor();
                cursor() = tmpcursor;
-               bv()->setSelection();
+               cur.setSelection();
                // What is this test for ??? (JMarc)
-               if (!bv()->selection().set())
+               if (!cur.selection())
                        deleteWordForward();
                else
                        cutSelection(true, false);
@@ -1221,19 +1227,20 @@ void LyXText::deleteLineForward()
 
 void LyXText::changeCase(LyXText::TextCase action)
 {
+       LCursor & cur = bv()->cursor();
        CursorSlice from;
        CursorSlice to;
 
-       if (bv()->selection().set()) {
-               from = bv()->selStart();
-               to = bv()->selEnd();
+       if (cur.selection()) {
+               from = cur.selBegin();
+               to = cur.selEnd();
        } else {
                from = cursor();
                getWord(from, to, lyx::PARTIAL_WORD);
                setCursor(to.par(), to.pos() + 1);
        }
 
-       recordUndo(Undo::ATOMIC, this, from.par(), to.par());
+       recordUndo(cur, Undo::ATOMIC, cur.anchor().par());
 
        pos_type pos = from.pos();
        int par = from.par();
@@ -1277,9 +1284,9 @@ void LyXText::Delete()
        cursorRight(true);
 
        // if you had success make a backspace
-       if (old_cursor.par() != cursor().par() || old_cursor.pos() !=
-cursor().pos()) {
-               recordUndo(Undo::DELETE, this, old_cursor.par());
+       if (old_cursor.par() != cursor().par()
+           || old_cursor.pos() != cursor().pos()) {
+               recordUndo(bv()->cursor(), Undo::DELETE, old_cursor.par());
                backspace();
        }
 }
@@ -1318,7 +1325,7 @@ void LyXText::backspace()
                }
 
                if (cursor().par() != 0)
-                       recordUndo(Undo::DELETE, this, cursor().par() - 1, cursor().par());
+                       recordUndo(bv()->cursor(), Undo::DELETE, cursor().par() - 1);
 
                ParagraphList::iterator tmppit = cursorPar();
                // We used to do cursorLeftIntern() here, but it is
@@ -1357,7 +1364,7 @@ void LyXText::backspace()
        } else {
                // this is the code for a normal backspace, not pasting
                // any paragraphs
-               recordUndo(Undo::DELETE, this, cursor().par());
+               recordUndo(bv()->cursor(), Undo::DELETE);
                // 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,
@@ -1378,6 +1385,8 @@ void LyXText::backspace()
 
 ParagraphList::iterator LyXText::cursorPar() const
 {
+       //lyxerr << "### cursorPar: cursor: " << bv()->cursor() << endl;
+       //lyxerr << "xxx cursorPar: cursor: " << cursor() << endl;
        return getPar(cursor().par());
 }
 
@@ -1396,10 +1405,11 @@ ParagraphList::iterator LyXText::getPar(CursorSlice const & cur) const
 
 ParagraphList::iterator LyXText::getPar(int par) const
 {
+       //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
        BOOST_ASSERT(par >= 0);
        BOOST_ASSERT(par < int(paragraphs().size()));
        ParagraphList::iterator pit = paragraphs().begin();
-       std::advance(pit, par);
+       advance(pit, par);
        return pit;
 }
 
@@ -1407,7 +1417,6 @@ ParagraphList::iterator LyXText::getPar(int par) const
 RowList::iterator
 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
 {
-       //lyxerr << "getRowNearY: y " << y << endl;
 #if 1
        ParagraphList::iterator const
                pend = boost::prior(paragraphs().end());
@@ -1448,36 +1457,18 @@ RowList::iterator LyXText::firstRow() const
 }
 
 
-ParagraphList::iterator LyXText::firstPar() const
-{
-       return paragraphs().begin();
-}
-
-
 RowList::iterator LyXText::lastRow() const
 {
        return boost::prior(endRow());
 }
 
 
-ParagraphList::iterator LyXText::lastPar() const
-{
-       return boost::prior(endPar());
-}
-
-
 RowList::iterator LyXText::endRow() const
 {
        return paragraphs().back().rows.end();
 }
 
 
-ParagraphList::iterator LyXText::endPar() const
-{
-       return paragraphs().end();
-}
-
-
 void LyXText::nextRow(ParagraphList::iterator & pit,
        RowList::iterator & rit) const
 {
@@ -1507,14 +1498,15 @@ void LyXText::previousRow(ParagraphList::iterator & pit,
 
 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
 {
-       if (!bv()->selection().set())
+       LCursor & cur = bv()->cursor();
+       if (!cur.selection())
                return string();
 
        // should be const ...
-       ParagraphList::iterator startpit = getPar(bv()->selStart());
-       ParagraphList::iterator endpit = getPar(bv()->selEnd());
-       size_t const startpos = bv()->selStart().pos();
-       size_t const endpos = bv()->selEnd().pos();
+       ParagraphList::iterator startpit = getPar(cur.selBegin());
+       ParagraphList::iterator endpit = getPar(cur.selEnd());
+       size_t const startpos = cur.selBegin().pos();
+       size_t const endpos = cur.selEnd().pos();
 
        if (startpit == endpit)
                return startpit->asString(buffer, startpos, endpos, label);
@@ -1537,7 +1529,7 @@ string LyXText::selectionAsString(Buffer const & buffer, bool label) const
 
 int LyXText::parOffset(ParagraphList::iterator pit) const
 {
-       return std::distance(paragraphs().begin(), pit);
+       return distance(paragraphs().begin(), pit);
 }
 
 
@@ -1601,7 +1593,7 @@ void LyXText::redoParagraph(ParagraphList::iterator pit)
 void LyXText::fullRebreak()
 {
        redoParagraphs(paragraphs().begin(), paragraphs().end());
-       bv()->resetAnchor();
+       bv()->cursor().resetAnchor();
 }
 
 
@@ -1767,7 +1759,7 @@ void LyXText::write(Buffer const & buf, std::ostream & os) const
 
 
 bool LyXText::read(Buffer const & buf, LyXLex & lex)
-{      
+{
        static Change current_change;
 
        bool the_end_read = false;
@@ -1854,23 +1846,11 @@ int LyXText::descent() const
 }
 
 
-int LyXText::cursorX() const
-{
-       return cursorX(cursor());
-}
-
-
-int LyXText::cursorY() const
-{
-       return cursorY(cursor());
-}
-
-
 int LyXText::cursorX(CursorSlice const & cur) const
 {
        ParagraphList::iterator pit = getPar(cur);
        if (pit->rows.empty())
-               return 0;
+               return xo_;
        Row const & row         = *pit->getRow(cur.pos());
        pos_type pos            = cur.pos();
        pos_type cursor_vpos    = 0;
@@ -1923,7 +1903,7 @@ int LyXText::cursorX(CursorSlice const & cur) const
                } else
                        x += singleWidth(pit, pos);
        }
-       return int(x);
+       return xo_ + int(x);
 }
 
 
@@ -1931,29 +1911,30 @@ int LyXText::cursorY(CursorSlice const & cur) const
 {
        Paragraph & par = *getPar(cur);
        Row & row = *par.getRow(cur.pos());
-       return par.y + row.y_offset() + row.baseline();
+       return yo_ + par.y + row.y_offset() + row.baseline();
 }
 
 
 CursorSlice & LyXText::cursor()
 {
-       return bv()->cursor();
+       //lyxerr << "# accessing slice " << findText(this) << endl;
+       if (this != bv()->cursor().text()) {
+               lyxerr << "cursor: " << bv()->cursor()
+                       << "\ntext: " << bv()->cursor().text() 
+                       << "\nthis: " << this << endl;
+               BOOST_ASSERT(false);
+       }
+       return bv()->cursor().current();
 }
 
 
 CursorSlice const & LyXText::cursor() const
 {
-       return bv()->cursor();
-}
-
-
-CursorSlice & LyXText::anchor()
-{
-       return bv()->anchor();
-}
-
-
-CursorSlice const & LyXText::anchor() const
-{
-       return bv()->anchor();
+       if (this != bv()->cursor().text()) {
+               lyxerr << "cursor: " << bv()->cursor()
+                       << "\ntext: " << bv()->cursor().text() 
+                       << "\nthis: " << this << endl;
+               BOOST_ASSERT(false);
+       }
+       return bv()->cursor().current();
 }