]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
first go at mathed file cleanup
[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 #include "support/lyxlib.h"
34
35 using std::ostream;
36 using std::istream;
37
38 InsetFormulaMacro::InsetFormulaMacro()
39        : InsetFormula(true)
40 {
41     tmacro = 0;
42     opened = false;
43 }
44
45
46 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
47         : InsetFormula(true), name(nm)
48 {
49     tmacro = MathMacroTable::mathMTable.getTemplate(name);
50     if (!tmacro) {
51         tmacro = new MathMacroTemplate(name.c_str(), na);
52         MathMacroTable::mathMTable.addTemplate(tmacro);
53     }
54     opened = false;
55 }
56
57
58 InsetFormulaMacro::~InsetFormulaMacro()
59 {
60     par = 0;
61 }
62
63
64 Inset * InsetFormulaMacro::Clone(Buffer const &) const
65 {
66    return new InsetFormulaMacro(name);
67 }
68
69
70 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
71 {
72         os << "FormulaMacro ";
73         tmacro->WriteDef(os, false);
74 }
75
76
77 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
78                              bool /*free_spacing*/) const
79 {
80         tmacro->WriteDef(os, true); // or false?
81         return 2;
82 }
83
84
85 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
86 {
87     return Ascii(buf, os, 0);
88 }
89
90
91 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
92 {
93     return Ascii(buf, os, 0);
94 }
95
96
97 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
98 {
99         istream & is = lex.getStream();
100         mathed_parser_file(is, lex.GetLineNo());   
101         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
102     
103         // Update line number
104         lex.setLineNo(mathed_parser_lineno());
105         
106         MathMacroTable::mathMTable.addTemplate(tmacro);
107         name = tmacro->GetName();
108         par = tmacro;
109         // reading of end_inset in the inset!!!
110         while (lex.IsOK()) {
111                 lex.nextToken();
112                 if (lex.GetString() == "\\end_inset")
113                         break;
114         }
115 }
116
117
118 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
119 {
120     if (opened) {
121         tmacro->update();
122         return InsetFormula::ascent(pain, f);
123     }
124     return lyxfont::maxAscent(f) + 3;
125 }
126
127
128 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
129 {
130     if (opened) {
131         tmacro->update();
132         return InsetFormula::descent(pain, f);
133     }
134     return lyxfont::maxDescent(f) + 1;
135 }
136
137
138 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
139 {
140     if (opened) {
141         tmacro->update();
142         return InsetFormula::width(bv, f);
143     }
144     string ilabel(_("Macro: "));
145     ilabel += name;
146     return 6 + lyxfont::width(ilabel, f);
147 }
148
149
150 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
151                              int baseline, float & x, bool cleared) const
152 {
153         Painter & pain = bv->painter();
154         LyXFont font(f);
155         tmacro->update();
156         if (opened) {
157                 tmacro->setEditMode(true);
158                 InsetFormula::draw(bv, font, baseline, x, cleared);
159                 tmacro->setEditMode(false);     
160         } else {
161                 font.setColor(LColor::math);
162                 
163                 int y = baseline - ascent(bv, font) + 1;
164                 int w = width(bv, font) - 2;
165                 int h = (ascent(bv, font) + descent(bv, font) - 2);
166
167         
168                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
169                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
170                 
171                 string s(_("Macro: "));
172                 s += name;
173                 pain.text(int(x + 2), baseline, s, font);
174                 x +=  width(bv, font) - 1;
175         }
176 }
177
178
179 string const InsetFormulaMacro::EditMessage() const 
180 {
181         return _("Math macro editor mode");
182 }
183
184
185 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
186 {
187     opened = true;
188     par = static_cast<MathParInset*>(tmacro->Clone());
189     InsetFormula::Edit(bv, x, y, button);
190 }
191
192                
193 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
194 {
195     opened = false;
196     MathedArray * tarray = tmacro->GetData();
197     MathedIter it(tarray);
198     it.Clear();
199     tmacro->SetData(par->GetData());
200     tmacro->setEditMode(false);
201     InsetFormula::InsetUnlock(bv);
202 }
203
204
205 UpdatableInset::RESULT
206 InsetFormulaMacro::LocalDispatch(BufferView * bv,
207                                  int action, string const & arg)
208 {
209     if (action == LFUN_MATH_MACROARG) {
210         int i = lyx::atoi(arg) - 1;
211         if (i >= 0 && i < tmacro->getNoArgs()) {
212             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
213             InsetFormula::UpdateLocal(bv);
214         }
215         
216         return DISPATCHED;
217     }
218     tmacro->setEditMode(true);
219     tmacro->Metrics();
220     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
221     tmacro->setEditMode(false);
222     
223     return result;
224 }