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