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(ostream & os) const
77 os << "FormulaMacro ";
78 tmacro().write(os, false);
82 int InsetFormulaMacro::latex(ostream & os, bool fragile,
83 bool /*free_spacing*/) const
85 tmacro().write(os, fragile);
89 int InsetFormulaMacro::ascii(ostream & os, int) const
91 tmacro().write(os, false);
96 int InsetFormulaMacro::linuxdoc(ostream & os) const
102 int InsetFormulaMacro::docBook(ostream & os) const
108 void InsetFormulaMacro::read(LyXLex & lex)
110 MathMacroTemplate * t = mathed_parse_macro(lex);
111 MathMacroTable::insertTemplate(*t);
112 setInsetName(t->name());
118 string InsetFormulaMacro::prefix() const
120 return string(" ") + _("Macro: ") + tmacro().name() + ": ";
124 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
126 return tmacro().ascent() + 5;
130 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
132 return tmacro().descent() + 5;
136 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
139 return 10 + lyxfont::width(prefix(), f) + tmacro().width();
144 UpdatableInset::RESULT
145 InsetFormulaMacro::localDispatch(BufferView * bv,
146 kb_action action, string const & arg)
148 RESULT result = DISPATCHED;
150 case LFUN_MATH_MACROARG: {
151 int const i = lyx::atoi(arg);
152 lyxerr << "inserting macro arg " << i << "\n";
153 if (i > 0 && i <= tmacro().numargs()) {
154 mathcursor->insert(new MathMacroArgument(i));
155 updateLocal(bv, true);
157 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
163 result = InsetFormulaBase::localDispatch(bv, action, arg);
169 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
171 return MathMacroTable::provideTemplate(getInsetName());
175 Inset::Code InsetFormulaMacro::lyxCode() const
177 return Inset::MATHMACRO_CODE;
181 MathInsetTypes InsetFormulaMacro::getType() const
187 MathInset const * InsetFormulaMacro::par() const
193 void InsetFormulaMacro::metrics() const
195 tmacro().metrics(LM_ST_TEXT);
199 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
200 int baseline, float & x, bool /*cleared*/) const
202 Painter & pain = bv->painter();
206 font.setColor(LColor::math);
208 int const y = baseline - ascent(bv, font) + 1;
209 int const w = width(bv, font) - 2;
210 int const h = ascent(bv, font) + descent(bv, font) - 2;
212 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
213 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
214 pain.rectangle(int(x), y, w, h, LColor::mathframe);
216 if (mathcursor && mathcursor->formula() == this)
217 mathcursor->drawSelection(pain);
219 pain.text(int(x + 2), baseline, prefix(), font);
220 x += width(bv, font);
223 float t = tmacro().width() + 5;
225 tmacro().draw(pain, int(x), baseline);