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"
39 using support::bformat;
42 InsetFormulaMacro::InsetFormulaMacro()
43 : InsetMathNest(2), name_("unknownA")
47 InsetFormulaMacro::InsetFormulaMacro
48 (docstring const & name, int nargs, docstring const & type)
49 : InsetMathNest(2), name_(name)
51 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
55 InsetFormulaMacro::InsetFormulaMacro(string const & s)
56 : InsetMathNest(2), name_("unknownB")
58 std::istringstream is(s);
63 Inset * InsetFormulaMacro::clone() const
65 return new InsetFormulaMacro(*this);
69 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
71 os << "FormulaMacro\n";
72 WriteStream wi(os, false, false);
77 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
78 OutputParams const & runparams) const
80 //lyxerr << "InsetFormulaMacro::latex" << endl;
81 WriteStream wi(os, runparams.moving_arg, true);
87 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
88 OutputParams const &) const
91 WriteStream wi(oss, false, true);
94 docstring const str = oss.str();
100 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
101 OutputParams const & runparams) const
103 return plaintext(buf, os, runparams);
107 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
109 read(lex.getStream());
113 void InsetFormulaMacro::read(std::istream & is)
115 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
117 MathMacroTable::create(MathAtom(p.release()));
121 string InsetFormulaMacro::prefix() const
123 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
127 bool InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
129 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
130 tmpl()->metrics(mi, dim);
133 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
134 bool const changed = dim_ != dim;
140 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
143 Font font = p.base.font;
144 font.setColor(Color::math);
146 PainterInfo pi(p.base.bv, p.pain);
147 pi.base.style = LM_ST_TEXT;
150 int const a = y - dim_.asc + 1;
151 int const w = dim_.wid - 2;
152 int const h = dim_.height() - 2;
154 // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
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);
165 pi.pain.text(x + 2, y, prefix(), font);
169 x + theFontMetrics(p.base.font).width(prefix()) + 5,
172 setPosCache(pi, x, y);
176 MathAtom & InsetFormulaMacro::tmpl() const
178 return MathMacroTable::provide(name_);