]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
Finish the task of removing all cruft from the header files.
[lyx.git] / src / mathed / math_macro.C
1 /**
2  * \file math_macro.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 "math_macro.h"
15 #include "math_support.h"
16 #include "math_extern.h"
17 #include "math_macrotable.h"
18 #include "math_macrotemplate.h"
19 #include "math_mathmlstream.h"
20 #include "debug.h"
21 #include "LaTeXFeatures.h"
22
23
24 using std::max;
25 using std::auto_ptr;
26 using std::endl;
27
28
29 MathMacro::MathMacro(string const & name)
30         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
31                 tmplate_(MathMacroTable::provide(name))
32 {}
33
34
35 MathMacro::MathMacro(MathMacro const & m)
36         : MathNestInset(m),
37                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
38 {}
39
40
41
42 auto_ptr<InsetBase> MathMacro::clone() const
43 {
44         return auto_ptr<InsetBase>(new MathMacro(*this));
45 }
46
47
48 string MathMacro::name() const
49 {
50         return tmplate_->asMacroTemplate()->name();
51 }
52
53
54 bool MathMacro::defining() const
55 {
56         return 0;
57         //return mathcursor && mathcursor->formula()->getInsetName() == name();
58 }
59
60
61 void MathMacro::expand() const
62 {
63         expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
64 }
65
66
67 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
68 {
69         augmentFont(font_, "lyxtex");
70         mi_ = mi;
71
72         if (defining()) {
73
74                 mathed_string_dim(font_, name(), dim_);
75
76         } else if (editing()) {
77
78                 expand();
79                 expanded_.metrics(mi_, dim_);
80                 metricsMarkers();
81
82                 dim_.wid +=  mathed_string_width(font_, name()) + 10;
83
84                 int ww = mathed_string_width(font_, "#1: ");
85
86                 for (idx_type i = 0; i < nargs(); ++i) {
87                         MathArray const & c = cell(i);
88                         c.metrics(mi_);
89                         dim_.wid  = max(dim_.wid, c.width() + ww);
90                         dim_.des += max(c.ascent(),  dim_.asc) + 5;
91                         dim_.des += max(c.descent(), dim_.des) + 5;
92                 }
93
94         } else {
95
96                 expand();
97                 expanded_.substitute(*this);
98                 expanded_.metrics(mi_, dim_);
99
100         }
101
102         dim = dim_;
103 }
104
105
106 void MathMacro::draw(PainterInfo & pi, int x, int y) const
107 {
108         metrics(mi_, dim_);
109
110         LyXFont texfont;
111         augmentFont(texfont, "lyxtex");
112
113         if (defining()) {
114                 drawStr(pi, texfont, x, y, name());
115                 return;
116         }
117
118         if (editing()) {
119                 int h = y - dim_.ascent() + 2 + expanded_.ascent();
120                 drawStr(pi, font_, x + 3, h, name());
121
122                 int const w = mathed_string_width(font_, name());
123                 expanded_.draw(pi, x + w + 12, h);
124                 h += expanded_.descent();
125
126                 Dimension ldim;
127                 mathed_string_dim(font_, "#1: ", ldim);
128
129                 for (idx_type i = 0; i < nargs(); ++i) {
130                         MathArray const & c = cell(i);
131                         h += max(c.ascent(), ldim.asc) + 5;
132                         c.draw(pi, x + ldim.wid, h);
133                         char str[] = "#1:";
134                         str[1] += static_cast<char>(i);
135                         drawStr(pi, texfont, x + 3, h, str);
136                         h += max(c.descent(), ldim.des) + 5;
137                 }
138                 return;
139         }
140
141         expanded_.draw(pi, x, y);
142 }
143
144
145 void MathMacro::dump() const
146 {
147         MathMacroTable::dump();
148         lyxerr << "\n macro: '" << this << "'\n"
149                << " name: '" << name() << "'\n"
150                << " template: '";
151         WriteStream wi(lyxerr);
152         tmplate_->write(wi);
153         lyxerr << "'" << endl;
154 }
155
156
157 bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
158 {
159         pos_type pos;
160         if (up) {
161                 if (!MathNestInset::idxLeft(idx, pos))
162                         return false;
163                 pos = cell(idx).x2pos(x);
164                 return true;
165         } else {
166                 if (!MathNestInset::idxRight(idx, pos))
167                         return false;
168                 pos = cell(idx).x2pos(x);
169                 return true;
170         }
171 }
172
173
174 bool MathMacro::idxLeft(idx_type &, pos_type &) const
175 {
176         return false;
177 }
178
179
180 bool MathMacro::idxRight(idx_type &, pos_type &) const
181 {
182         return false;
183 }
184
185
186 void MathMacro::validate(LaTeXFeatures & features) const
187 {
188         if (name() == "binom" || name() == "mathcircumflex")
189                 features.require(name());
190         //MathInset::validate(features);
191 }
192
193
194 void MathMacro::maple(MapleStream & os) const
195 {
196         updateExpansion();
197         ::maple(expanded_, os);
198 }
199
200
201 void MathMacro::mathmlize(MathMLStream & os) const
202 {
203         updateExpansion();
204         ::mathmlize(expanded_, os);
205 }
206
207
208 void MathMacro::octave(OctaveStream & os) const
209 {
210         updateExpansion();
211         ::octave(expanded_, os);
212 }
213
214
215 void MathMacro::updateExpansion() const
216 {
217         expand();
218         expanded_.substitute(*this);
219 }
220
221
222 void MathMacro::infoize(std::ostream & os) const
223 {
224         os << "Macro: " << name();
225 }
226
227
228 void MathMacro::infoize2(std::ostream & os) const
229 {
230         os << "Macro: " << name();
231 }