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