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"
30 using lyx::support::bformat;
38 MathMacroTemplate::MathMacroTemplate()
39 : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
45 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
46 string const & type, MathArray const & ar1, MathArray const & ar2)
47 : MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
52 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
53 << numargs_ << std::endl;
59 MathMacroTemplate::MathMacroTemplate(std::istream & is)
60 : MathNestInset(2), numargs_(0), name_()
65 mathed_parse_cell(ar, is);
66 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
67 lyxerr << "cannot read macro from '" << ar << "'" << endl;
70 operator=( *(ar[0]->asMacroTemplate()) );
74 auto_ptr<InsetBase> MathMacroTemplate::doClone() const
76 return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
80 void MathMacroTemplate::edit(LCursor & cur, bool)
82 lyxerr << "MathMacroTemplate: edit left/right" << endl;
87 int MathMacroTemplate::numargs() const
93 void MathMacroTemplate::numargs(int numargs)
99 string MathMacroTemplate::name() const
105 string MathMacroTemplate::prefix() const
107 return bformat(_(" Macro: %1$s: "), name_);
111 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
115 docstring dp(prefix().begin(), prefix().end());
116 dim.wid = cell(0).width() + cell(1).width() + 20
117 + font_metrics::width(dp, mi.base.font);
118 dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7;
119 dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
124 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
126 setPosCache(p, x, y);
129 LyXFont font = p.base.font;
130 font.setColor(LColor::math);
132 PainterInfo pi(p.base.bv, p.pain);
133 pi.base.style = LM_ST_TEXT;
136 int const a = y - dim_.asc + 1;
137 int const w = dim_.wid - 2;
138 int const h = dim_.height() - 2;
140 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
141 // the next line would overwrite the selection!
142 //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
143 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
149 LCursor & cur = p.base.bv->cursor();
150 if (cur.isInside(this))
151 cur.drawSelection(pi);
153 docstring dp(prefix().begin(), prefix().end());
154 pi.pain.text(x + 2, y, dp, font);
155 x += font_metrics::width(dp, pi.base.font) + 6;
157 int const w0 = cell(0).width();
158 int const w1 = cell(1).width();
159 cell(0).draw(pi, x + 2, y + 1);
160 pi.pain.rectangle(x, y - dim_.ascent() + 3,
161 w0 + 4, dim_.height() - 6, LColor::mathline);
162 cell(1).draw(pi, x + 8 + w0, y + 1);
163 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
164 w1 + 4, dim_.height() - 6, LColor::mathline);
168 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
171 mathed_parse_cell(ar, lex.getStream());
172 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
173 lyxerr << "cannot read macro from '" << ar << "'" << endl;
176 operator=( *(ar[0]->asMacroTemplate()) );
180 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
182 WriteStream wi(os, false, false);
183 os << "FormulaMacro\n";
188 void MathMacroTemplate::write(WriteStream & os) const
190 if (type_ == "def") {
191 os << "\\def\\" << name_.c_str();
192 for (int i = 1; i <= numargs_; ++i)
195 // newcommand or renewcommand
196 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
198 os << '[' << numargs_ << ']';
201 os << '{' << cell(0) << "}";
204 // writing .tex. done.
207 // writing .lyx, write special .tex export only if necessary
208 if (!cell(1).empty())
209 os << "\n{" << cell(1) << '}';
214 MacroData MathMacroTemplate::asMacroData() const
216 return MacroData(asString(cell(0)), numargs(), asString(cell(1)));