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 : tmacro_(new MathMacroTemplate("unknown", 0))
50 InsetFormulaMacro::InsetFormulaMacro(InsetFormulaMacro const & m)
51 : InsetFormulaBase(m),
52 tmacro_(static_cast<MathMacroTemplate *>(m.tmacro_->clone()))
56 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
57 : tmacro_(new MathMacroTemplate(nm, na))
59 MathMacroTable::insertTemplate(tmacro_);
63 InsetFormulaMacro::~InsetFormulaMacro()
66 #warning Need to unregister from MathMacroTable.
68 // Instead of unregister an delete leak this until it gets fixed
73 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
76 #warning This should not be needed in reality...
78 return new InsetFormulaMacro(*this);
82 void InsetFormulaMacro::write(ostream & os) const
84 os << "FormulaMacro ";
85 tmacro().write(os, false);
89 int InsetFormulaMacro::latex(ostream & os, bool fragile,
90 bool /*free_spacing*/) const
92 tmacro().write(os, fragile);
96 int InsetFormulaMacro::ascii(ostream & os, int) const
98 tmacro().write(os, false);
103 int InsetFormulaMacro::linuxdoc(ostream & os) const
109 int InsetFormulaMacro::docBook(ostream & os) const
115 void InsetFormulaMacro::read(LyXLex & lex)
119 tmacro_ = mathed_parse_macro(lex);
120 MathMacroTable::insertTemplate(tmacro_);
125 string InsetFormulaMacro::prefix() const
127 return string(" ") + _("Macro: ") + tmacro().name() + ": ";
131 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
133 return tmacro().ascent() + 5;
137 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
139 return tmacro().descent() + 5;
143 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
146 return 10 + lyxfont::width(prefix(), f) + tmacro().width();
151 UpdatableInset::RESULT
152 InsetFormulaMacro::localDispatch(BufferView * bv,
153 kb_action action, string const & arg)
155 RESULT result = DISPATCHED;
157 case LFUN_MATH_MACROARG: {
158 int const i = lyx::atoi(arg);
159 lyxerr << "inserting macro arg " << i << "\n";
160 if (i > 0 && i <= tmacro().numargs()) {
161 mathcursor->insert(new MathMacroArgument(i));
162 updateLocal(bv, true);
164 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
170 result = InsetFormulaBase::localDispatch(bv, action, arg);
176 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
182 Inset::Code InsetFormulaMacro::lyxCode() const
184 return Inset::MATHMACRO_CODE;
188 MathInsetTypes InsetFormulaMacro::getType() const
194 MathInset const * InsetFormulaMacro::par() const
200 void InsetFormulaMacro::metrics() const
202 tmacro_->metrics(LM_ST_TEXT);
206 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
207 int baseline, float & x, bool /*cleared*/) const
209 Painter & pain = bv->painter();
213 font.setColor(LColor::math);
215 int const y = baseline - ascent(bv, font) + 1;
216 int const w = width(bv, font) - 2;
217 int const h = ascent(bv, font) + descent(bv, font) - 2;
219 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
220 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
221 pain.rectangle(int(x), y, w, h, LColor::mathframe);
223 if (mathcursor && mathcursor->formula() == this)
224 mathcursor->drawSelection(pain);
226 pain.text(int(x + 2), baseline, prefix(), font);
227 x += width(bv, font);
230 float t = tmacro().width() + 5;
232 tmacro_->draw(pain, int(x), baseline);