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 setInsetName("unknown");
51 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
54 MathMacroTable::createTemplate(nm, na, string());
58 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
60 return new InsetFormulaMacro(*this);
64 void InsetFormulaMacro::write(ostream & os) const
66 os << "FormulaMacro ";
67 tmacro().write(os, false);
71 int InsetFormulaMacro::latex(ostream & os, bool fragile,
72 bool /*free_spacing*/) const
74 tmacro().write(os, fragile);
78 int InsetFormulaMacro::ascii(ostream & os, int) const
80 tmacro().write(os, false);
85 int InsetFormulaMacro::linuxdoc(ostream & os) const
91 int InsetFormulaMacro::docBook(ostream & os) const
97 void InsetFormulaMacro::read(LyXLex & lex)
99 MathMacroTemplate * t = mathed_parse_macro(lex);
100 MathMacroTable::insertTemplate(*t);
101 setInsetName(t->name());
107 string InsetFormulaMacro::prefix() const
109 return string(" ") + _("Macro: ") + tmacro().name() + ": ";
113 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
115 return tmacro().ascent() + 5;
119 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
121 return tmacro().descent() + 5;
125 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
128 return 10 + lyxfont::width(prefix(), f) + tmacro().width();
133 UpdatableInset::RESULT
134 InsetFormulaMacro::localDispatch(BufferView * bv,
135 kb_action action, string const & arg)
137 RESULT result = DISPATCHED;
139 case LFUN_MATH_MACROARG: {
140 int const i = lyx::atoi(arg);
141 lyxerr << "inserting macro arg " << i << "\n";
142 if (i > 0 && i <= tmacro().numargs()) {
143 mathcursor->insert(new MathMacroArgument(i));
144 updateLocal(bv, true);
146 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
152 result = InsetFormulaBase::localDispatch(bv, action, arg);
158 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
160 return MathMacroTable::provideTemplate(getInsetName());
164 Inset::Code InsetFormulaMacro::lyxCode() const
166 return Inset::MATHMACRO_CODE;
170 MathInsetTypes InsetFormulaMacro::getType() const
176 MathInset const * InsetFormulaMacro::par() const
182 void InsetFormulaMacro::metrics() const
184 tmacro().metrics(LM_ST_TEXT);
188 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
189 int baseline, float & x, bool /*cleared*/) const
191 Painter & pain = bv->painter();
195 font.setColor(LColor::math);
197 int const y = baseline - ascent(bv, font) + 1;
198 int const w = width(bv, font) - 2;
199 int const h = ascent(bv, font) + descent(bv, font) - 2;
201 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
202 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
203 pain.rectangle(int(x), y, w, h, LColor::mathframe);
205 if (mathcursor && mathcursor->formula() == this)
206 mathcursor->drawSelection(pain);
208 pain.text(int(x + 2), baseline, prefix(), font);
209 x += width(bv, font);
212 float t = tmacro().width() + 5;
214 tmacro().draw(pain, int(x), baseline);