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 "MathMacroTable.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"
37 using support::bformat;
46 InsetFormulaMacro::InsetFormulaMacro()
47 : InsetMathNest(2), name_("unknownA")
51 InsetFormulaMacro::InsetFormulaMacro
52 (string const & name, int nargs, string const & type)
53 : InsetMathNest(2), name_(name)
55 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
59 InsetFormulaMacro::InsetFormulaMacro(string const & s)
60 : InsetMathNest(2), name_("unknownB")
62 std::istringstream is(s);
67 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
69 return auto_ptr<InsetBase>(new InsetFormulaMacro(*this));
73 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
75 os << "FormulaMacro\n";
76 WriteStream wi(os, false, false);
81 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
82 OutputParams const & runparams) const
84 //lyxerr << "InsetFormulaMacro::latex" << endl;
85 WriteStream wi(os, runparams.moving_arg, true);
91 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
92 OutputParams const &) const
95 WriteStream wi(oss, false, true);
98 docstring const str = oss.str();
104 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
105 OutputParams const & runparams) const
107 return plaintext(buf, os, runparams);
111 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
113 read(lex.getStream());
117 void InsetFormulaMacro::read(std::istream & is)
119 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
121 MathMacroTable::create(MathAtom(p.release()));
125 string InsetFormulaMacro::prefix() const
127 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
131 bool InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
133 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
134 tmpl()->metrics(mi, dim);
137 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
138 bool const changed = dim_ != dim;
144 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
147 LyXFont font = p.base.font;
148 font.setColor(LColor::math);
150 PainterInfo pi(p.base.bv, p.pain);
151 pi.base.style = LM_ST_TEXT;
154 int const a = y - dim_.asc + 1;
155 int const w = dim_.wid - 2;
156 int const h = dim_.height() - 2;
158 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
159 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
160 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
166 Cursor & cur = p.base.bv->cursor();
167 if (cur.isInside(this))
168 cur.drawSelection(pi);
171 pi.pain.text(x + 2, y, prefix(), font);
175 x + theFontMetrics(p.base.font).width(prefix()) + 5,
178 setPosCache(pi, x, y);
182 MathAtom & InsetFormulaMacro::tmpl() const
184 return MathMacroTable::provide(name_);