]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
replace (ascent, descent, width) triples by a structure 'dimension'
[lyx.git] / src / mathed / math_macro.C
1 /*
2  *  File:        math_macro.C
3  *  Purpose:     Implementation of macro class for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5  *  Created:     November 1996
6  *  Description: WYSIWYG math macros
7  *
8  *  Dependencies: Math
9  *
10  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
11  *
12  *  Version: 0.2, Math & Lyx project.
13  *
14  *  This code is under the GNU General Public Licence version 2 or later.
15  */
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "math_macro.h"
22 #include "math_support.h"
23 #include "math_extern.h"
24 #include "math_macrotable.h"
25 #include "math_macrotemplate.h"
26 #include "math_mathmlstream.h"
27 #include "math_streamstr.h"
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
30 #include "debug.h"
31 #include "LaTeXFeatures.h"
32
33
34 using std::max;
35
36
37 MathMacro::MathMacro(string const & name)
38         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
39                 tmplate_(MathMacroTable::provide(name))
40 {}
41
42
43 MathMacro::MathMacro(MathMacro const & m)
44         : MathNestInset(m),
45                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
46 {}
47
48
49
50 MathInset * MathMacro::clone() const
51 {
52         return new MathMacro(*this);
53 }
54
55
56 string const & MathMacro::name() const
57 {
58         return tmplate_->asMacroTemplate()->name();
59 }
60
61
62 bool MathMacro::defining() const
63 {
64         return 0;
65         //return mathcursor && mathcursor->formula()->getInsetName() == name();
66 }
67
68
69 void MathMacro::expand() const
70 {
71         expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
72 }
73
74
75 void MathMacro::metrics(MathMetricsInfo & mi) const
76 {
77         augmentFont(font_, "lyxtex");
78         mi_ = mi;
79
80         if (defining()) {
81                 mathed_string_dim(font_, name(), dim_);
82                 return;
83         }
84
85         if (editing()) {
86                 expand();
87                 dim_ = expanded_.metrics(mi_);
88                 metricsMarkers2(2); 
89
90                 dim_.w +=  mathed_string_width(font_, name()) + 10;
91
92                 Dimension ldim;
93                 mathed_string_dim(font_, "#1: ", ldim);
94
95                 for (idx_type i = 0; i < nargs(); ++i) {
96                         MathXArray const & c = xcell(i);
97                         c.metrics(mi_);
98                         dim_.w  = max(dim_.w, c.width() + ldim.w);
99                         dim_.d += max(c.ascent(),  ldim.a) + 5;
100                         dim_.d += max(c.descent(), ldim.d) + 5;
101                 }
102                 return;
103         }
104
105         expand();
106         expanded_.data().substitute(*this);
107         expanded_.metrics(mi_);
108         dim_ = expanded_.dim();
109 }
110
111
112 void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
113 {
114         metrics(mi_);
115
116         LyXFont texfont;
117         augmentFont(texfont, "lyxtex");
118
119         if (defining()) {
120                 drawStr(pi, texfont, x, y, name());
121                 return;
122         }
123
124         if (editing()) {
125                 int h = y - ascent() + 2 + expanded_.ascent();
126                 drawStr(pi, font_, x + 3, h, name());
127
128                 int const w = mathed_string_width(font_, name());
129                 expanded_.draw(pi, x + w + 12, h);
130                 h += expanded_.descent();
131
132                 Dimension ldim;
133                 mathed_string_dim(font_, "#1: ", ldim);
134
135                 for (idx_type i = 0; i < nargs(); ++i) {
136                         MathXArray const & c = xcell(i);
137                         h += max(c.ascent(), ldim.a) + 5;
138                         c.draw(pi, x + ldim.w, h);
139                         char str[] = "#1:";
140                         str[1] += static_cast<char>(i);
141                         drawStr(pi, texfont, x + 3, h, str);
142                         h += max(c.descent(), ldim.d) + 5;
143                 }
144                 return;
145         }
146
147         expanded_.draw(pi, x, y);
148 }
149
150
151 void MathMacro::dump() const
152 {
153         MathMacroTable::dump();
154         lyxerr << "\n macro: '" << this << "'\n";
155         lyxerr << " name: '" << name() << "'\n";
156         lyxerr << " template: '";
157         WriteStream wi(lyxerr);
158         tmplate_->write(wi);
159         lyxerr << "'\n";
160 }
161
162
163 bool MathMacro::idxUpDown(idx_type & idx, bool up) const
164 {
165         pos_type pos;
166         return
167                 up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
168 }
169
170
171 bool MathMacro::idxLeft(idx_type &, pos_type &) const
172 {
173         return false;
174 }
175
176
177 bool MathMacro::idxRight(idx_type &, pos_type &) const
178 {
179         return false;
180 }
181
182
183 void MathMacro::validate(LaTeXFeatures & features) const
184 {
185         if (name() == "binom" || name() == "mathcircumflex")
186                 features.require(name());
187         //MathInset::validate(features);
188 }
189
190
191 void MathMacro::maplize(MapleStream & os) const
192 {
193         updateExpansion();
194         ::maplize(expanded_.data(), os);
195 }
196
197
198 void MathMacro::mathmlize(MathMLStream & os) const
199 {
200         updateExpansion();
201         ::mathmlize(expanded_.data(), os);
202 }
203
204
205 void MathMacro::octavize(OctaveStream & os) const
206 {
207         updateExpansion();
208         ::octavize(expanded_.data(), os);
209 }
210
211
212 void MathMacro::normalize(NormalStream & os) const
213 {
214         os << "[macro " << name() << " ";
215         for (idx_type i = 0; i < nargs(); ++i)
216                 os << cell(i) << ' ';
217         os << ']';
218 }
219
220
221 void MathMacro::write(WriteStream & os) const
222 {
223         os << '\\' << name();
224         for (idx_type i = 0; i < nargs(); ++i)
225                 os << '{' << cell(i) << '}';
226         if (nargs() == 0)
227                 os << ' ';
228 }
229
230
231 void MathMacro::updateExpansion() const
232 {
233         expand();
234         expanded_.data().substitute(*this);
235 }