]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
Only set buffer if it is not null.
[lyx.git] / src / mathed / InsetMathHull.cpp
index 5dd492cbfb794bc925baf07c855faf8ba750d8f9..91b3c6d6a34d43f96fa15ac56fac1ffff9166395 100644 (file)
@@ -26,6 +26,7 @@
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "ColorSet.h"
+#include "Cursor.h"
 #include "CutAndPaste.h"
 #include "Encoding.h"
 #include "Exporter.h"
@@ -38,7 +39,6 @@
 #include "InsetMathMacro.h"
 #include "InsetMathMacroTemplate.h"
 #include "MetricsInfo.h"
-#include "xml.h"
 #include "Paragraph.h"
 #include "ParIterator.h"
 #include "xml.h"
@@ -56,6 +56,7 @@
 #include "graphics/PreviewLoader.h"
 
 #include "frontends/alert.h"
+#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include "support/convert.h"
@@ -64,7 +65,7 @@
 #include "support/filetools.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 
 #include <sstream>
 
@@ -146,51 +147,9 @@ namespace {
                        os << "}\\\\\n";
        }
 
-
 } // namespace
 
 
-HullType hullType(docstring const & s)
-{
-       if (s == "none")      return hullNone;
-       if (s == "simple")    return hullSimple;
-       if (s == "equation")  return hullEquation;
-       if (s == "eqnarray")  return hullEqnArray;
-       if (s == "align")     return hullAlign;
-       if (s == "alignat")   return hullAlignAt;
-       if (s == "xalignat")  return hullXAlignAt;
-       if (s == "xxalignat") return hullXXAlignAt;
-       if (s == "multline")  return hullMultline;
-       if (s == "gather")    return hullGather;
-       if (s == "flalign")   return hullFlAlign;
-       if (s == "regexp")    return hullRegexp;
-       lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
-       return hullUnknown;
-}
-
-
-docstring hullName(HullType type)
-{
-       switch (type) {
-       case hullNone:       return from_ascii("none");
-       case hullSimple:     return from_ascii("simple");
-       case hullEquation:   return from_ascii("equation");
-       case hullEqnArray:   return from_ascii("eqnarray");
-       case hullAlign:      return from_ascii("align");
-       case hullAlignAt:    return from_ascii("alignat");
-       case hullXAlignAt:   return from_ascii("xalignat");
-       case hullXXAlignAt:  return from_ascii("xxalignat");
-       case hullMultline:   return from_ascii("multline");
-       case hullGather:     return from_ascii("gather");
-       case hullFlAlign:    return from_ascii("flalign");
-       case hullRegexp:     return from_ascii("regexp");
-       case hullUnknown:
-               lyxerr << "unknown hull type" << endl;
-               break;
-       }
-       return from_ascii("none");
-}
-
 static InsetLabel * dummy_pointer = 0;
 
 InsetMathHull::InsetMathHull(Buffer * buf)
@@ -227,8 +186,8 @@ InsetMathHull::InsetMathHull(InsetMathHull const & other) : InsetMathGrid(other)
 
 InsetMathHull::~InsetMathHull()
 {
-       for (size_t i = 0; i < label_.size(); ++i)
-               delete label_[i];
+       for (auto & i : label_)
+               delete i;
 }
 
 
@@ -247,8 +206,8 @@ InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
        numbered_ = other.numbered_;
        numbers_ = other.numbers_;
        buffer_ = other.buffer_;
-       for (size_t i = 0; i < label_.size(); ++i)
-               delete label_[i];
+       for (auto & i : label_)
+               delete i;
        label_ = other.label_;
        for (size_t i = 0; i != label_.size(); ++i) {
                if (label_[i])
@@ -520,9 +479,18 @@ bool previewTooSmall(Dimension const & dim)
 
 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       // true value in LaTeX is 12pt plus 3pt minus 9pt
-       // FIXME: even better would be to handle the short skip case.
-       int const display_margin = display() ? mi.base.inPixels(Length(12, Length::PT)) : 0;
+       /* Compute \(above|below)displayskip
+          true value in LaTeX is 10pt plus 2pt minus 5pt (in normal size at 10pt)
+          FIXME: make this dependent of current size? (minor improvement)
+          FIXME: if would be nice if this was not part of the inset, but
+                 just increased the row ascent/descent.
+          FIXME: even better would be to handle the short skip case.
+       */
+       int const bottom_display_margin = mi.base.inPixels(Length(10, Length::PT));
+       int top_display_margin = bottom_display_margin;
+       // at start of paragraph, add an empty line
+       if (mi.vmode)
+               top_display_margin += theFontMetrics(mi.base.font).maxHeight() + 2;
 
        if (previewState(mi.base.bv)) {
                preview_->metrics(mi, dim);
@@ -535,8 +503,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                        // value was hardcoded to 1 pixel
                        dim.wid += mi.base.bv->zoomedPixels(1) ;
                        if (display()) {
-                               dim.asc += display_margin;
-                               dim.des += display_margin;
+                               dim.asc += top_display_margin;
+                               dim.des += bottom_display_margin;
                        }
                }
                return;
@@ -577,8 +545,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 
 
        if (display()) {
-               dim.asc += display_margin;
-               dim.des += display_margin;
+               dim.asc += top_display_margin;
+               dim.des += bottom_display_margin;
        }
 
        // reserve some space for marker.
@@ -604,7 +572,7 @@ void InsetMathHull::drawMarkers(PainterInfo & pi, int x, int y) const
        ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
                Color_mathframe : Color_mathcorners;
        // If the corners have the same color as the background, do not paint them.
-       if (lcolor.getX11Name(Color_mathbg) == lcolor.getX11Name(pen_color))
+       if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
                return;
 
        Inset::drawMarkers(pi, x, y);
@@ -648,8 +616,8 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        if (previewState(bv)) {
                // Do not draw change tracking cue if taken care of by RowPainter
                // already.
-               Changer dummy = !canPaintChange(*bv) ? make_change(pi.change, Change())
-                       : Changer();
+               Changer dummy = !canPaintChange(*bv) ? changeVar(pi.change, Change())
+                       : noChange();
                if (previewTooSmall(dim)) {
                        // we have an extra frame
                        preview_->draw(pi, x + ERROR_FRAME_WIDTH, y);
@@ -667,7 +635,7 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                                ? Color_selectiontext : standardColor();
        bool const really_change_color = pi.base.font.color() == Color_none;
        Changer dummy0 = really_change_color ? pi.base.font.changeColor(color)
-               : Changer();
+               : noChange();
        if (numberedType()) {
                BufferParams::MathNumber const math_number = buffer().params().getMathNumber();
                for (row_type row = 0; row < nrows(); ++row) {
@@ -985,8 +953,8 @@ bool InsetMathHull::ams() const
        case hullEqnArray:
                break;
        }
-       for (size_t row = 0; row < numbered_.size(); ++row)
-               if (numbered_[row] == NOTAG)
+       for (auto const & row : numbered_)
+               if (row == NOTAG)
                        return true;
        return false;
 }
@@ -1569,7 +1537,7 @@ void InsetMathHull::mutate(HullType newtype)
                        numbered(0, false);
                } else {
                        setType(hullEquation);
-                       numbered(0, label_[0] ? true : false);
+                       numbered(0, label_[0] != nullptr);
                        mutate(newtype);
                }
                break;
@@ -2408,12 +2376,16 @@ void InsetMathHull::docbook(XMLStream & xs, OutputParams const & runparams) cons
 {
        // Choose the tag around the MathML equation.
        docstring name;
+       bool doCR = false;
        if (getType() == hullSimple)
                name = from_ascii("inlineequation");
-       else
+       else {
+               doCR = true; // This is a block equation, always have <informalequation> on its own line.
                name = from_ascii("informalequation");
+       }
 
        // DocBook also has <equation>, but it comes with a title.
+       // TODO: recognise \tag from amsmath? This would allow having <equation> with a proper title.
 
        docstring attr;
        for (row_type i = 0; i < nrows(); ++i) {
@@ -2423,7 +2395,12 @@ void InsetMathHull::docbook(XMLStream & xs, OutputParams const & runparams) cons
                }
        }
 
+       if (doCR)
+               if (!xs.isLastTagCR())
+                       xs << xml::CR();
+
        xs << xml::StartTag(name, attr);
+       xs << xml::CR();
 
        // With DocBook 5, MathML must be within its own namespace; defined in Buffer.cpp::writeDocBookSource as "m".
        // Output everything in a separate stream so that this does not interfere with the standard flow of DocBook tags.
@@ -2460,12 +2437,16 @@ void InsetMathHull::docbook(XMLStream & xs, OutputParams const & runparams) cons
                osmath << ostmp.str(); // osmath is not a XMLStream, so no need for XMLStream::ESCAPE_NONE.
                ms << ETag("math");
        } catch (MathExportException const &) {
-               osmath << "MathML export failed. Please report this as a bug.";
+               ms.cr();
+               osmath << "<mathphrase>MathML export failed. Please report this as a bug.</mathphrase>";
        }
 
        // Output the complete formula to the DocBook stream.
        xs << XMLStream::ESCAPE_NONE << osmath.str();
+       xs << xml::CR();
        xs << xml::EndTag(name);
+       if (doCR)
+               xs << xml::CR();
 }
 
 
@@ -2682,9 +2663,9 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const
                        string const tag = (getType() == hullSimple) ? "span" : "div";
                        xs << xml::CR()
                           << xml::StartTag(tag, "style = \"text-align: center;\"")
-                                << xml::CompTag("img", "src=\"" + filename + "\" alt=\"Mathematical Equation\"")
-                                << xml::EndTag(tag)
-                                << xml::CR();
+                          << xml::CompTag("img", "src=\"" + filename + "\" alt=\"Mathematical Equation\"")
+                          << xml::EndTag(tag)
+                          << xml::CR();
                        success = true;
                }
        }