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"
28 #include "math_macrotable.h"
29 #include "math_macrotemplate.h"
31 #include "BufferView.h"
35 #include "support/lyxlib.h"
40 InsetFormulaMacro::InsetFormulaMacro()
48 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
49 : InsetFormula(true), name(nm)
51 tmacro = MathMacroTable::mathMTable.getTemplate(name);
53 tmacro = new MathMacroTemplate(name.c_str(), na);
54 MathMacroTable::mathMTable.addTemplate(tmacro);
60 InsetFormulaMacro::~InsetFormulaMacro()
66 Inset * InsetFormulaMacro::Clone(Buffer const &) const
68 return new InsetFormulaMacro(name);
72 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
74 os << "FormulaMacro ";
75 tmacro->WriteDef(os, false);
79 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
80 bool /*free_spacing*/) const
82 tmacro->WriteDef(os, true); // or false?
87 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
89 return Ascii(buf, os, 0);
93 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
95 return Ascii(buf, os, 0);
99 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
101 istream & is = lex.getStream();
102 mathed_parser_file(is, lex.GetLineNo());
103 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
105 // Update line number
106 lex.setLineNo(mathed_parser_lineno());
108 MathMacroTable::mathMTable.addTemplate(tmacro);
109 name = tmacro->GetName();
111 // reading of end_inset in the inset!!!
114 if (lex.GetString() == "\\end_inset")
120 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
124 return InsetFormula::ascent(pain, f);
126 return lyxfont::maxAscent(f) + 3;
130 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
134 return InsetFormula::descent(pain, f);
136 return lyxfont::maxDescent(f) + 1;
140 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
144 return InsetFormula::width(bv, f);
146 string ilabel(_("Macro: "));
148 return 6 + lyxfont::width(ilabel, f);
152 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
153 int baseline, float & x, bool cleared) const
155 Painter & pain = bv->painter();
159 tmacro->setEditMode(true);
160 InsetFormula::draw(bv, font, baseline, x, cleared);
161 tmacro->setEditMode(false);
163 font.setColor(LColor::math);
165 int y = baseline - ascent(bv, font) + 1;
166 int w = width(bv, font) - 2;
167 int h = (ascent(bv, font) + descent(bv, font) - 2);
170 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
171 pain.rectangle(int(x), y, w, h, LColor::mathframe);
173 string s(_("Macro: "));
175 pain.text(int(x + 2), baseline, s, font);
176 x += width(bv, font) - 1;
181 string const InsetFormulaMacro::EditMessage() const
183 return _("Math macro editor mode");
187 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
190 par = static_cast<MathParInset*>(tmacro->Clone());
191 InsetFormula::Edit(bv, x, y, button);
195 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
198 MathedArray * tarray = tmacro->GetData();
199 MathedIter it(tarray);
201 tmacro->SetData(par->GetData());
202 tmacro->setEditMode(false);
203 InsetFormula::InsetUnlock(bv);
207 UpdatableInset::RESULT
208 InsetFormulaMacro::LocalDispatch(BufferView * bv,
209 int action, string const & arg)
211 if (action == LFUN_MATH_MACROARG) {
212 int i = lyx::atoi(arg) - 1;
213 if (i >= 0 && i < tmacro->getNoArgs()) {
214 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
215 InsetFormula::UpdateLocal(bv);
220 tmacro->setEditMode(true);
222 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
223 tmacro->setEditMode(false);