]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
architectural changes to tex2lyx
[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 #include <config.h>
18
19 #include "math_macro.h"
20 #include "math_support.h"
21 #include "math_extern.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "math_mathmlstream.h"
25 #include "math_streamstr.h"
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
28 #include "debug.h"
29 #include "LaTeXFeatures.h"
30
31
32 using std::max;
33 using std::auto_ptr;
34 using std::endl;
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 auto_ptr<InsetBase> MathMacro::clone() const
51 {
52         return auto_ptr<InsetBase>(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(MetricsInfo & mi, Dimension & dim) const
76 {
77         augmentFont(font_, "lyxtex");
78         mi_ = mi;
79
80         if (defining()) {
81
82                 mathed_string_dim(font_, name(), dim_);
83
84         } else if (editing()) {
85
86                 expand();
87                 expanded_.metrics(mi_, dim_);
88                 metricsMarkers();
89
90                 dim_.wid +=  mathed_string_width(font_, name()) + 10;
91
92                 int ww = mathed_string_width(font_, "#1: ");
93
94                 for (idx_type i = 0; i < nargs(); ++i) {
95                         MathArray const & c = cell(i);
96                         c.metrics(mi_);
97                         dim_.wid  = max(dim_.wid, c.width() + ww);
98                         dim_.des += max(c.ascent(),  dim_.asc) + 5;
99                         dim_.des += max(c.descent(), dim_.des) + 5;
100                 }
101
102         } else {
103
104                 expand();
105                 expanded_.substitute(*this);
106                 expanded_.metrics(mi_, dim_);
107
108         }
109
110         dim = dim_;
111 }
112
113
114 void MathMacro::draw(PainterInfo & pi, int x, int y) const
115 {
116         metrics(mi_, dim_);
117
118         LyXFont texfont;
119         augmentFont(texfont, "lyxtex");
120
121         if (defining()) {
122                 drawStr(pi, texfont, x, y, name());
123                 return;
124         }
125
126         if (editing()) {
127                 int h = y - dim_.ascent() + 2 + expanded_.ascent();
128                 drawStr(pi, font_, x + 3, h, name());
129
130                 int const w = mathed_string_width(font_, name());
131                 expanded_.draw(pi, x + w + 12, h);
132                 h += expanded_.descent();
133
134                 Dimension ldim;
135                 mathed_string_dim(font_, "#1: ", ldim);
136
137                 for (idx_type i = 0; i < nargs(); ++i) {
138                         MathArray const & c = cell(i);
139                         h += max(c.ascent(), ldim.asc) + 5;
140                         c.draw(pi, x + ldim.wid, h);
141                         char str[] = "#1:";
142                         str[1] += static_cast<char>(i);
143                         drawStr(pi, texfont, x + 3, h, str);
144                         h += max(c.descent(), ldim.des) + 5;
145                 }
146                 return;
147         }
148
149         expanded_.draw(pi, x, y);
150 }
151
152
153 void MathMacro::dump() const
154 {
155         MathMacroTable::dump();
156         lyxerr << "\n macro: '" << this << "'\n"
157                << " name: '" << name() << "'\n"
158                << " template: '";
159         WriteStream wi(lyxerr);
160         tmplate_->write(wi);
161         lyxerr << "'" << endl;
162 }
163
164
165 bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
166 {
167         pos_type pos;
168         if (up) {
169                 if (!MathNestInset::idxLeft(idx, pos))
170                         return false;
171                 pos = cell(idx).x2pos(x);
172                 return true;
173         } else {
174                 if (!MathNestInset::idxRight(idx, pos))
175                         return false;
176                 pos = cell(idx).x2pos(x);
177                 return true;
178         }
179 }
180
181
182 bool MathMacro::idxLeft(idx_type &, pos_type &) const
183 {
184         return false;
185 }
186
187
188 bool MathMacro::idxRight(idx_type &, pos_type &) const
189 {
190         return false;
191 }
192
193
194 void MathMacro::validate(LaTeXFeatures & features) const
195 {
196         if (name() == "binom" || name() == "mathcircumflex")
197                 features.require(name());
198         //MathInset::validate(features);
199 }
200
201
202 void MathMacro::maple(MapleStream & os) const
203 {
204         updateExpansion();
205         ::maple(expanded_, os);
206 }
207
208
209 void MathMacro::mathmlize(MathMLStream & os) const
210 {
211         updateExpansion();
212         ::mathmlize(expanded_, os);
213 }
214
215
216 void MathMacro::octave(OctaveStream & os) const
217 {
218         updateExpansion();
219         ::octave(expanded_, os);
220 }
221
222
223 void MathMacro::updateExpansion() const
224 {
225         expand();
226         expanded_.substitute(*this);
227 }
228
229
230 void MathMacro::infoize(std::ostream & os) const
231 {
232         os << "Macro: " << name();
233 }
234
235
236 void MathMacro::infoize2(std::ostream & os) const
237 {
238         os << "Macro: " << name();
239 }