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: (c) 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*/) const
81 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
84 tmacro->WriteDef(file);
89 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
95 int InsetFormulaMacro::DocBook(string &/*file*/) const
101 void InsetFormulaMacro::Read(LyXLex & lex)
103 istream & is = lex.getStream();
104 mathed_parser_file(is, lex.GetLineNo());
105 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
107 // Update line number
108 lex.setLineNo(mathed_parser_lineno());
110 MathMacroTable::mathMTable.addTemplate(tmacro);
111 name = tmacro->GetName();
116 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
120 return InsetFormula::ascent(pain, f);
122 return f.maxAscent()+3;
126 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
130 return InsetFormula::descent(pain, f);
132 return f.maxDescent()+1;
136 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
140 return InsetFormula::width(pain, f);
142 string ilabel(_("Macro: "));
144 return 6 + f.stringWidth(ilabel);
148 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
149 int baseline, float & x) const
154 tmacro->setEditMode(true);
155 InsetFormula::draw(pain, font, baseline, x);
156 tmacro->setEditMode(false);
158 font.setColor(LColor::math);
160 int y = baseline - ascent(pain, font) + 1;
161 int w = width(pain, font) - 2;
162 int h = (ascent(pain, font) + descent(pain, font) - 2);
165 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
166 pain.rectangle(int(x), y, w, h, LColor::mathframe);
168 string s(_("Macro: "));
170 pain.text(int(x + 2), baseline, s, font);
171 x += width(pain, font) - 1;
176 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
179 par = static_cast<MathParInset*>(tmacro->Clone());
180 InsetFormula::Edit(bv, x, y, button);
184 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
187 LyxArrayBase * tarray = tmacro->GetData();
188 MathedIter it(tarray);
190 tmacro->SetData(par->GetData());
191 tmacro->setEditMode(false);
192 InsetFormula::InsetUnlock(bv);
196 UpdatableInset::RESULT
197 InsetFormulaMacro::LocalDispatch(BufferView * bv,
198 int action, string const & arg)
200 if (action == LFUN_MATH_MACROARG) {
201 int i = atoi(arg.c_str()) - 1;
202 if (i >= 0 && i < tmacro->getNoArgs()) {
203 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
204 InsetFormula::UpdateLocal(bv);
209 tmacro->setEditMode(true);
211 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
212 tmacro->setEditMode(false);