]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Upadate NEWS and some small other tweaks
[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(ostream & os) const
70 {
71         os << "FormulaMacro ";
72         tmacro->WriteDef(os, false);
73 }
74
75
76 int InsetFormulaMacro::Latex(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(ostream &) const
85 {
86     return 0;
87 }
88
89
90 int InsetFormulaMacro::DocBook(ostream &) const
91 {
92     return 0;
93 }
94
95
96 void InsetFormulaMacro::Read(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 }
109
110
111 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
112 {
113     if (opened) {
114         tmacro->update();
115         return InsetFormula::ascent(pain, f);
116     }
117     return lyxfont::maxAscent(f) + 3;
118 }
119
120
121 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
122 {
123     if (opened) {
124         tmacro->update();
125         return InsetFormula::descent(pain, f);
126     }
127     return lyxfont::maxDescent(f) + 1;
128 }
129
130
131 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
132 {
133     if (opened) {
134         tmacro->update();
135         return InsetFormula::width(pain, f);
136     }
137     string ilabel(_("Macro: "));
138     ilabel += name;
139     return 6 + lyxfont::width(ilabel, f);
140 }
141
142
143 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
144                              int baseline, float & x) const
145 {
146         LyXFont font(f);
147         tmacro->update();
148         if (opened) {
149                 tmacro->setEditMode(true);
150                 InsetFormula::draw(pain, font, baseline, x);
151                 tmacro->setEditMode(false);     
152         } else {
153                 font.setColor(LColor::math);
154                 
155                 int y = baseline - ascent(pain, font) + 1;
156                 int w = width(pain, font) - 2;
157                 int h = (ascent(pain, font) + descent(pain, font) - 2);
158
159         
160                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
161                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
162                 
163                 string s(_("Macro: "));
164                 s += name;
165                 pain.text(int(x + 2), baseline, s, font);
166                 x +=  width(pain, font) - 1;
167         }
168 }
169
170
171 char const * InsetFormulaMacro::EditMessage() const 
172 {
173         return _("Math macro editor mode");
174 }
175
176
177 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
178 {
179     opened = true;
180     par = static_cast<MathParInset*>(tmacro->Clone());
181     InsetFormula::Edit(bv, x, y, button);
182 }
183
184                
185 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
186 {
187     opened = false;
188     LyxArrayBase * tarray = tmacro->GetData();
189     MathedIter it(tarray);
190     it.Clear();
191     tmacro->SetData(par->GetData());
192     tmacro->setEditMode(false);
193     InsetFormula::InsetUnlock(bv);
194 }
195
196
197 UpdatableInset::RESULT
198 InsetFormulaMacro::LocalDispatch(BufferView * bv,
199                                  int action, string const & arg)
200 {
201     if (action == LFUN_MATH_MACROARG) {
202         int i = atoi(arg.c_str()) - 1;
203         if (i >= 0 && i < tmacro->getNoArgs()) {
204             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
205             InsetFormula::UpdateLocal(bv);
206         }
207         
208         return DISPATCHED;
209     }
210     tmacro->setEditMode(true);
211     tmacro->Metrics();
212     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
213     tmacro->setEditMode(false);
214     
215     return result;
216 }