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()
49 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
50 : InsetFormula(true), name_(nm)
52 tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
54 tmacro_.reset(new MathMacroTemplate(name_, na));
55 MathMacroTable::mathMTable.addTemplate(tmacro_);
61 InsetFormulaMacro::~InsetFormulaMacro()
63 // We do not want the InsetFormula destructor to
64 // delete this. That is taken care of elsewhere (Lgb)
69 Inset * InsetFormulaMacro::Clone(Buffer const &) const
71 // This should really use a proper copy constructor
72 return new InsetFormulaMacro(name_, 0);
76 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
78 os << "FormulaMacro ";
79 tmacro_->WriteDef(os, false);
83 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
84 bool /*free_spacing*/) const
86 tmacro_->WriteDef(os, true); // or false?
91 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
93 return Ascii(buf, os, 0);
97 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
99 return Ascii(buf, os, 0);
103 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
105 istream & is = lex.getStream();
106 mathed_parser_file(is, lex.GetLineNo());
109 mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
110 // Since tmacro_ == 0 when mathed_parse is called we need to set
111 // its contents explicitly afterwards (Lgb)
112 tmacro_->setData(ar);
114 // Update line number
115 lex.setLineNo(mathed_parser_lineno());
117 MathMacroTable::mathMTable.addTemplate(tmacro_);
118 name_ = tmacro_->GetName();
121 // reading of end_inset in the inset!!!
124 if (lex.GetString() == "\\end_inset")
130 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
133 return InsetFormula::ascent(pain, f);
135 return lyxfont::maxAscent(f) + 3;
139 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
142 return InsetFormula::descent(pain, f);
144 return lyxfont::maxDescent(f) + 1;
148 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();
165 tmacro_->setEditMode(true);
166 InsetFormula::draw(bv, font, baseline, x, cleared);
167 tmacro_->setEditMode(false);
169 font.setColor(LColor::math);
171 int const y = baseline - ascent(bv, font) + 1;
172 int const w = width(bv, font) - 2;
173 int const h = (ascent(bv, font) + descent(bv, font) - 2);
175 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
176 pain.rectangle(int(x), y, w, h, LColor::mathframe);
178 string s(_("Macro: "));
180 pain.text(int(x + 2), baseline, s, font);
181 x += width(bv, font) - 1;
186 string const InsetFormulaMacro::EditMessage() const
188 return _("Math macro editor mode");
192 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
195 par = static_cast<MathParInset*>(tmacro_->Clone());
196 InsetFormula::Edit(bv, x, y, button);
200 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
203 tmacro_->setData(par->GetData());
204 tmacro_->setEditMode(false);
205 InsetFormula::InsetUnlock(bv);
209 UpdatableInset::RESULT
210 InsetFormulaMacro::LocalDispatch(BufferView * bv,
211 kb_action action, string const & arg)
213 if (action == LFUN_MATH_MACROARG) {
214 int const i = lyx::atoi(arg) - 1;
215 if (i >= 0 && i < tmacro_->getNoArgs()) {
216 mathcursor->insertInset(tmacro_->getMacroPar(i),
218 InsetFormula::UpdateLocal(bv);
223 tmacro_->setEditMode(true);
225 RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
226 tmacro_->setEditMode(false);