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/lstrings.h"
33 #include "support/LOstream.h"
41 extern MathCursor * mathcursor;
44 InsetFormulaMacro::InsetFormulaMacro()
46 // inset name is inherited from Inset
47 setInsetName("unknown");
51 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
54 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
58 InsetFormulaMacro::InsetFormulaMacro(string const & s)
60 std::istringstream is(STRCONV(s));
65 Inset * InsetFormulaMacro::clone(Buffer const &) const
67 return new InsetFormulaMacro(*this);
71 // Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
73 // return new InsetFormulaMacro(*this);
77 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
79 os << "FormulaMacro ";
80 WriteStream wi(os, false, false);
85 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
86 LatexRunParams const & runparams) const
88 WriteStream wi(os, runparams.moving_arg, true);
94 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
96 WriteStream wi(os, false, true);
102 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
104 return ascii(buf, os, 0);
108 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
110 return ascii(buf, os, 0);
114 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
116 read(lex.getStream());
120 void InsetFormulaMacro::read(std::istream & is)
122 MathMacroTemplate * p = new MathMacroTemplate(is);
123 setInsetName(p->name());
124 MathMacroTable::create(MathAtom(p));
129 string InsetFormulaMacro::prefix() const
131 return bformat(_(" Macro: %s: "), getInsetName());
135 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
137 par()->metrics(mi, dim_);
140 dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
145 MathAtom const & InsetFormulaMacro::par() const
147 return MathMacroTable::provide(getInsetName());
151 MathAtom & InsetFormulaMacro::par()
153 return MathMacroTable::provide(getInsetName());
157 Inset::Code InsetFormulaMacro::lyxCode() const
159 return Inset::MATHMACRO_CODE;
163 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
166 LyXFont font = p.base.font;
167 font.setColor(LColor::math);
169 PainterInfo pi(p.base.bv);
170 pi.base.style = LM_ST_TEXT;
173 int const a = y - dim_.asc + 1;
174 int const w = dim_.wid - 2;
175 int const h = dim_.height() - 2;
177 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
178 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
179 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
182 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
183 mathcursor->drawSelection(pi);
185 pi.pain.text(x + 2, y, prefix(), font);
188 par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);