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