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/support.h"
36 #include "support/LOstream.h"
44 extern MathCursor * mathcursor;
46 InsetFormulaMacro::InsetFormulaMacro()
48 // inset name is inherited from Inset
49 setInsetName("unknown");
53 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
56 MathMacroTable::create(nm, na, string());
60 InsetFormulaMacro::InsetFormulaMacro(string const & s)
62 string name = mathed_parse_macro(s);
65 #warning "metrics disabled"
71 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
73 return new InsetFormulaMacro(*this);
77 void InsetFormulaMacro::write(Buffer const * buf, ostream & os) const
79 os << "FormulaMacro ";
80 MathWriteInfo wi(buf, os, false);
85 int InsetFormulaMacro::latex(Buffer const * buf, ostream & os, bool fragile,
86 bool /*free_spacing*/) const
88 MathWriteInfo wi(buf, os, fragile);
94 int InsetFormulaMacro::ascii(Buffer const * buf, ostream & os, int) const
96 MathWriteInfo wi(buf, os, false);
102 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
104 return ascii(buf, os, 0);
108 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
110 return ascii(buf, os, 0);
114 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
116 string name = mathed_parse_macro(lex);
118 lyxerr << "metrics disabled";
123 string InsetFormulaMacro::prefix() const
125 return string(" ") + _("Macro: ") + getInsetName() + ": ";
129 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
131 return par()->ascent() + 5;
135 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
137 return par()->descent() + 5;
141 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
144 return 10 + lyxfont::width(prefix(), f) + par()->width();
149 UpdatableInset::RESULT
150 InsetFormulaMacro::localDispatch(BufferView * bv,
151 kb_action action, string const & arg)
153 RESULT result = DISPATCHED;
155 case LFUN_MATH_MACROARG: {
156 int const i = lyx::atoi(arg);
157 lyxerr << "inserting macro arg " << i << "\n";
158 //if (i > 0 && i <= par()->numargs()) {
159 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
160 updateLocal(bv, true);
162 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
168 result = InsetFormulaBase::localDispatch(bv, action, arg);
169 // force redraw if anything happened
170 if (result != UNDISPATCHED) {
171 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
172 bv->updateInset(this, false);
180 MathAtom const & InsetFormulaMacro::par() const
182 return MathMacroTable::provide(getInsetName());
186 MathAtom & InsetFormulaMacro::par()
188 return MathMacroTable::provide(getInsetName());
192 Inset::Code InsetFormulaMacro::lyxCode() const
194 return Inset::MATHMACRO_CODE;
198 MathInsetTypes InsetFormulaMacro::getType() const
204 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
205 int baseline, float & x, bool /*cleared*/) const
207 Painter & pain = bv->painter();
211 font.setColor(LColor::math);
213 int const y = baseline - ascent(bv, font) + 1;
214 int const w = width(bv, font) - 2;
215 int const h = ascent(bv, font) + descent(bv, font) - 2;
217 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
218 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
219 pain.rectangle(int(x), y, w, h, LColor::mathframe);
221 if (mathcursor && mathcursor->formula() == this)
222 mathcursor->drawSelection(pain);
224 pain.text(int(x + 2), baseline, prefix(), font);
225 x += width(bv, font);
228 float t = par()->width() + 5;
230 par()->draw(pain, int(x), baseline);