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.
16 #include "formulamacro.h"
18 #include "math_cursor.h"
19 #include "math_parser.h"
20 #include "math_macro.h"
21 #include "math_macrotable.h"
22 #include "math_macrotemplate.h"
23 #include "metricsinfo.h"
24 #include "math_support.h"
25 #include "math_mathmlstream.h"
26 #include "BufferView.h"
28 #include "latexrunparams.h"
29 #include "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "support/lyxlib.h"
32 #include "support/LOstream.h"
40 #include "support/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 &) const
70 return new InsetFormulaMacro(*this);
74 // Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
76 // return new InsetFormulaMacro(*this);
80 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
82 os << "FormulaMacro ";
83 WriteStream wi(os, false, false);
88 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
89 LatexRunParams const & runparams) const
91 WriteStream wi(os, runparams.moving_arg, true);
97 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
99 WriteStream wi(os, false, true);
105 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
107 return ascii(buf, os, 0);
111 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
113 return ascii(buf, os, 0);
117 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
119 read(lex.getStream());
123 void InsetFormulaMacro::read(std::istream & is)
125 MathMacroTemplate * p = new MathMacroTemplate(is);
126 setInsetName(p->name());
127 MathMacroTable::create(MathAtom(p));
132 string InsetFormulaMacro::prefix() const
135 return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
136 STRCONV(getInsetName())));
138 return _(" Macro: ") + getInsetName() + ": ";
143 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
144 Dimension & dim) const
149 dim = par()->metrics(mi);
152 dim.wid += 10 + font_metrics::width(prefix(), font);
156 MathAtom const & InsetFormulaMacro::par() const
158 return MathMacroTable::provide(getInsetName());
162 MathAtom & InsetFormulaMacro::par()
164 return MathMacroTable::provide(getInsetName());
168 Inset::Code InsetFormulaMacro::lyxCode() const
170 return Inset::MATHMACRO_CODE;
174 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
177 LyXFont font = p.base.font;
178 font.setColor(LColor::math);
180 PainterInfo pi(p.base.bv);
181 pi.base.style = LM_ST_TEXT;
185 dimension(pi.base.bv, font, dim);
186 int const a = y - dim.asc + 1;
187 int const w = dim.wid - 2;
188 int const h = dim.height() - 2;
190 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
191 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
192 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
195 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
196 mathcursor->drawSelection(pi);
198 pi.pain.text(x + 2, y, prefix(), font);
201 par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);