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);
68 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
70 return new InsetFormulaMacro(*this);
74 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
76 os << "FormulaMacro ";
77 par()->write(os, false);
81 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
82 bool /*free_spacing*/) const
84 par()->write(os, fragile);
88 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
90 par()->write(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);
115 string InsetFormulaMacro::prefix() const
117 return string(" ") + _("Macro: ") + getInsetName() + ": ";
121 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
123 return par()->ascent() + 5;
127 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
129 return par()->descent() + 5;
133 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
136 return 10 + lyxfont::width(prefix(), f) + par()->width();
141 UpdatableInset::RESULT
142 InsetFormulaMacro::localDispatch(BufferView * bv,
143 kb_action action, string const & arg)
145 RESULT result = DISPATCHED;
147 case LFUN_MATH_MACROARG: {
148 int const i = lyx::atoi(arg);
149 lyxerr << "inserting macro arg " << i << "\n";
150 //if (i > 0 && i <= par()->numargs()) {
151 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
152 updateLocal(bv, true);
154 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
160 result = InsetFormulaBase::localDispatch(bv, action, arg);
161 // force redraw if anything happened
162 if (result != UNDISPATCHED) {
163 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
164 bv->updateInset(this, false);
172 MathAtom const & InsetFormulaMacro::par() const
174 return MathMacroTable::provide(getInsetName());
178 MathAtom & InsetFormulaMacro::par()
180 return MathMacroTable::provide(getInsetName());
184 Inset::Code InsetFormulaMacro::lyxCode() const
186 return Inset::MATHMACRO_CODE;
190 MathInsetTypes InsetFormulaMacro::getType() const
196 void InsetFormulaMacro::metrics() const
198 par()->metrics(LM_ST_TEXT);
202 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
203 int baseline, float & x, bool /*cleared*/) const
205 Painter & pain = bv->painter();
209 font.setColor(LColor::math);
211 int const y = baseline - ascent(bv, font) + 1;
212 int const w = width(bv, font) - 2;
213 int const h = ascent(bv, font) + descent(bv, font) - 2;
215 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
216 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
217 pain.rectangle(int(x), y, w, h, LColor::mathframe);
219 if (mathcursor && mathcursor->formula() == this)
220 mathcursor->drawSelection(pain);
222 pain.text(int(x + 2), baseline, prefix(), font);
223 x += width(bv, font);
226 float t = par()->width() + 5;
228 par()->draw(pain, int(x), baseline);