]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Reduce Michael's buglist.
[lyx.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_hullinset.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/math_support.h"
36 #include "support/LOstream.h"
37 #include "debug.h"
38 #include "lyxlex.h"
39 #include "lyxtext.h"
40 #include "lyxfont.h"
41 #include "math_mathmlstream.h"
42
43 using std::ostream;
44
45 extern MathCursor * mathcursor;
46
47 InsetFormulaMacro::InsetFormulaMacro()
48 {
49         // inset name is inherited from Inset
50         setInsetName("unknown");
51 }
52
53
54 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
55 {
56         setInsetName(nm);
57         MathMacroTable::create(nm, na, string());
58 }
59
60
61 InsetFormulaMacro::InsetFormulaMacro(string const & s)
62 {
63         string name = mathed_parse_macro(s);
64         setInsetName(name);
65 }
66
67
68 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
69 {
70         return new InsetFormulaMacro(*this);
71 }
72
73
74 void InsetFormulaMacro::write(Buffer const * buf, ostream & os) const
75 {
76         os << "FormulaMacro ";
77         WriteStream wi(buf, os, false);
78         par()->write(wi);
79 }
80
81
82 int InsetFormulaMacro::latex(Buffer const * buf, ostream & os, bool fragile, 
83                              bool /*free_spacing*/) const
84 {
85         WriteStream wi(buf, os, fragile);
86         par()->write(wi);
87         return 2;
88 }
89
90
91 int InsetFormulaMacro::ascii(Buffer const * buf, ostream & os, int) const
92 {
93         WriteStream wi(buf, os, false);
94         par()->write(wi);
95         return 0;
96 }
97
98
99 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
100 {
101         return ascii(buf, os, 0);
102 }
103
104
105 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os) const
106 {
107         return ascii(buf, os, 0);
108 }
109
110
111 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
112 {
113         string name = mathed_parse_macro(lex);
114         setInsetName(name);
115         //lyxerr << "metrics disabled";
116         metrics();
117 }
118
119
120 string InsetFormulaMacro::prefix() const
121 {
122         return string(" ") + _("Macro: ") + getInsetName() + ": ";
123 }
124
125
126 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
127 {
128         return par()->ascent() + 5;
129 }
130
131
132 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
133 {
134         return par()->descent() + 5;
135 }
136
137
138 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
139 {
140         metrics(bv, f);
141         return 10 + lyxfont::width(prefix(), f) + par()->width();
142 }
143
144
145
146 UpdatableInset::RESULT
147 InsetFormulaMacro::localDispatch(BufferView * bv,
148                                  kb_action action, string const & arg)
149 {
150         RESULT result = DISPATCHED;
151         switch (action) {
152                 case LFUN_MATH_MACROARG: {
153                         int const i = lyx::atoi(arg);
154                         lyxerr << "inserting macro arg " << i << "\n";
155                         //if (i > 0 && i <= par()->numargs()) {
156                                 mathcursor->insert(MathAtom(new MathMacroArgument(i)));
157                                 updateLocal(bv, true);
158                         //} else {
159                         //      lyxerr << "not in range 0.." << par()->numargs() << "\n";
160                         //}
161                         break;
162                 }
163                 
164                 default: {
165                         result = InsetFormulaBase::localDispatch(bv, action, arg);
166                         // force redraw if anything happened
167                         if (result != UNDISPATCHED) {
168                                 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
169                                 bv->updateInset(this, false);
170                         }
171                 }
172         }
173         return result;
174 }
175
176
177 MathAtom const & InsetFormulaMacro::par() const
178 {
179         return MathMacroTable::provide(getInsetName());
180 }
181
182
183 MathAtom & InsetFormulaMacro::par()
184 {
185         return MathMacroTable::provide(getInsetName());
186 }
187
188
189 Inset::Code InsetFormulaMacro::lyxCode() const
190 {
191         return Inset::MATHMACRO_CODE;
192 }
193
194
195 MathInsetTypes InsetFormulaMacro::getType() const
196 {
197         return LM_OT_MACRO;
198 }
199
200
201 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
202                              int y, float & x, bool /*cleared*/) const
203 {
204         Painter & pain = bv->painter();
205         LyXFont font(f);
206
207         // label
208         font.setColor(LColor::math);
209         
210         int const a = y - ascent(bv, font) + 1;
211         int const w = width(bv, font) - 2;
212         int const h = ascent(bv, font) + descent(bv, font) - 2;
213
214         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
215         pain.fillRectangle(int(x), a , w, h, LColor::mathmacrobg);
216         pain.rectangle(int(x), a, w, h, LColor::mathframe);
217
218         if (mathcursor && mathcursor->formula() == this)
219                 mathcursor->drawSelection(pain);
220
221         pain.text(int(x + 2), y, prefix(), font);
222         x += width(bv, font);
223
224         // formula
225         xo_ = int(x) - par()->width() - 5;
226         yo_ = y;
227         par()->draw(pain, xo_, yo_);
228 }
229