3 #include "math_macrotemplate.h"
4 #include "math_mathmlstream.h"
5 #include "math_parser.h"
6 #include "frontends/Painter.h"
13 MathMacroTemplate::MathMacroTemplate()
14 : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
18 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
19 string const & type, MathArray const & ar1, MathArray const & ar2)
20 : MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
23 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
24 << numargs_ << std::endl;
31 MathMacroTemplate::MathMacroTemplate(std::istream & is)
32 : MathNestInset(2), numargs_(0), name_()
35 mathed_parse_cell(ar, is);
36 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
37 lyxerr << "cannot read macro from '" << ar << "'" << endl;
40 operator=( *(ar[0]->asMacroTemplate()) );
44 auto_ptr<InsetBase> MathMacroTemplate::clone() const
46 //lyxerr << "cloning MacroTemplate!" << endl;
47 return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
51 int MathMacroTemplate::numargs() const
57 void MathMacroTemplate::numargs(int numargs)
63 string MathMacroTemplate::name() const
69 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
73 dim_.wid = cell(0).width() + cell(1).width() + 10;
74 dim_.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 2;
75 dim_.des = std::max(cell(0).descent(), cell(1).descent()) + 2;
80 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
82 int const w0 = cell(0).width();
83 int const w1 = cell(1).width();
84 cell(0).draw(pi, x + 2, y + 1);
85 pi.pain.rectangle(x, y - dim_.ascent() + 1, w0 + 4, dim_.height(),
87 cell(1).draw(pi, x + 8 + w0, y + 1);
88 pi.pain.rectangle(x + w0 + 6 , y - dim_.ascent() + 1, w1 + 4,
89 dim_.height(), LColor::mathline);
93 void MathMacroTemplate::write(WriteStream & os) const
96 os << "\n\\def\\" << name_.c_str();
97 for (int i = 1; i <= numargs_; ++i)
100 // newcommand or renewcommand
101 os << "\n\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
103 os << '[' << numargs_ << ']';
106 os << '{' << cell(0) << "}";
109 // writing .tex. done.
112 // writing .lyx, write special .tex export only if necessary
113 if (!cell(1).empty())
114 os << "\n{" << cell(1) << '}';