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.
19 #pragma implementation
22 #include "formulamacro.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_macro.h"
27 #include "math_macrotable.h"
28 #include "math_macrotemplate.h"
30 #include "BufferView.h"
34 #include "support/lyxlib.h"
35 #include "mathed/support.h"
36 #include "support/LOstream.h"
41 InsetFormulaMacro::InsetFormulaMacro()
49 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
50 : InsetFormula(true), name(nm)
52 tmacro = MathMacroTable::mathMTable.getTemplate(name);
54 tmacro = new MathMacroTemplate(name.c_str(), na);
55 MathMacroTable::mathMTable.addTemplate(tmacro);
61 InsetFormulaMacro::~InsetFormulaMacro()
67 Inset * InsetFormulaMacro::Clone(Buffer const &) const
69 return new InsetFormulaMacro(name);
73 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
75 os << "FormulaMacro ";
76 tmacro->WriteDef(os, false);
80 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
81 bool /*free_spacing*/) const
83 tmacro->WriteDef(os, true); // or false?
88 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
90 return Ascii(buf, os, 0);
94 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
96 return Ascii(buf, os, 0);
100 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
102 istream & is = lex.getStream();
103 mathed_parser_file(is, lex.GetLineNo());
104 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
106 // Update line number
107 lex.setLineNo(mathed_parser_lineno());
109 MathMacroTable::mathMTable.addTemplate(tmacro);
110 name = tmacro->GetName();
112 // reading of end_inset in the inset!!!
115 if (lex.GetString() == "\\end_inset")
121 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
125 return InsetFormula::ascent(pain, f);
127 return lyxfont::maxAscent(f) + 3;
131 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
135 return InsetFormula::descent(pain, f);
137 return lyxfont::maxDescent(f) + 1;
141 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
145 return InsetFormula::width(bv, f);
147 string ilabel(_("Macro: "));
149 return 6 + lyxfont::width(ilabel, f);
153 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
154 int baseline, float & x, bool cleared) const
156 Painter & pain = bv->painter();
160 tmacro->setEditMode(true);
161 InsetFormula::draw(bv, font, baseline, x, cleared);
162 tmacro->setEditMode(false);
164 font.setColor(LColor::math);
166 int const y = baseline - ascent(bv, font) + 1;
167 int const w = width(bv, font) - 2;
168 int const 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->insertInset(tmacro->getMacroPar(i),
216 InsetFormula::UpdateLocal(bv);
221 tmacro->setEditMode(true);
223 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
224 tmacro->setEditMode(false);