]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
make "make distcheck" work
[lyx.git] / src / text2.C
index 2f90fd73b1a4db07bf11eaef1795a8746fb05638..673287fbbb595796f0fedb4242fbf7cb15b9ebde 100644 (file)
@@ -58,7 +58,7 @@
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
-#include "support/tostr.h"
+#include "support/convert.h"
 
 #include <sstream>
 
@@ -74,7 +74,8 @@ using std::string;
 LyXText::LyXText(BufferView * bv)
        : maxwidth_(bv ? bv->workWidth() : 100),
          background_color_(LColor::background),
-         bv_owner(bv)
+         bv_owner(bv),
+         autoBreakRows_(false)
 {}
 
 
@@ -118,7 +119,7 @@ InsetBase * LyXText::checkInsetHit(int x, int y) const
                InsetBase * inset = iit->inset;
 #if 1
                lyxerr << "examining inset " << inset << endl;
-               if (theCoords.insets_.has(inset))
+               if (theCoords.getInsets().has(inset))
                        lyxerr
                                << " xo: " << inset->xo() << "..."
                                << inset->xo() + inset->width()
@@ -335,7 +336,7 @@ void LyXText::setLayout(LCursor & cur, string const & layout)
 
        pit_type start = cur.selBegin().pit();
        pit_type end = cur.selEnd().pit() + 1;
-       pit_type endpit = setLayout(start, end, layout);
+       setLayout(start, end, layout);
        updateCounters();
 }
 
@@ -343,18 +344,6 @@ void LyXText::setLayout(LCursor & cur, string const & layout)
 namespace {
 
 
-void getSelectionSpan(LCursor & cur, pit_type & beg, pit_type & end)
-{
-       if (!cur.selection()) {
-               beg = cur.pit();
-               end = cur.pit() + 1;
-       } else {
-               beg = cur.selBegin().pit();
-               end = cur.selEnd().pit() + 1;
-       }
-}
-
-
 bool changeDepthAllowed(LyXText::DEPTH_CHANGE type,
        Paragraph const & par, int max_depth)
 {
@@ -375,11 +364,9 @@ bool changeDepthAllowed(LyXText::DEPTH_CHANGE type,
 bool LyXText::changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const
 {
        BOOST_ASSERT(this == cur.text());
-       pit_type beg, end;
-       getSelectionSpan(cur, beg, end);
-       int max_depth = 0;
-       if (beg != 0)
-               max_depth = pars_[beg - 1].getMaxDepthAfter();
+       pit_type const beg = cur.selBegin().pit();
+       pit_type const end = cur.selEnd().pit() + 1;
+       int max_depth = (beg != 0 ? pars_[beg - 1].getMaxDepthAfter() : 0);
 
        for (pit_type pit = beg; pit != end; ++pit) {
                if (::changeDepthAllowed(type, pars_[pit], max_depth))
@@ -393,13 +380,10 @@ bool LyXText::changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const
 void LyXText::changeDepth(LCursor & cur, DEPTH_CHANGE type)
 {
        BOOST_ASSERT(this == cur.text());
-       pit_type beg, end;
-       getSelectionSpan(cur, beg, end);
+       pit_type const beg = cur.selBegin().pit();
+       pit_type const end = cur.selEnd().pit() + 1;
        recordUndoSelection(cur);
-
-       int max_depth = 0;
-       if (beg != 0)
-               max_depth = pars_[beg - 1].getMaxDepthAfter();
+       int max_depth = (beg != 0 ? pars_[beg - 1].getMaxDepthAfter() : 0);
 
        for (pit_type pit = beg; pit != end; ++pit) {
                Paragraph & par = pars_[pit];
@@ -456,7 +440,9 @@ void LyXText::setFont(LCursor & cur, LyXFont const & font, bool toggleall)
 
        // Don't use forwardChar here as ditend might have
        // pos() == lastpos() and forwardChar would miss it.
-       for (; dit != ditend; dit.forwardPos()) {
+       // Can't use forwardPos either as this descends into
+       // nested insets. 
+       for (; dit != ditend; dit.forwardPosNoDescend()) {
                if (dit.pos() != dit.lastpos()) {
                        LyXFont f = getFont(dit.paragraph(), dit.pos());
                        f.update(font, params.language, toggleall);
@@ -807,8 +793,10 @@ void LyXText::setCounter(Buffer const & buf, pit_type pit)
                        bool isOK = false;
                        while (tmppit != end) {
                                in = pars_[tmppit].inInset();
-                               if (in->lyxCode() == InsetBase::FLOAT_CODE ||
-                                   in->lyxCode() == InsetBase::WRAP_CODE) {
+                               // FIXME: in should be always valid.
+                               if (in &&
+                                   (in->lyxCode() == InsetBase::FLOAT_CODE ||
+                                    in->lyxCode() == InsetBase::WRAP_CODE)) {
                                        isOK = true;
                                        break;
                                }
@@ -910,7 +898,8 @@ void LyXText::insertStringAsLines(LCursor & cur, string const & str)
 
        // only to be sure, should not be neccessary
        cur.clearSelection();
-       cur.buffer().insertStringAsLines(pars_, pit, pos, current_font, str);
+       cur.buffer().insertStringAsLines(pars_, pit, pos, current_font, str,
+               autoBreakRows_);
 
        cur.resetAnchor();
        setCursor(cur, cur.pit(), pos);
@@ -1137,8 +1126,8 @@ pos_type LyXText::getColumnNearX(pit_type const pit,
 pit_type LyXText::getPitNearY(int y) const
 {
        BOOST_ASSERT(!paragraphs().empty());
-       BOOST_ASSERT(theCoords.pars_.find(this) != theCoords.pars_.end());
-       CoordCache::InnerParPosCache const & cc = theCoords.pars_[this];
+       BOOST_ASSERT(theCoords.getParPos().find(this) != theCoords.getParPos().end());
+       CoordCache::InnerParPosCache const & cc = theCoords.getParPos().find(this)->second;
        lyxerr << "LyXText::getPitNearY: y: " << y << " cache size: "
                << cc.size() << endl;
 
@@ -1174,6 +1163,7 @@ Row const & LyXText::getRowNearY(int y, pit_type pit) const
        return *rit;
 }
 
+
 // x,y are absolute screen coordinates
 // sets cursor recursively descending into nested editable insets
 InsetBase * LyXText::editXY(LCursor & cur, int x, int y) const
@@ -1194,12 +1184,12 @@ InsetBase * LyXText::editXY(LCursor & cur, int x, int y) const
        InsetBase * inset = checkInsetHit(x, y);
        lyxerr << "inset " << inset << " hit at x: " << x << " y: " << y << endl;
        if (!inset) {
-               //either we deconst editXY or better we move current_font
-               //and real_current_font to LCursor
+               // Either we deconst editXY or better we move current_font
+               // and real_current_font to LCursor
                const_cast<LyXText *>(this)->setCurrentFont(cur);
                return 0;
        }
-       
+
        // This should be just before or just behind the
        // cursor position set above.
        BOOST_ASSERT((pos != 0 && inset == pars_[pit].getInset(pos - 1))
@@ -1229,49 +1219,52 @@ bool LyXText::checkAndActivateInset(LCursor & cur, bool front)
 }
 
 
-void LyXText::cursorLeft(LCursor & cur)
+bool LyXText::cursorLeft(LCursor & cur)
 {
        if (cur.pos() != 0) {
                bool boundary = cur.boundary();
-               setCursor(cur, cur.pit(), cur.pos() - 1, true, false);
+               bool updateNeeded = setCursor(cur, cur.pit(), cur.pos() - 1, true, false);
                if (!checkAndActivateInset(cur, false)) {
                        if (false && !boundary &&
                                        bidi.isBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
-                               setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
+                               updateNeeded |=
+                                       setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
                }
-               return;
+               return updateNeeded;
        }
 
        if (cur.pit() != 0) {
-               // steps into the paragraph above
-               setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
+               // Steps into the paragraph above
+               return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
        }
+       return false;
 }
 
 
-void LyXText::cursorRight(LCursor & cur)
+bool LyXText::cursorRight(LCursor & cur)
 {
        if (false && cur.boundary()) {
-               setCursor(cur, cur.pit(), cur.pos(), true, false);
-               return;
+               return setCursor(cur, cur.pit(), cur.pos(), true, false);
        }
 
        if (cur.pos() != cur.lastpos()) {
+               bool updateNeeded = false;
                if (!checkAndActivateInset(cur, true)) {
-                       setCursor(cur, cur.pit(), cur.pos() + 1, true, false);
+                       updateNeeded |= setCursor(cur, cur.pit(), cur.pos() + 1, true, false);
                        if (false && bidi.isBoundary(cur.buffer(), cur.paragraph(),
                                                         cur.pos()))
-                               setCursor(cur, cur.pit(), cur.pos(), true, true);
+                               updateNeeded |= setCursor(cur, cur.pit(), cur.pos(), true, true);
                }
-               return;
+               return updateNeeded;
        }
 
        if (cur.pit() != cur.lastpit())
-               setCursor(cur, cur.pit() + 1, 0);
+               return setCursor(cur, cur.pit() + 1, 0);
+       return false;
 }
 
 
-void LyXText::cursorUp(LCursor & cur)
+bool LyXText::cursorUp(LCursor & cur)
 {
        Paragraph const & par = cur.paragraph();
        int const row = par.pos2row(cur.pos());
@@ -1279,62 +1272,102 @@ void LyXText::cursorUp(LCursor & cur)
 
        if (!cur.selection()) {
                int const y = bv_funcs::getPos(cur).y_;
+               LCursor old = cur;
                editXY(cur, x, y - par.rows()[row].ascent() - 1);
-               return;
+
+               // This happens when you move out of an inset.
+               // And to give the DEPM the possibility of doing
+               // something we must provide it with two different
+               // cursors. (Lgb)
+               LCursor dummy = cur;
+               if (dummy == old)
+                       ++dummy.pos();
+
+               return deleteEmptyParagraphMechanism(dummy, old);
        }
 
+       bool updateNeeded = false;
+
        if (row > 0) {
-               setCursor(cur, cur.pit(), x2pos(cur.pit(), row - 1, x));
+               updateNeeded |= setCursor(cur, cur.pit(),
+                                         x2pos(cur.pit(), row - 1, x));
        } else if (cur.pit() > 0) {
                --cur.pit();
-               setCursor(cur, cur.pit(), x2pos(cur.pit(), cur.paragraph().rows().size() - 1, x));
-               
+               updateNeeded |= setCursor(cur, cur.pit(),
+                                         x2pos(cur.pit(), par.rows().size() - 1, x));
        }
 
        cur.x_target() = x;
+
+       return updateNeeded;
 }
 
 
-void LyXText::cursorDown(LCursor & cur)
+bool LyXText::cursorDown(LCursor & cur)
 {
-
        Paragraph const & par = cur.paragraph();
        int const row = par.pos2row(cur.pos());
        int const x = cur.targetX();
 
        if (!cur.selection()) {
                int const y = bv_funcs::getPos(cur).y_;
+               LCursor old = cur;
                editXY(cur, x, y + par.rows()[row].descent() + 1);
-               return;
+
+               // This happens when you move out of an inset.
+               // And to give the DEPM the possibility of doing
+               // something we must provide it with two different
+               // cursors. (Lgb)
+               LCursor dummy = cur;
+               if (dummy == old)
+                       ++dummy.pos();
+
+               bool const changed = deleteEmptyParagraphMechanism(dummy, old);
+
+               // Make sure that cur gets back whatever happened to dummy(Lgb)
+               if (changed)
+                       cur = dummy;
+
+               return changed;
+
        }
-       
+
+       bool updateNeeded = false;
+
        if (row + 1 < int(par.rows().size())) {
-               setCursor(cur, cur.pit(), x2pos(cur.pit(), row + 1, x));
+               updateNeeded |= setCursor(cur, cur.pit(),
+                                         x2pos(cur.pit(), row + 1, x));
        } else if (cur.pit() + 1 < int(paragraphs().size())) {
                ++cur.pit();
-               setCursor(cur, cur.pit(), x2pos(cur.pit(), 0, x));
+               updateNeeded |= setCursor(cur, cur.pit(),
+                                         x2pos(cur.pit(), 0, x));
        }
-       
+
        cur.x_target() = x;
+
+       return updateNeeded;
 }
 
 
-void LyXText::cursorUpParagraph(LCursor & cur)
+bool LyXText::cursorUpParagraph(LCursor & cur)
 {
+       bool updated = false;
        if (cur.pos() > 0)
-               setCursor(cur, cur.pit(), 0);
+               updated = setCursor(cur, cur.pit(), 0);
        else if (cur.pit() != 0)
-               setCursor(cur, cur.pit() - 1, 0);
+               updated = setCursor(cur, cur.pit() - 1, 0);
+       return updated;
 }
 
 
-void LyXText::cursorDownParagraph(LCursor & cur)
+bool LyXText::cursorDownParagraph(LCursor & cur)
 {
+       bool updated = false;
        if (cur.pit() != cur.lastpit())
-               setCursor(cur, cur.pit() + 1, 0);
+               updated = setCursor(cur, cur.pit() + 1, 0);
        else
-               setCursor(cur, cur.pit(), cur.lastpos());
+               updated = setCursor(cur, cur.pit(), cur.lastpos());
+       return updated;
 }
 
 
@@ -1342,16 +1375,16 @@ void LyXText::cursorDownParagraph(LCursor & cur)
 // position. Called by deleteEmptyParagraphMechanism
 void LyXText::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
 {
-       // do notheing if cursor is not in the paragraph where the
+       // Do nothing if cursor is not in the paragraph where the
        // deletion occured,
        if (cur.pit() != where.pit())
                return;
 
-       // if cursor position is after the deletion place update it
+       // If cursor position is after the deletion place update it
        if (cur.pos() > where.pos())
                --cur.pos();
 
-       // check also if we don't want to set the cursor on a spot behind the
+       // Check also if we don't want to set the cursor on a spot behind the
        // pagragraph because we erased the last character.
        if (cur.pos() > cur.lastpos())
                cur.pos() = cur.lastpos();
@@ -1360,7 +1393,6 @@ void LyXText::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
 
 bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
 {
-       BOOST_ASSERT(cur.size() == old.size());
        // Would be wrong to delete anything if we have a selection.
        if (cur.selection())
                return false;
@@ -1416,7 +1448,7 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
 #warning DEPM, look here
 #endif
                        //fixCursorAfterDelete(cur.anchor(), old.top());
-                       return false;
+                       return true;
                }
        }
 
@@ -1438,8 +1470,6 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
 
        if (oldpar.empty() || (oldpar.size() == 1 && oldpar.isLineSeparator(0))) {
                // ok, we will delete something
-               CursorSlice tmpcursor;
-
                deleted = true;
 
                bool selection_position_was_oldcursor_position =