]> git.lyx.org Git - lyx.git/commitdiff
Use Color_selectiontext as needed for partial selection in math
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 3 Oct 2023 09:38:40 +0000 (11:38 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 3 Oct 2023 10:51:11 +0000 (12:51 +0200)
Currently, selected math hull insets use Color_selectiontext when
system colors are in use. This commit implements the same behavior
for partial selection.

This is done by introducing two element types (BEGIN_SEL and END_SEL)
to MathRow.

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

index a92511919a999a848da2077250a4f7743d5f876a..eee53d2f50cb42d969e350da2f2e33e589b2aaaa 100644 (file)
@@ -229,6 +229,18 @@ bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
        ar->updateMacros(&bv->cursor(), mi.macrocontext,
                         InternalUpdate, mi.base.macro_nesting);
 
+       pos_type bspos = -1, espos = -1;
+       Cursor const & cur = bv->cursor();
+       InsetMath const * inset = cur.inset().asInsetMath();
+       if (cur.selection() && inset) {
+               CursorSlice const s1 = cur.selBegin();
+               CursorSlice const s2 = cur.selEnd();
+               // Detect inner selection in this math data.
+               if (s1.idx() == s2.idx() && &inset->cell(s1.idx()) == this) {
+                       bspos = s1.pos();
+                       espos = s2.pos();
+               }
+       }
 
        // FIXME: for completion, try to insert the relevant data in the
        // mathrow (like is done for text rows). We could add a pair of
@@ -240,11 +252,15 @@ bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
        size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
 
        for (size_t i = 0 ; i < size() ; ++i) {
+               if (i == bspos)
+                       mrow.push_back(MathRow::Element(mi, MathRow::BEGIN_SEL));
                has_contents |= (*this)[i]->addToMathRow(mrow, mi);
                if (i + 1 == compl_pos) {
                        mrow.back().compl_text = bv->inlineCompletion();
                        mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
                }
+               if (i + 1 == espos)
+                       mrow.push_back(MathRow::Element(mi, MathRow::END_SEL));
        }
        return has_contents;
 }
index 81b6ad9e9218cc406dfbae1521a4389f7b205951..fee869c89ace1df2edd394053dc9a1aece432b82 100644 (file)
@@ -18,6 +18,7 @@
 #include "BufferView.h"
 #include "ColorSet.h"
 #include "CoordCache.h"
+#include "LyXRC.h"
 #include "MetricsInfo.h"
 
 #include "mathed/InsetMath.h"
@@ -247,6 +248,8 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
                Dimension d;
                switch (e.type) {
                case DUMMY:
+               case BEGIN_SEL:
+               case END_SEL:
                        break;
                case INSET:
                        e.inset->metrics(mi, d);
@@ -315,6 +318,7 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
 
 void MathRow::draw(PainterInfo & pi, int x, int const y) const
 {
+       Changer change_color;
        CoordCache & coords = pi.base.bv->coordCache();
        for (Element const & e : elements_) {
                switch (e.type) {
@@ -351,6 +355,13 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const
                                e.inset->afterDraw(pi);
                        x += e.before + e.after;
                        break;
+               case BEGIN_SEL:
+                       if (lyxrc.use_system_colors)
+                               change_color = pi.base.font.changeColor(Color_selectiontext);
+                       break;
+               case END_SEL:
+                       change_color = noChange();
+                       break;
                case BOX: {
                        if (e.color == Color_none)
                                break;
@@ -424,6 +435,12 @@ ostream & operator<<(ostream & os, MathRow::Element const & e)
                if (e.inset)
                        os << "]";
                break;
+       case MathRow::BEGIN_SEL:
+               os << "<sel>";
+               break;
+       case MathRow::END_SEL:
+               os << "</sel>" ;
+               break;
        case MathRow::BOX:
                os << "<" << e.before << "-[]-" << e.after << ">";
                break;
index 9efab92e8535c03c52c73ddab6ffd3ad5200a10a..2678ac4e23c7ad146ae40ef759d7541b0f080841 100644 (file)
@@ -50,6 +50,8 @@ public:
                BOX, // an empty box
                BEGIN, // an inset and/or a math array begins here
                END, // an inset and/or a math array ends here
+               BEGIN_SEL, // the selection begins here
+               END_SEL, // the selection ends here
                DUMMY // a dummy element (used before or after row)
        };