]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
6f7e06bd8b3910d59d42a8b8369a2043561ff764
[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(Buffer const *, ostream & os) const
70 {
71         os << "FormulaMacro ";
72         tmacro->WriteDef(os, false);
73 }
74
75
76 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
77                              bool /*free_spacing*/) const
78 {
79         tmacro->WriteDef(os, true); // or false?
80         return 1;
81 }
82
83
84 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
85 {
86     return Ascii(buf, os);
87 }
88
89
90 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
91 {
92     return Ascii(buf, os);
93 }
94
95
96 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
97 {
98         istream & is = lex.getStream();
99         mathed_parser_file(is, lex.GetLineNo());   
100         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
101     
102         // Update line number
103         lex.setLineNo(mathed_parser_lineno());
104         
105         MathMacroTable::mathMTable.addTemplate(tmacro);
106         name = tmacro->GetName();
107         par = tmacro;
108         // reading of end_inset in the inset!!!
109         while (lex.IsOK()) {
110                 lex.nextToken();
111                 if (lex.GetString() == "\\end_inset")
112                         break;
113         }
114 }
115
116
117 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
118 {
119     if (opened) {
120         tmacro->update();
121         return InsetFormula::ascent(pain, f);
122     }
123     return lyxfont::maxAscent(f) + 3;
124 }
125
126
127 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
128 {
129     if (opened) {
130         tmacro->update();
131         return InsetFormula::descent(pain, f);
132     }
133     return lyxfont::maxDescent(f) + 1;
134 }
135
136
137 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
138 {
139     if (opened) {
140         tmacro->update();
141         return InsetFormula::width(pain, f);
142     }
143     string ilabel(_("Macro: "));
144     ilabel += name;
145     return 6 + lyxfont::width(ilabel, f);
146 }
147
148
149 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
150                              int baseline, float & x) const
151 {
152         Painter & pain = bv->painter();
153         LyXFont font(f);
154         tmacro->update();
155         if (opened) {
156                 tmacro->setEditMode(true);
157                 InsetFormula::draw(bv, 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 char const * InsetFormulaMacro::EditMessage() const 
179 {
180         return _("Math macro editor mode");
181 }
182
183
184 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
185 {
186     opened = true;
187     par = static_cast<MathParInset*>(tmacro->Clone());
188     InsetFormula::Edit(bv, x, y, button);
189 }
190
191                
192 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
193 {
194     opened = false;
195     LyxArrayBase * tarray = tmacro->GetData();
196     MathedIter it(tarray);
197     it.Clear();
198     tmacro->SetData(par->GetData());
199     tmacro->setEditMode(false);
200     InsetFormula::InsetUnlock(bv);
201 }
202
203
204 UpdatableInset::RESULT
205 InsetFormulaMacro::LocalDispatch(BufferView * bv,
206                                  int action, string const & arg)
207 {
208     if (action == LFUN_MATH_MACROARG) {
209         int i = atoi(arg.c_str()) - 1;
210         if (i >= 0 && i < tmacro->getNoArgs()) {
211             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
212             InsetFormula::UpdateLocal(bv);
213         }
214         
215         return DISPATCHED;
216     }
217     tmacro->setEditMode(true);
218     tmacro->Metrics();
219     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
220     tmacro->setEditMode(false);
221     
222     return result;
223 }