]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
move some selection related stuff over to textcursor.C
[lyx.git] / src / text.C
index 632370f5991e47c4c9f11e6733cdf74ca7e39b73..4396e1989459bf91eaa2ddc0f26d4b09b5df8591 100644 (file)
@@ -87,7 +87,7 @@ void LyXText::updateRowPositions()
 
 int LyXText::top_y() const
 {
-       if (anchor_row_ == rowlist_.end() )
+       if (anchor_row_ == rowlist_.end())
                return 0;
 
        return anchor_row_->y() + anchor_row_offset_;
@@ -145,10 +145,12 @@ int LyXText::workWidth() const
 }
 
 
-int LyXText::workWidth(Inset * inset) const
+int LyXText::workWidth(Inset const * inset) const
 {
-       Paragraph * par = inset->parOwner();
-       lyx::Assert(par);
+       ParagraphList::iterator par = std::find(ownerParagraphs().begin(),
+                                               ownerParagraphs().end(),
+                                               *inset->parOwner());
+       //lyx::Assert(par);
 
        pos_type pos = par->getPositionOfInset(inset);
        lyx::Assert(pos != -1);
@@ -768,8 +770,7 @@ pos_type addressBreakPoint(pos_type i, Paragraph const & par)
 };
 
 
-pos_type
-LyXText::rowBreakPoint(Row const & row) const
+pos_type LyXText::rowBreakPoint(Row const & row) const
 {
        ParagraphList::iterator pit = row.par();
 
@@ -798,7 +799,7 @@ LyXText::rowBreakPoint(Row const & row) const
        // or the end of the par, then choose the possible break
        // nearest that.
 
-       int const left = leftMargin(const_cast<Row&>(row));
+       int const left = leftMargin(row);
        int x = left;
 
        // pixel width since last breakpoint
@@ -1459,10 +1460,9 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
        LyXLayout_ptr const & layout = cursor.par()->layout();
 
        // this is only allowed, if the current paragraph is not empty or caption
-       // and if it has not the keepempty flag aktive
-       if (cursor.par()->empty()
-          && layout->labeltype != LABEL_SENSITIVE
-          && !layout->keepempty)
+       // and if it has not the keepempty flag active
+       if (cursor.par()->empty() && !cursor.par()->allowEmpty()
+          && layout->labeltype != LABEL_SENSITIVE)
                return;
 
        setUndo(bv(), Undo::FINISH, cursor.par());
@@ -1485,7 +1485,7 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
        // breakParagraph call should return a bool if it inserts the
        // paragraph before or behind and we should react on that one
        // but we can fix this in 1.3.0 (Jug 20020509)
-       bool const isempty = (layout->keepempty && cursor.par()->empty());
+       bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
        ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(), cursor.pos(),
                       keep_layout);
 
@@ -1503,17 +1503,17 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
        // move one row up!
        // This touches only the screen-update. Otherwise we would may have
        // an empty row on the screen
-       if (cursor.pos() && cursor.row()->pos() == cursor.pos()
-           && !cursor.row()->par()->isNewline(cursor.pos() - 1))
+       if (cursor.pos() && cursorRow()->pos() == cursor.pos()
+           && !cursorRow()->par()->isNewline(cursor.pos() - 1))
        {
                cursorLeft(bv());
        }
 
-       int y = cursor.y() - cursor.row()->baseline();
+       int y = cursor.y() - cursorRow()->baseline();
 
        // Do not forget the special right address boxes
        if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
-               RowList::iterator r = cursor.row();
+               RowList::iterator r = cursorRow();
                RowList::iterator beg = rows().begin();
 
                while (r != beg && boost::prior(r)->par() == r->par()) {
@@ -1524,12 +1524,12 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
 
        postPaint(y);
 
-       removeParagraph(cursor.row());
+       removeParagraph(cursorRow());
 
        // set the dimensions of the cursor row
-       cursor.row()->fill(fill(cursor.row(), workWidth()));
+       cursorRow()->fill(fill(cursorRow(), workWidth()));
 
-       setHeightOfRow(cursor.row());
+       setHeightOfRow(cursorRow());
 
 #warning Trouble Point! (Lgb)
        // When ::breakParagraph is called from within an inset we must
@@ -1540,7 +1540,7 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
        while (!next_par->empty() && next_par->isNewline(0))
                next_par->erase(0);
 
-       insertParagraph(next_par, boost::next(cursor.row()));
+       insertParagraph(next_par, boost::next(cursorRow()));
        updateCounters();
 
        // This check is necessary. Otherwise the new empty paragraph will
@@ -1550,8 +1550,8 @@ void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
        else
                setCursor(cursor.par(), 0);
 
-       if (boost::next(cursor.row()) != rows().end())
-               breakAgain(boost::next(cursor.row()));
+       if (boost::next(cursorRow()) != rows().end())
+               breakAgain(boost::next(cursorRow()));
 
        need_break_row = rows().end();
 }
@@ -1575,8 +1575,8 @@ void LyXText::insertChar(char c)
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
 
-       bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
-               cursor.row()->par()->isFreeSpacing();
+       bool const freeSpacing = cursorRow()->par()->layout()->free_spacing ||
+               cursorRow()->par()->isFreeSpacing();
 
        if (lyxrc.auto_number) {
                static string const number_operators = "+-/*";
@@ -1670,17 +1670,17 @@ void LyXText::insertChar(char c)
        }
 
        // the display inset stuff
-       if (cursor.row()->pos() < cursor.row()->par()->size()
-           && cursor.row()->par()->isInset(cursor.row()->pos())) {
-               Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
+       if (cursorRow()->pos() < cursorRow()->par()->size()
+           && cursorRow()->par()->isInset(cursorRow()->pos())) {
+               Inset * inset = cursorRow()->par()->getInset(cursorRow()->pos());
                if (inset && (inset->display() || inset->needFullRow())) {
                        // force a new break
-                       cursor.row()->fill(-1); // to force a new break
+                       cursorRow()->fill(-1); // to force a new break
                }
        }
 
        // get the cursor row fist
-       RowList::iterator row = cursor.row();
+       RowList::iterator row = cursorRow();
        int y = cursor.y() - row->baseline();
        if (c != Paragraph::META_INSET) {
                // Here case LyXText::InsertInset  already insertet the character
@@ -1705,7 +1705,7 @@ void LyXText::insertChar(char c)
                || cursor.par()->isNewline(cursor.pos())
                || ((cursor.pos() + 1 < cursor.par()->size()) &&
                    cursor.par()->isInset(cursor.pos() + 1))
-               || cursor.row()->fill() == -1))
+               || cursorRow()->fill() == -1))
        {
                pos_type z = rowBreakPoint(*boost::prior(row));
 
@@ -1731,9 +1731,10 @@ void LyXText::insertChar(char c)
                                  false, cursor.boundary());
                        // cursor MUST be in row now.
 
-                       if (boost::next(row) != rows().end() &&
-                           boost::next(row)->par() == row->par())
-                               need_break_row = boost::next(row);
+                       RowList::iterator next_row = boost::next(row);
+                       if (next_row != rows().end() &&
+                           next_row->par() == row->par())
+                               need_break_row = next_row;
                        else
                                need_break_row = rows().end();
 
@@ -1757,13 +1758,18 @@ void LyXText::insertChar(char c)
        if (c == Paragraph::META_INSET || row->fill() < 0) {
                postPaint(y);
                breakAgainOneRow(row);
+
+               RowList::iterator next_row = boost::next(row);
+
                // will the cursor be in another row now?
                if (lastPos(*this, row) <= cursor.pos() + 1 &&
-                   boost::next(row) != rows().end()) {
-                       if (boost::next(row) != rows().end() &&
-                           boost::next(row)->par() == row->par())
+                   next_row != rows().end()) {
+                       if (next_row != rows().end() &&
+                           next_row->par() == row->par()) {
                                // this should always be true
                                ++row;
+                       }
+
                        breakAgainOneRow(row);
                }
                current_font = rawtmpfont;
@@ -1775,9 +1781,12 @@ void LyXText::insertChar(char c)
                    != cursor.boundary())
                        setCursor(cursor.par(), cursor.pos(), false,
                          !cursor.boundary());
-               if (boost::next(row) != rows().end() &&
-                   boost::next(row)->par() == row->par())
-                       need_break_row = boost::next(row);
+
+               next_row = boost::next(row);
+
+               if (next_row != rows().end() &&
+                   next_row->par() == row->par())
+                       need_break_row = next_row;
                else
                        need_break_row = rows().end();
        } else {
@@ -2281,7 +2290,6 @@ void LyXText::deleteWordForward()
                cursorRight(bv());
        else {
                LyXCursor tmpcursor = cursor;
-               tmpcursor.row(0); // ??
                selection.set(true); // to avoid deletion
                cursorRightOneWord();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
@@ -2302,7 +2310,6 @@ void LyXText::deleteWordBackward()
                cursorLeft(bv());
        else {
                LyXCursor tmpcursor = cursor;
-               tmpcursor.row(0); // ??
                selection.set(true); // to avoid deletion
                cursorLeftOneWord();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
@@ -2324,7 +2331,6 @@ void LyXText::deleteLineForward()
                LyXCursor tmpcursor = cursor;
                // We can't store the row over a regular setCursor
                // so we set it to 0 and reset it afterwards.
-               tmpcursor.row(0); // ??
                selection.set(true); // to avoid deletion
                cursorEnd();
                setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
@@ -2354,8 +2360,6 @@ void LyXText::changeCase(LyXText::TextCase action)
                setCursor(to.par(), to.pos() + 1);
        }
 
-       lyx::Assert(from <= to);
-
        setUndo(bv(), Undo::FINISH, from.par(), to.par());
 
        pos_type pos = from.pos();
@@ -2390,8 +2394,8 @@ void LyXText::changeCase(LyXText::TextCase action)
                ++pos;
        }
 
-       if (to.row() != from.row())
-               postPaint(from.y() - from.row()->baseline());
+       if (getRow(to) != getRow(from))
+               postPaint(from.y() - getRow(from)->baseline());
 }
 
 
@@ -2473,10 +2477,10 @@ void LyXText::backspace()
                                cursorLeft(bv());
 
                                // the layout things can change the height of a row !
-                               int const tmpheight = cursor.row()->height();
-                               setHeightOfRow(cursor.row());
-                               if (cursor.row()->height() != tmpheight) {
-                                       postPaint(cursor.y() - cursor.row()->baseline());
+                               int const tmpheight = cursorRow()->height();
+                               setHeightOfRow(cursorRow());
+                               if (cursorRow()->height() != tmpheight) {
+                                       postPaint(cursor.y() - cursorRow()->baseline());
                                }
                                return;
                        }
@@ -2489,7 +2493,7 @@ void LyXText::backspace()
                }
 
                ParagraphList::iterator tmppit = cursor.par();
-               RowList::iterator tmprow = cursor.row();
+               RowList::iterator tmprow = cursorRow();
 
                // We used to do cursorLeftIntern() here, but it is
                // not a good idea since it triggers the auto-delete
@@ -2531,7 +2535,7 @@ void LyXText::backspace()
                                if (cursor.pos())
                                        cursor.pos(cursor.pos() - 1);
 
-                       postPaint(cursor.y() - cursor.row()->baseline());
+                       postPaint(cursor.y() - cursorRow()->baseline());
 
                        // remove the lost paragraph
                        // This one is not safe, since the paragraph that the tmprow and the
@@ -2542,7 +2546,7 @@ void LyXText::backspace()
                        //RemoveRow(tmprow);
 
                        // This rebuilds the rows.
-                       appendParagraph(cursor.row());
+                       appendParagraph(cursorRow());
                        updateCounters();
 
                        // the row may have changed, block, hfills etc.
@@ -2570,7 +2574,7 @@ void LyXText::backspace()
                        }
                }
 
-               RowList::iterator row = cursor.row();
+               RowList::iterator row = cursorRow();
                int y = cursor.y() - row->baseline();
                pos_type z;
                // remember that a space at the end of a row doesnt count
@@ -2749,6 +2753,42 @@ void LyXText::backspace()
 }
 
 
+RowList::iterator LyXText::cursorRow() const
+{
+       return getRow(cursor.par(), cursor.pos());
+}
+
+
+RowList::iterator LyXText::getRow(LyXCursor const & cur) const
+{
+       return getRow(cur.par(), cur.pos());
+}
+
+
+RowList::iterator
+LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
+{
+       if (rows().empty())
+               return rowlist_.end();
+
+       // find the first row of the specified paragraph
+       RowList::iterator rit = rowlist_.begin();
+       RowList::iterator end = rowlist_.end();
+       while (boost::next(rit) != end && rit->par() != pit) {
+               ++rit;
+       }
+
+       // now find the wanted row
+       while (rit->pos() < pos
+              && boost::next(rit) != end
+              && boost::next(rit)->par() == pit
+              && boost::next(rit)->pos() <= pos) {
+               ++rit;
+       }
+
+       return rit;
+}
+
 // returns pointer to a specified row
 RowList::iterator
 LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const