]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
- reduce sizeof(MathCharInset) by 20 by better font caching
[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 "support/lstrings.h"
28 #include "support/LAssert.h"
29 #include "debug.h"
30 #include "Painter.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 const char * MathMacro::name() const
57 {
58         return tmplate_->asMacroTemplate()->name().c_str();
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::metrics(MathMetricsInfo const & mi) const
70 {
71         whichFont(font_, LM_TC_TEX, mi);
72         mi_ = mi;
73
74         if (defining()) {
75                 mathed_string_dim(font_, name(), ascent_, descent_, width_);
76                 return;
77         }
78
79         if (editing()) {
80                 expanded_ = tmplate_->xcell(0);
81                 expanded_.metrics(mi_);
82                 width_   = expanded_.width()   + 4;
83                 ascent_  = expanded_.ascent()  + 2;
84                 descent_ = expanded_.descent() + 2;
85
86                 width_ +=  mathed_string_width(font_, name()) + 10;
87
88                 int lasc;
89                 int ldes;
90                 int lwid;
91                 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
92
93                 for (idx_type i = 0; i < nargs(); ++i) {
94                         MathXArray const & c = xcell(i);
95                         c.metrics(mi_);
96                         width_    = max(width_, c.width() + lwid);
97                         descent_ += max(c.ascent(),  lasc) + 5;
98                         descent_ += max(c.descent(), ldes) + 5;
99                 }
100                 return;
101         } 
102
103         expanded_ = tmplate_->xcell(0);
104         expanded_.data_.substitute(*this);
105         expanded_.metrics(mi_);
106         width_   = expanded_.width();
107         ascent_  = expanded_.ascent();
108         descent_ = expanded_.descent();
109 }
110
111
112 void MathMacro::draw(Painter & pain, int x, int y) const
113 {
114         metrics(mi_);
115
116         LyXFont texfont;
117         whichFont(texfont, LM_TC_TEX, mi_);
118
119         if (defining()) {
120                 drawStr(pain, texfont, x, y, name());
121                 return;
122         }
123
124         if (editing()) {
125                 int h = y - ascent() + 2 + expanded_.ascent();
126                 drawStr(pain, font_, x + 3, h, name());
127
128                 int const w = mathed_string_width(font_, name());
129                 expanded_.draw(pain, x + w + 12, h);
130                 h += expanded_.descent();
131
132                 int lasc;
133                 int ldes;
134                 int lwid;
135                 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
136
137                 for (idx_type i = 0; i < nargs(); ++i) {
138                         MathXArray const & c = xcell(i);
139                         h += max(c.ascent(), lasc) + 5;
140                         c.draw(pain, x + lwid, h);
141                         char str[] = "#1:";
142                         str[1] += static_cast<char>(i);
143                         drawStr(pain, texfont, x + 3, h, str);
144                         h += max(c.descent(), ldes) + 5;
145                 }
146                 return;
147         }
148
149         expanded_.draw(pain, x, y);
150 }
151
152
153 void MathMacro::dump() const
154 {
155         MathMacroTable::dump();
156         lyxerr << "\n macro: '" << this << "'\n";
157         lyxerr << " name: '" << name() << "'\n";
158         lyxerr << " template: '";
159         WriteStream wi(lyxerr);
160         tmplate_->write(wi);
161         lyxerr << "'\n";
162 }
163
164
165 bool MathMacro::idxUp(idx_type & idx) const
166 {
167         pos_type pos;
168         return MathNestInset::idxLeft(idx, pos);
169 }
170
171
172 bool MathMacro::idxDown(idx_type & idx) const
173 {
174         pos_type pos;
175         return MathNestInset::idxRight(idx, pos);
176 }
177
178
179 bool MathMacro::idxLeft(idx_type &, pos_type &) const
180 {
181         return false;
182 }
183
184
185 bool MathMacro::idxRight(idx_type &, pos_type &) const
186 {
187         return false;
188 }
189
190
191 void MathMacro::validate(LaTeXFeatures & features) const
192 {
193         if (name() == "binom")
194                 features.require("binom");
195         //MathInset::validate(features);
196 }
197
198
199 void MathMacro::maplize(MapleStream & os) const
200 {
201         updateExpansion();
202         ::maplize(expanded_.data_, os);
203 }
204
205
206 void MathMacro::mathmlize(MathMLStream & os) const
207 {
208         updateExpansion();
209         ::mathmlize(expanded_.data_, os);
210 }
211
212
213 void MathMacro::octavize(OctaveStream & os) const
214 {
215         updateExpansion();
216         ::octavize(expanded_.data_, os);
217 }
218
219
220 void MathMacro::normalize(NormalStream & os) const
221 {
222         os << "[macro " << name() << " ";
223         for (idx_type i = 0; i < nargs(); ++i) 
224                 os << cell(i) << ' ';
225         os << ']';
226 }
227
228
229 void MathMacro::write(WriteStream & os) const
230 {
231         os << '\\' << name();
232         for (idx_type i = 0; i < nargs(); ++i)
233                 os << '{' << cell(i) << '}';
234         if (nargs() == 0) 
235                 os << ' ';
236 }
237
238
239 void MathMacro::updateExpansion() const
240 {
241         expanded_ = tmplate_->xcell(0);
242         expanded_.data_.substitute(*this);
243 }