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 "math_support.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
27 #include "support/lstrings.h"
29 using lyx::support::bformat;
37 MathMacroTemplate::MathMacroTemplate()
38 : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
42 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
43 string const & type, MathArray const & ar1, MathArray const & ar2)
44 : MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
47 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
48 << numargs_ << std::endl;
54 MathMacroTemplate::MathMacroTemplate(std::istream & is)
55 : MathNestInset(2), numargs_(0), name_()
58 mathed_parse_cell(ar, is);
59 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
60 lyxerr << "cannot read macro from '" << ar << "'" << endl;
63 operator=( *(ar[0]->asMacroTemplate()) );
67 auto_ptr<InsetBase> MathMacroTemplate::doClone() const
69 return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
73 void MathMacroTemplate::edit(LCursor & cur, bool)
75 lyxerr << "MathMacroTemplate: edit left/right" << endl;
80 int MathMacroTemplate::numargs() const
86 void MathMacroTemplate::numargs(int numargs)
92 string MathMacroTemplate::name() const
98 string MathMacroTemplate::prefix() const
100 return bformat(_(" Macro: %1$s: "), name_);
104 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
108 dim.wid = cell(0).width() + cell(1).width() + 20
109 + font_metrics::width(prefix(), mi.base.font);
110 dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7;
111 dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
116 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
118 setPosCache(p, x, y);
121 LyXFont font = p.base.font;
122 font.setColor(LColor::math);
124 PainterInfo pi(p.base.bv, p.pain);
125 pi.base.style = LM_ST_TEXT;
128 int const a = y - dim_.asc + 1;
129 int const w = dim_.wid - 2;
130 int const h = dim_.height() - 2;
132 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
133 // the next line would overwrite the selection!
134 //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
135 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
141 LCursor & cur = p.base.bv->cursor();
142 if (cur.isInside(this))
143 cur.drawSelection(pi);
146 pi.pain.text(x + 2, y, prefix(), font);
147 x += font_metrics::width(prefix(), pi.base.font) + 6;
149 int const w0 = cell(0).width();
150 int const w1 = cell(1).width();
151 cell(0).draw(pi, x + 2, y + 1);
152 pi.pain.rectangle(x, y - dim_.ascent() + 3,
153 w0 + 4, dim_.height() - 6, LColor::mathline);
154 cell(1).draw(pi, x + 8 + w0, y + 1);
155 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
156 w1 + 4, dim_.height() - 6, LColor::mathline);
160 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
163 mathed_parse_cell(ar, lex.getStream());
164 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
165 lyxerr << "cannot read macro from '" << ar << "'" << endl;
168 operator=( *(ar[0]->asMacroTemplate()) );
172 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
174 WriteStream wi(os, false, false);
175 os << "FormulaMacro\n";
180 void MathMacroTemplate::write(WriteStream & os) const
182 if (type_ == "def") {
183 os << "\\def\\" << name_.c_str();
184 for (int i = 1; i <= numargs_; ++i)
187 // newcommand or renewcommand
188 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
190 os << '[' << numargs_ << ']';
193 os << '{' << cell(0) << "}";
196 // writing .tex. done.
199 // writing .lyx, write special .tex export only if necessary
200 if (!cell(1).empty())
201 os << "\n{" << cell(1) << '}';
206 MacroData MathMacroTemplate::asMacroData() const
208 return MacroData(asString(cell(0)), numargs(), asString(cell(1)));