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