]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
the clone auto_ptr patch
[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 using std::auto_ptr;
33
34
35 MathMacro::MathMacro(string const & name)
36         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
37                 tmplate_(MathMacroTable::provide(name))
38 {}
39
40
41 MathMacro::MathMacro(MathMacro const & m)
42         : MathNestInset(m),
43                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
44 {}
45
46
47
48 auto_ptr<InsetBase> MathMacro::clone() const
49 {
50         return auto_ptr<InsetBase>(new MathMacro(*this));
51 }
52
53
54 string MathMacro::name() const
55 {
56         return tmplate_->asMacroTemplate()->name();
57 }
58
59
60 bool MathMacro::defining() const
61 {
62         return 0;
63         //return mathcursor && mathcursor->formula()->getInsetName() == name();
64 }
65
66
67 void MathMacro::expand() const
68 {
69         expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
70 }
71
72
73 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
74 {
75         augmentFont(font_, "lyxtex");
76         mi_ = mi;
77
78         if (defining()) {
79
80                 mathed_string_dim(font_, name(), dim_);
81
82         } else if (editing()) {
83
84                 expand();
85                 expanded_.metrics(mi_, dim_);
86                 metricsMarkers();
87
88                 dim_.wid +=  mathed_string_width(font_, name()) + 10;
89
90                 int ww = mathed_string_width(font_, "#1: ");
91
92                 for (idx_type i = 0; i < nargs(); ++i) {
93                         MathArray const & c = cell(i);
94                         c.metrics(mi_);
95                         dim_.wid  = max(dim_.wid, c.width() + ww);
96                         dim_.des += max(c.ascent(),  dim_.asc) + 5;
97                         dim_.des += max(c.descent(), dim_.des) + 5;
98                 }
99
100         } else {
101
102                 expand();
103                 expanded_.substitute(*this);
104                 expanded_.metrics(mi_, dim_);
105
106         }
107
108         dim = dim_;
109 }
110
111
112 void MathMacro::draw(PainterInfo & pi, int x, int y) const
113 {
114         metrics(mi_, dim_);
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 - dim_.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.asc) + 5;
138                         c.draw(pi, x + ldim.wid, 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.des) + 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::maple(MapleStream & os) const
201 {
202         updateExpansion();
203         ::maple(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::octave(OctaveStream & os) const
215 {
216         updateExpansion();
217         ::octave(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 }