]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
some small updates to Painter, and make the new painter the default.
[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: (c) 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 "lyx_cb.h"
30 #include "BufferView.h"
31 #include "lyxscreen.h"
32 #ifndef USE_PAINTER
33 #include "lyxdraw.h"
34 #endif
35 #include "gettext.h"
36 #include "Painter.h"
37
38
39 InsetFormulaMacro::InsetFormulaMacro()
40        : InsetFormula(true)
41 {
42     tmacro = 0;
43     opened = false;
44 }
45
46
47 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
48         : InsetFormula(true), name(nm)
49 {
50     tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
51     if (!tmacro) {
52         tmacro = new MathMacroTemplate(name.c_str(), na);
53         MathMacroTable::mathMTable.addTemplate(tmacro);
54     }
55     opened = false;
56 }
57
58
59 InsetFormulaMacro::~InsetFormulaMacro()
60 {
61     par = 0;
62 }
63
64
65 Inset * InsetFormulaMacro::Clone() const
66 {
67    return new InsetFormulaMacro(name);
68 }
69
70
71 void InsetFormulaMacro::Write(ostream & os)
72 {
73         os << "FormulaMacro ";
74         Latex(os, 0);
75 }
76
77
78 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/)
79 {
80     int ret = 1;
81     tmacro->WriteDef(os);
82     return ret;
83 }
84
85
86 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
87 {
88     int ret = 1;
89     tmacro->WriteDef(file);
90     return ret;
91 }
92
93
94 int InsetFormulaMacro::Linuxdoc(string &/*file*/)
95 {
96     return 0;
97 }
98
99
100 int InsetFormulaMacro::DocBook(string &/*file*/)
101 {
102     return 0;
103 }
104
105
106 void InsetFormulaMacro::Read(LyXLex & lex)
107 {
108         istream & is = lex.getStream();
109         mathed_parser_file(is, lex.GetLineNo());   
110         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
111     
112         // Update line number
113         lex.setLineNo(mathed_parser_lineno());
114         
115         MathMacroTable::mathMTable.addTemplate(tmacro);
116         name = tmacro->GetName();
117         par = tmacro;
118 }
119
120
121 #ifdef USE_PAINTER
122 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
123 {
124     if (opened) {
125         tmacro->update();
126         return InsetFormula::ascent(pain, f);
127     }
128     return f.maxAscent()+3;
129 }
130 #else
131 int InsetFormulaMacro::Ascent(LyXFont const & f) const
132 {
133     if (opened) {
134         tmacro->update();
135         return InsetFormula::Ascent(f);
136     }
137     return f.maxAscent()+3;
138 }
139 #endif
140
141
142 #ifdef USE_PAINTER
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 #else
152 int InsetFormulaMacro::Descent(LyXFont const & f) const
153 {
154     if (opened) {
155         tmacro->update();
156         return InsetFormula::Descent(f);
157     }
158     return f.maxDescent()+1;
159 }
160 #endif
161
162
163 #ifdef USE_PAINTER
164 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
165 {
166     if (opened) {
167         tmacro->update();
168         return InsetFormula::width(pain, f);
169     }
170     string ilabel(_("Macro: "));
171     ilabel += name;
172     return 6 + f.stringWidth(ilabel);
173 }
174 #else
175 int InsetFormulaMacro::Width(LyXFont const & f) const
176 {
177     if (opened) {
178         tmacro->update();
179         return InsetFormula::Width(f);
180     }
181     string ilabel(_("Macro: "));
182     ilabel += name;
183     return 6 + f.stringWidth(ilabel);
184 }
185 #endif
186
187
188 #ifdef USE_PAINTER
189 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
190                              int baseline, float & x) const
191 {
192         LyXFont font(f);
193         tmacro->update();
194         if (opened) {
195                 tmacro->setEditMode(true);
196                 InsetFormula::draw(pain, font, baseline, x);
197                 tmacro->setEditMode(false);     
198         } else {
199                 font.setColor(LColor::math);
200                 
201                 int y = baseline - ascent(pain, font) + 1;
202                 int w = width(pain, font) - 2;
203                 int h = (ascent(pain, font) + descent(pain, font) - 2);
204
205         
206                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
207                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
208                 
209                 string s(_("Macro: "));
210                 s += name;
211                 pain.text(int(x + 2), baseline, s, font);
212                 x +=  width(pain, font) - 1;
213         }
214 }
215 #else
216 void InsetFormulaMacro::Draw(LyXFont font, LyXScreen & scr,
217                              int baseline, float & x)
218 {
219     tmacro->update();
220     if (opened) {
221         tmacro->setEditMode(true);
222         InsetFormula::Draw(font, scr, baseline, x);
223         tmacro->setEditMode(false);     
224     } else {
225         font.setColor(LyXFont::MATH);
226         
227         int y = baseline - Ascent(font) + 1;
228         int w = Width(font) - 2, h = (Ascent(font) + Descent(font) - 2);
229
230         
231         scr.fillRectangle(gc_lighted, int(x), y,  w,  h);
232         scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1); 
233         
234         string s(_("Macro: "));
235         s += name;
236         scr.drawString(font, s, baseline, int(x +2));
237         x +=  Width(font) - 1;
238     }
239 }
240 #endif
241
242
243 void InsetFormulaMacro::Edit(int x, int y)
244 {
245     opened = true;
246     par = static_cast<MathParInset*>(tmacro->Clone());
247     InsetFormula::Edit(x, y);
248 }
249
250                
251 void InsetFormulaMacro::InsetUnlock()
252 {
253     opened = false;
254     LyxArrayBase * tarray = tmacro->GetData();
255     MathedIter it(tarray);
256     it.Clear();
257     tmacro->SetData(par->GetData());
258     tmacro->setEditMode(false);
259     InsetFormula::InsetUnlock();
260 }
261
262
263 bool InsetFormulaMacro::LocalDispatch(int action, char const * arg)
264 {
265     if (action == LFUN_MATH_MACROARG) {
266         int i = atoi(arg) - 1;
267         if (i >= 0 && i < tmacro->getNoArgs()) {
268             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
269             InsetFormula::UpdateLocal();
270         }
271         
272         return true;
273     }
274     tmacro->setEditMode(true);
275     tmacro->Metrics();
276     bool result = InsetFormula::LocalDispatch(action, arg);
277     tmacro->setEditMode(false);
278     
279     return result;
280 }