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"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "support/lstrings.h"
32 using support::bformat;
38 MathMacroTemplate::MathMacroTemplate()
39 : InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("newcommand"))
45 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
46 docstring const & type, MathData const & ar1, MathData const & ar2)
47 : InsetMathNest(2), numargs_(numargs), name_(name), type_(type)
52 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
53 << numargs_ << std::endl;
59 MathMacroTemplate::MathMacroTemplate(docstring const & str)
60 : InsetMathNest(2), numargs_(0), name_()
65 mathed_parse_cell(ar, str);
66 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
67 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
70 operator=( *(ar[0]->asMacroTemplate()) );
74 Inset * MathMacroTemplate::clone() const
76 return new MathMacroTemplate(*this);
80 void MathMacroTemplate::edit(Cursor & cur, bool)
82 //lyxerr << "MathMacroTemplate: edit left/right" << endl;
87 int MathMacroTemplate::numargs() const
93 void MathMacroTemplate::numargs(int numargs)
99 docstring MathMacroTemplate::name() const
105 docstring MathMacroTemplate::prefix() const
107 return bformat(_(" Macro: %1$s: "), name_);
111 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
113 bool lockMacro = MacroTable::globalMacros().has(name_);
115 MacroTable::globalMacros().get(name_).lock();
118 cell(0).metrics(mi, dim0);
120 cell(1).metrics(mi, dim1);
121 docstring dp = prefix();
122 dim.wid = dim0.width() + dim1.width() + 20
123 + theFontMetrics(mi.base.font).width(dp);
124 dim.asc = std::max(dim0.ascent(), dim1.ascent()) + 7;
125 dim.des = std::max(dim0.descent(), dim1.descent()) + 7;
128 MacroTable::globalMacros().get(name_).unlock();
130 // Cache the inset dimension.
131 setDimCache(mi, dim);
135 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
137 bool lockMacro = MacroTable::globalMacros().has(name_);
139 MacroTable::globalMacros().get(name_).lock();
141 setPosCache(p, x, y);
143 Dimension const dim = dimension(*p.base.bv);
146 FontInfo 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).dimension(*pi.base.bv).width();
174 int const w1 = cell(1).dimension(*pi.base.bv).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());