]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathRow.cpp
Fixup 89662a68: remove markers that should not be there
[lyx.git] / src / mathed / MathRow.cpp
index cfb41642d476d6506cb30f797fa4d7cf6403c8e1..3263d1c48315f373bd04cd5c642a236839de0e40 100644 (file)
@@ -15,7 +15,6 @@
 #include "InsetMath.h"
 #include "MathClass.h"
 #include "MathData.h"
-#include "MathMacro.h"
 #include "MathSupport.h"
 
 #include "BufferView.h"
@@ -38,8 +37,8 @@ namespace lyx {
 
 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
        : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
-         marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0),
-         macro(0), color(Color_red)
+         marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
+         color(Color_red)
 {}
 
 
@@ -51,10 +50,10 @@ MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
        // Then insert the MathData argument
        bool const has_contents = ar->addToMathRow(*this, mi);
 
-       // empty arrays are visible when they are editable
-       // we reserve the necessary space anyway (even if nothing gets drawn)
+       // A MathRow should not be completely empty
        if (!has_contents) {
                Element e(mi, BOX, MC_ORD);
+               // empty arrays are visible when they are editable
                e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
                push_back(e);
        }
@@ -133,8 +132,8 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.wid = 0;
        // In order to compute the dimension of macros and their
        // arguments, it is necessary to keep track of them.
-       map<MathMacro const *, Dimension> dim_macros;
-       map<MathData const *, Dimension> dim_arrays;
+       vector<pair<InsetMath const *, Dimension>> dim_insets;
+       vector<pair<MathData const *, Dimension>> dim_arrays;
        CoordCache & coords = mi.base.bv->coordCache();
        for (Element const & e : elements_) {
                mi.base.macro_nesting = e.macro_nesting;
@@ -147,35 +146,36 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
                        d.wid += e.before + e.after;
                        coords.insets().add(e.inset, d);
                        break;
-               case BEG_MACRO:
-                       e.macro->macro()->lock();
-                       // Add a macro to current list
-                       dim_macros[e.macro] = Dimension();
+               case BEGIN:
+                       if (e.inset) {
+                               dim_insets.push_back(make_pair(e.inset, Dimension()));
+                               e.inset->beforeMetrics();
+                       }
+                       if (e.ar)
+                               dim_arrays.push_back(make_pair(e.ar, Dimension()));
                        break;
-               case END_MACRO:
-                       LATTEST(dim_macros.find(e.macro) != dim_macros.end());
-                       e.macro->macro()->unlock();
-                       // Cache the dimension of the macro and remove it from
-                       // tracking map.
-                       coords.insets().add(e.macro, dim_macros[e.macro]);
-                       dim_macros.erase(e.macro);
-                       break;
-                       // This is basically like macros
-               case BEG_ARG:
-                       if (e.macro)
-                               e.macro->macro()->unlock();
-                       dim_arrays[e.ar] = Dimension();
-                       break;
-               case END_ARG:
-                       LATTEST(dim_arrays.find(e.ar) != dim_arrays.end());
-                       if (e.macro)
-                               e.macro->macro()->lock();
-                       coords.arrays().add(e.ar, dim_arrays[e.ar]);
-                       dim_arrays.erase(e.ar);
+               case END:
+                       if (e.inset) {
+                               e.inset->afterMetrics();
+                               LATTEST(dim_insets.back().first == e.inset);
+                               coords.insets().add(e.inset, dim_insets.back().second);
+                               dim_insets.pop_back();
+                       }
+                       if (e.ar) {
+                               LATTEST(dim_arrays.back().first == e.ar);
+                               coords.arrays().add(e.ar, dim_arrays.back().second);
+                               dim_arrays.pop_back();
+                       }
                        break;
                case BOX:
                        d = theFontMetrics(mi.base.font).dimension('I');
-                       d.wid += e.before + e.after;
+                       if (e.color != Color_none) {
+                               // allow for one pixel before/after the box.
+                               d.wid += e.before + e.after + 2;
+                       } else {
+                               // hide the box, but give it some height
+                               d.wid = 0;
+                       }
                        break;
                }
 
@@ -194,7 +194,7 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
                if (!d.empty()) {
                        dim += d;
                        // Now add the dimension to current macros and arguments.
-                       for (auto & dim_macro : dim_macros)
+                       for (auto & dim_macro : dim_insets)
                                dim_macro.second += d;
                        for (auto & dim_array : dim_arrays)
                                dim_array.second += d;
@@ -206,7 +206,7 @@ void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
                augmentFont(font, "mathnormal");
                dim.wid += mathed_string_width(font, e.compl_text);
        }
-       LATTEST(dim_macros.empty() && dim_arrays.empty());
+       LATTEST(dim_insets.empty() && dim_arrays.empty());
 }
 
 
@@ -268,33 +268,29 @@ void MathRow::draw(PainterInfo & pi, int x, int const y) const
                        x += d.wid;
                        break;
                }
-               case BEG_MACRO:
-                       coords.insets().add(e.macro, x, y);
-
-                       drawMarkers(pi, e, x, y);
-                       break;
-               case BEG_ARG:
-                       coords.arrays().add(e.ar, x, y);
-                       // if the macro is being edited, then the painter is in
-                       // monochrome mode.
-                       if (e.macro->editMetrics(pi.base.bv))
-                               pi.pain.leaveMonochromeMode();
+               case BEGIN:
+                       if (e.inset) {
+                               coords.insets().add(e.inset, x, y);
+                               drawMarkers(pi, e, x, y);
+                               e.inset->beforeDraw(pi);
+                       }
+                       if (e.ar)
+                               coords.arrays().add(e.ar, x, y);
                        break;
-               case END_ARG:
-                       if (e.macro->editMetrics(pi.base.bv))
-                               pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
+               case END:
+                       if (e.inset)
+                               e.inset->afterDraw(pi);
                        break;
                case BOX: {
+                       if (e.color == Color_none)
+                               break;
                        Dimension const d = theFontMetrics(pi.base.font).dimension('I');
-                       // the box is not visible in non-editable context (except for grey macro boxes).
-                       if (e.color != Color_none)
-                               pi.pain.rectangle(x + e.before, y - d.ascent(),
-                                                 d.width(), d.height(), e.color);
-                       x += d.wid + e.before + e.after;
+                       pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
+                                         d.width() - 1, d.height() - 1, e.color);
+                       x += d.wid + 2 + e.before + e.after;
                        break;
                }
                case DUMMY:
-               case END_MACRO:
                        break;
                }
 
@@ -343,18 +339,18 @@ ostream & operator<<(ostream & os, MathRow::Element const & e)
                   << to_utf8(class_to_string(e.mclass))
                   << "-" << e.after << ">";
                break;
-       case MathRow::BEG_MACRO:
-               os << "\\" << to_utf8(e.macro->name())
-                  << "^" << e.macro->nesting() << "[";
-               break;
-       case MathRow::END_MACRO:
-               os << "]";
-               break;
-       case MathRow::BEG_ARG:
-               os << "#(";
+       case MathRow::BEGIN:
+               if (e.inset)
+                       os << "\\" << to_utf8(e.inset->name())
+                          << "^" << e.macro_nesting << "[";
+               if (e.ar)
+                       os << "(";
                break;
-       case MathRow::END_ARG:
-               os << ")";
+       case MathRow::END:
+               if (e.ar)
+                       os << ")";
+               if (e.inset)
+                       os << "]";
                break;
        case MathRow::BOX:
                os << "<" << e.before << "-[]-" << e.after << ">";