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