]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
5082e24c21a2e83aad30b6bb617997a2c435b6c1
[lyx.git] / src / mathed / formulamacro.C
1 /*
2  *  File:        formula.h
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. 
7  *
8  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
9  *
10  *  Version: 0.4, Lyx project.
11  *
12  *   You are free to use and modify this code under the terms of
13  *   the GNU General Public Licence version 2 or later.
14  */
15
16 #include <config.h>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
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"
29 #include "lyx_main.h"
30 #include "BufferView.h"
31 #include "gettext.h"
32 #include "Painter.h"
33 #include "font.h"
34 #include "support/lyxlib.h"
35 #include "mathed/support.h"
36 #include "support/LOstream.h"
37
38 using std::ostream;
39 using std::istream;
40
41 InsetFormulaMacro::InsetFormulaMacro()
42         : InsetFormula(true)
43 {
44         tmacro = 0;
45         opened = false;
46 }
47
48
49 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
50         : InsetFormula(true), name(nm)
51 {
52         tmacro = MathMacroTable::mathMTable.getTemplate(name);
53         if (!tmacro) {
54                 tmacro = new MathMacroTemplate(name.c_str(), na);
55                 MathMacroTable::mathMTable.addTemplate(tmacro);
56         }
57         opened = false;
58 }
59
60
61 InsetFormulaMacro::~InsetFormulaMacro()
62 {
63         par = 0;
64 }
65
66
67 Inset * InsetFormulaMacro::Clone(Buffer const &) const
68 {
69         return new InsetFormulaMacro(name);
70 }
71
72
73 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
74 {
75         os << "FormulaMacro ";
76         tmacro->WriteDef(os, false);
77 }
78
79
80 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
81                              bool /*free_spacing*/) const
82 {
83         tmacro->WriteDef(os, true); // or false?
84         return 2;
85 }
86
87
88 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
89 {
90         return Ascii(buf, os, 0);
91 }
92
93
94 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
95 {
96         return Ascii(buf, os, 0);
97 }
98
99
100 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
101 {
102         istream & is = lex.getStream();
103         mathed_parser_file(is, lex.GetLineNo());   
104         MathedArray ar;
105         mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro));
106     
107         // Update line number
108         lex.setLineNo(mathed_parser_lineno());
109         
110         MathMacroTable::mathMTable.addTemplate(tmacro);
111         name = tmacro->GetName();
112         par = tmacro;
113         // reading of end_inset in the inset!!!
114         while (lex.IsOK()) {
115                 lex.nextToken();
116                 if (lex.GetString() == "\\end_inset")
117                         break;
118         }
119 }
120
121
122 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
123 {
124         if (opened) {
125                 tmacro->update();
126                 return InsetFormula::ascent(pain, f);
127         }
128         return lyxfont::maxAscent(f) + 3;
129 }
130
131
132 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
133 {
134         if (opened) {
135                 tmacro->update();
136                 return InsetFormula::descent(pain, f);
137         }
138         return lyxfont::maxDescent(f) + 1;
139 }
140
141
142 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
143 {
144         if (opened) {
145                 tmacro->update();
146                 return InsetFormula::width(bv, f);
147         }
148         string ilabel(_("Macro: "));
149         ilabel += name;
150         return 6 + lyxfont::width(ilabel, f);
151 }
152
153
154 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
155                              int baseline, float & x, bool cleared) const
156 {
157         Painter & pain = bv->painter();
158         LyXFont font(f);
159         tmacro->update();
160         if (opened) {
161                 tmacro->setEditMode(true);
162                 InsetFormula::draw(bv, font, baseline, x, cleared);
163                 tmacro->setEditMode(false);     
164         } else {
165                 font.setColor(LColor::math);
166                 
167                 int const y = baseline - ascent(bv, font) + 1;
168                 int const w = width(bv, font) - 2;
169                 int const h = (ascent(bv, font) + descent(bv, font) - 2);
170         
171                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
172                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
173                 
174                 string s(_("Macro: "));
175                 s += name;
176                 pain.text(int(x + 2), baseline, s, font);
177                 x +=  width(bv, font) - 1;
178         }
179 }
180
181
182 string const InsetFormulaMacro::EditMessage() const 
183 {
184         return _("Math macro editor mode");
185 }
186
187
188 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
189 {
190         opened = true;
191         par = static_cast<MathParInset*>(tmacro->Clone());
192         InsetFormula::Edit(bv, x, y, button);
193 }
194
195                
196 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
197 {
198         opened = false;
199         tmacro->setData(par->GetData());
200         tmacro->setEditMode(false);
201         InsetFormula::InsetUnlock(bv);
202 }
203
204
205 UpdatableInset::RESULT
206 InsetFormulaMacro::LocalDispatch(BufferView * bv,
207                                  int action, string const & arg)
208 {
209         if (action == LFUN_MATH_MACROARG) {
210                 int i = lyx::atoi(arg) - 1;
211                 if (i >= 0 && i < tmacro->getNoArgs()) {
212                         mathcursor->insertInset(tmacro->getMacroPar(i),
213                                                  LM_TC_INSET);
214                         InsetFormula::UpdateLocal(bv);
215                 }
216         
217                 return DISPATCHED;
218         }
219         tmacro->setEditMode(true);
220         tmacro->Metrics();
221         RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
222         tmacro->setEditMode(false);
223     
224         return result;
225 }