3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
19 #pragma implementation
22 #include "formulamacro.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_macro.h"
27 #include "math_macroarg.h"
28 #include "math_macrotable.h"
29 #include "math_macrotemplate.h"
30 #include "math_matrixinset.h"
32 #include "BufferView.h"
36 #include "support/lyxlib.h"
37 #include "mathed/support.h"
38 #include "support/LOstream.h"
44 extern MathCursor * mathcursor;
46 InsetFormulaMacro::InsetFormulaMacro()
47 : InsetFormulaBase(new MathMacroTemplate("unknown", 0))
51 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
52 : InsetFormulaBase(new MathMacroTemplate(nm, na))
54 MathMacroTable::insertTemplate(tmacro());
58 Inset * InsetFormulaMacro::clone(Buffer const &) const
60 return new InsetFormulaMacro(*this);
64 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
66 os << "FormulaMacro ";
67 tmacro()->Write(os, false);
71 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
72 bool /*free_spacing*/) const
74 tmacro()->Write(os, fragile);
78 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
80 tmacro()->Write(os, false);
85 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
87 return ascii(buf, os, 0);
91 int InsetFormulaMacro::docBook(Buffer const * buf, ostream & os) const
93 return ascii(buf, os, 0);
97 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
100 par_ = mathed_parse(lex);
101 MathMacroTable::insertTemplate(tmacro());
102 par_->Metrics(LM_ST_TEXT);
106 string InsetFormulaMacro::prefix() const
108 return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
112 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
114 return tmacro()->ascent() + 5;
118 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
120 return tmacro()->descent() + 5;
124 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
126 tmacro()->Metrics(LM_ST_TEXT);
127 return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
131 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
132 int baseline, float & x, bool /*cleared*/) const
134 Painter & pain = bv->painter();
138 font.setColor(LColor::math);
140 int const y = baseline - ascent(bv, font) + 1;
141 int const w = width(bv, font) - 2;
142 int const h = ascent(bv, font) + descent(bv, font) - 2;
144 pain.fillRectangle(int(x), y , w, h, LColor::mathbg);
145 pain.rectangle(int(x), y, w, h, LColor::mathframe);
147 if (mathcursor && mathcursor->formula() == this && mathcursor->Selection()) {
151 mathcursor->SelGetArea(xp, yp, n);
152 pain.fillPolygon(xp, yp, n, LColor::selection);
155 pain.text(int(x + 2), baseline, prefix(), font);
156 x += width(bv, font);
159 float t = tmacro()->width() + 5;
161 tmacro()->draw(pain, int(x), baseline);
166 UpdatableInset::RESULT
167 InsetFormulaMacro::localDispatch(BufferView * bv,
168 kb_action action, string const & arg)
170 RESULT result = DISPATCHED;
172 case LFUN_MATH_MACROARG: {
173 int const i = lyx::atoi(arg);
174 lyxerr << "inserting macro arg " << i << "\n";
175 if (i > 0 && i <= tmacro()->nargs()) {
176 mathcursor->insert(new MathMacroArgument(i));
179 lyxerr << "not in range 0.." << tmacro()->nargs() << "\n";
185 result = InsetFormulaBase::localDispatch(bv, action, arg);
191 MathMacroTemplate * InsetFormulaMacro::tmacro() const
193 return static_cast<MathMacroTemplate *>(par_);
197 Inset::Code InsetFormulaMacro::lyxCode() const
199 return Inset::MATHMACRO_CODE;