2 * \file InsetFormulaMacro.C
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
94 WriteStream wi(os, false, true);
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 &, LyXLex & 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 void 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());
138 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
141 LyXFont font = p.base.font;
142 font.setColor(LColor::math);
144 PainterInfo pi(p.base.bv, p.pain);
145 pi.base.style = LM_ST_TEXT;
148 int const a = y - dim_.asc + 1;
149 int const w = dim_.wid - 2;
150 int const h = dim_.height() - 2;
152 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
153 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
154 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
160 LCursor & 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_);