]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
a1c8b6c337b7cdcaf63e1b548bacc851cee3deed
[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
18 #include "math_macro.h"
19 #include "math_support.h"
20 #include "math_extern.h"
21 #include "math_macrotable.h"
22 #include "math_macrotemplate.h"
23 #include "math_mathmlstream.h"
24 #include "math_streamstr.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "debug.h"
28 #include "LaTeXFeatures.h"
29
30
31 using std::max;
32
33
34 MathMacro::MathMacro(string const & name)
35         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
36                 tmplate_(MathMacroTable::provide(name))
37 {}
38
39
40 MathMacro::MathMacro(MathMacro const & m)
41         : MathNestInset(m),
42                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
43 {}
44
45
46
47 MathInset * MathMacro::clone() const
48 {
49         return new MathMacro(*this);
50 }
51
52
53 string MathMacro::name() const
54 {
55         return tmplate_->asMacroTemplate()->name();
56 }
57
58
59 bool MathMacro::defining() const
60 {
61         return 0;
62         //return mathcursor && mathcursor->formula()->getInsetName() == name();
63 }
64
65
66 void MathMacro::expand() const
67 {
68         expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
69 }
70
71
72 void MathMacro::metrics(MathMetricsInfo & mi) const
73 {
74         augmentFont(font_, "lyxtex");
75         mi_ = mi;
76
77         if (defining()) {
78                 mathed_string_dim(font_, name(), dim_);
79                 return;
80         }
81
82         if (editing()) {
83                 expand();
84                 dim_ = expanded_.metrics(mi_);
85                 metricsMarkers2(2);
86
87                 dim_.w +=  mathed_string_width(font_, name()) + 10;
88
89                 Dimension ldim;
90                 mathed_string_dim(font_, "#1: ", ldim);
91
92                 for (idx_type i = 0; i < nargs(); ++i) {
93                         MathArray const & c = cell(i);
94                         c.metrics(mi_);
95                         dim_.w  = max(dim_.w, c.width() + ldim.w);
96                         dim_.d += max(c.ascent(),  ldim.a) + 5;
97                         dim_.d += max(c.descent(), ldim.d) + 5;
98                 }
99                 return;
100         }
101
102         expand();
103         expanded_.substitute(*this);
104         expanded_.metrics(mi_);
105         dim_ = expanded_.dim();
106 }
107
108
109 void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
110 {
111         metrics(mi_);
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 - 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.a) + 5;
135                         c.draw(pi, x + ldim.w, 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.d) + 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         lyxerr << " name: '" << name() << "'\n";
153         lyxerr << " template: '";
154         WriteStream wi(lyxerr);
155         tmplate_->write(wi);
156         lyxerr << "'\n";
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::maplize(MapleStream & os) const
198 {
199         updateExpansion();
200         ::maplize(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::octavize(OctaveStream & os) const
212 {
213         updateExpansion();
214         ::octavize(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 }