2 * \file InsetFormulaMacro.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetFormulaMacro.h"
15 #include "MacroTable.h"
16 #include "MathMacroTemplate.h"
18 #include "BufferView.h"
24 #include "OutputParams.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/lstrings.h"
40 using support::bformat;
43 InsetFormulaMacro::InsetFormulaMacro()
44 : InsetMathNest(2), name_("unknownA")
48 InsetFormulaMacro::InsetFormulaMacro
49 (docstring const & name, int nargs, docstring const & type)
50 : InsetMathNest(2), name_(name)
52 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
56 InsetFormulaMacro::InsetFormulaMacro(string const & s)
57 : InsetMathNest(2), name_("unknownB")
59 std::istringstream is(s);
64 auto_ptr<Inset> InsetFormulaMacro::clone() const
66 return auto_ptr<Inset>(new InsetFormulaMacro(*this));
70 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
72 os << "FormulaMacro\n";
73 WriteStream wi(os, false, false);
78 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
79 OutputParams const & runparams) const
81 //lyxerr << "InsetFormulaMacro::latex" << endl;
82 WriteStream wi(os, runparams.moving_arg, true);
88 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
89 OutputParams const &) const
92 WriteStream wi(oss, false, true);
95 docstring const str = oss.str();
101 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
102 OutputParams const & runparams) const
104 return plaintext(buf, os, runparams);
108 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
110 read(lex.getStream());
114 void InsetFormulaMacro::read(std::istream & is)
116 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
118 MathMacroTable::create(MathAtom(p.release()));
122 string InsetFormulaMacro::prefix() const
124 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
128 bool InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
130 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
131 tmpl()->metrics(mi, dim);
134 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
135 bool const changed = dim_ != dim;
141 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
144 Font font = p.base.font;
145 font.setColor(Color::math);
147 PainterInfo pi(p.base.bv, p.pain);
148 pi.base.style = LM_ST_TEXT;
151 int const a = y - dim_.asc + 1;
152 int const w = dim_.wid - 2;
153 int const h = dim_.height() - 2;
155 // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
156 pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
157 pi.pain.rectangle(x, a, w, h, Color::mathframe);
163 Cursor & cur = p.base.bv->cursor();
164 if (cur.isInside(this))
165 cur.drawSelection(pi);
168 pi.pain.text(x + 2, y, prefix(), font);
172 x + theFontMetrics(p.base.font).width(prefix()) + 5,
175 setPosCache(pi, x, y);
179 MathAtom & InsetFormulaMacro::tmpl() const
181 return MathMacroTable::provide(name_);