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