]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
344457c48f84072d66c1203a50a8eaba25abd9d4
[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                 return InsetFormula::ascent(pain, f);
132         }
133         return lyxfont::maxAscent(f) + 3;
134 }
135
136
137 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
138 {
139         if (opened_) {
140                 return InsetFormula::descent(pain, f);
141         }
142         return lyxfont::maxDescent(f) + 1;
143 }
144
145
146 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
147 {
148         if (opened_) {
149                 return InsetFormula::width(bv, f);
150         }
151         string ilabel(_("Macro: "));
152         ilabel += name_;
153         return 6 + lyxfont::width(ilabel, f);
154 }
155
156
157 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
158                              int baseline, float & x, bool cleared) const
159 {
160         Painter & pain = bv->painter();
161         LyXFont font(f);
162         if (opened_) {
163                 tmacro_->setEditMode(true);
164                 InsetFormula::draw(bv, font, baseline, x, cleared);
165                 tmacro_->setEditMode(false);    
166         } else {
167                 font.setColor(LColor::math);
168                 
169                 int const y = baseline - ascent(bv, font) + 1;
170                 int const w = width(bv, font) - 2;
171                 int const h = (ascent(bv, font) + descent(bv, font) - 2);
172         
173                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
174                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
175                 
176                 string s(_("Macro: "));
177                 s += name_;
178                 pain.text(int(x + 2), baseline, s, font);
179                 x +=  width(bv, font) - 1;
180         }
181 }
182
183
184 string const InsetFormulaMacro::EditMessage() const 
185 {
186         return _("Math macro editor mode");
187 }
188
189
190 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
191 {
192         opened_ = true;
193         par = static_cast<MathParInset*>(tmacro_->Clone());
194         InsetFormula::Edit(bv, x, y, button);
195 }
196
197                
198 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
199 {
200         opened_ = false;
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                                  kb_action action, string const & arg)
210 {
211         if (action == LFUN_MATH_MACROARG) {
212                 int const i = lyx::atoi(arg) - 1;
213                 if (i >= 0 && i < tmacro_->getNoArgs()) {
214                         mathcursor->insertInset(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 }