]> git.lyx.org Git - lyx.git/blobdiff - src/rowpainter.C
minimal effort implementation of:
[lyx.git] / src / rowpainter.C
index 8543e147debb00e34680de51d50d402027b5e7f7..45bae7438c8b98a0d038623f75ad8f8d609a3e18 100644 (file)
 
 #include "frontends/font_metrics.h"
 #include "frontends/nullpainter.h"
+#include "frontends/LyXView.h"
+#include "frontends/WorkArea.h"
 #include "frontends/Painter.h"
 
 #include "insets/insettext.h"
 
 #include "support/textutils.h"
 
+#include <boost/crc.hpp>
+
+using lyx::docstring;
+using lyx::frontend::Painter;
+using lyx::frontend::NullPainter;
+using lyx::char_type;
 using lyx::pos_type;
 using lyx::pit_type;
 
@@ -51,6 +59,10 @@ using std::string;
 
 namespace {
 
+/// Flag: do a full redraw of inside text of inset
+/// Working variable indicating a full screen refresh
+bool refreshInside;
+
 /**
  * A class used for painting an individual row of text.
  */
@@ -69,11 +81,11 @@ public:
        void paintText();
 
 private:
-       void paintForeignMark(double orig_x, LyXFont const & font);
+       void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
        void paintHebrewComposeChar(lyx::pos_type & vpos, LyXFont const & font);
        void paintArabicComposeChar(lyx::pos_type & vpos, LyXFont const & font);
-       void paintChars(lyx::pos_type & vpos, LyXFont font, 
-                       bool hebrew, bool arabic);
+       void paintChars(lyx::pos_type & vpos, LyXFont font,
+                       bool hebrew, bool arabic);
        int paintAppendixStart(int y);
        void paintFromPos(lyx::pos_type & vpos);
        void paintInset(lyx::pos_type const pos, LyXFont const & font);
@@ -154,12 +166,25 @@ void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
        InsetBase const * inset = par_.getInset(pos);
        BOOST_ASSERT(inset);
        PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
-       pi.base.font = font;
+       // FIXME: We should always use font, see documentation of
+       // noFontChange() in insetbase.h.
+       pi.base.font = inset->noFontChange() ?
+               bv_.buffer()->params().getFont() :
+               font;
        pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
        pi.erased_ = erased_ || isDeletedText(par_, pos);
        theCoords.insets().add(inset, int(x_), yo_);
-       inset->drawSelection(pi, int(x_), yo_);
+       InsetText const * const in = inset->asTextInset();
+       // non-wide insets are painted completely. Recursive
+       bool tmp = refreshInside;
+       if (!in || !in->Wide()) {
+               refreshInside = true;
+               lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
+       }
+       if (refreshInside)
+               inset->drawSelection(pi, int(x_), yo_);
        inset->draw(pi, int(x_), yo_);
+       refreshInside = tmp;
        x_ += inset->width();
 }
 
@@ -171,7 +196,7 @@ void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
        string str;
 
        // first char
-       char c = par_.getChar(pos);
+       char_type c = par_.getChar(pos);
        str += c;
        ++vpos;
 
@@ -180,8 +205,8 @@ void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
 
        for (pos_type i = pos - 1; i >= 0; --i) {
                c = par_.getChar(i);
-               if (!Encodings::IsComposeChar_hebrew(c)) {
-                       if (IsPrintableNonspace(c)) {
+               if (!Encodings::isComposeChar_hebrew(c)) {
+                       if (isPrintableNonspace(c)) {
                                int const width2 =
                                        text_.singleWidth(par_, i, c, text_.getFont(par_, i));
                                // dalet / resh
@@ -194,7 +219,8 @@ void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
        }
 
        // Draw nikud
-       pain_.text(int(x_) + dx, yo_, str, font);
+        docstring dstr(str.begin(), str.end());
+       pain_.text(int(x_) + dx, yo_, dstr, font);
 }
 
 
@@ -204,7 +230,7 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
        string str;
 
        // first char
-       char c = par_.getChar(pos);
+       char_type c = par_.getChar(pos);
        c = par_.transformChar(c, pos);
        str += c;
        ++vpos;
@@ -214,9 +240,9 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
 
        for (pos_type i = pos - 1; i >= 0; --i) {
                c = par_.getChar(i);
-               if (!Encodings::IsComposeChar_arabic(c)) {
-                       if (IsPrintableNonspace(c)) {
-                               int const width2 = 
+               if (!Encodings::isComposeChar_arabic(c)) {
+                       if (isPrintableNonspace(c)) {
+                               int const width2 =
                                        text_.singleWidth(par_, i, c, text_.getFont(par_, i));
                                dx = (width2 - width) / 2;
                        }
@@ -224,23 +250,29 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
                }
        }
        // Draw nikud
-       pain_.text(int(x_) + dx, yo_, str, font);
+        docstring dstr(str.begin(), str.end());
+       pain_.text(int(x_) + dx, yo_, dstr, font);
 }
 
 
-void RowPainter::paintChars(pos_type & vpos, LyXFont font, 
+void RowPainter::paintChars(pos_type & vpos, LyXFont font,
                            bool hebrew, bool arabic)
 {
        pos_type pos = text_.bidi.vis2log(vpos);
        pos_type const end = row_.endpos();
        FontSpan const font_span = par_.fontSpan(pos);
-       Change::Type const prev_change = par_.lookupChange(pos);
+       Change::Type const prev_change = par_.lookupChange(pos).type;
 
        // first character
+#if 0
        string str;
        str += par_.getChar(pos);
+#else
+       std::vector<char_type> str;
+       str.push_back(par_.getChar(pos));
+#endif
        if (arabic) {
-               unsigned char c = str[0];
+               char_type c = str[0];
                str[0] = par_.transformChar(c, pos);
        }
 
@@ -253,21 +285,25 @@ void RowPainter::paintChars(pos_type & vpos, LyXFont font,
                if (prev_change != par_.lookupChange(pos))
                        break;
 
-               char c = par_.getChar(pos);
+               char_type c = par_.getChar(pos);
 
-               if (!IsPrintableNonspace(c))
+               if (!isPrintableNonspace(c))
                        break;
 
-               if (arabic && Encodings::IsComposeChar_arabic(c))
+               if (arabic && Encodings::isComposeChar_arabic(c))
                        break;
 
-               if (hebrew && Encodings::IsComposeChar_hebrew(c))
+               if (hebrew && Encodings::isComposeChar_hebrew(c))
                        break;
 
                if (arabic)
                        c = par_.transformChar(c, pos);
 
+#if 0
                str += c;
+#else
+               str.push_back(c);
+#endif
        }
 
        if (prev_change == Change::DELETED)
@@ -277,12 +313,17 @@ void RowPainter::paintChars(pos_type & vpos, LyXFont font,
 
        // Draw text and set the new x position
        //lyxerr << "paint row: yo_ " << yo_ << "\n";
+#if 0
        pain_.text(int(x_), yo_, str, font);
        x_ += font_metrics::width(str, font);
+#else
+       pain_.text(int(x_), yo_, &str[0], str.size(), font);
+       x_ += font_metrics::width(&str[0], str.size(), font);
+#endif
 }
 
 
-void RowPainter::paintForeignMark(double orig_x, LyXFont const & font)
+void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
 {
        if (!lyxrc.mark_foreign_language)
                return;
@@ -291,7 +332,7 @@ void RowPainter::paintForeignMark(double orig_x, LyXFont const & font)
        if (font.language() == bv_.buffer()->params().language)
                return;
 
-       int const y = yo_ + 1;
+       int const y = yo_ + 1 + desc;
        pain_.line(int(orig_x), y, int(x_), y, LColor::language);
 }
 
@@ -300,19 +341,19 @@ void RowPainter::paintFromPos(pos_type & vpos)
 {
        pos_type const pos = text_.bidi.vis2log(vpos);
        LyXFont orig_font = text_.getFont(par_, pos);
-       text_.applyOuterFont(orig_font);
 
        double const orig_x = x_;
 
        if (par_.isInset(pos)) {
                paintInset(pos, orig_font);
                ++vpos;
-               paintForeignMark(orig_x, orig_font);
+               paintForeignMark(orig_x, orig_font,
+                       par_.getInset(pos)->descent());
                return;
        }
 
        // usual characters, no insets
-       char const c = par_.getChar(pos);
+       char_type const c = par_.getChar(pos);
 
        // special case languages
        std::string const & lang = orig_font.language()->lang();
@@ -323,8 +364,8 @@ void RowPainter::paintFromPos(pos_type & vpos)
 
        // draw as many chars as we can
        if ((!hebrew && !arabic)
-               || (hebrew && !Encodings::IsComposeChar_hebrew(c))
-               || (arabic && !Encodings::IsComposeChar_arabic(c))) {
+               || (hebrew && !Encodings::isComposeChar_hebrew(c))
+               || (arabic && !Encodings::isComposeChar_arabic(c))) {
                paintChars(vpos, orig_font, hebrew, arabic);
        } else if (hebrew) {
                paintHebrewComposeChar(vpos, orig_font);
@@ -341,7 +382,7 @@ void RowPainter::paintChangeBar()
        pos_type const start = row_.pos();
        pos_type const end = row_.endpos();
 
-       if (start == end || !par_.isChanged(start, end - 1))
+       if (start == end || !par_.isChanged(start, end))
                return;
 
        int const height = text_.isLastRow(pit_, row_)
@@ -420,12 +461,13 @@ int RowPainter::paintAppendixStart(int y)
        int w = 0;
        int a = 0;
        int d = 0;
-       font_metrics::rectText(label, pb_font, w, a, d);
+        docstring dlab(label.begin(), label.end());
+       font_metrics::rectText(dlab, pb_font, w, a, d);
 
        int const text_start = int(xo_ + (width_ - w) / 2);
        int const text_end = text_start + w;
 
-       pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
+       pain_.rectText(text_start, y + d, dlab, pb_font, LColor::none, LColor::none);
 
        pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
        pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
@@ -478,6 +520,7 @@ void RowPainter::paintFirst()
                string const str = par_.getLabelstring();
                if (!str.empty()) {
                        double x = x_;
+                        docstring dstr(str.begin(), str.end());
 
                        // this is special code for the chapter layout. This is
                        // printed in an extra row and has a pagebreak at
@@ -497,20 +540,22 @@ void RowPainter::paintFirst()
 
                                if (is_rtl) {
                                        x = width_ - leftMargin() -
-                                               font_metrics::width(str, font);
+                                               font_metrics::width(dstr, font);
                                }
 
-                               pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
+                               pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
                        } else {
+                                string lab = layout->labelsep;
+                                docstring dlab(lab.begin(), lab.end());
                                if (is_rtl) {
                                        x = width_ - leftMargin()
-                                               + font_metrics::width(layout->labelsep, font);
+                                               + font_metrics::width(dlab, font);
                                } else {
-                                       x = x_ - font_metrics::width(layout->labelsep, font)
-                                               - font_metrics::width(str, font);
+                                       x = x_ - font_metrics::width(dlab, font)
+                                               - font_metrics::width(dstr, font);
                                }
 
-                               pain_.text(int(x), yo_, str, font);
+                               pain_.text(int(x), yo_, dstr, font);
                        }
                }
 
@@ -523,6 +568,7 @@ void RowPainter::paintFirst()
                LyXFont font = getLabelFont();
                if (!par_.getLabelstring().empty()) {
                        string const str = par_.getLabelstring();
+                        docstring dstr(str.begin(), str.end());
                        double spacing_val = 1.0;
                        if (!parparams.spacing().isDefault())
                                spacing_val = parparams.spacing().getValue();
@@ -540,12 +586,12 @@ void RowPainter::paintFirst()
                                if (is_rtl)
                                        x = leftMargin();
                                x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
-                               x -= font_metrics::width(str, font) / 2;
+                               x -= font_metrics::width(dstr, font) / 2;
                        } else if (is_rtl) {
                                x = width_ - leftMargin() -
-                                       font_metrics::width(str, font);
+                                       font_metrics::width(dstr, font);
                        }
-                       pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
+                       pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
                }
        }
 }
@@ -578,10 +624,11 @@ void RowPainter::paintLast()
        case END_LABEL_STATIC: {
                LyXFont font = getLabelFont();
                string const & str = par_.layout()->endlabelstring();
+                docstring dstr(str.begin(), str.end());
                double const x = is_rtl ?
-                       x_ - font_metrics::width(str, font)
+                       x_ - font_metrics::width(dstr, font)
                        : - text_.rightMargin(par_) - row_.width();
-               pain_.text(int(x), yo_, str, font);
+               pain_.text(int(x), yo_, dstr, font);
                break;
        }
 
@@ -606,7 +653,7 @@ void RowPainter::paintText()
        bool is_struckout = false;
        int last_strikeout_x = 0;
 
-       // Use font span to speed things up, see below 
+       // Use font span to speed things up, see below
        FontSpan font_span;
        LyXFont font;
 
@@ -651,14 +698,17 @@ void RowPainter::paintText()
                if (running_strikeout && (highly_editable_inset || !is_struckout)) {
                        // Calculate 1/3 height of the buffer's default font
                        int const middle =
-                               yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
+                               yo_ -
+                               font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
                        pain_.line(last_strikeout_x, middle, int(x_), middle,
                                LColor::strikeout, Painter::line_solid, Painter::line_thin);
                        running_strikeout = false;
                }
 
                if (body_pos > 0 && pos == body_pos - 1) {
-                       int const lwidth = font_metrics::width(layout->labelsep,
+                        string lab = layout->labelsep;
+                        docstring dlab(lab.begin(), lab.end());
+                       int const lwidth = font_metrics::width(dlab,
                                getLabelFont());
 
                        x_ += label_hfill_ + lwidth - width_pos;
@@ -704,7 +754,8 @@ void RowPainter::paintText()
        if (running_strikeout) {
                // calculate 1/3 height of the buffer's default font
                int const middle =
-                       yo_ - font_metrics::maxAscent(text_.defaultfont_) / 3;
+                       yo_ -
+                       font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
                pain_.line(last_strikeout_x, middle, int(x_), middle,
                        LColor::strikeout, Painter::line_solid, Painter::line_thin);
                running_strikeout = false;
@@ -712,62 +763,177 @@ void RowPainter::paintText()
 }
 
 
+lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par,
+       int x, int y)
+{
+       boost::crc_32_type crc;
+       for (lyx::pos_type i = row.pos(); i < row.endpos(); ++i) {
+               const unsigned char b[] = { par.getChar(i) };
+               crc.process_bytes(b, 1);
+       }
+       const unsigned char b[] = { x, y, row.width() };
+       crc.process_bytes(b, 3);
+       return crc.checksum();
+}
+
+
+bool CursorOnRow(PainterInfo & pi, pit_type const pit,
+       RowList::const_iterator rit, LyXText const & text)
+{
+       // Is there a cursor on this row (or inside inset on row)
+       LCursor & cur = pi.base.bv->cursor();
+       for (lyx::size_type d = 0; d < cur.depth(); d++) {
+               CursorSlice const & sl = cur[d];
+               if (sl.text() == &text
+                   && sl.pit() == pit
+                   && sl.pos() >= rit->pos()
+                   && sl.pos() <= rit->endpos())
+                       return true;
+       }
+       return false;
+}
+
+
+bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
+       RowList::const_iterator rit, LyXText const & text)
+{
+       // Is there a cursor inside an inset on this row, and is this inset
+       // the only "character" on this row
+       LCursor & cur = pi.base.bv->cursor();
+       if (rit->pos() + 1 != rit->endpos())
+               return false;
+       for (lyx::size_type d = 0; d < cur.depth(); d++) {
+               CursorSlice const & sl = cur[d];
+               if (sl.text() == &text
+                   && sl.pit() == pit
+                   && sl.pos() == rit->pos())
+                       return d < cur.depth() - 1;
+       }
+       return false;
+}
+
+
 void paintPar
-       (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
+       (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
+        bool repaintAll)
 {
 //     lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
        static NullPainter nop;
        static PainterInfo nullpi(pi.base.bv, nop);
        int const ww = pi.base.bv->workHeight();
 
+       theCoords.parPos()[&text][pit] = Point(x, y);
+
        Paragraph const & par = text.paragraphs()[pit];
+       if (par.rows().empty())
+               return;
 
        RowList::const_iterator const rb = par.rows().begin();
        RowList::const_iterator const re = par.rows().end();
-       theCoords.parPos()[&text][pit] = Point(x, y);
 
        y -= rb->ascent();
-       for (RowList::const_iterator rit = rb; rit != re; ++rit) {
+       lyx::size_type rowno(0);
+       for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
                y += rit->ascent();
-               bool const inside = (y + rit->descent() >= 0
+               // Allow setting of refreshInside for nested insets in
+               // this row only
+               bool tmp = refreshInside;
+
+               // Row signature; has row changed since last paint?
+               lyx::size_type const row_sig = calculateRowSignature(*rit, par, x, y);
+               bool row_has_changed = par.rowSignature()[rowno] != row_sig;
+
+               bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
+               bool in_inset_alone_on_row = innerCursorOnRow(pi, pit, rit,
+                       text);
+
+               // If this is the only object on the row, we can make it wide
+               for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
+                       InsetBase const * const in = par.getInset(i);
+                       if (in) {
+                               InsetText const * const t = in->asTextInset();
+                               if (t)
+                                       t->Wide() = in_inset_alone_on_row;
+                       }
+               }
+
+               // If selection is on, the current row signature differs
+               // from cache, or cursor is inside an inset _on this row_,
+               // then paint the row
+               if (repaintAll || row_has_changed || cursor_on_row) {
+                       // Add to row signature cache
+                       par.rowSignature()[rowno] = row_sig;
+
+                       bool const inside = (y + rit->descent() >= 0
                                       && y - rit->ascent() < ww);
-               RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
+                       RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
+                       // Clear background of this row
+                       // (if paragraph background was not cleared)
+                       if (!repaintAll &&
+                           (!in_inset_alone_on_row || row_has_changed)) {
+                               pi.pain.fillRectangle(x, y - rit->ascent(),
+                                   text.maxwidth_, rit->height(),
+                                   text.backgroundColor());
+                               // If outer row has changed, force nested
+                               // insets to repaint completely
+                               if (row_has_changed)
+                                       refreshInside = true;
+                       }
 
+                       // Instrumentation for testing row cache (see also
+                       // 12 lines lower):
+                       if (text.isMainText())
+                               lyxerr[Debug::PAINTING] << "#";
+                       else
+                               lyxerr[Debug::PAINTING] << "[" <<
+                                   repaintAll << row_has_changed <<
+                                   cursor_on_row << "]";
+                       rp.paintAppendix();
+                       rp.paintDepthBar();
+                       rp.paintChangeBar();
+                       if (rit == rb)
+                               rp.paintFirst();
+                       if (rit + 1 == re)
+                               rp.paintLast();
+                       rp.paintText();
+               }
                y += rit->descent();
-               rp.paintAppendix();
-               rp.paintDepthBar();
-               rp.paintChangeBar();
-               if (rit == rb)
-                       rp.paintFirst();
-               if (rit + 1 == re)
-                       rp.paintLast();
-               rp.paintText();
+               // Restore, see above
+               refreshInside = tmp;
        }
+       lyxerr[Debug::PAINTING] << "." << endl;
 }
 
 } // namespace anon
 
 
-void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
+void paintText(BufferView const & bv, ViewMetricsInfo const & vi,
+              Painter & pain)
 {
-       Painter & pain = bv.painter();
        LyXText * const text = bv.text();
+       bool const select = bv.cursor().selection();
 
-       // clear background
-       pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
-                          LColor::background);
-
-       // draw selection
        PainterInfo pi(const_cast<BufferView *>(&bv), pain);
+       // Should the whole screen, including insets, be refreshed?
+       bool repaintAll = select || !vi.singlepar;
 
-       text->drawSelection(pi, 0, 0);
+       if (repaintAll) {
+               // Clear background (if not delegated to rows)
+               pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
+                       text->backgroundColor());
+       }
+       if (select) {
+               text->drawSelection(pi, 0, 0);
+       }
 
        int yy = vi.y1;
        // draw contents
        for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
-               yy += text->getPar(pit).ascent();
-               paintPar(pi, *bv.text(), pit, 0, yy);
-               yy += text->getPar(pit).descent();
+               refreshInside = repaintAll;
+               Paragraph const & par = text->getPar(pit);
+               yy += par.ascent();
+               paintPar(pi, *bv.text(), pit, 0, yy, repaintAll);
+               yy += par.descent();
        }
 
        // Cache one paragraph above and one below
@@ -776,13 +942,13 @@ void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
 
        if (vi.p1 > 0) {
                text->redoParagraph(vi.p1 - 1);
-               theCoords.parPos()[bv.text()][vi.p1 - 1] = 
+               theCoords.parPos()[bv.text()][vi.p1 - 1] =
                        Point(0, vi.y1 - text->getPar(vi.p1 - 1).descent());
        }
 
        if (vi.p2 < lyx::pit_type(text->paragraphs().size()) - 1) {
                text->redoParagraph(vi.p2 + 1);
-               theCoords.parPos()[bv.text()][vi.p2 + 1] = 
+               theCoords.parPos()[bv.text()][vi.p2 + 1] =
                        Point(0, vi.y2 + text->getPar(vi.p2 + 1).ascent());
        }
 
@@ -803,9 +969,11 @@ void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
 //     lyxerr << "  paintTextInset: y: " << y << endl;
 
        y -= text.getPar(0).ascent();
+       // This flag can not be set from within same inset:
+       bool repaintAll = refreshInside;
        for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
                y += text.getPar(pit).ascent();
-               paintPar(pi, text, pit, x, y);
+               paintPar(pi, text, pit, x, y, repaintAll);
                y += text.getPar(pit).descent();
        }
 }