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