2 * \file math_macrotemplate.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_macrotemplate.h"
14 #include "math_mathmlstream.h"
15 #include "math_parser.h"
16 #include "frontends/Painter.h"
24 MathMacroTemplate::MathMacroTemplate()
25 : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
29 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
30 string const & type, MathArray const & ar1, MathArray const & ar2)
31 : MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
34 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
35 << numargs_ << std::endl;
42 MathMacroTemplate::MathMacroTemplate(std::istream & is)
43 : MathNestInset(2), numargs_(0), name_()
46 mathed_parse_cell(ar, is);
47 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
48 lyxerr << "cannot read macro from '" << ar << "'" << endl;
51 operator=( *(ar[0]->asMacroTemplate()) );
55 auto_ptr<InsetBase> MathMacroTemplate::clone() const
57 //lyxerr << "cloning MacroTemplate!" << endl;
58 return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
62 int MathMacroTemplate::numargs() const
68 void MathMacroTemplate::numargs(int numargs)
74 string MathMacroTemplate::name() const
80 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
84 dim_.wid = cell(0).width() + cell(1).width() + 10;
85 dim_.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 2;
86 dim_.des = std::max(cell(0).descent(), cell(1).descent()) + 2;
91 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
93 int const w0 = cell(0).width();
94 int const w1 = cell(1).width();
95 cell(0).draw(pi, x + 2, y + 1);
96 pi.pain.rectangle(x, y - dim_.ascent() + 1, w0 + 4, dim_.height(),
98 cell(1).draw(pi, x + 8 + w0, y + 1);
99 pi.pain.rectangle(x + w0 + 6 , y - dim_.ascent() + 1, w1 + 4,
100 dim_.height(), LColor::mathline);
104 void MathMacroTemplate::write(WriteStream & os) const
106 if (type_ == "def") {
107 os << "\n\\def\\" << name_.c_str();
108 for (int i = 1; i <= numargs_; ++i)
111 // newcommand or renewcommand
112 os << "\n\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
114 os << '[' << numargs_ << ']';
117 os << '{' << cell(0) << "}";
120 // writing .tex. done.
123 // writing .lyx, write special .tex export only if necessary
124 if (!cell(1).empty())
125 os << "\n{" << cell(1) << '}';