]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
more mathed cleanup
[features.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 #include <cstdlib>
18
19 #ifdef __GNUG__
20 #pragma implementation "formulamacro.h"
21 #endif
22
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"
30 #include "lyx_main.h"
31 #include "BufferView.h"
32 #include "gettext.h"
33 #include "Painter.h"
34 #include "font.h"
35 #include "support/lyxlib.h"
36 #include "mathed/support.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         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
105     
106         // Update line number
107         lex.setLineNo(mathed_parser_lineno());
108         
109         MathMacroTable::mathMTable.addTemplate(tmacro);
110         name = tmacro->GetName();
111         par = tmacro;
112         // reading of end_inset in the inset!!!
113         while (lex.IsOK()) {
114                 lex.nextToken();
115                 if (lex.GetString() == "\\end_inset")
116                         break;
117         }
118 }
119
120
121 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
122 {
123     if (opened) {
124         tmacro->update();
125         return InsetFormula::ascent(pain, f);
126     }
127     return lyxfont::maxAscent(f) + 3;
128 }
129
130
131 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
132 {
133     if (opened) {
134         tmacro->update();
135         return InsetFormula::descent(pain, f);
136     }
137     return lyxfont::maxDescent(f) + 1;
138 }
139
140
141 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
142 {
143     if (opened) {
144         tmacro->update();
145         return InsetFormula::width(bv, f);
146     }
147     string ilabel(_("Macro: "));
148     ilabel += name;
149     return 6 + lyxfont::width(ilabel, f);
150 }
151
152
153 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
154                              int baseline, float & x, bool cleared) const
155 {
156         Painter & pain = bv->painter();
157         LyXFont font(f);
158         tmacro->update();
159         if (opened) {
160                 tmacro->setEditMode(true);
161                 InsetFormula::draw(bv, font, baseline, x, cleared);
162                 tmacro->setEditMode(false);     
163         } else {
164                 font.setColor(LColor::math);
165                 
166                 int y = baseline - ascent(bv, font) + 1;
167                 int w = width(bv, font) - 2;
168                 int h = (ascent(bv, font) + descent(bv, font) - 2);
169
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     MathedArray * tarray = tmacro->GetData();
200     MathedIter it(tarray);
201     it.Clear();
202     tmacro->SetData(par->GetData());
203     tmacro->setEditMode(false);
204     InsetFormula::InsetUnlock(bv);
205 }
206
207
208 UpdatableInset::RESULT
209 InsetFormulaMacro::LocalDispatch(BufferView * bv,
210                                  int action, string const & arg)
211 {
212     if (action == LFUN_MATH_MACROARG) {
213         int i = lyx::atoi(arg) - 1;
214         if (i >= 0 && i < tmacro->getNoArgs()) {
215             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
216             InsetFormula::UpdateLocal(bv);
217         }
218         
219         return DISPATCHED;
220     }
221     tmacro->setEditMode(true);
222     tmacro->Metrics();
223     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
224     tmacro->setEditMode(false);
225     
226     return result;
227 }