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()
46 : InsetFormulaBase(new MathMacroTemplate("unknown", 0))
50 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
51 : InsetFormulaBase(new MathMacroTemplate(nm, na))
53 MathMacroTable::insertTemplate(tmacro());
57 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
59 return new InsetFormulaMacro(*this);
63 void InsetFormulaMacro::write(ostream & os) const
65 os << "FormulaMacro ";
66 tmacro()->write(os, false);
70 int InsetFormulaMacro::latex(ostream & os, bool fragile,
71 bool /*free_spacing*/) const
73 tmacro()->write(os, fragile);
77 int InsetFormulaMacro::ascii(ostream & os, int) const
79 tmacro()->write(os, false);
84 int InsetFormulaMacro::linuxdoc(ostream & os) const
90 int InsetFormulaMacro::docBook(ostream & os) const
96 void InsetFormulaMacro::read(LyXLex & lex)
100 par_ = mathed_parse(lex);
101 MathMacroTable::insertTemplate(tmacro());
102 par_->metrics(LM_ST_TEXT);
106 string InsetFormulaMacro::prefix() const
108 return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
112 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
114 return tmacro()->ascent() + 5;
118 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
120 return tmacro()->descent() + 5;
124 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
126 tmacro()->metrics(LM_ST_TEXT);
127 return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
131 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
132 int baseline, float & x, bool /*cleared*/) const
134 Painter & pain = bv->painter();
138 font.setColor(LColor::math);
140 int const y = baseline - ascent(bv, font) + 1;
141 int const w = width(bv, font) - 2;
142 int const h = ascent(bv, font) + descent(bv, font) - 2;
144 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
145 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
146 pain.rectangle(int(x), y, w, h, LColor::mathframe);
148 if (mathcursor && mathcursor->formula() == this)
149 mathcursor->drawSelection(pain);
151 pain.text(int(x + 2), baseline, prefix(), font);
152 x += width(bv, font);
155 float t = tmacro()->width() + 5;
157 tmacro()->draw(pain, int(x), baseline);
162 UpdatableInset::RESULT
163 InsetFormulaMacro::localDispatch(BufferView * bv,
164 kb_action action, string const & arg)
166 RESULT result = DISPATCHED;
168 case LFUN_MATH_MACROARG: {
169 int const i = lyx::atoi(arg);
170 lyxerr << "inserting macro arg " << i << "\n";
171 if (i > 0 && i <= tmacro()->numargs()) {
172 mathcursor->insert(new MathMacroArgument(i));
173 updateLocal(bv, true);
175 lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
181 result = InsetFormulaBase::localDispatch(bv, action, arg);
187 MathMacroTemplate * InsetFormulaMacro::tmacro() const
189 return static_cast<MathMacroTemplate *>(par_);
193 Inset::Code InsetFormulaMacro::lyxCode() const
195 return Inset::MATHMACRO_CODE;
199 MathInsetTypes InsetFormulaMacro::getType() const