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 #ifndef USE_OSTREAM_ONLY
82 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
85 tmacro->WriteDef(file);
90 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
96 int InsetFormulaMacro::DocBook(string &/*file*/) const
103 int InsetFormulaMacro::Linuxdoc(ostream &) const
109 int InsetFormulaMacro::DocBook(ostream &) const
116 void InsetFormulaMacro::Read(LyXLex & lex)
118 istream & is = lex.getStream();
119 mathed_parser_file(is, lex.GetLineNo());
120 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
122 // Update line number
123 lex.setLineNo(mathed_parser_lineno());
125 MathMacroTable::mathMTable.addTemplate(tmacro);
126 name = tmacro->GetName();
131 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
135 return InsetFormula::ascent(pain, f);
137 return f.maxAscent()+3;
141 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
145 return InsetFormula::descent(pain, f);
147 return f.maxDescent()+1;
151 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
155 return InsetFormula::width(pain, f);
157 string ilabel(_("Macro: "));
159 return 6 + f.stringWidth(ilabel);
163 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
164 int baseline, float & x) const
169 tmacro->setEditMode(true);
170 InsetFormula::draw(pain, font, baseline, x);
171 tmacro->setEditMode(false);
173 font.setColor(LColor::math);
175 int y = baseline - ascent(pain, font) + 1;
176 int w = width(pain, font) - 2;
177 int h = (ascent(pain, font) + descent(pain, font) - 2);
180 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
181 pain.rectangle(int(x), y, w, h, LColor::mathframe);
183 string s(_("Macro: "));
185 pain.text(int(x + 2), baseline, s, font);
186 x += width(pain, font) - 1;
191 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
194 par = static_cast<MathParInset*>(tmacro->Clone());
195 InsetFormula::Edit(bv, x, y, button);
199 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
202 LyxArrayBase * tarray = tmacro->GetData();
203 MathedIter it(tarray);
205 tmacro->SetData(par->GetData());
206 tmacro->setEditMode(false);
207 InsetFormula::InsetUnlock(bv);
211 UpdatableInset::RESULT
212 InsetFormulaMacro::LocalDispatch(BufferView * bv,
213 int action, string const & arg)
215 if (action == LFUN_MATH_MACROARG) {
216 int i = atoi(arg.c_str()) - 1;
217 if (i >= 0 && i < tmacro->getNoArgs()) {
218 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
219 InsetFormula::UpdateLocal(bv);
224 tmacro->setEditMode(true);
226 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
227 tmacro->setEditMode(false);