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