]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Andre's mathinset shrink patch ; default .lyx extension when loading files
[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         par_ = mathed_parse(lex);
100         MathMacroTable::insertTemplate(tmacro());
101         par_->metrics(LM_ST_TEXT);
102 }
103
104
105 string InsetFormulaMacro::prefix() const
106 {
107         return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
108 }
109
110
111 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
112 {
113         return tmacro()->ascent() + 5;
114 }
115
116
117 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
118 {
119         return tmacro()->descent() + 5;
120 }
121
122
123 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
124 {
125         tmacro()->metrics(LM_ST_TEXT);
126         return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
127 }
128
129
130 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
131                              int baseline, float & x, bool /*cleared*/) const
132 {
133         Painter & pain = bv->painter();
134         LyXFont font(f);
135
136         // label
137         font.setColor(LColor::math);
138         
139         int const y = baseline - ascent(bv, font) + 1;
140         int const w = width(bv, font) - 2;
141         int const h = ascent(bv, font) + descent(bv, font) - 2;
142
143         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
144         pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
145         pain.rectangle(int(x), y, w, h, LColor::mathframe);
146
147         if (mathcursor && mathcursor->formula() == this)
148                 mathcursor->drawSelection(pain);
149
150         pain.text(int(x + 2), baseline, prefix(), font);
151         x += width(bv, font);
152
153         // formula
154         float t = tmacro()->width() + 5;
155         x -= t;
156         tmacro()->draw(pain, int(x), baseline);
157         x += t;
158 }
159
160
161 UpdatableInset::RESULT
162 InsetFormulaMacro::localDispatch(BufferView * bv,
163                                  kb_action action, string const & arg)
164 {
165         RESULT result = DISPATCHED;
166         switch (action) {
167                 case LFUN_MATH_MACROARG: {
168                         int const i = lyx::atoi(arg);
169                         lyxerr << "inserting macro arg " << i << "\n";
170                         if (i > 0 && i <= tmacro()->numargs()) {
171                                 mathcursor->insert(new MathMacroArgument(i));
172                                 updateLocal(bv, true);
173                         } else {
174                                 lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
175                         }
176                         break;
177                 }
178                 
179                 default:
180                         result = InsetFormulaBase::localDispatch(bv, action, arg);
181         }
182         return result;
183 }
184
185
186 MathMacroTemplate * InsetFormulaMacro::tmacro() const
187 {
188         return static_cast<MathMacroTemplate *>(par_);
189 }
190
191
192 Inset::Code InsetFormulaMacro::lyxCode() const
193 {
194         return Inset::MATHMACRO_CODE;
195 }
196
197
198 MathInsetTypes InsetFormulaMacro::getType() const
199 {
200         return LM_OT_MACRO;
201 }