3 * Purpose: Implementation of the formula macro LyX inset
4 * Author: André Pönitz, based on ideas of Alejandro Aguilar Sierra
6 * Description: Allows the edition of math macros inside Lyx.
8 * Copyright: 2001 The LyX Project
10 * You are free to use and modify this code under the terms of
11 * the GNU General Public Licence version 2 or later.
17 #include "formulamacro.h"
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_macro.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "metricsinfo.h"
25 #include "math_support.h"
26 #include "math_mathmlstream.h"
27 #include "BufferView.h"
29 #include "latexrunparams.h"
30 #include "frontends/Painter.h"
31 #include "frontends/font_metrics.h"
32 #include "support/lyxlib.h"
33 #include "support/LOstream.h"
41 #include "support/BoostFormat.h"
45 extern MathCursor * mathcursor;
48 InsetFormulaMacro::InsetFormulaMacro()
50 // inset name is inherited from Inset
51 setInsetName("unknown");
55 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
58 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
62 InsetFormulaMacro::InsetFormulaMacro(string const & s)
64 std::istringstream is(STRCONV(s));
69 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
71 return new InsetFormulaMacro(*this);
75 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
77 os << "FormulaMacro ";
78 WriteStream wi(os, false, false);
83 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
84 LatexRunParams const & runparams,
85 bool /*free_spacing*/) const
87 WriteStream wi(os, runparams.fragile, true);
93 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
95 WriteStream wi(os, false, true);
101 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
103 return ascii(buf, os, 0);
107 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
109 return ascii(buf, os, 0);
113 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
115 read(lex.getStream());
119 void InsetFormulaMacro::read(std::istream & is)
121 MathMacroTemplate * p = new MathMacroTemplate(is);
122 setInsetName(p->name());
123 MathMacroTable::create(MathAtom(p));
128 string InsetFormulaMacro::prefix() const
131 return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
132 STRCONV(getInsetName())));
134 return _(" Macro: ") + getInsetName() + ": ";
139 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
140 Dimension & dim) const
143 dim = par()->dimensions();
146 dim.w += 10 + font_metrics::width(prefix(), font);
150 MathAtom const & InsetFormulaMacro::par() const
152 return MathMacroTable::provide(getInsetName());
156 MathAtom & InsetFormulaMacro::par()
158 return MathMacroTable::provide(getInsetName());
162 Inset::Code InsetFormulaMacro::lyxCode() const
164 return Inset::MATHMACRO_CODE;
168 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
169 int y, float & xx) const
173 font.setColor(LColor::math);
175 PainterInfo pi = PainterInfo(bv->painter());
176 pi.base.style = LM_ST_TEXT;
179 int const x = int(xx);
180 int const a = y - ascent(bv, font) + 1;
181 int const w = width(bv, font) - 2;
182 int const h = ascent(bv, font) + descent(bv, font) - 2;
184 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
185 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
186 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
189 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
190 mathcursor->drawSelection(pi);
192 pi.pain.text(x + 2, y, prefix(), font);
195 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);