]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
removed a warning from screen and added CFLAGS in lyx.spec.in.
[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: (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 "bufferlist.h"
30 #include "lyx_cb.h"
31 #include "BufferView.h"
32 #include "lyxscreen.h"
33 #include "lyxdraw.h"
34 #include "gettext.h"
35
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()
64 {
65    InsetFormulaMacro* f = new InsetFormulaMacro(name);
66    return (Inset*)f;
67 }
68
69
70 void InsetFormulaMacro::Write(FILE *file)
71 {
72     fprintf(file, "FormulaMacro ");
73     Latex(file, 0);
74 }
75
76
77 int InsetFormulaMacro::Latex(FILE *file, signed char /*fragile*/)
78 {
79     int ret = 1;
80     tmacro->WriteDef(file);
81     return ret;
82 }
83
84
85 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
86 {
87     int ret = 1;
88     tmacro->WriteDef(file);
89     return ret;
90 }
91
92
93 int InsetFormulaMacro::Linuxdoc(string &/*file*/)
94 {
95     return 0;
96 }
97
98
99 int InsetFormulaMacro::DocBook(string &/*file*/)
100 {
101     return 0;
102 }
103
104
105 void InsetFormulaMacro::Read(LyXLex &lex)
106 {
107     FILE *file = lex.getFile();
108     mathed_parser_file(file, lex.GetLineNo());   
109     mathed_parse(0, 0, (MathParInset **)&tmacro);
110     
111     // Update line number
112     lex.setLineNo(mathed_parser_lineno());
113     
114     MathMacroTable::mathMTable.addTemplate(tmacro);
115     name = tmacro->GetName();
116     par = tmacro;
117 }
118
119
120 int InsetFormulaMacro::Ascent(LyXFont const &f) const
121 {
122     if (opened) {
123         tmacro->update();
124         return InsetFormula::Ascent(f);
125     }
126     return f.maxAscent()+3;
127 }
128
129
130 int InsetFormulaMacro::Descent(LyXFont const &f) const
131 {
132     if (opened) {
133         tmacro->update();
134         return InsetFormula::Descent(f);
135     }
136     return f.maxDescent()+1;
137 }
138
139
140 int InsetFormulaMacro::Width(LyXFont const &f) const
141 {
142     if (opened) {
143         tmacro->update();
144         return InsetFormula::Width(f);
145     }
146     string ilabel(_("Macro: "));
147     ilabel += name;
148     return 6 + f.stringWidth(ilabel);
149 }
150
151
152 void InsetFormulaMacro::Draw(LyXFont font, LyXScreen &scr,
153                              int baseline, float &x)
154 {
155     tmacro->update();
156     if (opened) {
157         tmacro->setEditMode(true);
158         InsetFormula::Draw(font, scr, baseline, x);
159         tmacro->setEditMode(false);     
160     } else {
161         font.setColor(LyXFont::MATH);
162         
163         int y=baseline - Ascent(font)+1;
164         int w=Width(font) - 2, h=(Ascent(font)+Descent(font)-2);
165
166         
167         scr.fillRectangle(gc_lighted, int(x), y,  w,  h);
168         scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1); 
169         
170         string s(_("Macro: "));
171         s += name;
172         scr.drawString(font, s, baseline, int(x +2));
173         x +=  Width(font) - 1;
174     }
175 }
176
177
178 void InsetFormulaMacro::Edit(int x, int y)
179 {
180     opened = true;
181     par = (MathParInset*)tmacro->Clone();
182     InsetFormula::Edit(x, y);
183 }
184
185                
186 void InsetFormulaMacro::InsetUnlock()
187 {
188     opened = false;
189     LyxArrayBase *tarray = tmacro->GetData();
190     MathedIter it(tarray);
191     it.Clear();
192     tmacro->SetData(par->GetData());
193     tmacro->setEditMode(false);
194     InsetFormula::InsetUnlock();
195 }
196
197
198 bool InsetFormulaMacro::LocalDispatch(int action, char const *arg)
199 {
200     if (action==LFUN_MATH_MACROARG) {
201         int i = atoi(arg) - 1;
202         if (i>=0 && i<tmacro->getNoArgs()) {
203             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
204             InsetFormula::UpdateLocal();
205         }
206         
207         return true;
208     }
209     tmacro->setEditMode(true);
210     tmacro->Metrics();
211     bool result = InsetFormula::LocalDispatch(action, arg);
212     tmacro->setEditMode(false);
213     
214     return result;
215 }