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