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.
20 #pragma implementation "formulamacro.h"
23 #include "formulamacro.h"
24 #include "commandtags.h"
25 #include "math_cursor.h"
26 #include "math_parser.h"
27 #include "math_macro.h"
29 #include "BufferView.h"
37 InsetFormulaMacro::InsetFormulaMacro()
45 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
46 : InsetFormula(true), name(nm)
48 tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
50 tmacro = new MathMacroTemplate(name.c_str(), na);
51 MathMacroTable::mathMTable.addTemplate(tmacro);
57 InsetFormulaMacro::~InsetFormulaMacro()
63 Inset * InsetFormulaMacro::Clone() const
65 return new InsetFormulaMacro(name);
69 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
71 os << "FormulaMacro ";
72 tmacro->WriteDef(os, false);
76 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
77 bool /*free_spacing*/) const
79 tmacro->WriteDef(os, true); // or false?
84 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
86 return Ascii(buf, os);
90 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
92 return Ascii(buf, os);
96 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
98 istream & is = lex.getStream();
99 mathed_parser_file(is, lex.GetLineNo());
100 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
102 // Update line number
103 lex.setLineNo(mathed_parser_lineno());
105 MathMacroTable::mathMTable.addTemplate(tmacro);
106 name = tmacro->GetName();
108 // reading of end_inset in the inset!!!
111 if (lex.GetString() == "\\end_inset")
117 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
121 return InsetFormula::ascent(pain, f);
123 return lyxfont::maxAscent(f) + 3;
127 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
131 return InsetFormula::descent(pain, f);
133 return lyxfont::maxDescent(f) + 1;
137 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
141 return InsetFormula::width(pain, f);
143 string ilabel(_("Macro: "));
145 return 6 + lyxfont::width(ilabel, f);
149 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
150 int baseline, float & x, bool cleared) const
152 Painter & pain = bv->painter();
156 tmacro->setEditMode(true);
157 InsetFormula::draw(bv, font, baseline, x, cleared);
158 tmacro->setEditMode(false);
160 font.setColor(LColor::math);
162 int y = baseline - ascent(pain, font) + 1;
163 int w = width(pain, font) - 2;
164 int h = (ascent(pain, font) + descent(pain, font) - 2);
167 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
168 pain.rectangle(int(x), y, w, h, LColor::mathframe);
170 string s(_("Macro: "));
172 pain.text(int(x + 2), baseline, s, font);
173 x += width(pain, font) - 1;
178 char const * InsetFormulaMacro::EditMessage() const
180 return _("Math macro editor mode");
184 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
187 par = static_cast<MathParInset*>(tmacro->Clone());
188 InsetFormula::Edit(bv, x, y, button);
192 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
195 LyxArrayBase * tarray = tmacro->GetData();
196 MathedIter it(tarray);
198 tmacro->SetData(par->GetData());
199 tmacro->setEditMode(false);
200 InsetFormula::InsetUnlock(bv);
204 UpdatableInset::RESULT
205 InsetFormulaMacro::LocalDispatch(BufferView * bv,
206 int action, string const & arg)
208 if (action == LFUN_MATH_MACROARG) {
209 int i = atoi(arg.c_str()) - 1;
210 if (i >= 0 && i < tmacro->getNoArgs()) {
211 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
212 InsetFormula::UpdateLocal(bv);
217 tmacro->setEditMode(true);
219 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
220 tmacro->setEditMode(false);