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 "formulamacro.h"
15 #include "math_cursor.h"
16 #include "math_macrotable.h"
17 #include "math_macrotemplate.h"
18 #include "math_mathmlstream.h"
21 #include "latexrunparams.h"
25 #include "frontends/Painter.h"
26 #include "frontends/font_metrics.h"
28 #include "support/lstrings.h"
29 #include "support/std_sstream.h"
31 using lyx::support::bformat;
38 extern MathCursor * mathcursor;
41 InsetFormulaMacro::InsetFormulaMacro()
43 // inset name is inherited from Inset
44 setInsetName("unknown");
48 InsetFormulaMacro::InsetFormulaMacro
49 (string const & name, int nargs, string const & type)
52 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
56 InsetFormulaMacro::InsetFormulaMacro(string const & s)
58 std::istringstream is(s);
63 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
65 return auto_ptr<InsetBase>(new InsetFormulaMacro(*this));
69 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
71 os << "FormulaMacro ";
72 WriteStream wi(os, false, false);
77 int InsetFormulaMacro::latex(Buffer const &, ostream & os,
78 LatexRunParams const & runparams) const
80 WriteStream wi(os, runparams.moving_arg, true);
86 int InsetFormulaMacro::ascii(Buffer const &, ostream & os, int) const
88 WriteStream wi(os, false, true);
94 int InsetFormulaMacro::linuxdoc(Buffer const & buf, ostream & os) const
96 return ascii(buf, os, 0);
100 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os, bool) const
102 return ascii(buf, os, 0);
106 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
108 read(lex.getStream());
112 void InsetFormulaMacro::read(std::istream & is)
114 MathMacroTemplate * p = new MathMacroTemplate(is);
115 setInsetName(p->name());
116 MathMacroTable::create(MathAtom(p));
121 string InsetFormulaMacro::prefix() const
123 return bformat(_(" Macro: %1$s: "), getInsetName());
127 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
129 par()->metrics(mi, dim_);
132 dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
137 MathAtom const & InsetFormulaMacro::par() const
139 return MathMacroTable::provide(getInsetName());
143 MathAtom & InsetFormulaMacro::par()
145 return MathMacroTable::provide(getInsetName());
149 InsetOld::Code InsetFormulaMacro::lyxCode() const
151 return InsetOld::MATHMACRO_CODE;
155 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
158 LyXFont font = p.base.font;
159 font.setColor(LColor::math);
161 PainterInfo pi(p.base.bv);
162 pi.base.style = LM_ST_TEXT;
165 int const a = y - dim_.asc + 1;
166 int const w = dim_.wid - 2;
167 int const h = dim_.height() - 2;
169 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
170 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
171 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
174 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
175 mathcursor->drawSelection(pi);
177 pi.pain.text(x + 2, y, prefix(), font);
180 par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);