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 name, int nargs)
53 MathMacroTable::create(name, nargs, string());
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);
79 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
80 bool /*free_spacing*/) const
82 WriteStream wi(os, fragile);
88 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
90 WriteStream wi(os, false);
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 + lyxfont::width(prefix(), f) + par()->width();
144 UpdatableInset::RESULT
145 InsetFormulaMacro::localDispatch(BufferView * bv,
146 kb_action action, string const & arg)
148 RESULT result = DISPATCHED;
150 case LFUN_MATH_MACROARG: {
151 int const i = lyx::atoi(arg);
152 lyxerr << "inserting macro arg " << i << "\n";
153 //if (i > 0 && i <= par()->numargs()) {
154 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
155 updateLocal(bv, true);
157 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
163 result = InsetFormulaBase::localDispatch(bv, action, arg);
164 // force redraw if anything happened
165 if (result != UNDISPATCHED) {
166 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
167 bv->updateInset(this, false);
175 MathAtom const & InsetFormulaMacro::par() const
177 return MathMacroTable::provide(getInsetName());
181 MathAtom & InsetFormulaMacro::par()
183 return MathMacroTable::provide(getInsetName());
187 Inset::Code InsetFormulaMacro::lyxCode() const
189 return Inset::MATHMACRO_CODE;
193 MathInsetTypes InsetFormulaMacro::getType() const
199 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
200 int y, float & x, bool /*cleared*/) const
202 Painter & pain = bv->painter();
206 font.setColor(LColor::math);
208 int const a = y - ascent(bv, font) + 1;
209 int const w = width(bv, font) - 2;
210 int const h = ascent(bv, font) + descent(bv, font) - 2;
212 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
213 pain.fillRectangle(int(x), a , w, h, LColor::mathmacrobg);
214 pain.rectangle(int(x), a, w, h, LColor::mathframe);
217 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
218 mathcursor->drawSelection(pain);
220 pain.text(int(x + 2), y, prefix(), font);
221 x += width(bv, font);
224 xo_ = int(x) - par()->width() - 5;
226 par()->draw(pain, xo_, yo_);