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::createTemplate(nm, na, string());
59 InsetFormulaMacro::InsetFormulaMacro(string const & s)
61 MathMacroTemplate * t = mathed_parse_macro(s);
62 MathMacroTable::insertTemplate(*t);
63 setInsetName(t->name());
69 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
71 return new InsetFormulaMacro(*this);
75 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
77 os << "FormulaMacro ";
78 tmacro().write(os, false);
82 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
83 bool /*free_spacing*/) const
85 tmacro().write(os, fragile);
89 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
91 tmacro().write(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)
110 MathMacroTemplate * t = mathed_parse_macro(lex);
112 t = new MathMacroTemplate("{parse error}", 0);
113 MathMacroTable::insertTemplate(*t);
114 setInsetName(t->name());
120 string InsetFormulaMacro::prefix() const
122 return string(" ") + _("Macro: ") + tmacro().name() + ": ";
126 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
128 return tmacro().ascent() + 5;
132 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
134 return tmacro().descent() + 5;
138 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
141 return 10 + lyxfont::width(prefix(), f) + tmacro().width();
146 UpdatableInset::RESULT
147 InsetFormulaMacro::localDispatch(BufferView * bv,
148 kb_action action, string const & arg)
150 RESULT result = DISPATCHED;
152 case LFUN_MATH_MACROARG: {
153 int const i = lyx::atoi(arg);
154 lyxerr << "inserting macro arg " << i << "\n";
155 if (i > 0 && i <= tmacro().numargs()) {
156 mathcursor->insert(new MathMacroArgument(i));
157 updateLocal(bv, true);
159 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
165 result = InsetFormulaBase::localDispatch(bv, action, arg);
171 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
173 return MathMacroTable::provideTemplate(getInsetName());
177 Inset::Code InsetFormulaMacro::lyxCode() const
179 return Inset::MATHMACRO_CODE;
183 MathInsetTypes InsetFormulaMacro::getType() const
189 MathInset const * InsetFormulaMacro::par() const
195 void InsetFormulaMacro::metrics() const
197 tmacro().metrics(LM_ST_TEXT);
201 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
202 int baseline, float & x, bool /*cleared*/) const
204 Painter & pain = bv->painter();
208 font.setColor(LColor::math);
210 int const y = baseline - ascent(bv, font) + 1;
211 int const w = width(bv, font) - 2;
212 int const h = ascent(bv, font) + descent(bv, font) - 2;
214 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
215 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
216 pain.rectangle(int(x), y, w, h, LColor::mathframe);
218 if (mathcursor && mathcursor->formula() == this)
219 mathcursor->drawSelection(pain);
221 pain.text(int(x + 2), baseline, prefix(), font);
222 x += width(bv, font);
225 float t = tmacro().width() + 5;
227 tmacro().draw(pain, int(x), baseline);