]> git.lyx.org Git - lyx.git/blobdiff - src/rowpainter.C
Fix bug 2485 and crash on middle mouse paste on math
[lyx.git] / src / rowpainter.C
index bdd1f6bcf13e70cafc70b2da64e7789cf771dd3e..5971b39d4603beb591bacfd7106137449e10d8fe 100644 (file)
@@ -42,6 +42,7 @@
 
 #include <boost/crc.hpp>
 
+using lyx::char_type;
 using lyx::pos_type;
 using lyx::pit_type;
 
@@ -74,8 +75,8 @@ private:
        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);
@@ -151,15 +152,6 @@ int RowPainter::leftMargin() const
 }
 
 
-bool isTrueTextInset(InsetBase * in)
-{
-       // Math and tabular insets have isTextInset = true, though they are
-       // not derived from InsetText. Paint them fully
-       return (in && in->isTextInset() && in->asMathInset() == 0
-               && in->lyxCode() != InsetBase::TABULAR_CODE);
-}
-
-
 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
 {
        InsetBase const * inset = par_.getInset(pos);
@@ -173,11 +165,13 @@ void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
        pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
        pi.erased_ = erased_ || isDeletedText(par_, pos);
        theCoords.insets().add(inset, int(x_), yo_);
-       InsetBase * in = const_cast<InsetBase *>(inset);
+       InsetText const * const in = inset->asTextInset();
        // non-wide insets are painted completely. Recursive
        bool tmp = bv_.repaintAll();
-       if (!isTrueTextInset(in) || !static_cast<InsetText*>(in)->Wide())
+       if (!in || !in->Wide()) {
                bv_.repaintAll(true);
+               lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
+       }
        if (bv_.repaintAll())
                inset->drawSelection(pi, int(x_), yo_);
        inset->draw(pi, int(x_), yo_);
@@ -193,7 +187,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;
 
@@ -202,8 +196,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
@@ -226,7 +220,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;
@@ -236,9 +230,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;
                        }
@@ -250,7 +244,7 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & 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);
@@ -275,15 +269,15 @@ 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)
@@ -334,7 +328,7 @@ void RowPainter::paintFromPos(pos_type & vpos)
        }
 
        // 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();
@@ -345,8 +339,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);
@@ -628,7 +622,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;
 
@@ -750,7 +744,7 @@ lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par,
 }
 
 
-bool CursorOnRow(PainterInfo & pi, pit_type const pit, 
+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)
@@ -759,15 +753,15 @@ bool CursorOnRow(PainterInfo & pi, pit_type const pit,
                CursorSlice const & sl = cur[d];
                if (sl.text() == &text
                    && sl.pit() == pit
-                   && sl.pos() >= rit->pos()
-                   && sl.pos() < rit->endpos())
+                   && sl.pos() >= rit->pos()
+                   && sl.pos() <= rit->endpos())
                        return true;
        }
        return false;
 }
 
 
-bool innerCursorOnRow(PainterInfo & pi, pit_type pit, 
+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
@@ -778,7 +772,7 @@ bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
        for (lyx::size_type d = 0; d < cur.depth(); d++) {
                CursorSlice const & sl = cur[d];
                if (sl.text() == &text
-                   && sl.pit() == pit 
+                   && sl.pit() == pit
                    && sl.pos() == rit->pos())
                        return d < cur.depth() - 1;
        }
@@ -812,22 +806,24 @@ void paintPar
                // 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* in 
-                           = const_cast<InsetBase*>(par.getInset(i));
-                       if (isTrueTextInset(in))
-                               static_cast<InsetText*>(in)->Wide()
-                                   = in_inset_alone_on_row;
+                       InsetBase const * const in = par.getInset(i);
+                       if (in) {
+                               InsetText const * const t = in->asTextInset();
+                               if (t)
+                                       t->Wide() = in_inset_alone_on_row &&
+                                                   t->Tall();
+                       }
                }
 
-               // If selection is on, the current row signature differs 
-               // from cache, or cursor is inside an inset _on this 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
@@ -836,11 +832,11 @@ void paintPar
                        bool const inside = (y + rit->descent() >= 0
                                       && y - rit->ascent() < ww);
                        RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
-                       // Clear background of this row 
+                       // Clear background of this row
                        // (if paragraph background was not cleared)
-                       if (!repaintAll && 
+                       if (!repaintAll &&
                            (!in_inset_alone_on_row || row_has_changed)) {
-                               pi.pain.fillRectangle(( rowno ? 0 : x - 10 ), y - rit->ascent(), 
+                               pi.pain.fillRectangle(( rowno ? 0 : x - 10 ), y - rit->ascent(),
                                    pi.base.bv->workWidth(), rit->height(),
                                    text.backgroundColor());
                                // If outer row has changed, force nested
@@ -848,13 +844,13 @@ void paintPar
                                if (row_has_changed)
                                        pi.base.bv->repaintAll(true);
                        }
-                       
+
                        // Instrumentation for testing row cache (see also
                        // 12 lines lower):
                        if (text.isMainText())
-                               lyxerr[Debug::PAINTING] << "#";
+                               lyxerr[Debug::PAINTING] << "#";
                        else
-                               lyxerr[Debug::PAINTING] << "[" <<
+                               lyxerr[Debug::PAINTING] << "[" <<
                                    repaintAll << row_has_changed <<
                                    cursor_on_row << "]";
                        rp.paintAppendix();
@@ -885,7 +881,7 @@ void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
        PainterInfo pi(const_cast<BufferView *>(&bv), pain);
        // Should the whole screen, including insets, be refreshed?
        bool repaintAll(select || !vi.singlepar);
-       
+
        if (repaintAll) {
                // Clear background (if not delegated to rows)
                pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
@@ -911,13 +907,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());
        }