]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
IU of drawing phase one without 'semantic changes' as requested by John
[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(MetricsInfo & mi, Dimension & dim) const
73 {
74         augmentFont(font_, "lyxtex");
75         mi_ = mi;
76
77         if (defining()) {
78
79                 mathed_string_dim(font_, name(), dim_);
80
81         } else if (editing()) {
82
83                 expand();
84                 expanded_.metrics(mi_, dim_);
85                 metricsMarkers();
86
87                 dim_.wid +=  mathed_string_width(font_, name()) + 10;
88
89                 int ww = mathed_string_width(font_, "#1: ");
90
91                 for (idx_type i = 0; i < nargs(); ++i) {
92                         MathArray const & c = cell(i);
93                         c.metrics(mi_);
94                         dim_.wid  = max(dim_.wid, c.width() + ww);
95                         dim_.des += max(c.ascent(),  dim_.asc) + 5;
96                         dim_.des += max(c.descent(), dim_.des) + 5;
97                 }
98
99         } else {
100
101                 expand();
102                 expanded_.substitute(*this);
103                 expanded_.metrics(mi_, dim_);
104
105         }
106
107         dim = dim_;
108 }
109
110
111 void MathMacro::draw(PainterInfo & pi, int x, int y) const
112 {
113         metrics(mi_, dim_);
114
115         LyXFont texfont;
116         augmentFont(texfont, "lyxtex");
117
118         if (defining()) {
119                 drawStr(pi, texfont, x, y, name());
120                 return;
121         }
122
123         if (editing()) {
124                 int h = y - dim_.ascent() + 2 + expanded_.ascent();
125                 drawStr(pi, font_, x + 3, h, name());
126
127                 int const w = mathed_string_width(font_, name());
128                 expanded_.draw(pi, x + w + 12, h);
129                 h += expanded_.descent();
130
131                 Dimension ldim;
132                 mathed_string_dim(font_, "#1: ", ldim);
133
134                 for (idx_type i = 0; i < nargs(); ++i) {
135                         MathArray const & c = cell(i);
136                         h += max(c.ascent(), ldim.asc) + 5;
137                         c.draw(pi, x + ldim.wid, h);
138                         char str[] = "#1:";
139                         str[1] += static_cast<char>(i);
140                         drawStr(pi, texfont, x + 3, h, str);
141                         h += max(c.descent(), ldim.des) + 5;
142                 }
143                 return;
144         }
145
146         expanded_.draw(pi, x, y);
147 }
148
149
150 void MathMacro::dump() const
151 {
152         MathMacroTable::dump();
153         lyxerr << "\n macro: '" << this << "'\n";
154         lyxerr << " name: '" << name() << "'\n";
155         lyxerr << " template: '";
156         WriteStream wi(lyxerr);
157         tmplate_->write(wi);
158         lyxerr << "'\n";
159 }
160
161
162 bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
163 {
164         pos_type pos;
165         if (up) {
166                 if (!MathNestInset::idxLeft(idx, pos))
167                         return false;
168                 pos = cell(idx).x2pos(x);
169                 return true;
170         } else {
171                 if (!MathNestInset::idxRight(idx, pos))
172                         return false;
173                 pos = cell(idx).x2pos(x);
174                 return true;
175         }
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" || name() == "mathcircumflex")
194                 features.require(name());
195         //MathInset::validate(features);
196 }
197
198
199 void MathMacro::maple(MapleStream & os) const
200 {
201         updateExpansion();
202         ::maple(expanded_, os);
203 }
204
205
206 void MathMacro::mathmlize(MathMLStream & os) const
207 {
208         updateExpansion();
209         ::mathmlize(expanded_, os);
210 }
211
212
213 void MathMacro::octave(OctaveStream & os) const
214 {
215         updateExpansion();
216         ::octave(expanded_, os);
217 }
218
219
220 void MathMacro::updateExpansion() const
221 {
222         expand();
223         expanded_.substitute(*this);
224 }
225
226
227 void MathMacro::infoize(std::ostream & os) const
228 {
229         os << "Macro: " << name();
230 }
231
232
233 void MathMacro::infoize2(std::ostream & os) const
234 {
235         os << "Macro: " << name();
236 }