]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
d971ac8b2bfa18b9fd125275a5b6b3cd57de65a1
[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 "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);
70 }
71
72
73 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/) const
74 {
75     int ret = 1;
76     tmacro->WriteDef(os);
77     return ret;
78 }
79
80
81 #ifndef USE_OSTREAM_ONLY
82 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
83 {
84     int ret = 1;
85     tmacro->WriteDef(file);
86     return ret;
87 }
88 #endif
89
90
91 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
92 {
93     return 0;
94 }
95
96
97 int InsetFormulaMacro::DocBook(string &/*file*/) const
98 {
99     return 0;
100 }
101
102
103 void InsetFormulaMacro::Read(LyXLex & lex)
104 {
105         istream & is = lex.getStream();
106         mathed_parser_file(is, lex.GetLineNo());   
107         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
108     
109         // Update line number
110         lex.setLineNo(mathed_parser_lineno());
111         
112         MathMacroTable::mathMTable.addTemplate(tmacro);
113         name = tmacro->GetName();
114         par = tmacro;
115 }
116
117
118 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
119 {
120     if (opened) {
121         tmacro->update();
122         return InsetFormula::ascent(pain, f);
123     }
124     return f.maxAscent()+3;
125 }
126
127
128 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
129 {
130     if (opened) {
131         tmacro->update();
132         return InsetFormula::descent(pain, f);
133     }
134     return f.maxDescent()+1;
135 }
136
137
138 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
139 {
140     if (opened) {
141         tmacro->update();
142         return InsetFormula::width(pain, f);
143     }
144     string ilabel(_("Macro: "));
145     ilabel += name;
146     return 6 + f.stringWidth(ilabel);
147 }
148
149
150 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
151                              int baseline, float & x) const
152 {
153         LyXFont font(f);
154         tmacro->update();
155         if (opened) {
156                 tmacro->setEditMode(true);
157                 InsetFormula::draw(pain, font, baseline, x);
158                 tmacro->setEditMode(false);     
159         } else {
160                 font.setColor(LColor::math);
161                 
162                 int y = baseline - ascent(pain, font) + 1;
163                 int w = width(pain, font) - 2;
164                 int h = (ascent(pain, font) + descent(pain, font) - 2);
165
166         
167                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
168                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
169                 
170                 string s(_("Macro: "));
171                 s += name;
172                 pain.text(int(x + 2), baseline, s, font);
173                 x +=  width(pain, font) - 1;
174         }
175 }
176
177
178 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
179 {
180     opened = true;
181     par = static_cast<MathParInset*>(tmacro->Clone());
182     InsetFormula::Edit(bv, x, y, button);
183 }
184
185                
186 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
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(bv);
195 }
196
197
198 UpdatableInset::RESULT
199 InsetFormulaMacro::LocalDispatch(BufferView * bv,
200                                  int action, string const & arg)
201 {
202     if (action == LFUN_MATH_MACROARG) {
203         int i = atoi(arg.c_str()) - 1;
204         if (i >= 0 && i < tmacro->getNoArgs()) {
205             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
206             InsetFormula::UpdateLocal(bv);
207         }
208         
209         return DISPATCHED;
210     }
211     tmacro->setEditMode(true);
212     tmacro->Metrics();
213     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
214     tmacro->setEditMode(false);
215     
216     return result;
217 }