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