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_support.h"
27 #include "math_mathmlstream.h"
28 #include "BufferView.h"
30 #include "frontends/Painter.h"
31 #include "frontends/font_metrics.h"
32 #include "support/lyxlib.h"
33 #include "support/LOstream.h"
41 extern MathCursor * mathcursor;
43 InsetFormulaMacro::InsetFormulaMacro()
45 // inset name is inherited from Inset
46 setInsetName("unknown");
50 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
53 MathMacroTable::create(name, nargs);
57 InsetFormulaMacro::InsetFormulaMacro(string const & s)
60 mathed_parse_macro(name, s);
65 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
67 return new InsetFormulaMacro(*this);
71 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
73 os << "FormulaMacro ";
74 WriteStream wi(os, false, false);
79 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
80 bool /*free_spacing*/) const
82 WriteStream wi(os, fragile, true);
88 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
90 WriteStream wi(os, false, true);
96 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
98 return ascii(buf, os, 0);
102 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
104 return ascii(buf, os, 0);
108 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
111 mathed_parse_macro(name, lex);
113 //lyxerr << "metrics disabled";
118 string InsetFormulaMacro::prefix() const
120 return string(" ") + _("Macro: ") + getInsetName() + ": ";
124 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
126 return par()->ascent() + 5;
130 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
132 return par()->descent() + 5;
136 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
139 return 10 + font_metrics::width(prefix(), f) + par()->width();
143 MathAtom const & InsetFormulaMacro::par() const
145 return MathMacroTable::provide(getInsetName());
149 MathAtom & InsetFormulaMacro::par()
151 return MathMacroTable::provide(getInsetName());
155 Inset::Code InsetFormulaMacro::lyxCode() const
157 return Inset::MATHMACRO_CODE;
161 MathInsetTypes InsetFormulaMacro::getType() const
167 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
168 int y, float & xx, bool /*cleared*/) const
170 Painter & pain = bv->painter();
174 font.setColor(LColor::math);
176 int const x = int(xx);
177 int const a = y - ascent(bv, font) + 1;
178 int const w = width(bv, font) - 2;
179 int const h = ascent(bv, font) + descent(bv, font) - 2;
181 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
182 pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
183 pain.rectangle(x, a, w, h, LColor::mathframe);
186 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
187 mathcursor->drawSelection(pain);
189 pain.text(x + 2, y, prefix(), font);
192 par()->draw(pain, x + font_metrics::width(prefix(), f) + 5, y);
197 setCursorVisible(false);