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 "bufferlist.h"
31 #include "BufferView.h"
32 #include "lyxscreen.h"
37 InsetFormulaMacro::InsetFormulaMacro():
45 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/):
46 InsetFormula(true), name(nm)
48 tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
50 tmacro = new MathMacroTemplate(name.c_str(), na);
51 MathMacroTable::mathMTable.addTemplate(tmacro);
57 InsetFormulaMacro::~InsetFormulaMacro()
63 InsetFormulaMacro * InsetFormulaMacro::Clone() const
65 return new InsetFormulaMacro(name);
69 void InsetFormulaMacro::Write(FILE *file)
71 fprintf(file, "FormulaMacro ");
76 int InsetFormulaMacro::Latex(FILE *file, signed char /*fragile*/)
79 tmacro->WriteDef(file);
84 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
87 tmacro->WriteDef(file);
92 int InsetFormulaMacro::Linuxdoc(string &/*file*/)
98 int InsetFormulaMacro::DocBook(string &/*file*/)
104 void InsetFormulaMacro::Read(LyXLex &lex)
106 FILE *file = lex.getFile();
107 mathed_parser_file(file, lex.GetLineNo());
108 mathed_parse(0, 0, (MathParInset **)&tmacro);
110 // Update line number
111 lex.setLineNo(mathed_parser_lineno());
113 MathMacroTable::mathMTable.addTemplate(tmacro);
114 name = tmacro->GetName();
119 int InsetFormulaMacro::Ascent(LyXFont const &f) const
123 return InsetFormula::Ascent(f);
125 return f.maxAscent()+3;
129 int InsetFormulaMacro::Descent(LyXFont const &f) const
133 return InsetFormula::Descent(f);
135 return f.maxDescent()+1;
139 int InsetFormulaMacro::Width(LyXFont const &f) const
143 return InsetFormula::Width(f);
145 string ilabel(_("Macro: "));
147 return 6 + f.stringWidth(ilabel);
151 void InsetFormulaMacro::Draw(LyXFont font, LyXScreen &scr,
152 int baseline, float &x)
156 tmacro->setEditMode(true);
157 InsetFormula::Draw(font, scr, baseline, x);
158 tmacro->setEditMode(false);
160 font.setColor(LyXFont::MATH);
162 int y= baseline - Ascent(font)+1;
163 int w= Width(font) - 2, h= (Ascent(font)+Descent(font)-2);
166 scr.fillRectangle(gc_lighted, int(x), y, w, h);
167 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
169 string s(_("Macro: "));
171 scr.drawString(font, s, baseline, int(x +2));
172 x += Width(font) - 1;
177 void InsetFormulaMacro::Edit(int x, int y)
180 par = (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);