]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
- fix nasty bug due to missing InsetFormula copy c'tor
[features.git] / src / mathed / formulamacro.C
1 /*
2  *  File:        formulamacro.C
3  *  Purpose:     Implementation of the formula macro LyX inset
4  *  Author:      André Pönitz
5  *  Created:     March 2001
6  *  Description: Allows the edition of math macros inside Lyx. 
7  *
8  *  Copyright: 2001  The LyX Project
9  *
10  *   You are free to use and modify this code under the terms of
11  *   the GNU General Public Licence version 2 or later.
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "formulamacro.h"
21 #include "commandtags.h"
22 #include "math_cursor.h"
23 #include "math_parser.h"
24 #include "math_macro.h"
25 #include "math_macroarg.h"
26 #include "math_macrotable.h"
27 #include "math_macrotemplate.h"
28 #include "math_matrixinset.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 #include "lyxlex.h"
39 #include "lyxfont.h"
40
41 using std::ostream;
42
43 extern MathCursor * mathcursor;
44
45 InsetFormulaMacro::InsetFormulaMacro()
46         : tmacro_(new MathMacroTemplate("unknown", 0))
47 {}
48
49
50 InsetFormulaMacro::InsetFormulaMacro(InsetFormulaMacro const & m)
51         : InsetFormulaBase(m),
52                 tmacro_(static_cast<MathMacroTemplate *>(m.tmacro_->clone()))
53 {}
54
55
56 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
57         : tmacro_(new MathMacroTemplate(nm, na))
58 {
59         MathMacroTable::insertTemplate(tmacro_);
60 }
61
62
63 InsetFormulaMacro::~InsetFormulaMacro()
64 {
65 #ifdef WITH_WARNINGS
66 #warning Need to unregister from MathMacroTable.
67 #endif
68         // Instead of unregister an delete leak this until it gets fixed
69         //delete tmacro_;
70 }
71
72
73 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
74 {
75 #ifdef WITH_WARNINGS
76 #warning This should not be needed in reality...
77 #endif
78         return new InsetFormulaMacro(*this);
79 }
80
81
82 void InsetFormulaMacro::write(ostream & os) const
83 {
84         os << "FormulaMacro ";
85         tmacro().write(os, false);
86 }
87
88
89 int InsetFormulaMacro::latex(ostream & os, bool fragile, 
90                              bool /*free_spacing*/) const
91 {
92         tmacro().write(os, fragile);
93         return 2;
94 }
95
96 int InsetFormulaMacro::ascii(ostream & os, int) const
97 {
98         tmacro().write(os, false);
99         return 0;
100 }
101
102
103 int InsetFormulaMacro::linuxdoc(ostream & os) const
104 {
105         return ascii(os, 0);
106 }
107
108
109 int InsetFormulaMacro::docBook(ostream & os) const
110 {
111         return ascii(os, 0);
112 }
113
114
115 void InsetFormulaMacro::read(LyXLex & lex)
116 {
117         // Awful hack...
118         delete tmacro_;
119         tmacro_ = mathed_parse_macro(lex);
120         MathMacroTable::insertTemplate(tmacro_);
121         metrics();
122 }
123
124
125 string InsetFormulaMacro::prefix() const
126 {
127         return string(" ") + _("Macro: ") + tmacro().name() + ": ";
128 }
129
130
131 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
132 {
133         return tmacro().ascent() + 5;
134 }
135
136
137 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
138 {
139         return tmacro().descent() + 5;
140 }
141
142
143 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
144 {
145         metrics();
146         return 10 + lyxfont::width(prefix(), f) + tmacro().width();
147 }
148
149
150
151 UpdatableInset::RESULT
152 InsetFormulaMacro::localDispatch(BufferView * bv,
153                                  kb_action action, string const & arg)
154 {
155         RESULT result = DISPATCHED;
156         switch (action) {
157                 case LFUN_MATH_MACROARG: {
158                         int const i = lyx::atoi(arg);
159                         lyxerr << "inserting macro arg " << i << "\n";
160                         if (i > 0 && i <= tmacro().numargs()) {
161                                 mathcursor->insert(new MathMacroArgument(i));
162                                 updateLocal(bv, true);
163                         } else {
164                                 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
165                         }
166                         break;
167                 }
168                 
169                 default:
170                         result = InsetFormulaBase::localDispatch(bv, action, arg);
171         }
172         return result;
173 }
174
175
176 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
177 {
178         return *tmacro_;
179 }
180
181
182 Inset::Code InsetFormulaMacro::lyxCode() const
183 {
184         return Inset::MATHMACRO_CODE;
185 }
186
187
188 MathInsetTypes InsetFormulaMacro::getType() const
189 {
190         return LM_OT_MACRO;
191 }
192
193
194 MathInset const * InsetFormulaMacro::par() const
195 {
196         return tmacro_;
197 }
198
199
200 void InsetFormulaMacro::metrics() const
201 {
202         tmacro_->metrics(LM_ST_TEXT);
203 }
204
205
206 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
207                              int baseline, float & x, bool /*cleared*/) const
208 {
209         Painter & pain = bv->painter();
210         LyXFont font(f);
211
212         // label
213         font.setColor(LColor::math);
214         
215         int const y = baseline - ascent(bv, font) + 1;
216         int const w = width(bv, font) - 2;
217         int const h = ascent(bv, font) + descent(bv, font) - 2;
218
219         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
220         pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
221         pain.rectangle(int(x), y, w, h, LColor::mathframe);
222
223         if (mathcursor && mathcursor->formula() == this)
224                 mathcursor->drawSelection(pain);
225
226         pain.text(int(x + 2), baseline, prefix(), font);
227         x += width(bv, font);
228
229         // formula
230         float t = tmacro().width() + 5;
231         x -= t;
232         tmacro_->draw(pain, int(x), baseline);
233         x += t;
234 }
235