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;
39 MathMacroTemplate::MathMacroTemplate()
40 : InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("newcommand"))
46 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
47 docstring const & type, MathData const & ar1, MathData const & ar2)
48 : InsetMathNest(2), numargs_(numargs), name_(name), type_(type)
53 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
54 << numargs_ << std::endl;
60 MathMacroTemplate::MathMacroTemplate(docstring const & str)
61 : InsetMathNest(2), numargs_(0), name_()
66 mathed_parse_cell(ar, str);
67 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
68 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
71 operator=( *(ar[0]->asMacroTemplate()) );
75 Inset * MathMacroTemplate::clone() const
77 return new MathMacroTemplate(*this);
81 void MathMacroTemplate::edit(Cursor & cur, bool)
83 //lyxerr << "MathMacroTemplate: edit left/right" << endl;
88 int MathMacroTemplate::numargs() const
94 void MathMacroTemplate::numargs(int numargs)
100 docstring MathMacroTemplate::name() const
106 docstring MathMacroTemplate::prefix() const
108 return bformat(_(" Macro: %1$s: "), name_);
112 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
114 bool lockMacro = MacroTable::globalMacros().has(name_);
116 MacroTable::globalMacros().get(name_).lock();
120 docstring dp = prefix();
121 dim.wid = cell(0).width() + cell(1).width() + 20
122 + theFontMetrics(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;
127 MacroTable::globalMacros().get(name_).unlock();
133 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
135 bool lockMacro = MacroTable::globalMacros().has(name_);
137 MacroTable::globalMacros().get(name_).lock();
139 setPosCache(p, x, y);
142 Font font = p.base.font;
143 font.setColor(Color::math);
145 PainterInfo pi(p.base.bv, p.pain);
146 pi.base.style = LM_ST_TEXT;
149 int const a = y - dim_.asc + 1;
150 int const w = dim_.wid - 2;
151 int const h = dim_.height() - 2;
153 // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
154 // the next line would overwrite the selection!
155 //pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
156 pi.pain.rectangle(x, a, w, h, Color::mathframe);
160 Cursor & cur = p.base.bv->cursor();
161 if (cur.isInside(this))
162 cur.drawSelection(pi);
164 docstring dp = prefix();
165 pi.pain.text(x + 2, y, dp, font);
166 // FIXME: Painter text should retain the drawn text width
167 x += theFontMetrics(font).width(dp) + 6;
169 int const w0 = cell(0).width();
170 int const w1 = cell(1).width();
171 cell(0).draw(pi, x + 2, y + 1);
172 pi.pain.rectangle(x, y - dim_.ascent() + 3,
173 w0 + 4, dim_.height() - 6, Color::mathline);
174 cell(1).draw(pi, x + 8 + w0, y + 1);
175 pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
176 w1 + 4, dim_.height() - 6, Color::mathline);
179 MacroTable::globalMacros().get(name_).unlock();
183 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
186 mathed_parse_cell(ar, lex.getStream());
187 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
188 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
189 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
192 operator=( *(ar[0]->asMacroTemplate()) );
196 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
198 odocstringstream oss;
199 WriteStream wi(oss, false, false);
200 oss << "FormulaMacro\n";
202 os << to_utf8(oss.str());
206 void MathMacroTemplate::write(WriteStream & os) const
208 if (type_ == "def") {
209 os << "\\def\\" << name_.c_str();
210 for (int i = 1; i <= numargs_; ++i)
213 // newcommand or renewcommand
214 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
216 os << '[' << numargs_ << ']';
219 os << '{' << cell(0) << "}";
222 // writing .tex. done.
225 // writing .lyx, write special .tex export only if necessary
226 if (!cell(1).empty())
227 os << "\n{" << cell(1) << '}';
232 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
233 OutputParams const &) const
235 static docstring const str = '[' + buf.B_("math macro") + ']';
242 MacroData MathMacroTemplate::asMacroData() const
244 return MacroData(asString(cell(0)), numargs(), asString(cell(1)), std::string());