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"
38 using support::bformat;
41 InsetFormulaMacro::InsetFormulaMacro()
42 : InsetMathNest(2), name_("unknownA")
46 InsetFormulaMacro::InsetFormulaMacro
47 (docstring const & name, int nargs, docstring const & type)
48 : InsetMathNest(2), name_(name)
50 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
54 InsetFormulaMacro::InsetFormulaMacro(string const & s)
55 : InsetMathNest(2), name_("unknownB")
57 std::istringstream is(s);
62 Inset * InsetFormulaMacro::clone() const
64 return new InsetFormulaMacro(*this);
68 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
70 os << "FormulaMacro\n";
71 WriteStream wi(os, false, false);
76 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
77 OutputParams const & runparams) const
79 //lyxerr << "InsetFormulaMacro::latex" << endl;
80 WriteStream wi(os, runparams.moving_arg, true);
86 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
87 OutputParams const &) const
90 WriteStream wi(oss, false, true);
93 docstring const str = oss.str();
99 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
100 OutputParams const & runparams) const
102 return plaintext(buf, os, runparams);
106 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
108 read(lex.getStream());
112 void InsetFormulaMacro::read(std::istream & is)
114 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
116 MathMacroTable::create(MathAtom(p.release()));
120 string InsetFormulaMacro::prefix() const
122 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
126 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
128 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
129 tmpl()->metrics(mi, dim);
132 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
137 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
140 Font font = p.base.font;
141 font.setColor(Color_math);
143 PainterInfo pi(p.base.bv, p.pain);
144 pi.base.style = LM_ST_TEXT;
147 int const a = y - dim_.asc + 1;
148 int const w = dim_.wid - 2;
149 int const h = dim_.height() - 2;
151 // Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
152 pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
153 pi.pain.rectangle(x, a, w, h, Color_mathframe);
157 Cursor & cur = p.base.bv->cursor();
158 if (cur.isInside(this))
159 cur.drawSelection(pi);
162 pi.pain.text(x + 2, y, prefix(), font);
166 x + theFontMetrics(p.base.font).width(prefix()) + 5,
169 setPosCache(pi, x, y);
173 MathAtom & InsetFormulaMacro::tmpl() const
175 return MathMacroTable::provide(name_);