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