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"
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 "metricsinfo.h"
25 #include "math_support.h"
26 #include "math_mathmlstream.h"
27 #include "BufferView.h"
29 #include "latexrunparams.h"
30 #include "frontends/Painter.h"
31 #include "frontends/font_metrics.h"
32 #include "support/lyxlib.h"
33 #include "support/LOstream.h"
41 #include "support/BoostFormat.h"
45 extern MathCursor * mathcursor;
48 InsetFormulaMacro::InsetFormulaMacro()
50 // inset name is inherited from Inset
51 setInsetName("unknown");
55 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
58 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
62 InsetFormulaMacro::InsetFormulaMacro(string const & s)
64 std::istringstream is(STRCONV(s));
69 Inset * InsetFormulaMacro::clone(Buffer const &) const
71 return new InsetFormulaMacro(*this);
75 // Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
77 // return new InsetFormulaMacro(*this);
81 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
83 os << "FormulaMacro ";
84 WriteStream wi(os, false, false);
89 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
90 LatexRunParams const & runparams) const
92 WriteStream wi(os, runparams.moving_arg, true);
98 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
100 WriteStream wi(os, false, true);
106 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
108 return ascii(buf, os, 0);
112 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
114 return ascii(buf, os, 0);
118 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
120 read(lex.getStream());
124 void InsetFormulaMacro::read(std::istream & is)
126 MathMacroTemplate * p = new MathMacroTemplate(is);
127 setInsetName(p->name());
128 MathMacroTable::create(MathAtom(p));
133 string InsetFormulaMacro::prefix() const
136 return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
137 STRCONV(getInsetName())));
139 return _(" Macro: ") + getInsetName() + ": ";
144 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
145 Dimension & dim) const
148 dim = par()->dimensions();
151 dim.wid += 10 + font_metrics::width(prefix(), font);
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) const
178 font.setColor(LColor::math);
180 PainterInfo pi = PainterInfo(bv->painter());
181 pi.base.style = LM_ST_TEXT;
185 dimension(bv, font, dim);
186 int const x = int(xx);
187 int const a = y - dim.asc + 1;
188 int const w = dim.wid - 2;
189 int const h = dim.height() - 2;
191 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
192 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
193 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
196 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
197 mathcursor->drawSelection(pi);
199 pi.pain.text(x + 2, y, prefix(), font);
202 par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);