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"
43 extern MathCursor * mathcursor;
45 InsetFormulaMacro::InsetFormulaMacro()
47 // inset name is inherited from Inset
48 setInsetName("unknown");
52 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
55 MathMacroTable::create(nm, na, string());
59 InsetFormulaMacro::InsetFormulaMacro(string const & s)
61 string name = mathed_parse_macro(s);
67 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
69 return new InsetFormulaMacro(*this);
73 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
75 os << "FormulaMacro ";
76 par()->write(os, false);
80 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
81 bool /*free_spacing*/) const
83 par()->write(os, fragile);
87 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
89 par()->write(os, false);
94 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
96 return ascii(buf, os, 0);
100 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
102 return ascii(buf, os, 0);
106 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
108 string name = mathed_parse_macro(lex);
114 string InsetFormulaMacro::prefix() const
116 return string(" ") + _("Macro: ") + getInsetName() + ": ";
120 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
122 return par()->ascent() + 5;
126 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
128 return par()->descent() + 5;
132 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
135 return 10 + lyxfont::width(prefix(), f) + par()->width();
140 UpdatableInset::RESULT
141 InsetFormulaMacro::localDispatch(BufferView * bv,
142 kb_action action, string const & arg)
144 RESULT result = DISPATCHED;
146 case LFUN_MATH_MACROARG: {
147 int const i = lyx::atoi(arg);
148 lyxerr << "inserting macro arg " << i << "\n";
149 //if (i > 0 && i <= par()->numargs()) {
150 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
151 updateLocal(bv, true);
153 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
159 result = InsetFormulaBase::localDispatch(bv, action, arg);
165 MathAtom const & InsetFormulaMacro::par() const
167 return MathMacroTable::provide(getInsetName());
171 MathAtom & InsetFormulaMacro::par()
173 return MathMacroTable::provide(getInsetName());
177 Inset::Code InsetFormulaMacro::lyxCode() const
179 return Inset::MATHMACRO_CODE;
183 MathInsetTypes InsetFormulaMacro::getType() const
189 void InsetFormulaMacro::metrics() const
191 par()->metrics(LM_ST_TEXT);
195 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
196 int baseline, float & x, bool /*cleared*/) const
198 Painter & pain = bv->painter();
202 font.setColor(LColor::math);
204 int const y = baseline - ascent(bv, font) + 1;
205 int const w = width(bv, font) - 2;
206 int const h = ascent(bv, font) + descent(bv, font) - 2;
208 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
209 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
210 pain.rectangle(int(x), y, w, h, LColor::mathframe);
212 if (mathcursor && mathcursor->formula() == this)
213 mathcursor->drawSelection(pain);
215 pain.text(int(x + 2), baseline, prefix(), font);
216 x += width(bv, font);
219 float t = par()->width() + 5;
221 par()->draw(pain, int(x), baseline);