]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Added new FINISED states FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN.
[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         MathMacroTable::insertTemplate(*t);
112         setInsetName(t->name());
113         delete t;
114         metrics();
115 }
116
117
118 string InsetFormulaMacro::prefix() const
119 {
120         return string(" ") + _("Macro: ") + tmacro().name() + ": ";
121 }
122
123
124 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
125 {
126         return tmacro().ascent() + 5;
127 }
128
129
130 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
131 {
132         return tmacro().descent() + 5;
133 }
134
135
136 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
137 {
138         metrics();
139         return 10 + lyxfont::width(prefix(), f) + tmacro().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 <= tmacro().numargs()) {
154                                 mathcursor->insert(new MathMacroArgument(i));
155                                 updateLocal(bv, true);
156                         } else {
157                                 lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
158                         }
159                         break;
160                 }
161                 
162                 default:
163                         result = InsetFormulaBase::localDispatch(bv, action, arg);
164         }
165         return result;
166 }
167
168
169 MathMacroTemplate const & InsetFormulaMacro::tmacro() const
170 {
171         return MathMacroTable::provideTemplate(getInsetName());
172 }
173
174
175 Inset::Code InsetFormulaMacro::lyxCode() const
176 {
177         return Inset::MATHMACRO_CODE;
178 }
179
180
181 MathInsetTypes InsetFormulaMacro::getType() const
182 {
183         return LM_OT_MACRO;
184 }
185
186
187 MathInset const * InsetFormulaMacro::par() const
188 {
189         return &tmacro();
190 }
191
192
193 void InsetFormulaMacro::metrics() const
194 {
195         tmacro().metrics(LM_ST_TEXT);
196 }
197
198
199 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
200                              int baseline, 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 y = baseline - 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), y , w, h, LColor::mathmacrobg);
214         pain.rectangle(int(x), y, w, h, LColor::mathframe);
215
216         if (mathcursor && mathcursor->formula() == this)
217                 mathcursor->drawSelection(pain);
218
219         pain.text(int(x + 2), baseline, prefix(), font);
220         x += width(bv, font);
221
222         // formula
223         float t = tmacro().width() + 5;
224         x -= t;
225         tmacro().draw(pain, int(x), baseline);
226         x += t;
227 }
228