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