]> git.lyx.org Git - lyx.git/blobdiff - src/TextMetrics.cpp
Make the non-drawing cases faster in TextMetrics::drawParagraph
[lyx.git] / src / TextMetrics.cpp
index 6fb12b3734e4de2a4e8dd65a8c9afa5de4bd4691..69ab7b5b26234ff62dfd5b8016ed7cb5ea1dfa44 100644 (file)
@@ -1113,14 +1113,8 @@ pos_type TextMetrics::getPosNearX(Row const & row, int & x,
        int const xo = origin_.x_;
        x -= xo;
 
-       int offset = 0;
-       CursorSlice rowSlice(const_cast<InsetText &>(text_->inset()));
-       rowSlice.pit() = row.pit();
-       rowSlice.pos() = row.pos();
-
        // Adapt to cursor row scroll offset if applicable.
-       if (bv_->currentRowSlice() == rowSlice)
-               offset = bv_->horizScrollOffset();
+       int const offset = bv_->horizScrollOffset(text_, row.pit(), row.pos());
        x += offset;
 
        pos_type pos = row.pos();
@@ -1622,7 +1616,7 @@ void TextMetrics::deleteLineForward(Cursor & cur)
                text_->cursorForward(cur);
        } else {
                cur.resetAnchor();
-               cur.setSelection(true); // to avoid deletion
+               cur.selection(true); // to avoid deletion
                cursorEnd(cur);
                cur.setSelection();
                // What is this test for ??? (JMarc)
@@ -1870,15 +1864,31 @@ void TextMetrics::draw(PainterInfo & pi, int x, int y) const
 
 void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const x, int y) const
 {
-       BufferParams const & bparams = bv_->buffer().params();
        ParagraphMetrics const & pm = par_metrics_[pit];
        if (pm.rows().empty())
                return;
-
-       bool const original_drawing_state = pi.pain.isDrawingEnabled();
-       int const ww = bv_->workHeight();
        size_t const nrows = pm.rows().size();
 
+       // Use fast lane when drawing is disabled.
+       if (!pi.pain.isDrawingEnabled()) {
+               for (size_t i = 0; i != nrows; ++i) {
+
+                       Row const & row = pm.rows()[i];
+                       // Adapt to cursor row scroll offset if applicable.
+                       int row_x = x - bv_->horizScrollOffset(text_, pit, row.pos());
+                       if (i)
+                               y += row.ascent();
+
+                       RowPainter rp(pi, *text_, pit, row, row_x, y);
+
+                       rp.paintOnlyInsets();
+                       y += row.descent();
+               }
+               return;
+       }
+
+       BufferParams const & bparams = bv_->buffer().params();
+       int const ww = bv_->workHeight();
        Cursor const & cur = bv_->cursor();
        DocIterator sel_beg = cur.selectionBegin();
        DocIterator sel_end = cur.selectionEnd();
@@ -1908,25 +1918,25 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
        for (size_t i = 0; i != nrows; ++i) {
 
                Row const & row = pm.rows()[i];
-               int row_x = x;
+               // Adapt to cursor row scroll offset if applicable.
+               int row_x = x - bv_->horizScrollOffset(text_, pit, row.pos());
                if (i)
                        y += row.ascent();
 
-               CursorSlice rowSlice(const_cast<InsetText &>(text_->inset()));
-               rowSlice.pit() = pit;
-               rowSlice.pos() = row.pos();
+               RowPainter rp(pi, *text_, pit, row, row_x, y);
 
+               // It is not needed to draw on screen if we are not inside.
                bool const inside = (y + row.descent() >= 0
                        && y - row.ascent() < ww);
-
-               // Adapt to cursor row scroll offset if applicable.
-               if (bv_->currentRowSlice() == rowSlice)
-                       row_x -= bv_->horizScrollOffset();
-
-               // It is not needed to draw on screen if we are not inside.
-               pi.pain.setDrawingEnabled(inside && original_drawing_state);
-
-               RowPainter rp(pi, *text_, pit, row, row_x, y);
+               pi.pain.setDrawingEnabled(inside);
+               if (!inside) {
+                       // Paint only the insets to set inset cache correctly
+                       // FIXME: remove paintOnlyInsets when we know that positions
+                       // have already been set.
+                       rp.paintOnlyInsets();
+                       y += row.descent();
+                       continue;
+               }
 
                if (selection)
                        row.setSelectionAndMargins(sel_beg_par, sel_end_par);
@@ -1943,10 +1953,9 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
                }
 
                // Row signature; has row changed since last paint?
-               if (pi.pain.isDrawingEnabled())
-                       row.setCrc(pm.computeRowSignature(row, bparams));
+               row.setCrc(pm.computeRowSignature(row, bparams));
                bool row_has_changed = row.changed()
-                       || rowSlice == bv_->lastRowSlice();
+                       || bv_->hadHorizScrollOffset(text_, pit, row.pos());
 
                // Take this opportunity to spellcheck the row contents.
                if (row_has_changed && pi.do_spellcheck && lyxrc.spellcheck_continuously) {
@@ -1975,7 +1984,7 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
 
                // Instrumentation for testing row cache (see also
                // 12 lines lower):
-               if (lyxerr.debugging(Debug::PAINTING) && inside
+               if (lyxerr.debugging(Debug::PAINTING)
                        && (row.selection() || pi.full_repaint || row_has_changed)) {
                                string const foreword = text_->isMainText() ?
                                        "main text redraw " : "inset text redraw: ";
@@ -2013,7 +2022,7 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
                pi.full_repaint = tmp;
        }
        // Re-enable screen drawing for future use of the painter.
-       pi.pain.setDrawingEnabled(original_drawing_state);
+       pi.pain.setDrawingEnabled(true);
 
        //LYXERR(Debug::PAINTING, ".");
 }