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);
164 Cursor & cur = p.base.bv->cursor();
165 if (cur.isInside(this))
166 cur.drawSelection(pi);
168 docstring dp = prefix();
169 pi.pain.text(x + 2, y, dp, font);
170 // FIXME: Painter text should retain the drawn text width
171 x += theFontMetrics(font).width(dp) + 6;
173 int const w0 = cell(0).width();
174 int const w1 = cell(1).width();
175 cell(0).draw(pi, x + 2, y + 1);
176 pi.pain.rectangle(x, y - dim_.ascent() + 3,
177 w0 + 4, dim_.height() - 6, Color::mathline);
178 cell(1).draw(pi, x + 8 + w0, y + 1);
179 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
180 w1 + 4, dim_.height() - 6, Color::mathline);
183 MacroTable::globalMacros().get(name_).unlock();
187 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
190 mathed_parse_cell(ar, lex.getStream());
191 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
192 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
193 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
196 operator=( *(ar[0]->asMacroTemplate()) );
200 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
202 odocstringstream oss;
203 WriteStream wi(oss, false, false);
204 oss << "FormulaMacro\n";
206 os << to_utf8(oss.str());
210 void MathMacroTemplate::write(WriteStream & os) const
212 if (type_ == "def") {
213 os << "\\def\\" << name_.c_str();
214 for (int i = 1; i <= numargs_; ++i)
217 // newcommand or renewcommand
218 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
220 os << '[' << numargs_ << ']';
223 os << '{' << cell(0) << "}";
226 // writing .tex. done.
229 // writing .lyx, write special .tex export only if necessary
230 if (!cell(1).empty())
231 os << "\n{" << cell(1) << '}';
236 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
237 OutputParams const &) const
239 static docstring const str = '[' + buf.B_("math macro") + ']';
246 MacroData MathMacroTemplate::asMacroData() const
248 return MacroData(asString(cell(0)), numargs(), asString(cell(1)), std::string());