]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
73af819f5e45f8815660ec44edcf53d8e6c5907f
[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 "math_macrotable.h"
29 #include "math_macrotemplate.h"
30 #include "lyx_main.h"
31 #include "BufferView.h"
32 #include "gettext.h"
33 #include "Painter.h"
34 #include "font.h"
35 #include "support/lyxlib.h"
36
37 using std::ostream;
38 using std::istream;
39
40 InsetFormulaMacro::InsetFormulaMacro()
41        : InsetFormula(true)
42 {
43     tmacro = 0;
44     opened = false;
45 }
46
47
48 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
49         : InsetFormula(true), name(nm)
50 {
51     tmacro = MathMacroTable::mathMTable.getTemplate(name);
52     if (!tmacro) {
53         tmacro = new MathMacroTemplate(name.c_str(), na);
54         MathMacroTable::mathMTable.addTemplate(tmacro);
55     }
56     opened = false;
57 }
58
59
60 InsetFormulaMacro::~InsetFormulaMacro()
61 {
62     par = 0;
63 }
64
65
66 Inset * InsetFormulaMacro::Clone(Buffer const &) const
67 {
68    return new InsetFormulaMacro(name);
69 }
70
71
72 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
73 {
74         os << "FormulaMacro ";
75         tmacro->WriteDef(os, false);
76 }
77
78
79 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
80                              bool /*free_spacing*/) const
81 {
82         tmacro->WriteDef(os, true); // or false?
83         return 2;
84 }
85
86
87 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
88 {
89     return Ascii(buf, os, 0);
90 }
91
92
93 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
94 {
95     return Ascii(buf, os, 0);
96 }
97
98
99 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
100 {
101         istream & is = lex.getStream();
102         mathed_parser_file(is, lex.GetLineNo());   
103         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
104     
105         // Update line number
106         lex.setLineNo(mathed_parser_lineno());
107         
108         MathMacroTable::mathMTable.addTemplate(tmacro);
109         name = tmacro->GetName();
110         par = tmacro;
111         // reading of end_inset in the inset!!!
112         while (lex.IsOK()) {
113                 lex.nextToken();
114                 if (lex.GetString() == "\\end_inset")
115                         break;
116         }
117 }
118
119
120 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
121 {
122     if (opened) {
123         tmacro->update();
124         return InsetFormula::ascent(pain, f);
125     }
126     return lyxfont::maxAscent(f) + 3;
127 }
128
129
130 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
131 {
132     if (opened) {
133         tmacro->update();
134         return InsetFormula::descent(pain, f);
135     }
136     return lyxfont::maxDescent(f) + 1;
137 }
138
139
140 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
141 {
142     if (opened) {
143         tmacro->update();
144         return InsetFormula::width(bv, f);
145     }
146     string ilabel(_("Macro: "));
147     ilabel += name;
148     return 6 + lyxfont::width(ilabel, f);
149 }
150
151
152 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
153                              int baseline, float & x, bool cleared) const
154 {
155         Painter & pain = bv->painter();
156         LyXFont font(f);
157         tmacro->update();
158         if (opened) {
159                 tmacro->setEditMode(true);
160                 InsetFormula::draw(bv, font, baseline, x, cleared);
161                 tmacro->setEditMode(false);     
162         } else {
163                 font.setColor(LColor::math);
164                 
165                 int y = baseline - ascent(bv, font) + 1;
166                 int w = width(bv, font) - 2;
167                 int h = (ascent(bv, font) + descent(bv, font) - 2);
168
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), LM_TC_INSET);
215             InsetFormula::UpdateLocal(bv);
216         }
217         
218         return DISPATCHED;
219     }
220     tmacro->setEditMode(true);
221     tmacro->Metrics();
222     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
223     tmacro->setEditMode(false);
224     
225     return result;
226 }