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 "MathStream.h"
15 #include "MathParser.h"
16 #include "MathSupport.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lstrings.h"
33 using support::bformat;
40 MathMacroTemplate::MathMacroTemplate()
41 : InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("newcommand"))
47 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
48 docstring const & type, MathData const & ar1, MathData const & ar2)
49 : InsetMathNest(2), numargs_(numargs), name_(name), type_(type)
54 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
55 << numargs_ << std::endl;
61 MathMacroTemplate::MathMacroTemplate(docstring const & str)
62 : InsetMathNest(2), numargs_(0), name_()
67 mathed_parse_cell(ar, str);
68 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
69 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
72 operator=( *(ar[0]->asMacroTemplate()) );
76 auto_ptr<Inset> MathMacroTemplate::doClone() const
78 return auto_ptr<Inset>(new MathMacroTemplate(*this));
82 void MathMacroTemplate::edit(Cursor & cur, bool)
84 //lyxerr << "MathMacroTemplate: edit left/right" << endl;
89 int MathMacroTemplate::numargs() const
95 void MathMacroTemplate::numargs(int numargs)
101 docstring MathMacroTemplate::name() const
107 docstring MathMacroTemplate::prefix() const
109 return bformat(_(" Macro: %1$s: "), name_);
113 bool MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
115 bool lockMacro = MacroTable::globalMacros().has(name_);
117 MacroTable::globalMacros().get(name_).lock();
121 docstring dp = prefix();
122 dim.wid = cell(0).width() + cell(1).width() + 20
123 + theFontMetrics(mi.base.font).width(dp);
124 dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7;
125 dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
128 MacroTable::globalMacros().get(name_).unlock();
137 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
139 bool lockMacro = MacroTable::globalMacros().has(name_);
141 MacroTable::globalMacros().get(name_).lock();
143 setPosCache(p, x, y);
146 Font font = p.base.font;
147 font.setColor(Color::math);
149 PainterInfo pi(p.base.bv, p.pain);
150 pi.base.style = LM_ST_TEXT;
153 int const a = y - dim_.asc + 1;
154 int const w = dim_.wid - 2;
155 int const h = dim_.height() - 2;
157 // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
158 // the next line would overwrite the selection!
159 //pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
160 pi.pain.rectangle(x, a, w, h, Color::mathframe);
166 Cursor & cur = p.base.bv->cursor();
167 if (cur.isInside(this))
168 cur.drawSelection(pi);
170 docstring dp = prefix();
171 pi.pain.text(x + 2, y, dp, font);
172 // FIXME: Painter text should retain the drawn text width
173 x += theFontMetrics(font).width(dp) + 6;
175 int const w0 = cell(0).width();
176 int const w1 = cell(1).width();
177 cell(0).draw(pi, x + 2, y + 1);
178 pi.pain.rectangle(x, y - dim_.ascent() + 3,
179 w0 + 4, dim_.height() - 6, Color::mathline);
180 cell(1).draw(pi, x + 8 + w0, y + 1);
181 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
182 w1 + 4, dim_.height() - 6, Color::mathline);
185 MacroTable::globalMacros().get(name_).unlock();
189 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
192 mathed_parse_cell(ar, lex.getStream());
193 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
194 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
195 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
198 operator=( *(ar[0]->asMacroTemplate()) );
202 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
204 odocstringstream oss;
205 WriteStream wi(oss, false, false);
206 oss << "FormulaMacro\n";
208 os << to_utf8(oss.str());
212 void MathMacroTemplate::write(WriteStream & os) const
214 if (type_ == "def") {
215 os << "\\def\\" << name_.c_str();
216 for (int i = 1; i <= numargs_; ++i)
219 // newcommand or renewcommand
220 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
222 os << '[' << numargs_ << ']';
225 os << '{' << cell(0) << "}";
228 // writing .tex. done.
231 // writing .lyx, write special .tex export only if necessary
232 if (!cell(1).empty())
233 os << "\n{" << cell(1) << '}';
238 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
239 OutputParams const &) const
241 static docstring const str = '[' + buf.B_("math macro") + ']';
248 MacroData MathMacroTemplate::asMacroData() const
250 return MacroData(asString(cell(0)), numargs(), asString(cell(1)), std::string());