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: 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"
33 #include "support/lyxlib.h"
38 InsetFormulaMacro::InsetFormulaMacro()
46 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
47 : InsetFormula(true), name(nm)
49 tmacro = MathMacroTable::mathMTable.getTemplate(name);
51 tmacro = new MathMacroTemplate(name.c_str(), na);
52 MathMacroTable::mathMTable.addTemplate(tmacro);
58 InsetFormulaMacro::~InsetFormulaMacro()
64 Inset * InsetFormulaMacro::Clone() const
66 return new InsetFormulaMacro(name);
70 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
72 os << "FormulaMacro ";
73 tmacro->WriteDef(os, false);
77 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
78 bool /*free_spacing*/) const
80 tmacro->WriteDef(os, true); // or false?
85 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
87 return Ascii(buf, os, 0);
91 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
93 return Ascii(buf, os, 0);
97 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
99 istream & is = lex.getStream();
100 mathed_parser_file(is, lex.GetLineNo());
101 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
103 // Update line number
104 lex.setLineNo(mathed_parser_lineno());
106 MathMacroTable::mathMTable.addTemplate(tmacro);
107 name = tmacro->GetName();
109 // reading of end_inset in the inset!!!
112 if (lex.GetString() == "\\end_inset")
118 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
122 return InsetFormula::ascent(pain, f);
124 return lyxfont::maxAscent(f) + 3;
128 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
132 return InsetFormula::descent(pain, f);
134 return lyxfont::maxDescent(f) + 1;
138 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
142 return InsetFormula::width(bv, f);
144 string ilabel(_("Macro: "));
146 return 6 + lyxfont::width(ilabel, f);
150 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
151 int baseline, float & x, bool cleared) const
153 Painter & pain = bv->painter();
157 tmacro->setEditMode(true);
158 InsetFormula::draw(bv, font, baseline, x, cleared);
159 tmacro->setEditMode(false);
161 font.setColor(LColor::math);
163 int y = baseline - ascent(bv, font) + 1;
164 int w = width(bv, font) - 2;
165 int h = (ascent(bv, font) + descent(bv, font) - 2);
168 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
169 pain.rectangle(int(x), y, w, h, LColor::mathframe);
171 string s(_("Macro: "));
173 pain.text(int(x + 2), baseline, s, font);
174 x += width(bv, font) - 1;
179 string const InsetFormulaMacro::EditMessage() const
181 return _("Math macro editor mode");
185 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
188 par = static_cast<MathParInset*>(tmacro->Clone());
189 InsetFormula::Edit(bv, x, y, button);
193 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
196 LyxArrayBase * tarray = tmacro->GetData();
197 MathedIter it(tarray);
199 tmacro->SetData(par->GetData());
200 tmacro->setEditMode(false);
201 InsetFormula::InsetUnlock(bv);
205 UpdatableInset::RESULT
206 InsetFormulaMacro::LocalDispatch(BufferView * bv,
207 int action, string const & arg)
209 if (action == LFUN_MATH_MACROARG) {
210 int i = lyx::atoi(arg) - 1;
211 if (i >= 0 && i < tmacro->getNoArgs()) {
212 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
213 InsetFormula::UpdateLocal(bv);
218 tmacro->setEditMode(true);
220 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
221 tmacro->setEditMode(false);