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"
35 InsetFormulaMacro::InsetFormulaMacro()
43 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
44 : InsetFormula(true), name(nm)
46 tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
48 tmacro = new MathMacroTemplate(name.c_str(), na);
49 MathMacroTable::mathMTable.addTemplate(tmacro);
55 InsetFormulaMacro::~InsetFormulaMacro()
61 Inset * InsetFormulaMacro::Clone() const
63 return new InsetFormulaMacro(name);
67 void InsetFormulaMacro::Write(ostream & os) const
69 os << "FormulaMacro ";
74 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/) const
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
102 void InsetFormulaMacro::Read(LyXLex & lex)
104 istream & is = lex.getStream();
105 mathed_parser_file(is, lex.GetLineNo());
106 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
108 // Update line number
109 lex.setLineNo(mathed_parser_lineno());
111 MathMacroTable::mathMTable.addTemplate(tmacro);
112 name = tmacro->GetName();
117 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
121 return InsetFormula::ascent(pain, f);
123 return f.maxAscent()+3;
127 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
131 return InsetFormula::descent(pain, f);
133 return f.maxDescent()+1;
137 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
141 return InsetFormula::width(pain, f);
143 string ilabel(_("Macro: "));
145 return 6 + f.stringWidth(ilabel);
149 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
150 int baseline, float & x) const
155 tmacro->setEditMode(true);
156 InsetFormula::draw(pain, font, baseline, x);
157 tmacro->setEditMode(false);
159 font.setColor(LColor::math);
161 int y = baseline - ascent(pain, font) + 1;
162 int w = width(pain, font) - 2;
163 int h = (ascent(pain, font) + descent(pain, font) - 2);
166 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
167 pain.rectangle(int(x), y, w, h, LColor::mathframe);
169 string s(_("Macro: "));
171 pain.text(int(x + 2), baseline, s, font);
172 x += width(pain, font) - 1;
177 void InsetFormulaMacro::Edit(int x, int y)
180 par = static_cast<MathParInset*>(tmacro->Clone());
181 InsetFormula::Edit(x, y);
185 void InsetFormulaMacro::InsetUnlock()
188 LyxArrayBase * tarray = tmacro->GetData();
189 MathedIter it(tarray);
191 tmacro->SetData(par->GetData());
192 tmacro->setEditMode(false);
193 InsetFormula::InsetUnlock();
197 bool InsetFormulaMacro::LocalDispatch(int action, char const * arg)
199 if (action == LFUN_MATH_MACROARG) {
200 int i = atoi(arg) - 1;
201 if (i >= 0 && i < tmacro->getNoArgs()) {
202 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
203 InsetFormula::UpdateLocal();
208 tmacro->setEditMode(true);
210 bool result = InsetFormula::LocalDispatch(action, arg);
211 tmacro->setEditMode(false);