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