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