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"
42 InsetFormulaMacro::InsetFormulaMacro()
50 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
51 : InsetFormula(true), name_(nm)
53 tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
55 tmacro_ = new MathMacroTemplate(name_, 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());
107 mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
109 tmacro_->setData(ar);
111 // Update line number
112 lex.setLineNo(mathed_parser_lineno());
114 MathMacroTable::mathMTable.addTemplate(tmacro_);
115 name_ = tmacro_->GetName();
118 // reading of end_inset in the inset!!!
121 if (lex.GetString() == "\\end_inset")
127 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
131 return InsetFormula::ascent(pain, f);
133 return lyxfont::maxAscent(f) + 3;
137 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
141 return InsetFormula::descent(pain, f);
143 return lyxfont::maxDescent(f) + 1;
147 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
151 return InsetFormula::width(bv, f);
153 string ilabel(_("Macro: "));
155 return 6 + lyxfont::width(ilabel, f);
159 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
160 int baseline, float & x, bool cleared) const
162 Painter & pain = bv->painter();
166 tmacro_->setEditMode(true);
167 InsetFormula::draw(bv, font, baseline, x, cleared);
168 tmacro_->setEditMode(false);
170 font.setColor(LColor::math);
172 int const y = baseline - ascent(bv, font) + 1;
173 int const w = width(bv, font) - 2;
174 int const h = (ascent(bv, font) + descent(bv, font) - 2);
176 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
177 pain.rectangle(int(x), y, w, h, LColor::mathframe);
179 string s(_("Macro: "));
181 pain.text(int(x + 2), baseline, s, font);
182 x += width(bv, font) - 1;
187 string const InsetFormulaMacro::EditMessage() const
189 return _("Math macro editor mode");
193 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
196 par = static_cast<MathParInset*>(tmacro_->Clone());
197 InsetFormula::Edit(bv, x, y, button);
201 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
204 tmacro_->setData(par->GetData());
205 tmacro_->setEditMode(false);
206 InsetFormula::InsetUnlock(bv);
210 UpdatableInset::RESULT
211 InsetFormulaMacro::LocalDispatch(BufferView * bv,
212 kb_action action, string const & arg)
214 if (action == LFUN_MATH_MACROARG) {
215 int const i = lyx::atoi(arg) - 1;
216 if (i >= 0 && i < tmacro_->getNoArgs()) {
217 mathcursor->insertInset(tmacro_->getMacroPar(i),
219 InsetFormula::UpdateLocal(bv);
224 tmacro_->setEditMode(true);
226 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
227 tmacro_->setEditMode(false);