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"
30 #include "BufferView.h"
31 #include "lyxscreen.h"
39 InsetFormulaMacro::InsetFormulaMacro()
47 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
48 : InsetFormula(true), name(nm)
50 tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
52 tmacro = new MathMacroTemplate(name.c_str(), na);
53 MathMacroTable::mathMTable.addTemplate(tmacro);
59 InsetFormulaMacro::~InsetFormulaMacro()
65 Inset * InsetFormulaMacro::Clone() const
67 return new InsetFormulaMacro(name);
71 void InsetFormulaMacro::Write(ostream & os)
73 os << "FormulaMacro ";
78 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/)
86 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
89 tmacro->WriteDef(file);
94 int InsetFormulaMacro::Linuxdoc(string &/*file*/)
100 int InsetFormulaMacro::DocBook(string &/*file*/)
106 void InsetFormulaMacro::Read(LyXLex & lex)
108 istream & is = lex.getStream();
109 mathed_parser_file(is, lex.GetLineNo());
110 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
112 // Update line number
113 lex.setLineNo(mathed_parser_lineno());
115 MathMacroTable::mathMTable.addTemplate(tmacro);
116 name = tmacro->GetName();
122 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
126 return InsetFormula::ascent(pain, f);
128 return f.maxAscent()+3;
131 int InsetFormulaMacro::Ascent(LyXFont const & f) const
135 return InsetFormula::Ascent(f);
137 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;
152 int InsetFormulaMacro::Descent(LyXFont const & f) const
156 return InsetFormula::Descent(f);
158 return f.maxDescent()+1;
164 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
168 return InsetFormula::width(pain, f);
170 string ilabel(_("Macro: "));
172 return 6 + f.stringWidth(ilabel);
175 int InsetFormulaMacro::Width(LyXFont const & f) const
179 return InsetFormula::Width(f);
181 string ilabel(_("Macro: "));
183 return 6 + f.stringWidth(ilabel);
189 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
190 int baseline, float & x) const
195 tmacro->setEditMode(true);
196 InsetFormula::draw(pain, font, baseline, x);
197 tmacro->setEditMode(false);
199 font.setColor(LColor::math);
201 int y = baseline - ascent(pain, font) + 1;
202 int w = width(pain, font) - 2;
203 int h = (ascent(pain, font) + descent(pain, font) - 2);
206 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
207 pain.rectangle(int(x), y, w, h, LColor::mathframe);
209 string s(_("Macro: "));
211 pain.text(int(x + 2), baseline, s, font);
212 x += width(pain, font) - 1;
216 void InsetFormulaMacro::Draw(LyXFont font, LyXScreen & scr,
217 int baseline, float & x)
221 tmacro->setEditMode(true);
222 InsetFormula::Draw(font, scr, baseline, x);
223 tmacro->setEditMode(false);
225 font.setColor(LyXFont::MATH);
227 int y = baseline - Ascent(font) + 1;
228 int w = Width(font) - 2, h = (Ascent(font) + Descent(font) - 2);
231 scr.fillRectangle(gc_lighted, int(x), y, w, h);
232 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
234 string s(_("Macro: "));
236 scr.drawString(font, s, baseline, int(x +2));
237 x += Width(font) - 1;
243 void InsetFormulaMacro::Edit(int x, int y)
246 par = static_cast<MathParInset*>(tmacro->Clone());
247 InsetFormula::Edit(x, y);
251 void InsetFormulaMacro::InsetUnlock()
254 LyxArrayBase * tarray = tmacro->GetData();
255 MathedIter it(tarray);
257 tmacro->SetData(par->GetData());
258 tmacro->setEditMode(false);
259 InsetFormula::InsetUnlock();
263 bool InsetFormulaMacro::LocalDispatch(int action, char const * arg)
265 if (action == LFUN_MATH_MACROARG) {
266 int i = atoi(arg) - 1;
267 if (i >= 0 && i < tmacro->getNoArgs()) {
268 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
269 InsetFormula::UpdateLocal();
274 tmacro->setEditMode(true);
276 bool result = InsetFormula::LocalDispatch(action, arg);
277 tmacro->setEditMode(false);