3 * Purpose: Implementation of the formula macro LyX inset
4 * Author: André Pönitz, based on ideas of Alejandro Aguilar Sierra
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 #include "formulamacro.h"
18 #include "commandtags.h"
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_macro.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "math_metricsinfo.h"
25 #include "math_support.h"
26 #include "math_mathmlstream.h"
27 #include "BufferView.h"
29 #include "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "support/lyxlib.h"
32 #include "support/LOstream.h"
40 #include "BoostFormat.h"
44 extern MathCursor * mathcursor;
47 InsetFormulaMacro::InsetFormulaMacro()
49 // inset name is inherited from Inset
50 setInsetName("unknown");
54 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
57 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 std::istringstream is(STRCONV(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, false);
82 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
83 bool /*free_spacing*/) const
85 WriteStream wi(os, fragile, true);
91 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
93 WriteStream wi(os, false, true);
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, bool) const
107 return ascii(buf, os, 0);
111 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
113 read(lex.getStream());
117 void InsetFormulaMacro::read(std::istream & is)
119 MathMacroTemplate * p = new MathMacroTemplate(is);
120 setInsetName(p->name());
121 MathMacroTable::create(MathAtom(p));
126 string InsetFormulaMacro::prefix() const
129 return boost::io::str(boost::format(_(" Macro: %s: ")) % getInsetName());
131 return _(" Macro: ") + getInsetName() + ": ";
136 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
138 return par()->ascent() + 5;
142 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
144 return par()->descent() + 5;
148 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
151 return 10 + font_metrics::width(prefix(), f) + par()->width();
155 MathAtom const & InsetFormulaMacro::par() const
157 return MathMacroTable::provide(getInsetName());
161 MathAtom & InsetFormulaMacro::par()
163 return MathMacroTable::provide(getInsetName());
167 Inset::Code InsetFormulaMacro::lyxCode() const
169 return Inset::MATHMACRO_CODE;
173 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
174 int y, float & xx, bool /*cleared*/) const
178 font.setColor(LColor::math);
180 MathPainterInfo pi = MathPainterInfo(bv->painter());
181 pi.base.style = LM_ST_TEXT;
184 int const x = int(xx);
185 int const a = y - ascent(bv, font) + 1;
186 int const w = width(bv, font) - 2;
187 int const h = ascent(bv, font) + descent(bv, font) - 2;
189 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
190 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
191 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
194 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
195 mathcursor->drawSelection(pi);
197 pi.pain.text(x + 2, y, prefix(), font);
200 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
205 setCursorVisible(false);