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