]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
fix #1073
[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
17 #include "formulamacro.h"
18 #include "lfuns.h"
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_macro.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "metricsinfo.h"
25 #include "math_support.h"
26 #include "math_mathmlstream.h"
27 #include "BufferView.h"
28 #include "gettext.h"
29 #include "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "support/lyxlib.h"
32 #include "support/LOstream.h"
33 #include "debug.h"
34 #include "lyxlex.h"
35 #include "lyxtext.h"
36 #include "lyxfont.h"
37
38 #include "Lsstream.h"
39
40 #include "support/BoostFormat.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(STRCONV(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         setInsetName(p->name());
121         MathMacroTable::create(MathAtom(p));
122         metrics();
123 }
124
125
126 string InsetFormulaMacro::prefix() const
127 {
128 #if USE_BOOST_FORMAT
129         return boost::io::str(boost::format(_(" Macro: %s: ")) % getInsetName());
130 #else
131         return _(" Macro: ") + getInsetName() + ": ";
132 #endif
133 }
134
135
136 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
137 {
138         return par()->ascent() + 5;
139 }
140
141
142 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
143 {
144         return par()->descent() + 5;
145 }
146
147
148 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
149 {
150         metrics(bv, f);
151         return 10 + font_metrics::width(prefix(), f) + par()->width();
152 }
153
154
155 MathAtom const & InsetFormulaMacro::par() const
156 {
157         return MathMacroTable::provide(getInsetName());
158 }
159
160
161 MathAtom & InsetFormulaMacro::par()
162 {
163         return MathMacroTable::provide(getInsetName());
164 }
165
166
167 Inset::Code InsetFormulaMacro::lyxCode() const
168 {
169         return Inset::MATHMACRO_CODE;
170 }
171
172
173 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
174                              int y, float & xx) const
175 {
176         // label
177         LyXFont font(f);
178         font.setColor(LColor::math);
179
180         PainterInfo pi = PainterInfo(bv->painter());
181         pi.base.style = LM_ST_TEXT;
182         pi.base.font  = font;
183
184         int const x = int(xx);
185         int const a = y - ascent(bv, font) + 1;
186         int const w = width(bv, font) - 2;
187         int const h = ascent(bv, font) + descent(bv, font) - 2;
188
189         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
190         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
191         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
192
193         if (mathcursor &&
194                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
195                 mathcursor->drawSelection(pi);
196
197         pi.pain.text(x + 2, y, prefix(), font);
198
199         // formula
200         par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
201         xx += w + 2;
202         xo_ = x;
203         yo_ = y;
204
205         setCursorVisible(false);
206 }