]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
Remove hardcoded values
[lyx.git] / src / mathed / InsetMathHull.cpp
index 5dffc3d6055045185f2772213ef6ae34a7316932..807475e63aa771ed8381db02f0bc10d6424ed2f0 100644 (file)
@@ -15,7 +15,6 @@
 #include "InsetMathChar.h"
 #include "InsetMathColor.h"
 #include "InsetMathFrac.h"
-#include "InsetMathGrid.h"
 #include "InsetMathNest.h"
 #include "InsetMathScript.h"
 #include "MathExtern.h"
@@ -109,7 +108,7 @@ namespace {
        size_t firstRelOp(MathData const & ar)
        {
                for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
-                       if ((*it)->isMathRel())
+                       if ((*it)->mathClass() == MC_REL)
                                return it - ar.begin();
                return ar.size();
        }
@@ -121,6 +120,37 @@ namespace {
        }
 
 
+       // writes a preamble for underlined or struck out math display
+       void writeMathdisplayPreamble(WriteStream & os)
+       {
+               if (os.strikeoutMath()) {
+                       if (os.ulemCmd() == WriteStream::UNDERLINE)
+                               os << "\\raisebox{-\\belowdisplayshortskip}{"
+                                     "\\lyxmathsout{\\parbox[b]{\\columnwidth}{";
+                       else
+                               os << "\\lyxmathsout{\\parbox{\\columnwidth}{";
+               } else if (os.ulemCmd() == WriteStream::UNDERLINE)
+                       os << "\\raisebox{-\\belowdisplayshortskip}{"
+                             "\\parbox[b]{\\columnwidth}{";
+               else if (os.ulemCmd() == WriteStream::STRIKEOUT)
+                       os << "\\parbox{\\columnwidth}{";
+       }
+
+
+       // writes a postamble for underlined or struck out math display
+       void writeMathdisplayPostamble(WriteStream & os)
+       {
+               if (os.strikeoutMath()) {
+                       if (os.ulemCmd() == WriteStream::UNDERLINE)
+                               os << "}";
+                       os << "}}\\\\\n";
+               } else if (os.ulemCmd() == WriteStream::UNDERLINE)
+                       os << "}}\\\\\n";
+               else if (os.ulemCmd() == WriteStream::STRIKEOUT)
+                       os << "}\\\\\n";
+       }
+
+
 } // end anon namespace
 
 
@@ -485,14 +515,12 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                        if (display())
                                dim.des += displayMargin();
                }
-               // Cache the inset dimension.
-               setDimCache(mi, dim);
                return;
        }
 
-       // 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);
+       Changer dummy2 = mi.base.font.changeStyle(display() ? LM_ST_DISPLAY
+                                                           : LM_ST_TEXT);
 
        // let the cells adjust themselves
        InsetMathGrid::metrics(mi, dim);
@@ -520,9 +548,6 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
        math_font_max_dim(mi.base.font, asc, des);
        dim.asc = max(dim.asc, asc);
        dim.des = max(dim.des, des);
-       // Cache the inset dimension.
-       // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
-       setDimCache(mi, dim);
 }
 
 
@@ -563,15 +588,15 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        BufferView const * const bv = pi.base.bv;
        Dimension const dim = dimension(*bv);
 
-       if (type_ == hullRegexp) {
-               pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
-                       dim.width() - 2, dim.height() - 2, Color_regexpframe);
-       }
+       if (type_ == hullRegexp)
+               pi.pain.rectangle(x + 2, y - dim.ascent() + 1,
+                                 dim.width() - 3, dim.height() - 2, Color_regexpframe);
 
        if (previewState(bv)) {
                // Do not draw change tracking cue if taken care of by RowPainter
                // already.
-               Changer dummy = make_change(pi.change_, Change(), !canPaintChange(*bv));
+               Changer dummy = !canPaintChange(*bv) ? make_change(pi.change_, Change())
+                       : Changer();
                if (previewTooSmall(dim)) {
                        // we have an extra frame
                        preview_->draw(pi, x + ERROR_FRAME_WIDTH, y);
@@ -579,16 +604,17 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                        // one pixel gap in front
                        preview_->draw(pi, x + 1, y);
                }
-               setPosCache(pi, x, y);
                return;
        }
 
        ColorCode color = pi.selected && lyxrc.use_system_colors
                                ? Color_selectiontext : standardColor();
        bool const really_change_color = pi.base.font.color() == Color_none;
-       Changer dummy0 = pi.base.font.changeColor(color, really_change_color);
+       Changer dummy0 = really_change_color ? pi.base.font.changeColor(color)
+               : Changer();
        Changer dummy1 = pi.base.changeFontSet(standardFont());
-       Changer dummy2 = pi.base.changeStyle(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
+       Changer dummy2 = pi.base.font.changeStyle(display() ? LM_ST_DISPLAY
+                                                           : LM_ST_TEXT);
 
        InsetMathGrid::draw(pi, x + 1, y);
 
@@ -605,7 +631,6 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        if (canPaintChange(*bv))
                pi.change_.paintCue(pi, x + 1, y + 1 - dim.asc,
                                    x + dim.wid, y + dim.des);
-       setPosCache(pi, x, y);
 }
 
 
@@ -615,7 +640,7 @@ void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
                InsetMathGrid::metricsT(mi, dim);
        } else {
                odocstringstream os;
-               otexrowstream ots(os, false);
+               otexrowstream ots(os);
                WriteStream wi(ots, false, true, WriteStream::wsDefault);
                write(wi);
                dim.wid = os.str().size();
@@ -631,7 +656,7 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
                InsetMathGrid::drawT(pain, x, y);
        } else {
                odocstringstream os;
-               otexrowstream ots(os, false);
+               otexrowstream ots(os);
                WriteStream wi(ots, false, true, WriteStream::wsDefault);
                write(wi);
                pain.draw(x, y, os.str().c_str());
@@ -650,7 +675,7 @@ static docstring latexString(InsetMathHull const & inset)
        static Encoding const * encoding = 0;
        if (inset.isBufferValid())
                encoding = &(inset.buffer().params().encoding());
-       otexrowstream ots(ls, false);
+       otexrowstream ots(ls);
        WriteStream wi(ots, false, true, WriteStream::wsPreview, encoding);
        inset.write(wi);
        return ls.str();
@@ -899,6 +924,29 @@ bool InsetMathHull::ams() const
 }
 
 
+bool InsetMathHull::outerDisplay() const
+{
+       switch (type_) {
+       case hullEquation:
+       case hullEqnArray:
+       case hullAlign:
+       case hullFlAlign:
+       case hullGather:
+       case hullMultline:
+               return true;
+       case hullNone:
+       case hullSimple:
+       case hullAlignAt:
+       case hullXAlignAt:
+       case hullXXAlignAt:
+       case hullUnknown:
+       case hullRegexp:
+               break;
+       }
+       return false;
+}
+
+
 Inset::DisplayType InsetMathHull::display() const
 {
        switch (type_) {
@@ -956,14 +1004,17 @@ void InsetMathHull::validate(LaTeXFeatures & features) const
 
                if (type_ == hullRegexp) {
                        features.require("color");
-                       string frcol = lcolor.getLaTeXName(Color_regexpframe);
-                       string bgcol = "white";
+                       docstring frcol = from_utf8(lcolor.getLaTeXName(Color_regexpframe));
+                       docstring bgcol = from_ascii("white");
                        features.addPreambleSnippet(
-                               string("\\newcommand{\\regexp}[1]{\\fcolorbox{")
-                               + frcol + string("}{")
-                               + bgcol + string("}{\\ensuremath{\\mathtt{#1}}}}"));
+                               "\\newcommand{\\regexp}[1]{\\fcolorbox{"
+                               + frcol + "}{"
+                               + bgcol + "}{\\ensuremath{\\mathtt{#1}}}}");
                        features.addPreambleSnippet(
-                               string("\\newcommand{\\endregexp}{}"));
+                               from_ascii("\\newcommand{\\endregexp}{}"));
+               } else if (outerDisplay() && features.inDeletedInset()
+                          && !features.mustProvide("ct-dvipost")) {
+                               features.require("ct-tikz-math-sout");
                }
 
                // Validation is necessary only if not using AMS math.
@@ -995,6 +1046,8 @@ void InsetMathHull::header_write(WriteStream & os) const
                break;
 
        case hullSimple:
+               if (os.ulemCmd())
+                       os << "\\mbox{";
                os << '$';
                os.startOuterRow();
                if (cell(0).empty())
@@ -1002,6 +1055,7 @@ void InsetMathHull::header_write(WriteStream & os) const
                break;
 
        case hullEquation:
+               writeMathdisplayPreamble(os);
                os << "\n";
                os.startOuterRow();
                if (n)
@@ -1015,6 +1069,7 @@ void InsetMathHull::header_write(WriteStream & os) const
        case hullFlAlign:
        case hullGather:
        case hullMultline:
+               writeMathdisplayPreamble(os);
                os << "\n";
                os.startOuterRow();
                os << "\\begin{" << hullName(type_) << star(n) << "}\n";
@@ -1059,6 +1114,8 @@ void InsetMathHull::footer_write(WriteStream & os) const
 
        case hullSimple:
                os << '$';
+               if (os.ulemCmd())
+                       os << "}";
                break;
 
        case hullEquation:
@@ -1068,15 +1125,22 @@ void InsetMathHull::footer_write(WriteStream & os) const
                        os << "\\end{equation" << star(n) << "}\n";
                else
                        os << "\\]\n";
+               writeMathdisplayPostamble(os);
                break;
 
        case hullEqnArray:
        case hullAlign:
        case hullFlAlign:
-       case hullAlignAt:
-       case hullXAlignAt:
        case hullGather:
        case hullMultline:
+               os << "\n";
+               os.startOuterRow();
+               os << "\\end{" << hullName(type_) << star(n) << "}\n";
+               writeMathdisplayPostamble(os);
+               break;
+
+       case hullAlignAt:
+       case hullXAlignAt:
                os << "\n";
                os.startOuterRow();
                os << "\\end{" << hullName(type_) << star(n) << "}\n";
@@ -2180,7 +2244,7 @@ bool InsetMathHull::searchForward(BufferView * bv, string const & str,
 void InsetMathHull::write(ostream & os) const
 {
        odocstringstream oss;
-       otexrowstream ots(oss, false);
+       otexrowstream ots(oss);
        WriteStream wi(ots, false, false, WriteStream::wsDefault);
        oss << "Formula ";
        write(wi);
@@ -2223,7 +2287,7 @@ int InsetMathHull::plaintext(odocstringstream & os,
        }
 
        odocstringstream oss;
-       otexrowstream ots(oss, false);
+       otexrowstream ots(oss);
        Encoding const * const enc = encodings.fromLyXName("utf8");
        WriteStream wi(ots, false, true, WriteStream::wsDefault, enc);
 
@@ -2532,7 +2596,7 @@ docstring InsetMathHull::xhtml(XHTMLStream & xs, OutputParams const & op) const
                // Unfortunately, we cannot use latexString() because we do not want
                // $...$ or whatever.
                odocstringstream ls;
-               otexrowstream ots(ls, false);
+               otexrowstream ots(ls);
                WriteStream wi(ots, false, true, WriteStream::wsPreview);
                ModeSpecifier specifier(wi, MATH_MODE);
                mathAsLatex(wi);