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_macroarg.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"
43 extern MathedCursor * mathcursor;
45 InsetFormulaMacro::InsetFormulaMacro()
48 par = &MathMacroTable::provideTemplate("unknown", 0);
52 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
55 par = &MathMacroTable::provideTemplate(nm, na);
59 Inset * InsetFormulaMacro::Clone(Buffer const &) const
61 return new InsetFormulaMacro(*this);
65 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
67 os << "FormulaMacro ";
68 tmacro()->WriteDef(os, false);
72 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
73 bool /*free_spacing*/) const
75 tmacro()->WriteDef(os, true); // or false?
80 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
82 return Ascii(buf, os, 0);
86 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
88 return Ascii(buf, os, 0);
92 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
94 istream & is = lex.getStream();
95 mathed_parser_file(is, lex.GetLineNo());
97 MathParInset * tmp = new MathParInset;
99 mathed_parse(ar, tmp, 0);
100 par = &MathMacroTable::provideTemplate(tmp->GetName(), tmp->xo());
102 //cerr << "## InsetFormulaMacro::Read name: " << tmp->GetName() << endl;
103 //cerr << "## InsetFormulaMacro::Read nargs: " << tmp->xo() << endl;
104 //cerr << "## InsetFormulaMacro::Read 1: " << ar << endl;
106 // Update line number
107 lex.setLineNo(mathed_parser_lineno());
109 // reading of end_inset in the inset!!!
112 if (lex.GetString() == "\\end_inset")
117 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
119 return InsetFormula::ascent(pain, f);
123 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
125 return InsetFormula::descent(pain, f);
129 string InsetFormulaMacro::prefix() const
131 return string(" ") + _("Macro: ") + par->GetName() + ": ";
134 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
136 return 10 + lyxfont::width(prefix(), f) + InsetFormula::width(bv, f);
140 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
141 int baseline, float & x, bool cleared) const
143 Painter & pain = bv->painter();
147 font.setColor(LColor::math);
149 int const y = baseline - ascent(bv, font) + 1;
150 int const w = width(bv, font) - 2;
151 int const h = (ascent(bv, font) + descent(bv, font) - 2);
153 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
154 pain.rectangle(int(x), y, w, h, LColor::mathframe);
156 pain.text(int(x + 2), baseline, prefix(), font);
157 x += width(bv, font);
160 float t = InsetFormula::width(bv, f) + 5;
162 InsetFormula::draw(bv, font, baseline, x, cleared);
167 string const InsetFormulaMacro::EditMessage() const
169 return _("Math macro editor mode");
173 UpdatableInset::RESULT
174 InsetFormulaMacro::LocalDispatch(BufferView * bv,
175 kb_action action, string const & arg)
177 if (action == LFUN_MATH_MACROARG) {
178 int const i = lyx::atoi(arg);
179 if (i > 0 && i <= tmacro()->nargs()) {
180 mathcursor->insertInset(new MathMacroArgument(i), LM_TC_INSET);
181 InsetFormula::UpdateLocal(bv);
187 return InsetFormula::LocalDispatch(bv, action, arg);
190 MathMacroTemplate * InsetFormulaMacro::tmacro() const
192 return static_cast<MathMacroTemplate *>(par);