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