]> git.lyx.org Git - features.git/commitdiff
Rewrite selection code in mathed
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 22 Feb 2017 09:43:48 +0000 (10:43 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 22 Feb 2017 10:00:30 +0000 (11:00 +0100)
Now the selection is not done by the inset, but by the MathData
itself. This allows for some code simplification and avoids an
extra redraw.

Additionally, this fixes the selection inside macros, which was broken
by the new MathRow code.

src/mathed/InsetMathNest.cpp
src/mathed/InsetMathNest.h
src/mathed/MathData.cpp
src/mathed/MathData.h
src/mathed/MathMacro.cpp
src/mathed/MathMacro.h
src/mathed/MathRow.cpp

index 484e0ed56e9069b064f094d7a55fc1bb68f2264b..1f0adda5045a15428e02745687eeed430d2c8dfc 100644 (file)
@@ -277,54 +277,6 @@ void InsetMathNest::draw(PainterInfo &, int, int) const
 }
 
 
-void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
-{
-       BufferView & bv = *pi.base.bv;
-       // this should use the x/y values given, not the cached values
-       Cursor & cur = bv.cursor();
-       if (!cur.selection())
-               return;
-       if (&cur.inset() != this)
-               return;
-
-       // FIXME: hack to get position cache warm
-       bool const original_drawing_state = pi.pain.isDrawingEnabled();
-       pi.pain.setDrawingEnabled(false);
-       draw(pi, x, y);
-       pi.pain.setDrawingEnabled(original_drawing_state);
-
-       CursorSlice s1 = cur.selBegin();
-       CursorSlice s2 = cur.selEnd();
-
-       //lyxerr << "InsetMathNest::drawing selection: "
-       //      << " s1: " << s1 << " s2: " << s2 << endl;
-       if (s1.idx() == s2.idx()) {
-               MathData const & c = cell(s1.idx());
-               Geometry const & g = bv.coordCache().getArrays().geometry(&c);
-               int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
-               int y1 = g.pos.y_ - g.dim.ascent();
-               int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
-               int y2 = g.pos.y_ + g.dim.descent();
-               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
-       //lyxerr << "InsetMathNest::drawing selection 3: "
-       //      << " x1: " << x1 << " x2: " << x2
-       //      << " y1: " << y1 << " y2: " << y2 << endl;
-       } else {
-               for (idx_type i = 0; i < nargs(); ++i) {
-                       if (idxBetween(i, s1.idx(), s2.idx())) {
-                               MathData const & c = cell(i);
-                               Geometry const & g = bv.coordCache().getArrays().geometry(&c);
-                               int x1 = g.pos.x_;
-                               int y1 = g.pos.y_ - g.dim.ascent();
-                               int x2 = g.pos.x_ + g.dim.width();
-                               int y2 = g.pos.y_ + g.dim.descent();
-                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
-                       }
-               }
-       }
-}
-
-
 void InsetMathNest::validate(LaTeXFeatures & features) const
 {
        for (idx_type i = 0; i < nargs(); ++i)
index f1cfdd8beb66fa4d2b6eee557928fd33bcbaa318..51670a2c64ed48c98d1602c822185c4dc1eea87c 100644 (file)
@@ -37,8 +37,6 @@ public:
        void cellsMetrics(MetricsInfo const & mi) const;
        /// draw background if locked
        void draw(PainterInfo & pi, int x, int y) const;
-       /// draw selection background
-       void drawSelection(PainterInfo & pi, int x, int y) const;
        ///
        void updateBuffer(ParIterator const &, UpdateType);
        /// identifies NestInsets
@@ -49,7 +47,7 @@ public:
        void cursorPos(BufferView const & bv, CursorSlice const & sl,
                bool boundary, int & x, int & y) const;
        ///
-       void edit(Cursor & cur, bool front, 
+       void edit(Cursor & cur, bool front,
                EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
        ///
        Inset * editXY(Cursor & cur, int x, int y);
index dcf9647b2e3840ef82bf8526e5df4abced99a539..322a8e9ceba2b8378a213d0e96cbf48d332e7978 100644 (file)
@@ -31,6 +31,7 @@
 #include "mathed/InsetMathUnknown.h"
 
 #include "frontends/FontMetrics.h"
+#include "frontends/Painter.h"
 
 #include "support/debug.h"
 #include "support/docstream.h"
@@ -285,6 +286,40 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
 }
 
 
+void MathData::drawSelection(PainterInfo & pi, int const x, int const y) const
+{
+       BufferView const * bv = pi.base.bv;
+       Cursor const & cur = bv->cursor();
+       InsetMath const * inset = cur.inset().asInsetMath();
+       if (!cur.selection() || !inset || inset->nargs() == 0)
+               return;
+
+       CursorSlice const s1 = cur.selBegin();
+       CursorSlice const s2 = cur.selEnd();
+       MathData const & c1 = inset->cell(s1.idx());
+
+       if (s1.idx() == s2.idx() && &c1 == this) {
+               // selection indide cell
+               Dimension const dim = bv->coordCache().getArrays().dim(&c1);
+               int const beg = c1.pos2x(bv, s1.pos());
+               int const end = c1.pos2x(bv, s2.pos());
+               pi.pain.fillRectangle(x + beg, y - dim.ascent(),
+                                     end - beg, dim.height(), Color_selection);
+       } else {
+               for (idx_type i = 0; i < inset->nargs(); ++i) {
+                       MathData const & c = inset->cell(i);
+                       if (&c == this && inset->idxBetween(i, s1.idx(), s2.idx())) {
+                               // The whole cell is selected
+                               Dimension const dim = bv->coordCache().getArrays().dim(&c);
+                               pi.pain.fillRectangle(x, y - dim.ascent(),
+                                                     dim.width(), dim.height(),
+                                                     Color_selection);
+                       }
+               }
+       }
+}
+
+
 void MathData::draw(PainterInfo & pi, int const x, int const y) const
 {
        //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
@@ -300,6 +335,7 @@ void MathData::draw(PainterInfo & pi, int const x, int const y) const
                || x >= bv. workWidth())
                return;
 
+       drawSelection(pi, x, y);
        MathRow const & mrow = mrow_cache_[pi.base.bv];
        mrow.draw(pi, x, y);
 }
index 1d2617c8dd9a109fa75657ec207623f58244f3b2..ed85c78977ea68177f5dfc9e002cc94a7e43d027 100644 (file)
@@ -129,6 +129,8 @@ public:
        ///
        Dimension const & dimension(BufferView const &) const;
 
+       /// draw the selection over the cell
+       void drawSelection(PainterInfo & pi, int x, int y) const;
        /// redraw cell using cache metrics information
        void draw(PainterInfo & pi, int x, int y) const;
        /// rebuild cached metrics information
index 4f478d0995f78c3c8226c3f1826136f52f9b4cdc..8b86dc6422e7cad83781318bc6dbd4547718505e 100644 (file)
@@ -802,14 +802,6 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
-{
-       // We may have 0 arguments, but InsetMathNest requires at least one.
-       if (!cells_.empty())
-               InsetMathNest::drawSelection(pi, x, y);
-}
-
-
 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode, int appetite)
 {
        if (d->displayMode_ != mode) {
index f1e61dcb4c5bcf81d8171624c471d32f7939ea38..5392754969c7a71d13a9ae8cd6a4c862356bb1d2 100644 (file)
@@ -56,8 +56,6 @@ public:
        bool editMetrics(BufferView const * bv) const;
        ///
        void draw(PainterInfo & pi, int x, int y) const;
-       /// draw selection background
-       void drawSelection(PainterInfo & pi, int x, int y) const;
        ///
        int kerning(BufferView const * bv) const;
        /// get cursor position
index e046add470e1e0da3ac58c673aaf59340d6f505e..c3c9a1c9341936f95d04fa9afee7e35e97319721 100644 (file)
@@ -278,7 +278,6 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const
                        Dimension d2 = d;
                        d2.wid -= e.before + e.after;
                        coords.insets().add(e.inset, d2);
-                       e.inset->drawSelection(pi, x + e.before, y);
                        e.inset->draw(pi, x + e.before, y);
                        coords.insets().add(e.inset, x, y);
                        coords.insets().add(e.inset, d);
@@ -287,14 +286,16 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const
                        break;
                }
                case BEGIN:
+                       if (e.ar) {
+                               coords.arrays().add(e.ar, x, y);
+                               e.ar->drawSelection(pi, x, y);
+                       }
                        if (e.inset) {
                                coords.insets().add(e.inset, x, y);
                                drawMarkers(pi, e, x, y);
                                e.inset->beforeDraw(pi);
                        }
                        x += e.before + e.after;
-                       if (e.ar)
-                               coords.arrays().add(e.ar, x, y);
                        break;
                case END:
                        if (e.inset)