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)
99 par_ = mathed_parse(lex);
100 MathMacroTable::insertTemplate(tmacro());
101 par_->metrics(LM_ST_TEXT);
105 string InsetFormulaMacro::prefix() const
107 return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
111 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
113 return tmacro()->ascent() + 5;
117 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
119 return tmacro()->descent() + 5;
123 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
125 tmacro()->metrics(LM_ST_TEXT);
126 return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
130 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
131 int baseline, float & x, bool /*cleared*/) const
133 Painter & pain = bv->painter();
137 font.setColor(LColor::math);
139 int const y = baseline - ascent(bv, font) + 1;
140 int const w = width(bv, font) - 2;
141 int const h = ascent(bv, font) + descent(bv, font) - 2;
143 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
144 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
145 pain.rectangle(int(x), y, w, h, LColor::mathframe);
147 if (mathcursor && mathcursor->formula() == this)
148 mathcursor->drawSelection(pain);
150 pain.text(int(x + 2), baseline, prefix(), font);
151 x += width(bv, font);
154 float t = tmacro()->width() + 5;
156 tmacro()->draw(pain, int(x), baseline);
161 UpdatableInset::RESULT
162 InsetFormulaMacro::localDispatch(BufferView * bv,
163 kb_action action, string const & arg)
165 RESULT result = DISPATCHED;
167 case LFUN_MATH_MACROARG: {
168 int const i = lyx::atoi(arg);
169 lyxerr << "inserting macro arg " << i << "\n";
170 if (i > 0 && i <= tmacro()->numargs()) {
171 mathcursor->insert(new MathMacroArgument(i));
172 updateLocal(bv, true);
174 lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
180 result = InsetFormulaBase::localDispatch(bv, action, arg);
186 MathMacroTemplate * InsetFormulaMacro::tmacro() const
188 return static_cast<MathMacroTemplate *>(par_);
192 Inset::Code InsetFormulaMacro::lyxCode() const
194 return Inset::MATHMACRO_CODE;
198 MathInsetTypes InsetFormulaMacro::getType() const