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