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 "formulamacro.h"
15 #include "math_macrotable.h"
16 #include "math_macrotemplate.h"
17 #include "math_mathmlstream.h"
19 #include "BufferView.h"
25 #include "outputparams.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
30 #include "support/lstrings.h"
34 using lyx::support::bformat;
43 InsetFormulaMacro::InsetFormulaMacro()
44 : MathNestInset(2), name_("unknownA")
48 InsetFormulaMacro::InsetFormulaMacro
49 (string const & name, int nargs, string const & type)
50 : MathNestInset(2), name_(name)
52 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
56 InsetFormulaMacro::InsetFormulaMacro(string const & s)
57 : MathNestInset(2), name_("unknownB")
59 std::istringstream is(s);
64 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
66 return auto_ptr<InsetBase>(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 &, ostream & 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 &, ostream & os,
89 OutputParams const &) const
91 WriteStream wi(os, false, true);
97 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
98 OutputParams const & runparams) const
100 return plaintext(buf, os, runparams);
104 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
106 read(lex.getStream());
110 void InsetFormulaMacro::read(std::istream & is)
112 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
114 MathMacroTable::create(MathAtom(p.release()));
118 string InsetFormulaMacro::prefix() const
120 return bformat(_(" Macro: %1$s: "), name_);
124 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
126 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
127 tmpl()->metrics(mi, dim);
130 dim.wid += 10 + font_metrics::width(prefix(), mi.base.font);
135 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
138 LyXFont font = p.base.font;
139 font.setColor(LColor::math);
141 PainterInfo pi(p.base.bv, p.pain);
142 pi.base.style = LM_ST_TEXT;
145 int const a = y - dim_.asc + 1;
146 int const w = dim_.wid - 2;
147 int const h = dim_.height() - 2;
149 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
150 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
151 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
157 LCursor & cur = p.base.bv->cursor();
158 if (cur.isInside(this))
159 cur.drawSelection(pi);
162 pi.pain.text(x + 2, y, prefix(), font);
165 tmpl()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
167 setPosCache(pi, x, y);
171 MathAtom & InsetFormulaMacro::tmpl() const
173 return MathMacroTable::provide(name_);