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_hullinset.h"
30 #include "BufferView.h"
34 #include "support/lyxlib.h"
35 #include "mathed/math_support.h"
36 #include "support/LOstream.h"
41 #include "math_mathmlstream.h"
45 extern MathCursor * mathcursor;
47 InsetFormulaMacro::InsetFormulaMacro()
49 // inset name is inherited from Inset
50 setInsetName("unknown");
54 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
57 MathMacroTable::create(nm, na, string());
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 string name = mathed_parse_macro(s);
68 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
70 return new InsetFormulaMacro(*this);
74 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
76 os << "FormulaMacro ";
77 WriteStream wi(os, false);
82 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
83 bool /*free_spacing*/) const
85 WriteStream wi(os, fragile);
91 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
93 WriteStream wi(os, false);
99 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
101 return ascii(buf, os, 0);
105 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
107 return ascii(buf, os, 0);
111 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
113 string name = mathed_parse_macro(lex);
115 //lyxerr << "metrics disabled";
120 string InsetFormulaMacro::prefix() const
122 return string(" ") + _("Macro: ") + getInsetName() + ": ";
126 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
128 return par()->ascent() + 5;
132 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
134 return par()->descent() + 5;
138 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
141 return 10 + lyxfont::width(prefix(), f) + par()->width();
146 UpdatableInset::RESULT
147 InsetFormulaMacro::localDispatch(BufferView * bv,
148 kb_action action, string const & arg)
150 RESULT result = DISPATCHED;
152 case LFUN_MATH_MACROARG: {
153 int const i = lyx::atoi(arg);
154 lyxerr << "inserting macro arg " << i << "\n";
155 //if (i > 0 && i <= par()->numargs()) {
156 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
157 updateLocal(bv, true);
159 // lyxerr << "not in range 0.." << par()->numargs() << "\n";
165 result = InsetFormulaBase::localDispatch(bv, action, arg);
166 // force redraw if anything happened
167 if (result != UNDISPATCHED) {
168 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
169 bv->updateInset(this, false);
177 MathAtom const & InsetFormulaMacro::par() const
179 return MathMacroTable::provide(getInsetName());
183 MathAtom & InsetFormulaMacro::par()
185 return MathMacroTable::provide(getInsetName());
189 Inset::Code InsetFormulaMacro::lyxCode() const
191 return Inset::MATHMACRO_CODE;
195 MathInsetTypes InsetFormulaMacro::getType() const
201 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
202 int y, float & x, bool /*cleared*/) const
204 Painter & pain = bv->painter();
208 font.setColor(LColor::math);
210 int const a = y - ascent(bv, font) + 1;
211 int const w = width(bv, font) - 2;
212 int const h = ascent(bv, font) + descent(bv, font) - 2;
214 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
215 pain.fillRectangle(int(x), a , w, h, LColor::mathmacrobg);
216 pain.rectangle(int(x), a, w, h, LColor::mathframe);
219 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
220 mathcursor->drawSelection(pain);
222 pain.text(int(x + 2), y, prefix(), font);
223 x += width(bv, font);
226 xo_ = int(x) - par()->width() - 5;
228 par()->draw(pain, xo_, yo_);