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 "MathMacroTemplate.h"
14 #include "MathMLStream.h"
15 #include "MathParser.h"
16 #include "MathSupport.h"
24 #include "frontends/Application.h"
25 #include "frontends/FontLoader.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/lstrings.h"
32 using lyx::support::bformat;
40 MathMacroTemplate::MathMacroTemplate()
41 : InsetMathNest(2), numargs_(0), name_(), type_("newcommand")
47 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
48 string const & type, MathArray const & ar1, MathArray const & ar2)
49 : InsetMathNest(2), numargs_(numargs), name_(nm), type_(type)
54 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
55 << numargs_ << std::endl;
61 MathMacroTemplate::MathMacroTemplate(std::istream & is)
62 : InsetMathNest(2), numargs_(0), name_()
67 mathed_parse_cell(ar, is);
68 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
69 lyxerr << "cannot read macro from '" << ar << "'" << endl;
72 operator=( *(ar[0]->asMacroTemplate()) );
76 auto_ptr<InsetBase> MathMacroTemplate::doClone() const
78 return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
82 void MathMacroTemplate::edit(LCursor & cur, bool)
84 lyxerr << "MathMacroTemplate: edit left/right" << endl;
89 int MathMacroTemplate::numargs() const
95 void MathMacroTemplate::numargs(int numargs)
101 string MathMacroTemplate::name() const
107 docstring MathMacroTemplate::prefix() const
110 // delete the conversion when bformat() will return a docstring.
111 // delete the conversion when bformat() takes a docstring arg.
112 return bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_));
116 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
120 docstring dp = prefix();
121 dim.wid = cell(0).width() + cell(1).width() + 20
122 + theApp->fontLoader().metrics(mi.base.font).width(dp);
123 dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7;
124 dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
129 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
131 setPosCache(p, x, y);
134 LyXFont font = p.base.font;
135 font.setColor(LColor::math);
137 PainterInfo pi(p.base.bv, p.pain);
138 pi.base.style = LM_ST_TEXT;
141 int const a = y - dim_.asc + 1;
142 int const w = dim_.wid - 2;
143 int const h = dim_.height() - 2;
145 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
146 // the next line would overwrite the selection!
147 //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
148 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
154 LCursor & cur = p.base.bv->cursor();
155 if (cur.isInside(this))
156 cur.drawSelection(pi);
158 docstring dp = prefix();
159 pi.pain.text(x + 2, y, dp, font);
160 // FIXME: Painter text should retain the drawn text width
161 x += theApp->fontLoader().metrics(font).width(dp) + 6;
163 int const w0 = cell(0).width();
164 int const w1 = cell(1).width();
165 cell(0).draw(pi, x + 2, y + 1);
166 pi.pain.rectangle(x, y - dim_.ascent() + 3,
167 w0 + 4, dim_.height() - 6, LColor::mathline);
168 cell(1).draw(pi, x + 8 + w0, y + 1);
169 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
170 w1 + 4, dim_.height() - 6, LColor::mathline);
174 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
177 mathed_parse_cell(ar, lex.getStream());
178 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
179 lyxerr << "cannot read macro from '" << ar << "'" << endl;
182 operator=( *(ar[0]->asMacroTemplate()) );
186 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
188 WriteStream wi(os, false, false);
189 os << "FormulaMacro\n";
194 void MathMacroTemplate::write(WriteStream & os) const
196 if (type_ == "def") {
197 os << "\\def\\" << name_.c_str();
198 for (int i = 1; i <= numargs_; ++i)
201 // newcommand or renewcommand
202 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
204 os << '[' << numargs_ << ']';
207 os << '{' << cell(0) << "}";
210 // writing .tex. done.
213 // writing .lyx, write special .tex export only if necessary
214 if (!cell(1).empty())
215 os << "\n{" << cell(1) << '}';
220 MacroData MathMacroTemplate::asMacroData() const
222 return MacroData(asString(cell(0)), numargs(), asString(cell(1)));