3 * Purpose: Implementation of the formula macro LyX inset
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_macroarg.h"
26 #include "math_macrotable.h"
27 #include "math_macrotemplate.h"
28 #include "math_matrixinset.h"
30 #include "BufferView.h"
34 #include "support/lyxlib.h"
35 #include "mathed/math_support.h"
36 #include "support/LOstream.h"
41 #include "math_mathmlstream.h"
45 extern MathCursor * mathcursor;
47 InsetFormulaMacro::InsetFormulaMacro()
49 // inset name is inherited from Inset
50 setInsetName("unknown");
54 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
57 MathMacroTable::create(nm, na, string());
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 string name = mathed_parse_macro(s);
66 #warning "metrics disabled"
72 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
74 return new InsetFormulaMacro(*this);
78 void InsetFormulaMacro::write(Buffer const * buf, ostream & os) const
80 os << "FormulaMacro ";
81 MathWriteInfo wi(buf, os, false);
86 int InsetFormulaMacro::latex(Buffer const * buf, ostream & os, bool fragile,
87 bool /*free_spacing*/) const
89 MathWriteInfo wi(buf, os, fragile);
95 int InsetFormulaMacro::ascii(Buffer const * buf, ostream & os, int) const
97 MathWriteInfo wi(buf, os, false);
103 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
105 return ascii(buf, os, 0);
109 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
111 return ascii(buf, os, 0);
115 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
117 string name = mathed_parse_macro(lex);
119 //lyxerr << "metrics disabled";
124 string InsetFormulaMacro::prefix() const
126 return string(" ") + _("Macro: ") + getInsetName() + ": ";
130 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
132 return par()->ascent() + 5;
136 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
138 return par()->descent() + 5;
142 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
145 return 10 + lyxfont::width(prefix(), f) + par()->width();
150 UpdatableInset::RESULT
151 InsetFormulaMacro::localDispatch(BufferView * bv,
152 kb_action action, string const & arg)
154 RESULT result = DISPATCHED;
156 case LFUN_MATH_MACROARG: {
157 int const i = lyx::atoi(arg);
158 lyxerr << "inserting macro arg " << i << "\n";
159 //if (i > 0 && i <= par()->numargs()) {
160 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
161 updateLocal(bv, true);
163 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
169 result = InsetFormulaBase::localDispatch(bv, action, arg);
170 // force redraw if anything happened
171 if (result != UNDISPATCHED) {
172 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
173 bv->updateInset(this, false);
181 MathAtom const & InsetFormulaMacro::par() const
183 return MathMacroTable::provide(getInsetName());
187 MathAtom & InsetFormulaMacro::par()
189 return MathMacroTable::provide(getInsetName());
193 Inset::Code InsetFormulaMacro::lyxCode() const
195 return Inset::MATHMACRO_CODE;
199 MathInsetTypes InsetFormulaMacro::getType() const
205 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
206 int y, float & x, bool /*cleared*/) const
208 Painter & pain = bv->painter();
212 font.setColor(LColor::math);
214 int const a = y - ascent(bv, font) + 1;
215 int const w = width(bv, font) - 2;
216 int const h = ascent(bv, font) + descent(bv, font) - 2;
218 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
219 pain.fillRectangle(int(x), a , w, h, LColor::mathmacrobg);
220 pain.rectangle(int(x), a, w, h, LColor::mathframe);
222 if (mathcursor && mathcursor->formula() == this)
223 mathcursor->drawSelection(pain);
225 pain.text(int(x + 2), y, prefix(), font);
226 x += width(bv, font);
229 xo_ = int(x) - par()->width() - 5;
231 par()->draw(pain, xo_, yo_);