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"
20 #include "support/debug.h"
21 #include "support/gettext.h"
23 #include "OutputParams.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lstrings.h"
33 using namespace lyx::support;
38 InsetFormulaMacro::InsetFormulaMacro()
39 : InsetMathNest(2), name_("unknownA")
43 InsetFormulaMacro::InsetFormulaMacro
44 (docstring const & name, int nargs, docstring const & type)
45 : InsetMathNest(2), name_(name)
47 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
51 InsetFormulaMacro::InsetFormulaMacro(string const & s)
52 : InsetMathNest(2), name_("unknownB")
59 Inset * InsetFormulaMacro::clone() const
61 return new InsetFormulaMacro(*this);
65 void InsetFormulaMacro::write(ostream & os) const
67 os << "FormulaMacro\n";
68 WriteStream wi(os, false, false, false);
73 int InsetFormulaMacro::latex(odocstream & os,
74 OutputParams const & runparams) const
76 //lyxerr << "InsetFormulaMacro::latex" << endl;
77 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
84 int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & runparams) const
87 WriteStream wi(oss, false, true, false, runparams.encoding);
90 docstring const str = oss.str();
96 int InsetFormulaMacro::docbook(ostream & os,
97 OutputParams const & runparams) const
99 return plaintext(os, runparams);
103 void InsetFormulaMacro::read(Lexer & lex)
105 read(lex.getStream());
109 void InsetFormulaMacro::read(istream & is)
111 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
113 MathMacroTable::create(MathAtom(p.release()));
117 string InsetFormulaMacro::prefix() const
119 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
123 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
125 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
126 tmpl()->metrics(mi, dim);
129 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
134 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
137 Font font = p.base.font;
138 font.setColor(Color_math);
140 PainterInfo pi(p.base.bv, p.pain);
141 pi.base.style = LM_ST_TEXT;
144 int const a = y - dim_.asc + 1;
145 int const w = dim_.wid - 2;
146 int const h = dim_.height() - 2;
148 // Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
149 pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
150 pi.pain.rectangle(x, a, w, h, Color_mathframe);
154 Cursor & cur = p.base.bv->cursor();
155 if (cur.isInside(this))
156 cur.drawSelection(pi);
159 pi.pain.text(x + 2, y, prefix(), font);
163 x + theFontMetrics(p.base.font).width(prefix()) + 5,
166 setPosCache(pi, x, y);
170 MathAtom & InsetFormulaMacro::tmpl() const
172 return MathMacroTable::provide(name_);