]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.cpp
RefChanger
[lyx.git] / src / mathed / MathMacroTemplate.cpp
index 82538e8d641542147ce0f2bc7bfbb538e13b37da..4b68626062e59225208915ce1d90545d4a766bb7 100644 (file)
@@ -34,6 +34,7 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "Lexer.h"
+#include "TocBackend.h"
 
 #include "frontends/Painter.h"
 
@@ -395,7 +396,8 @@ void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
 
 MathMacroTemplate::MathMacroTemplate(Buffer * buf)
        : InsetMathNest(buf, 3), numargs_(0), argsInLook_(0), optionals_(0),
-         type_(MacroTypeNewcommand), lookOutdated_(true)
+         type_(MacroTypeNewcommand), redefinition_(false), lookOutdated_(true),
+         premetrics_(false), labelBoxAscent_(0), labelBoxDescent_(0)
 {
        initMath();
 }
@@ -406,7 +408,8 @@ MathMacroTemplate::MathMacroTemplate(Buffer * buf, docstring const & name, int n
        MathData const & def, MathData const & display)
        : InsetMathNest(buf, optionals + 3), numargs_(numargs), argsInLook_(numargs),
          optionals_(optionals), optionalValues_(optionalValues),
-         type_(type), lookOutdated_(true)
+         type_(type), redefinition_(false), lookOutdated_(true),
+         premetrics_(false), labelBoxAscent_(0), labelBoxDescent_(0)
 {
        initMath();
 
@@ -425,26 +428,20 @@ MathMacroTemplate::MathMacroTemplate(Buffer * buf, docstring const & name, int n
 }
 
 
-MathMacroTemplate::MathMacroTemplate(Buffer * buf, docstring const & str)
-       : InsetMathNest(buf, 3), numargs_(0), optionals_(0),
-       type_(MacroTypeNewcommand), lookOutdated_(true)
+bool MathMacroTemplate::fromString(docstring const & str)
 {
-       buffer_ = buf;
-       initMath();
-
-       MathData ar(buf);
+       MathData ar(buffer_);
        mathed_parse_cell(ar, str, Parse::NORMAL);
        if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
                lyxerr << "Cannot read macro from '" << ar << "'" << endl;
                asArray(from_ascii("invalidmacro"), cell(0));
-               // FIXME: The macro template does not make sense after this.
-               // The whole parsing should not be in a constructor which
-               // has no chance to report failure.
-               return;
+               // The macro template does not make sense after this.
+               return false;
        }
        operator=( *(ar[0]->asMacroTemplate()) );
 
        updateLook();
+       return true;
 }
 
 
@@ -545,8 +542,8 @@ void MathMacroTemplate::createLook(int args) const
 
 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
-       StyleChanger dummy2(mi.base, LM_ST_TEXT);
+       Changer dummy1 = mi.base.changeFontSet(from_ascii("mathnormal"));
+       Changer dummy2 = mi.base.changeStyle(LM_ST_TEXT);
 
        // valid macro?
        MacroData const * macro = 0;
@@ -587,9 +584,10 @@ void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
 {
-       ColorChanger dummy0(pi.base.font, Color_math);
-       FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
-       StyleChanger dummy2(pi.base, LM_ST_TEXT);
+       // FIXME: Calling Changer on the same object repeatedly is inefficient.
+       Changer dummy0 = pi.base.font.changeColor(Color_math);
+       Changer dummy1 = pi.base.changeFontSet(from_ascii("mathnormal"));
+       Changer dummy2 = pi.base.changeStyle(LM_ST_TEXT);
 
        setPosCache(pi, x, y);
        Dimension const dim = dimension(*pi.base.bv);
@@ -1173,7 +1171,9 @@ void MathMacroTemplate::read(Lexer & lex)
 void MathMacroTemplate::write(ostream & os) const
 {
        odocstringstream oss;
-       WriteStream wi(oss, false, false, WriteStream::wsDefault);
+       TexRow texrow(false);
+       otexrowstream ots(oss,texrow);
+       WriteStream wi(ots, false, false, WriteStream::wsDefault);
        oss << "FormulaMacro\n";
        write(wi);
        os << to_utf8(oss.str());
@@ -1273,7 +1273,7 @@ docstring MathMacroTemplate::xhtml(XHTMLStream &, OutputParams const &) const
 int MathMacroTemplate::plaintext(odocstringstream & os,
                                 OutputParams const &, size_t) const
 {
-       static docstring const str = '[' + buffer().B_("math macro") + ']';
+       docstring const str = '[' + buffer().B_("math macro") + ']';
 
        os << str;
        return str.size();
@@ -1389,4 +1389,17 @@ string MathMacroTemplate::contextMenuName() const
        return "context-math-macro-definition";
 }
 
+void MathMacroTemplate::addToToc(DocIterator const & pit, bool output_active,
+                                                                UpdateType) const
+{
+       shared_ptr<Toc> toc = buffer().tocBackend().toc("math-macro");
+       docstring str;
+       if (!validMacro())
+               str = bformat(_("Invalid macro! \\%1$s"), name());
+       else
+               str = "\\" + name();
+       toc->push_back(TocItem(pit, 0, str, output_active));
+}
+
+
 } // namespace lyx