]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
first shot at preview. It crashes. Don't know why. To see it work a bit, change
[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, based on ideas of Alejandro Aguilar Sierra
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_macrotable.h"
26 #include "math_support.h"
27 #include "math_mathmlstream.h"
28 #include "BufferView.h"
29 #include "gettext.h"
30 #include "frontends/Painter.h"
31 #include "frontends/font_metrics.h"
32 #include "support/lyxlib.h"
33 #include "support/LOstream.h"
34 #include "debug.h"
35 #include "lyxlex.h"
36 #include "lyxtext.h"
37 #include "lyxfont.h"
38
39 using std::ostream;
40
41 extern MathCursor * mathcursor;
42
43 InsetFormulaMacro::InsetFormulaMacro()
44 {
45         // inset name is inherited from Inset
46         setInsetName("unknown");
47 }
48
49
50 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
51 {
52         setInsetName(name);
53         MathMacroTable::create(name, nargs);
54 }
55
56
57 InsetFormulaMacro::InsetFormulaMacro(string const & s)
58 {
59         string name;
60         mathed_parse_macro(name, s);
61         setInsetName(name);
62 }
63
64
65 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
66 {
67         return new InsetFormulaMacro(*this);
68 }
69
70
71 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
72 {
73         os << "FormulaMacro ";
74         WriteStream wi(os, false, false);
75         par()->write(wi);
76 }
77
78
79 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile,
80                              bool /*free_spacing*/) const
81 {
82         WriteStream wi(os, fragile, true);
83         par()->write(wi);
84         return 2;
85 }
86
87
88 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
89 {
90         WriteStream wi(os, false, true);
91         par()->write(wi);
92         return 0;
93 }
94
95
96 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
97 {
98         return ascii(buf, os, 0);
99 }
100
101
102 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
103 {
104         return ascii(buf, os, 0);
105 }
106
107
108 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
109 {
110         string name;
111         mathed_parse_macro(name, lex);
112         setInsetName(name);
113         //lyxerr << "metrics disabled";
114         metrics();
115 }
116
117
118 string InsetFormulaMacro::prefix() const
119 {
120         return string(" ") + _("Macro: ") + getInsetName() + ": ";
121 }
122
123
124 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
125 {
126         return par()->ascent() + 5;
127 }
128
129
130 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
131 {
132         return par()->descent() + 5;
133 }
134
135
136 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
137 {
138         metrics(bv, f);
139         return 10 + font_metrics::width(prefix(), f) + par()->width();
140 }
141
142
143 MathAtom const & InsetFormulaMacro::par() const
144 {
145         return MathMacroTable::provide(getInsetName());
146 }
147
148
149 MathAtom & InsetFormulaMacro::par()
150 {
151         return MathMacroTable::provide(getInsetName());
152 }
153
154
155 Inset::Code InsetFormulaMacro::lyxCode() const
156 {
157         return Inset::MATHMACRO_CODE;
158 }
159
160
161 MathInsetTypes InsetFormulaMacro::getType() const
162 {
163         return LM_OT_MACRO;
164 }
165
166
167 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
168                              int y, float & xx, bool /*cleared*/) const
169 {
170         MathPainterInfo pain = MathPainterInfo(bv->painter());
171         LyXFont font(f);
172
173         // label
174         font.setColor(LColor::math);
175
176         int const x = int(xx);
177         int const a = y - ascent(bv, font) + 1;
178         int const w = width(bv, font) - 2;
179         int const h = ascent(bv, font) + descent(bv, font) - 2;
180
181         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
182         pain.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
183         pain.pain.rectangle(x, a, w, h, LColor::mathframe);
184
185         if (mathcursor &&
186                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
187                 mathcursor->drawSelection(pain);
188
189         pain.pain.text(x + 2, y, prefix(), font);
190
191         // formula
192         par()->draw(pain, x + font_metrics::width(prefix(), f) + 5, y);
193         xx += w + 2;
194         xo_ = x;
195         yo_ = y;
196
197         setCursorVisible(false);
198 }