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