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"
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 nm, int na)
53 MathMacroTable::create(nm, na, string());
57 InsetFormulaMacro::InsetFormulaMacro(string const & s)
59 string name = mathed_parse_macro(s);
64 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
66 return new InsetFormulaMacro(*this);
70 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
72 os << "FormulaMacro ";
73 WriteStream wi(os, false);
78 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
79 bool /*free_spacing*/) const
81 WriteStream wi(os, fragile);
87 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
89 WriteStream wi(os, false);
95 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
97 return ascii(buf, os, 0);
101 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
103 return ascii(buf, os, 0);
107 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
109 string name = mathed_parse_macro(lex);
111 //lyxerr << "metrics disabled";
116 string InsetFormulaMacro::prefix() const
118 return string(" ") + _("Macro: ") + getInsetName() + ": ";
122 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
124 return par()->ascent() + 5;
128 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
130 return par()->descent() + 5;
134 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
137 return 10 + lyxfont::width(prefix(), f) + par()->width();
142 UpdatableInset::RESULT
143 InsetFormulaMacro::localDispatch(BufferView * bv,
144 kb_action action, string const & arg)
146 RESULT result = DISPATCHED;
148 case LFUN_MATH_MACROARG: {
149 int const i = lyx::atoi(arg);
150 lyxerr << "inserting macro arg " << i << "\n";
151 //if (i > 0 && i <= par()->numargs()) {
152 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
153 updateLocal(bv, true);
155 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
161 result = InsetFormulaBase::localDispatch(bv, action, arg);
162 // force redraw if anything happened
163 if (result != UNDISPATCHED) {
164 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
165 bv->updateInset(this, false);
173 MathAtom const & InsetFormulaMacro::par() const
175 return MathMacroTable::provide(getInsetName());
179 MathAtom & InsetFormulaMacro::par()
181 return MathMacroTable::provide(getInsetName());
185 Inset::Code InsetFormulaMacro::lyxCode() const
187 return Inset::MATHMACRO_CODE;
191 MathInsetTypes InsetFormulaMacro::getType() const
197 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
198 int y, float & x, bool /*cleared*/) const
200 Painter & pain = bv->painter();
204 font.setColor(LColor::math);
206 int const a = y - ascent(bv, font) + 1;
207 int const w = width(bv, font) - 2;
208 int const h = ascent(bv, font) + descent(bv, font) - 2;
210 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
211 pain.fillRectangle(int(x), a , w, h, LColor::mathmacrobg);
212 pain.rectangle(int(x), a, w, h, LColor::mathframe);
215 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
216 mathcursor->drawSelection(pain);
218 pain.text(int(x + 2), y, prefix(), font);
219 x += width(bv, font);
222 xo_ = int(x) - par()->width() - 5;
224 par()->draw(pain, xo_, yo_);