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