]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
This patch does two closely related things.
[lyx.git] / src / mathed / InsetMathHull.cpp
index efdeb8b05623f107660f6f511a3b081960d68c90..513a8963345ae7ba0fe0083f02b8e234c4879de6 100644 (file)
@@ -38,6 +38,7 @@
 #include "MacroTable.h"
 #include "MathMacro.h"
 #include "MathMacroTemplate.h"
+#include "MetricsInfo.h"
 #include "output_xhtml.h"
 #include "Paragraph.h"
 #include "ParIterator.h"
@@ -106,7 +107,7 @@ namespace {
        size_t firstRelOp(MathData const & ar)
        {
                for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
-                       if ((*it)->isRelOp())
+                       if ((*it)->isMathRel())
                                return it - ar.begin();
                return ar.size();
        }
@@ -425,15 +426,15 @@ int InsetMathHull::defaultColSpace(col_type col)
 }
 
 
-docstring InsetMathHull::standardFont() const
+string InsetMathHull::standardFont() const
 {
        switch (type_) {
        case hullRegexp:
-               return from_ascii("texttt");
+               return "texttt";
        case hullNone:
-               return from_ascii("lyxnochange");
+               return "lyxnochange";
        default:
-               return from_ascii("mathnormal");
+               return "mathnormal";
        }
 }
 
@@ -487,8 +488,9 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                return;
        }
 
-       FontSetChanger dummy1(mi.base, standardFont());
-       StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
+       // FIXME: Changing the same object repeatedly is inefficient.
+       Changer dummy1 = mi.base.changeFontSet(standardFont());
+       Changer dummy2 = mi.base.changeStyle(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
 
        // let the cells adjust themselves
        InsetMathGrid::metrics(mi, dim);
@@ -499,7 +501,7 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
        }
 
        if (numberedType()) {
-               FontSetChanger dummy(mi.base, from_ascii("mathbf"));
+               Changer dummy = mi.base.changeFontSet("mathbf");
                int l = 0;
                for (row_type row = 0; row < nrows(); ++row)
                        l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
@@ -580,9 +582,9 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        ColorCode color = pi.selected && lyxrc.use_system_colors
                                ? Color_selectiontext : standardColor();
        bool const really_change_color = pi.base.font.color() == Color_none;
-       ColorChanger dummy0(pi.base.font, color, really_change_color);
-       FontSetChanger dummy1(pi.base, standardFont());
-       StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
+       Changer dummy0 = pi.base.font.changeColor(color, really_change_color);
+       Changer dummy1 = pi.base.changeFontSet(standardFont());
+       Changer dummy2 = pi.base.changeStyle(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
 
        InsetMathGrid::draw(pi, x + 1, y);
 
@@ -590,7 +592,7 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
                for (row_type row = 0; row < nrows(); ++row) {
                        int const yy = y + rowinfo_[row].offset_;
-                       FontSetChanger dummy(pi.base, from_ascii("mathrm"));
+                       Changer dummy = pi.base.changeFontSet("mathrm");
                        docstring const nl = nicelabel(row);
                        pi.draw(xx, yy, nl);
                }
@@ -1870,6 +1872,21 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
+namespace {
+
+bool allowDisplayMath(Cursor const & cur)
+{
+       LATTEST(cur.depth() > 1);
+       Cursor tmpcur = cur;
+       tmpcur.pop();
+       FuncStatus status;
+       FuncRequest cmd(LFUN_MATH_DISPLAY);
+       return tmpcur.getStatus(cmd, status) && status.enabled();
+}
+
+}
+
+
 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & status) const
 {
@@ -1896,30 +1913,12 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                status.setOnOff(type_ == ht);
                status.setEnabled(isMutable(ht) && isMutable(type_));
 
-               if (ht != hullSimple && status.enabled()) {
-                       Cursor tmpcur = cur;
-                       while (!tmpcur.empty()) {
-                               InsetCode code = tmpcur.inset().lyxCode();
-                               if (code == BOX_CODE) {
-                                       return true;
-                               } else if (code == TABULAR_CODE) {
-                                       FuncRequest tmpcmd(LFUN_MATH_DISPLAY);
-                                       if (tmpcur.getStatus(tmpcmd, status) && !status.enabled())
-                                               return true;
-                               }
-                               tmpcur.pop_back();
-                       }
-               }
+               if (ht != hullSimple && status.enabled())
+                       status.setEnabled(allowDisplayMath(cur));
                return true;
        }
        case LFUN_MATH_DISPLAY: {
-               bool enable = true;
-               if (cur.depth() > 1) {
-                       Inset const & in = cur[cur.depth()-2].inset();
-                       if (in.lyxCode() == SCRIPT_CODE)
-                               enable = display() != Inline;
-               }
-               status.setEnabled(enable);
+               status.setEnabled(display() != Inline || allowDisplayMath(cur));
                status.setOnOff(display() != Inline);
                return true;
        }