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 "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "support/lyxlib.h"
32 #include "support/LOstream.h"
40 #include "support/BoostFormat.h"
44 extern MathCursor * mathcursor;
47 InsetFormulaMacro::InsetFormulaMacro()
49 // inset name is inherited from Inset
50 setInsetName("unknown");
54 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
57 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 std::istringstream is(STRCONV(s));
68 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
70 return new InsetFormulaMacro(*this);
74 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
76 os << "FormulaMacro ";
77 WriteStream wi(os, false, false);
82 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
83 bool /*free_spacing*/) const
85 WriteStream wi(os, fragile, true);
91 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
93 WriteStream wi(os, false, true);
99 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
101 return ascii(buf, os, 0);
105 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
107 return ascii(buf, os, 0);
111 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
113 read(lex.getStream());
117 void InsetFormulaMacro::read(std::istream & is)
119 MathMacroTemplate * p = new MathMacroTemplate(is);
120 setInsetName(p->name());
121 MathMacroTable::create(MathAtom(p));
126 string InsetFormulaMacro::prefix() const
129 return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
130 STRCONV(getInsetName())));
132 return _(" Macro: ") + getInsetName() + ": ";
137 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
138 Dimension & dim) const
141 dim = par()->dimensions();
144 dim.w += 10 + font_metrics::width(prefix(), font);
148 MathAtom const & InsetFormulaMacro::par() const
150 return MathMacroTable::provide(getInsetName());
154 MathAtom & InsetFormulaMacro::par()
156 return MathMacroTable::provide(getInsetName());
160 Inset::Code InsetFormulaMacro::lyxCode() const
162 return Inset::MATHMACRO_CODE;
166 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
167 int y, float & xx) const
171 font.setColor(LColor::math);
173 PainterInfo pi = PainterInfo(bv->painter());
174 pi.base.style = LM_ST_TEXT;
177 int const x = int(xx);
178 int const a = y - ascent(bv, font) + 1;
179 int const w = width(bv, font) - 2;
180 int const h = ascent(bv, font) + descent(bv, font) - 2;
182 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
183 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
184 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
187 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
188 mathcursor->drawSelection(pi);
190 pi.pain.text(x + 2, y, prefix(), font);
193 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);