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