]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.C
cursor is no more damaging the background. L-shaped cursor is broken right now....
[lyx.git] / src / mathed / MathMacroTemplate.C
index 32e7bc885bb2192d8f397f9a258335d93f5cff4d..d44b507fb30785e7c752c98a651d68bbdab9d4ec 100644 (file)
@@ -11,7 +11,7 @@
 #include <config.h>
 
 #include "MathMacroTemplate.h"
-#include "MathMLStream.h"
+#include "MathStream.h"
 #include "MathParser.h"
 #include "MathSupport.h"
 
 #include "lyxlex.h"
 #include "LColor.h"
 
-#include "frontends/Application.h"
-#include "frontends/FontLoader.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include "support/lstrings.h"
 
-using lyx::docstring;
-using lyx::support::bformat;
 
-using std::string;
+namespace lyx {
+
+using support::bformat;
+
 using std::auto_ptr;
 using std::ostream;
 using std::endl;
 
 
 MathMacroTemplate::MathMacroTemplate()
-       : InsetMathNest(2), numargs_(0), name_(), type_("newcommand")
+       : InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("newcommand"))
 {
        initMath();
 }
 
 
-MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
-               string const & type, MathArray const & ar1, MathArray const & ar2)
-       : InsetMathNest(2), numargs_(numargs), name_(nm), type_(type)
+MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
+               docstring const & type, MathArray const & ar1, MathArray const & ar2)
+       : InsetMathNest(2), numargs_(numargs), name_(name), type_(type)
 {
        initMath();
 
@@ -58,15 +57,15 @@ MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
 }
 
 
-MathMacroTemplate::MathMacroTemplate(std::istream & is)
+MathMacroTemplate::MathMacroTemplate(docstring const & str)
        : InsetMathNest(2), numargs_(0), name_()
 {
        initMath();
 
        MathArray ar;
-       mathed_parse_cell(ar, is);
+       mathed_parse_cell(ar, str);
        if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
-               lyxerr << "cannot read macro from '" << ar << "'" << endl;
+               lyxerr << "Cannot read macro from '" << ar << "'" << endl;
                return;
        }
        operator=( *(ar[0]->asMacroTemplate()) );
@@ -81,7 +80,7 @@ auto_ptr<InsetBase> MathMacroTemplate::doClone() const
 
 void MathMacroTemplate::edit(LCursor & cur, bool)
 {
-       lyxerr << "MathMacroTemplate: edit left/right" << endl;
+       //lyxerr << "MathMacroTemplate: edit left/right" << endl;
        cur.push(*this);
 }
 
@@ -98,7 +97,7 @@ void MathMacroTemplate::numargs(int numargs)
 }
 
 
-string MathMacroTemplate::name() const
+docstring MathMacroTemplate::name() const
 {
        return name_;
 }
@@ -109,7 +108,7 @@ docstring MathMacroTemplate::prefix() const
        // FIXME UNICODE
        // delete the conversion when bformat() will return a docstring.
        // delete the conversion when bformat() takes a docstring arg.
-       return bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_));
+       return bformat(_(" Macro: %1$s: "), name_);
 }
 
 
@@ -119,7 +118,7 @@ void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
        cell(1).metrics(mi);
        docstring dp = prefix();
        dim.wid = cell(0).width() + cell(1).width() + 20
-               + theApp->fontLoader().metrics(mi.base.font).width(dp);
+               + theFontMetrics(mi.base.font).width(dp);
        dim.asc = std::max(cell(0).ascent(),  cell(1).ascent())  + 7;
        dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
        dim_ = dim;
@@ -158,7 +157,7 @@ void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
        docstring dp = prefix();
        pi.pain.text(x + 2, y, dp, font);
        // FIXME: Painter text should retain the drawn text width
-       x += theApp->fontLoader().metrics(font).width(dp) + 6;
+       x += theFontMetrics(font).width(dp) + 6;
 
        int const w0 = cell(0).width();
        int const w1 = cell(1).width();
@@ -174,9 +173,15 @@ void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
 {
        MathArray ar;
-       mathed_parse_cell(ar, lex.getStream());
+       lex.next(); // eat \begin_inset FormulaMacro line
+       docstring const str = lex.getDocString();
+       lex.next(); // eat that macro definition
+       lex.next(); // eat the \\end_insrt line
+       //lyxerr << "Got to read from: " << to_utf8(str) << endl;
+       mathed_parse_cell(ar, str);
        if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
-               lyxerr << "cannot read macro from '" << ar << "'" << endl;
+               lyxerr << "Cannot read macro from '" << ar << "'" << endl;
+               lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
                return;
        }
        operator=( *(ar[0]->asMacroTemplate()) );
@@ -185,9 +190,11 @@ void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
 
 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
 {
-       WriteStream wi(os, false, false);
-       os << "FormulaMacro\n";
+       odocstringstream oss;
+       WriteStream wi(oss, false, false);
+       oss << "FormulaMacro\n";
        write(wi);
+       os << to_utf8(oss.str());
 }
 
 
@@ -221,3 +228,6 @@ MacroData MathMacroTemplate::asMacroData() const
 {
        return MacroData(asString(cell(0)), numargs(), asString(cell(1)));
 }
+
+
+} // namespace lyx