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 Inset* InsetFormulaMacro::Clone()
65 InsetFormulaMacro* f = new InsetFormulaMacro(name);
70 void InsetFormulaMacro::Write(FILE *file)
72 fprintf(file, "FormulaMacro ");
77 int InsetFormulaMacro::Latex(FILE *file, signed char /*fragile*/)
80 tmacro->WriteDef(file);
85 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
88 tmacro->WriteDef(file);
93 int InsetFormulaMacro::Linuxdoc(string &/*file*/)
99 int InsetFormulaMacro::DocBook(string &/*file*/)
105 void InsetFormulaMacro::Read(LyXLex &lex)
107 FILE *file = lex.getFile();
108 mathed_parser_file(file, lex.GetLineNo());
109 mathed_parse(0, 0, (MathParInset **)&tmacro);
111 // Update line number
112 lex.setLineNo(mathed_parser_lineno());
114 MathMacroTable::mathMTable.addTemplate(tmacro);
115 name = tmacro->GetName();
120 int InsetFormulaMacro::Ascent(LyXFont const &f) const
124 return InsetFormula::Ascent(f);
126 return f.maxAscent()+3;
130 int InsetFormulaMacro::Descent(LyXFont const &f) const
134 return InsetFormula::Descent(f);
136 return f.maxDescent()+1;
140 int InsetFormulaMacro::Width(LyXFont const &f) const
144 return InsetFormula::Width(f);
146 string ilabel(_("Macro: "));
148 return 6 + f.stringWidth(ilabel);
152 void InsetFormulaMacro::Draw(LyXFont font, LyXScreen &scr,
153 int baseline, float &x)
157 tmacro->setEditMode(true);
158 InsetFormula::Draw(font, scr, baseline, x);
159 tmacro->setEditMode(false);
161 font.setColor(LyXFont::MATH);
163 int y=baseline - Ascent(font)+1;
164 int w=Width(font) - 2, h=(Ascent(font)+Descent(font)-2);
167 scr.fillRectangle(gc_lighted, int(x), y, w, h);
168 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
170 string s(_("Macro: "));
172 scr.drawString(font, s, baseline, int(x +2));
173 x += Width(font) - 1;
178 void InsetFormulaMacro::Edit(int x, int y)
181 par = (MathParInset*)tmacro->Clone();
182 InsetFormula::Edit(x, y);
186 void InsetFormulaMacro::InsetUnlock()
189 LyxArrayBase *tarray = tmacro->GetData();
190 MathedIter it(tarray);
192 tmacro->SetData(par->GetData());
193 tmacro->setEditMode(false);
194 InsetFormula::InsetUnlock();
198 bool InsetFormulaMacro::LocalDispatch(int action, char const *arg)
200 if (action==LFUN_MATH_MACROARG) {
201 int i = atoi(arg) - 1;
202 if (i>=0 && i<tmacro->getNoArgs()) {
203 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
204 InsetFormula::UpdateLocal();
209 tmacro->setEditMode(true);
211 bool result = InsetFormula::LocalDispatch(action, arg);
212 tmacro->setEditMode(false);