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"
39 using namespace lyx::support;
43 extern MathCursor * mathcursor;
46 InsetFormulaMacro::InsetFormulaMacro()
48 // inset name is inherited from Inset
49 setInsetName("unknown");
53 InsetFormulaMacro::InsetFormulaMacro
54 (string const & name, int nargs, string const & type)
57 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 std::istringstream is(STRCONV(s));
68 Inset * InsetFormulaMacro::clone() 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,
83 LatexRunParams const & runparams) const
85 WriteStream wi(os, runparams.moving_arg, 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
128 return bformat(_(" Macro: %s: "), getInsetName());
132 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
134 par()->metrics(mi, dim_);
137 dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
142 MathAtom const & InsetFormulaMacro::par() const
144 return MathMacroTable::provide(getInsetName());
148 MathAtom & InsetFormulaMacro::par()
150 return MathMacroTable::provide(getInsetName());
154 Inset::Code InsetFormulaMacro::lyxCode() const
156 return Inset::MATHMACRO_CODE;
160 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
163 LyXFont font = p.base.font;
164 font.setColor(LColor::math);
166 PainterInfo pi(p.base.bv);
167 pi.base.style = LM_ST_TEXT;
170 int const a = y - dim_.asc + 1;
171 int const w = dim_.wid - 2;
172 int const h = dim_.height() - 2;
174 // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
175 pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
176 pi.pain.rectangle(x, a, w, h, LColor::mathframe);
179 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
180 mathcursor->drawSelection(pi);
182 pi.pain.text(x + 2, y, prefix(), font);
185 par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);