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"
36 #include "mathed/support.h"
37 #include "support/LOstream.h"
42 InsetFormulaMacro::InsetFormulaMacro()
50 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
51 : InsetFormula(true), name(nm)
53 tmacro = MathMacroTable::mathMTable.getTemplate(name);
55 tmacro = new MathMacroTemplate(name.c_str(), na);
56 MathMacroTable::mathMTable.addTemplate(tmacro);
62 InsetFormulaMacro::~InsetFormulaMacro()
68 Inset * InsetFormulaMacro::Clone(Buffer const &) const
70 return new InsetFormulaMacro(name);
74 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
76 os << "FormulaMacro ";
77 tmacro->WriteDef(os, false);
81 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
82 bool /*free_spacing*/) const
84 tmacro->WriteDef(os, true); // or false?
89 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
91 return Ascii(buf, os, 0);
95 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
97 return Ascii(buf, os, 0);
101 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
103 istream & is = lex.getStream();
104 mathed_parser_file(is, lex.GetLineNo());
105 mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
107 // Update line number
108 lex.setLineNo(mathed_parser_lineno());
110 MathMacroTable::mathMTable.addTemplate(tmacro);
111 name = tmacro->GetName();
113 // reading of end_inset in the inset!!!
116 if (lex.GetString() == "\\end_inset")
122 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
126 return InsetFormula::ascent(pain, f);
128 return lyxfont::maxAscent(f) + 3;
132 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
136 return InsetFormula::descent(pain, f);
138 return lyxfont::maxDescent(f) + 1;
142 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
146 return InsetFormula::width(bv, f);
148 string ilabel(_("Macro: "));
150 return 6 + lyxfont::width(ilabel, f);
154 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
155 int baseline, float & x, bool cleared) const
157 Painter & pain = bv->painter();
161 tmacro->setEditMode(true);
162 InsetFormula::draw(bv, font, baseline, x, cleared);
163 tmacro->setEditMode(false);
165 font.setColor(LColor::math);
167 int y = baseline - ascent(bv, font) + 1;
168 int w = width(bv, font) - 2;
169 int h = (ascent(bv, font) + descent(bv, font) - 2);
172 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
173 pain.rectangle(int(x), y, w, h, LColor::mathframe);
175 string s(_("Macro: "));
177 pain.text(int(x + 2), baseline, s, font);
178 x += width(bv, font) - 1;
183 string const InsetFormulaMacro::EditMessage() const
185 return _("Math macro editor mode");
189 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
192 par = static_cast<MathParInset*>(tmacro->Clone());
193 InsetFormula::Edit(bv, x, y, button);
197 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
200 MathedArray * tarray = tmacro->GetData();
201 MathedIter it(tarray);
203 tmacro->SetData(par->GetData());
204 tmacro->setEditMode(false);
205 InsetFormula::InsetUnlock(bv);
209 UpdatableInset::RESULT
210 InsetFormulaMacro::LocalDispatch(BufferView * bv,
211 int action, string const & arg)
213 if (action == LFUN_MATH_MACROARG) {
214 int i = lyx::atoi(arg) - 1;
215 if (i >= 0 && i < tmacro->getNoArgs()) {
216 mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
217 InsetFormula::UpdateLocal(bv);
222 tmacro->setEditMode(true);
224 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
225 tmacro->setEditMode(false);