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"
42 extern MathCursor * mathcursor;
44 InsetFormulaMacro::InsetFormulaMacro()
45 : InsetFormulaBase(new MathMacroTemplate("unknown", 0))
49 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
50 : InsetFormulaBase(new MathMacroTemplate(nm, na))
52 MathMacroTable::insertTemplate(tmacro());
56 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
58 return new InsetFormulaMacro(*this);
62 void InsetFormulaMacro::write(ostream & os) const
64 os << "FormulaMacro ";
65 tmacro()->Write(os, false);
69 int InsetFormulaMacro::latex(ostream & os, bool fragile,
70 bool /*free_spacing*/) const
72 tmacro()->Write(os, fragile);
76 int InsetFormulaMacro::ascii(ostream & os, int) const
78 tmacro()->Write(os, false);
83 int InsetFormulaMacro::linuxdoc(ostream & os) const
89 int InsetFormulaMacro::docBook(ostream & os) const
95 void InsetFormulaMacro::read(LyXLex & lex)
98 par_ = mathed_parse(lex);
99 MathMacroTable::insertTemplate(tmacro());
100 par_->Metrics(LM_ST_TEXT);
104 string InsetFormulaMacro::prefix() const
106 return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
110 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
112 return tmacro()->ascent() + 5;
116 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
118 return tmacro()->descent() + 5;
122 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
124 tmacro()->Metrics(LM_ST_TEXT);
125 return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
129 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
130 int baseline, float & x, bool /*cleared*/) const
132 Painter & pain = bv->painter();
136 font.setColor(LColor::math);
138 int const y = baseline - ascent(bv, font) + 1;
139 int const w = width(bv, font) - 2;
140 int const h = ascent(bv, font) + descent(bv, font) - 2;
142 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
143 pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
144 pain.rectangle(int(x), y, w, h, LColor::mathframe);
146 if (mathcursor && mathcursor->formula() == this)
147 mathcursor->drawSelection(pain);
149 pain.text(int(x + 2), baseline, prefix(), font);
150 x += width(bv, font);
153 float t = tmacro()->width() + 5;
155 tmacro()->draw(pain, int(x), baseline);
160 UpdatableInset::RESULT
161 InsetFormulaMacro::localDispatch(BufferView * bv,
162 kb_action action, string const & arg)
164 RESULT result = DISPATCHED;
166 case LFUN_MATH_MACROARG: {
167 int const i = lyx::atoi(arg);
168 lyxerr << "inserting macro arg " << i << "\n";
169 if (i > 0 && i <= tmacro()->numargs()) {
170 mathcursor->insert(new MathMacroArgument(i));
171 updateLocal(bv, true);
173 lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
179 result = InsetFormulaBase::localDispatch(bv, action, arg);
185 MathMacroTemplate * InsetFormulaMacro::tmacro() const
187 return static_cast<MathMacroTemplate *>(par_);
191 Inset::Code InsetFormulaMacro::lyxCode() const
193 return Inset::MATHMACRO_CODE;