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"
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, bool fragile,
84 bool /*free_spacing*/) const
86 WriteStream wi(os, fragile, true);
92 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
94 WriteStream wi(os, false, true);
100 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
102 return ascii(buf, os, 0);
106 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
108 return ascii(buf, os, 0);
112 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
114 read(lex.getStream());
118 void InsetFormulaMacro::read(std::istream & is)
120 MathMacroTemplate * p = new MathMacroTemplate(is);
121 setInsetName(p->name());
122 MathMacroTable::create(MathAtom(p));
127 string InsetFormulaMacro::prefix() const
129 return string(" ") + _("Macro: ") + getInsetName() + ": ";
133 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
135 return par()->ascent() + 5;
139 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
141 return par()->descent() + 5;
145 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
148 return 10 + font_metrics::width(prefix(), f) + par()->width();
152 MathAtom const & InsetFormulaMacro::par() const
154 return MathMacroTable::provide(getInsetName());
158 MathAtom & InsetFormulaMacro::par()
160 return MathMacroTable::provide(getInsetName());
164 Inset::Code InsetFormulaMacro::lyxCode() const
166 return Inset::MATHMACRO_CODE;
170 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
171 int y, float & xx, bool /*cleared*/) const
175 font.setColor(LColor::math);
177 MathPainterInfo pi = MathPainterInfo(bv->painter());
178 pi.base.style = LM_ST_TEXT;
181 int const x = int(xx);
182 int const a = y - ascent(bv, font) + 1;
183 int const w = width(bv, font) - 2;
184 int const h = ascent(bv, font) + descent(bv, font) - 2;
186 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
187 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
188 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
191 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
192 mathcursor->drawSelection(pi);
194 pi.pain.text(x + 2, y, prefix(), font);
197 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
202 setCursorVisible(false);