]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
54642dc8fb5f15b108e8a778aead146c1d778add
[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 #include "debug.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)
51         : InsetFormula(true), name_(nm)
52 {
53         tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
54         if (!tmacro_) {
55                 tmacro_ = new MathMacroTemplate(name_, 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         MathedArray ar;
106         
107         mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
108         // since tmacro_ == 0 when mathed_parse is called we need to sett
109         // its contents explicitly afterwards (Lgb)
110         tmacro_->setData(ar);
111         
112         // Update line number
113         lex.setLineNo(mathed_parser_lineno());
114         
115         MathMacroTable::mathMTable.addTemplate(tmacro_);
116         name_ = tmacro_->GetName();
117         par = tmacro_;
118
119         // reading of end_inset in the inset!!!
120         while (lex.IsOK()) {
121                 lex.nextToken();
122                 if (lex.GetString() == "\\end_inset")
123                         break;
124         }
125 }
126
127
128 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
129 {
130         if (opened_) {
131                 tmacro_->update();
132                 return InsetFormula::ascent(pain, f);
133         }
134         return lyxfont::maxAscent(f) + 3;
135 }
136
137
138 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
139 {
140         if (opened_) {
141                 tmacro_->update();
142                 return InsetFormula::descent(pain, f);
143         }
144         return lyxfont::maxDescent(f) + 1;
145 }
146
147
148 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
149 {
150         if (opened_) {
151                 tmacro_->update();
152                 return InsetFormula::width(bv, f);
153         }
154         string ilabel(_("Macro: "));
155         ilabel += name_;
156         return 6 + lyxfont::width(ilabel, f);
157 }
158
159
160 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
161                              int baseline, float & x, bool cleared) const
162 {
163         Painter & pain = bv->painter();
164         LyXFont font(f);
165         tmacro_->update();
166         if (opened_) {
167                 tmacro_->setEditMode(true);
168                 InsetFormula::draw(bv, font, baseline, x, cleared);
169                 tmacro_->setEditMode(false);    
170         } else {
171                 font.setColor(LColor::math);
172                 
173                 int const y = baseline - ascent(bv, font) + 1;
174                 int const w = width(bv, font) - 2;
175                 int const h = (ascent(bv, font) + descent(bv, font) - 2);
176         
177                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
178                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
179                 
180                 string s(_("Macro: "));
181                 s += name_;
182                 pain.text(int(x + 2), baseline, s, font);
183                 x +=  width(bv, font) - 1;
184         }
185 }
186
187
188 string const InsetFormulaMacro::EditMessage() const 
189 {
190         return _("Math macro editor mode");
191 }
192
193
194 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
195 {
196         opened_ = true;
197         par = static_cast<MathParInset*>(tmacro_->Clone());
198         InsetFormula::Edit(bv, x, y, button);
199 }
200
201                
202 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
203 {
204         opened_ = false;
205         tmacro_->setData(par->GetData());
206         tmacro_->setEditMode(false);
207         InsetFormula::InsetUnlock(bv);
208 }
209
210
211 UpdatableInset::RESULT
212 InsetFormulaMacro::LocalDispatch(BufferView * bv,
213                                  kb_action action, string const & arg)
214 {
215         if (action == LFUN_MATH_MACROARG) {
216                 int const i = lyx::atoi(arg) - 1;
217                 if (i >= 0 && i < tmacro_->getNoArgs()) {
218                         mathcursor->insertInset(tmacro_->getMacroPar(i),
219                                                  LM_TC_INSET);
220                         InsetFormula::UpdateLocal(bv);
221                 }
222         
223                 return DISPATCHED;
224         }
225         tmacro_->setEditMode(true);
226         tmacro_->Metrics();
227         RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
228         tmacro_->setEditMode(false);
229     
230         return result;
231 }