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