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 #pragma implementation
20 #include "formulamacro.h"
21 #include "commandtags.h"
22 #include "math_cursor.h"
23 #include "math_parser.h"
24 #include "math_macro.h"
25 #include "math_macrotable.h"
26 #include "math_macrotemplate.h"
27 #include "math_metricsinfo.h"
28 #include "math_support.h"
29 #include "math_mathmlstream.h"
30 #include "BufferView.h"
32 #include "frontends/Painter.h"
33 #include "frontends/font_metrics.h"
34 #include "support/lyxlib.h"
35 #include "support/LOstream.h"
43 #include "BoostFormat.h"
47 extern MathCursor * mathcursor;
50 InsetFormulaMacro::InsetFormulaMacro()
52 // inset name is inherited from Inset
53 setInsetName("unknown");
57 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
60 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
64 InsetFormulaMacro::InsetFormulaMacro(string const & s)
66 std::istringstream is(STRCONV(s));
71 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
73 return new InsetFormulaMacro(*this);
77 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
79 os << "FormulaMacro ";
80 WriteStream wi(os, false, false);
85 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
86 bool /*free_spacing*/) const
88 WriteStream wi(os, fragile, true);
94 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
96 WriteStream wi(os, false, true);
102 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
104 return ascii(buf, os, 0);
108 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
110 return ascii(buf, os, 0);
114 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
116 read(lex.getStream());
120 void InsetFormulaMacro::read(std::istream & is)
122 MathMacroTemplate * p = new MathMacroTemplate(is);
123 setInsetName(p->name());
124 MathMacroTable::create(MathAtom(p));
129 string InsetFormulaMacro::prefix() const
132 return boost::io::str(boost::format(_(" Macro: %s: ")) % getInsetName());
134 return _(" Macro: ") + getInsetName() + ": ";
139 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
141 return par()->ascent() + 5;
145 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
147 return par()->descent() + 5;
151 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
154 return 10 + font_metrics::width(prefix(), f) + par()->width();
158 MathAtom const & InsetFormulaMacro::par() const
160 return MathMacroTable::provide(getInsetName());
164 MathAtom & InsetFormulaMacro::par()
166 return MathMacroTable::provide(getInsetName());
170 Inset::Code InsetFormulaMacro::lyxCode() const
172 return Inset::MATHMACRO_CODE;
176 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
177 int y, float & xx, bool /*cleared*/) const
181 font.setColor(LColor::math);
183 MathPainterInfo pi = MathPainterInfo(bv->painter());
184 pi.base.style = LM_ST_TEXT;
187 int const x = int(xx);
188 int const a = y - ascent(bv, font) + 1;
189 int const w = width(bv, font) - 2;
190 int const h = ascent(bv, font) + descent(bv, font) - 2;
192 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
193 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
194 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
197 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
198 mathcursor->drawSelection(pi);
200 pi.pain.text(x + 2, y, prefix(), font);
203 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
208 setCursorVisible(false);