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