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"
34 InsetFormulaMacro::InsetFormulaMacro()
42 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
43 : InsetFormula(true), name(nm)
45 tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
47 tmacro = new MathMacroTemplate(name.c_str(), na);
48 MathMacroTable::mathMTable.addTemplate(tmacro);
54 InsetFormulaMacro::~InsetFormulaMacro()
60 Inset * InsetFormulaMacro::Clone() const
62 return new InsetFormulaMacro(name);
66 void InsetFormulaMacro::Write(ostream & os) const
68 os << "FormulaMacro ";
73 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/,
74 bool /*free_spacing*/) const
82 #ifndef USE_OSTREAM_ONLY
83 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/,
84 bool /*free_spacing*/) const
87 tmacro->WriteDef(file);
92 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
98 int InsetFormulaMacro::DocBook(string &/*file*/) const
105 int InsetFormulaMacro::Linuxdoc(ostream &) const
111 int InsetFormulaMacro::DocBook(ostream &) const
118 void InsetFormulaMacro::Read(LyXLex & lex)
120 istream & is = lex.getStream();
121 mathed_parser_file(is, lex.GetLineNo());
122 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
124 // Update line number
125 lex.setLineNo(mathed_parser_lineno());
127 MathMacroTable::mathMTable.addTemplate(tmacro);
128 name = tmacro->GetName();
133 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
137 return InsetFormula::ascent(pain, f);
139 return f.maxAscent()+3;
143 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
147 return InsetFormula::descent(pain, f);
149 return f.maxDescent()+1;
153 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
157 return InsetFormula::width(pain, f);
159 string ilabel(_("Macro: "));
161 return 6 + f.stringWidth(ilabel);
165 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
166 int baseline, float & x) const
171 tmacro->setEditMode(true);
172 InsetFormula::draw(pain, font, baseline, x);
173 tmacro->setEditMode(false);
175 font.setColor(LColor::math);
177 int y = baseline - ascent(pain, font) + 1;
178 int w = width(pain, font) - 2;
179 int h = (ascent(pain, font) + descent(pain, font) - 2);
182 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
183 pain.rectangle(int(x), y, w, h, LColor::mathframe);
185 string s(_("Macro: "));
187 pain.text(int(x + 2), baseline, s, font);
188 x += width(pain, font) - 1;
193 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
196 par = static_cast<MathParInset*>(tmacro->Clone());
197 InsetFormula::Edit(bv, x, y, button);
201 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
204 LyxArrayBase * tarray = tmacro->GetData();
205 MathedIter it(tarray);
207 tmacro->SetData(par->GetData());
208 tmacro->setEditMode(false);
209 InsetFormula::InsetUnlock(bv);
213 UpdatableInset::RESULT
214 InsetFormulaMacro::LocalDispatch(BufferView * bv,
215 int action, string const & arg)
217 if (action == LFUN_MATH_MACROARG) {
218 int i = atoi(arg.c_str()) - 1;
219 if (i >= 0 && i < tmacro->getNoArgs()) {
220 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
221 InsetFormula::UpdateLocal(bv);
226 tmacro->setEditMode(true);
228 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
229 tmacro->setEditMode(false);