]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathMacro.C
make it compile again (hopefully)
[lyx.git] / src / mathed / InsetMathMacro.C
1 /**
2  * \file InsetMathMacro.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathMacro.h"
15 #include "MathSupport.h"
16 #include "MathExtern.h"
17 #include "MathMLStream.h"
18
19 #include "buffer.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
25
26 using lyx::docstring;
27
28 using std::string;
29 using std::max;
30 using std::auto_ptr;
31 using std::endl;
32
33
34 MathMacro::MathMacro(string const & name, int numargs)
35         : InsetMathNest(numargs), name_(name)
36 {}
37
38
39 auto_ptr<InsetBase> MathMacro::doClone() const
40 {
41         return auto_ptr<InsetBase>(new MathMacro(*this));
42 }
43
44
45 string MathMacro::name() const
46 {
47         return name_;
48 }
49
50
51 void MathMacro::cursorPos(BufferView const & bv,
52                 CursorSlice const & sl, bool boundary, int & x, int & y) const
53 {
54         // We may have 0 arguments, but InsetMathNest requires at least one.
55         if (nargs() > 0)
56                 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
57 }
58
59
60 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
61 {
62         if (!MacroTable::globalMacros().has(name())) {
63                 mathed_string_dim(mi.base.font, lyx::from_utf8("Unknown: " + name()), dim);
64         } else if (editing(mi.base.bv)) {
65                 // FIXME UNICODE
66                 asArray(lyx::from_utf8(MacroTable::globalMacros().get(name()).def()), tmpl_);
67                 LyXFont font = mi.base.font;
68                 augmentFont(font, "lyxtex");
69                 tmpl_.metrics(mi, dim);
70                 // FIXME UNICODE
71                 dim.wid += mathed_string_width(font, lyx::from_utf8(name())) + 10;
72                 // FIXME UNICODE
73                 int ww = mathed_string_width(font, lyx::from_ascii("#1: "));
74                 for (idx_type i = 0; i < nargs(); ++i) {
75                         MathArray const & c = cell(i);
76                         c.metrics(mi);
77                         dim.wid  = max(dim.wid, c.width() + ww);
78                         dim.des += c.height() + 10;
79                 }
80         } else {
81                 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
82                 expanded_.metrics(mi, dim);
83         }
84         metricsMarkers2(dim);
85         dim_ = dim;
86 }
87
88
89 void MathMacro::draw(PainterInfo & pi, int x, int y) const
90 {
91         if (!MacroTable::globalMacros().has(name())) {
92                 // FIXME UNICODE
93                 drawStrRed(pi, x, y, lyx::from_utf8("Unknown: " + name()));
94         } else if (editing(pi.base.bv)) {
95                 LyXFont font = pi.base.font;
96                 augmentFont(font, "lyxtex");
97                 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
98                 // FIXME UNICODE
99                 docstring dn = lyx::from_utf8(name());
100                 pi.pain.text(x + 3, h, dn, font);
101                 int const w = mathed_string_width(font, dn);
102                 tmpl_.draw(pi, x + w + 12, h);
103                 h += tmpl_.descent();
104                 Dimension ldim;
105                 mathed_string_dim(font, lyx::from_ascii("#1: "), ldim);
106                 for (idx_type i = 0; i < nargs(); ++i) {
107                         MathArray const & c = cell(i);
108                         h += max(c.ascent(), ldim.asc) + 5;
109                         c.draw(pi, x + ldim.wid, h);
110                         lyx::char_type str[] = { '#', '1', ':', '\0' };
111                         str[1] += static_cast<lyx::char_type>(i);
112                         pi.pain.text(x + 3, h, str, font);
113                         h += max(c.descent(), ldim.des) + 5;
114                 }
115         } else {
116                 expanded_.draw(pi, x, y);
117         }
118         drawMarkers2(pi, x, y);
119 }
120
121
122 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
123 {
124         // We may have 0 arguments, but InsetMathNest requires at least one.
125         if (nargs() > 0)
126                 InsetMathNest::drawSelection(pi, x, y);
127 }
128
129
130 void MathMacro::validate(LaTeXFeatures & features) const
131 {
132         if (name() == "binom" || name() == "mathcircumflex")
133                 features.require(name());
134 }
135
136
137 InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
138 {
139         // We may have 0 arguments, but InsetMathNest requires at least one.
140         if (nargs() > 0) {
141                 // Prevent crash due to cold coordcache
142                 // FIXME: This is only a workaround, the call of
143                 // InsetMathNest::editXY is correct. The correct fix would
144                 // ensure that the coordcache of the arguments is valid.
145                 if (!editing(&cur.bv())) {
146                         edit(cur, true);
147                         return this;
148                 }
149                 return InsetMathNest::editXY(cur, x, y);
150         }
151         return this;
152 }
153
154
155 void MathMacro::maple(MapleStream & os) const
156 {
157         updateExpansion();
158         ::maple(expanded_, os);
159 }
160
161
162 void MathMacro::mathmlize(MathMLStream & os) const
163 {
164         updateExpansion();
165         ::mathmlize(expanded_, os);
166 }
167
168
169 void MathMacro::octave(OctaveStream & os) const
170 {
171         updateExpansion();
172         ::octave(expanded_, os);
173 }
174
175
176 void MathMacro::updateExpansion() const
177 {
178         //expanded_.substitute(*this);
179 }
180
181
182 void MathMacro::infoize(std::ostream & os) const
183 {
184         os << "Macro: " << name();
185 }
186
187
188 void MathMacro::infoize2(std::ostream & os) const
189 {
190         os << "Macro: " << name();
191
192 }