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