]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
show the 'empty base dot' less frequently
[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 "support/lstrings.h"
28 #include "support/LAssert.h"
29 #include "debug.h"
30 #include "Painter.h"
31 #include "LaTeXFeatures.h"
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 const char * MathMacro::name() const
54 {
55         return tmplate_->asMacroTemplate()->name().c_str();
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::metrics(MathMetricsInfo const & mi) const
67 {
68         mi_ = mi;
69
70         if (defining()) {
71                 mathed_string_dim(LM_TC_TEX, mi_, name(), ascent_, descent_, width_);
72                 return;
73         }
74
75         if (editing()) {
76                 expanded_ = tmplate_->xcell(0);
77                 expanded_.metrics(mi_);
78                 width_   = expanded_.width()   + 4;
79                 ascent_  = expanded_.ascent()  + 2;
80                 descent_ = expanded_.descent() + 2;
81
82                 width_ +=  mathed_string_width(LM_TC_TEXTRM, mi_, name()) + 10;
83
84                 int lasc;
85                 int ldes;
86                 int lwid;
87                 mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
88
89                 for (idx_type i = 0; i < nargs(); ++i) {
90                         MathXArray const & c = xcell(i);
91                         c.metrics(mi_);
92                         width_    = std::max(width_, c.width() + lwid);
93                         descent_ += std::max(c.ascent(),  lasc) + 5;
94                         descent_ += std::max(c.descent(), ldes) + 5;
95                 }
96                 return;
97         } 
98
99         expanded_ = tmplate_->xcell(0);
100         expanded_.data_.substitute(*this);
101         expanded_.metrics(mi_);
102         width_   = expanded_.width();
103         ascent_  = expanded_.ascent();
104         descent_ = expanded_.descent();
105 }
106
107
108 void MathMacro::draw(Painter & pain, int x, int y) const
109 {
110         metrics(mi_);
111
112         if (defining()) {
113                 drawStr(pain, LM_TC_TEX, mi_, x, y, name());
114                 return;
115         }
116
117         if (editing()) {
118                 int h = y - ascent() + 2 + expanded_.ascent();
119                 drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, h, name());
120
121                 int const w = mathed_string_width(LM_TC_TEXTRM, mi_, name());
122                 expanded_.draw(pain, x + w + 12, h);
123                 h += expanded_.descent();
124
125                 int lasc;
126                 int ldes;
127                 int lwid;
128                 mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
129
130                 for (idx_type i = 0; i < nargs(); ++i) {
131                         MathXArray const & c = xcell(i);
132                         h += std::max(c.ascent(), lasc) + 5;
133                         c.draw(pain, x + lwid, h);
134                         char str[] = "#1:";
135                         str[1] += static_cast<char>(i);
136                         drawStr(pain, LM_TC_TEX, mi_, x + 3, h, str);
137                         h += std::max(c.descent(), ldes) + 5;
138                 }
139                 return;
140         }
141
142         expanded_.draw(pain, x, y);
143 }
144
145
146 void MathMacro::dump() const
147 {
148         MathMacroTable::dump();
149         lyxerr << "\n macro: '" << this << "'\n";
150         lyxerr << " name: '" << name() << "'\n";
151         lyxerr << " template: '";
152         WriteStream wi(lyxerr);
153         tmplate_->write(wi);
154         lyxerr << "'\n";
155 }
156
157
158 bool MathMacro::idxUp(idx_type & idx) const
159 {
160         pos_type pos;
161         return MathNestInset::idxLeft(idx, pos);
162 }
163
164
165 bool MathMacro::idxDown(idx_type & idx) const
166 {
167         pos_type pos;
168         return MathNestInset::idxRight(idx, pos);
169 }
170
171
172 bool MathMacro::idxLeft(idx_type &, pos_type &) const
173 {
174         return false;
175 }
176
177
178 bool MathMacro::idxRight(idx_type &, pos_type &) const
179 {
180         return false;
181 }
182
183
184 void MathMacro::validate(LaTeXFeatures & features) const
185 {
186         if (name() == "binom")
187                 features.require("binom");
188         //MathInset::validate(features);
189 }
190
191
192 void MathMacro::maplize(MapleStream & os) const
193 {
194         updateExpansion();
195         ::maplize(expanded_.data_, os);
196 }
197
198
199 void MathMacro::mathmlize(MathMLStream & os) const
200 {
201         updateExpansion();
202         ::mathmlize(expanded_.data_, os);
203 }
204
205
206 void MathMacro::octavize(OctaveStream & os) const
207 {
208         updateExpansion();
209         ::octavize(expanded_.data_, os);
210 }
211
212
213 void MathMacro::normalize(NormalStream & os) const
214 {
215         os << "[macro " << name() << " ";
216         for (idx_type i = 0; i < nargs(); ++i) 
217                 os << cell(i) << ' ';
218         os << ']';
219 }
220
221
222 void MathMacro::write(WriteStream & os) const
223 {
224         os << '\\' << name();
225         for (idx_type i = 0; i < nargs(); ++i)
226                 os << '{' << cell(i) << '}';
227         if (nargs() == 0) 
228                 os << ' ';
229 }
230
231
232 void MathMacro::updateExpansion() const
233 {
234         expanded_ = tmplate_->xcell(0);
235         expanded_.data_.substitute(*this);
236 }