]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
rename docBook method to docbook
[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::createTemplate(nm, na, string());
56 }
57
58
59 InsetFormulaMacro::InsetFormulaMacro(string const & s)
60 {
61         MathMacroTemplate * t = mathed_parse_macro(s);
62         MathMacroTable::insertTemplate(*t);
63         setInsetName(t->name());
64         delete t;
65         metrics();
66 }
67
68
69 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
70 {
71         return new InsetFormulaMacro(*this);
72 }
73
74
75 void InsetFormulaMacro::write(ostream & os) const
76 {
77         os << "FormulaMacro ";
78         tmacro().write(os, false);
79 }
80
81
82 int InsetFormulaMacro::latex(ostream & os, bool fragile, 
83                              bool /*free_spacing*/) const
84 {
85         tmacro().write(os, fragile);
86         return 2;
87 }
88
89 int InsetFormulaMacro::ascii(ostream & os, int) const
90 {
91         tmacro().write(os, false);
92         return 0;
93 }
94
95
96 int InsetFormulaMacro::linuxdoc(ostream & os) const
97 {
98         return ascii(os, 0);
99 }
100
101
102 int InsetFormulaMacro::docbook(ostream & os) const
103 {
104         return ascii(os, 0);
105 }
106
107
108 void InsetFormulaMacro::read(LyXLex & lex)
109 {
110         MathMacroTemplate * t = mathed_parse_macro(lex);
111         if (!t) 
112                 t = new MathMacroTemplate("{parse error}", 0);
113         MathMacroTable::insertTemplate(*t);
114         setInsetName(t->name());
115         delete t;
116         metrics();
117 }
118
119
120 string InsetFormulaMacro::prefix() const
121 {
122         return string(" ") + _("Macro: ") + tmacro().name() + ": ";
123 }
124
125
126 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
127 {
128         return tmacro().ascent() + 5;
129 }
130
131
132 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
133 {
134         return tmacro().descent() + 5;
135 }
136
137
138 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
139 {
140         metrics();
141         return 10 + lyxfont::width(prefix(), f) + tmacro().width();
142 }
143
144
145
146 UpdatableInset::RESULT
147 InsetFormulaMacro::localDispatch(BufferView * bv,
148                                  kb_action action, string const & arg)
149 {
150         RESULT result = DISPATCHED;
151         switch (action) {
152                 case LFUN_MATH_MACROARG: {
153                         int const i = lyx::atoi(arg);
154                         lyxerr << "inserting macro arg " << i << "\n";
155                         if (i > 0 && i <= tmacro().numargs()) {
156                                 mathcursor->insert(new MathMacroArgument(i));
157                                 updateLocal(bv, true);
158                         } else {
159                                 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
160                         }
161                         break;
162                 }
163                 
164                 default:
165                         result = InsetFormulaBase::localDispatch(bv, action, arg);
166         }
167         return result;
168 }
169
170
171 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
172 {
173         return MathMacroTable::provideTemplate(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 MathInset const * InsetFormulaMacro::par() const
190 {
191         return &tmacro();
192 }
193
194
195 void InsetFormulaMacro::metrics() const
196 {
197         tmacro().metrics(LM_ST_TEXT);
198 }
199
200
201 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
202                              int baseline, float & x, bool /*cleared*/) const
203 {
204         Painter & pain = bv->painter();
205         LyXFont font(f);
206
207         // label
208         font.setColor(LColor::math);
209         
210         int const y = baseline - ascent(bv, font) + 1;
211         int const w = width(bv, font) - 2;
212         int const h = ascent(bv, font) + descent(bv, font) - 2;
213
214         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
215         pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
216         pain.rectangle(int(x), y, w, h, LColor::mathframe);
217
218         if (mathcursor && mathcursor->formula() == this)
219                 mathcursor->drawSelection(pain);
220
221         pain.text(int(x + 2), baseline, prefix(), font);
222         x += width(bv, font);
223
224         // formula
225         float t = tmacro().width() + 5;
226         x -= t;
227         tmacro().draw(pain, int(x), baseline);
228         x += t;
229 }
230