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