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