]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
enable direct input of #1...#9; some whitespace changes
[lyx.git] / src / mathed / math_scriptinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
10 #include "debug.h"
11
12
13 MathScriptInset::MathScriptInset()
14         : MathNestInset(2), limits_(0)
15 {
16         script_[0] = false;
17         script_[1] = false;
18 }
19
20
21 MathScriptInset::MathScriptInset(bool up)
22         : MathNestInset(2), limits_(0)
23 {
24         script_[0] = !up;
25         script_[1] = up;
26 }
27
28
29 MathInset * MathScriptInset::clone() const
30 {
31         return new MathScriptInset(*this);
32 }
33
34
35 MathScriptInset const * MathScriptInset::asScriptInset() const
36 {
37         return this;
38 }
39
40
41 MathScriptInset * MathScriptInset::asScriptInset()
42 {
43         return this;
44 }
45
46
47 MathXArray const & MathScriptInset::up() const
48 {
49         return xcell(1);
50 }
51
52
53 MathXArray const & MathScriptInset::down() const
54 {
55         return xcell(0);
56 }
57
58
59 MathXArray & MathScriptInset::up()
60 {
61         return xcell(1);
62 }
63
64
65 MathXArray & MathScriptInset::down()
66 {
67         return xcell(0);
68 }
69
70
71 void MathScriptInset::ensure(bool up)
72 {
73         script_[up] = true;
74 }
75
76
77 int MathScriptInset::dy0(MathInset const * nuc) const
78 {
79         int nd = ndes(nuc);
80         if (!hasDown())
81                 return nd;
82         int des = down().ascent();
83         if (hasLimits(nuc))
84                 des += nd + 2;
85         else 
86                 des = std::max(des, nd);
87         return des;
88 }
89
90
91 int MathScriptInset::dy1(MathInset const * nuc) const
92 {
93         int na = nasc(nuc);
94         if (!hasUp())
95                 return na;
96         int asc = up().descent();
97         if (hasLimits(nuc))
98                 asc += na + 2;
99         else 
100                 asc = std::max(asc, na);
101         asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
102         return asc;
103 }
104
105
106 int MathScriptInset::dx0(MathInset const * nuc) const
107 {
108         lyx::Assert(hasDown());
109         return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
110 }
111
112
113 int MathScriptInset::dx1(MathInset const * nuc) const
114 {
115         lyx::Assert(hasUp());
116         return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
117 }
118
119
120 int MathScriptInset::dxx(MathInset const * nuc) const
121 {
122         //lyx::Assert(nuc());
123         return hasLimits(nuc)  ?  (width2(nuc) - nwid(nuc)) / 2  :  0;
124 }
125
126
127 int MathScriptInset::ascent2(MathInset const * nuc) const
128 {
129         return dy1(nuc) + (hasUp() ? up().ascent() : 0);
130 }
131
132
133 int MathScriptInset::descent2(MathInset const * nuc) const
134 {
135         return dy0(nuc) + (hasDown() ? down().descent() : 0);
136 }
137
138
139 int MathScriptInset::width2(MathInset const * nuc) const
140 {
141         int wid = 0;
142         if (hasLimits(nuc)) {
143                 wid = nwid(nuc);
144                 if (hasUp())
145                         wid = std::max(wid, up().width());
146                 if (hasDown())
147                         wid = std::max(wid, down().width());
148         } else {
149                 if (hasUp())
150                         wid = std::max(wid, up().width());
151                 if (hasDown())
152                         wid = std::max(wid, down().width());
153                 wid += nwid(nuc);
154         }
155         return wid;
156 }
157
158
159 int MathScriptInset::nwid(MathInset const * nuc) const
160 {
161         return nuc ?
162                 nuc->width() :
163                 mathed_char_width(LM_TC_TEX, mi_, '.');
164 }
165
166
167 int MathScriptInset::nasc(MathInset const * nuc) const
168 {
169         return nuc ? nuc->ascent()
170                 : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
171 }
172
173
174 int MathScriptInset::ndes(MathInset const * nuc) const
175 {
176         return nuc ? nuc->descent()
177                 : mathed_char_descent(LM_TC_VAR, mi_, 'I');
178 }
179
180
181 void MathScriptInset::metrics(MathMetricsInfo const & mi) const
182 {       
183         metrics(0, mi);
184 }
185
186
187 void MathScriptInset::metrics(MathInset const * nuc,
188         MathMetricsInfo const & mi) const
189 {       
190         MathNestInset::metrics(mi);
191         if (nuc)
192                 nuc->metrics(mi);
193         ascent_  = ascent2(nuc);
194         descent_ = descent2(nuc);
195         width_   = width2(nuc);
196 }
197
198
199 void MathScriptInset::draw(Painter & pain, int x, int y) const
200 {  
201         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
202         draw(0, pain, x, y);
203 }
204
205
206 void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
207         int x, int y) const
208 {  
209         if (nuc)
210                 nuc->draw(pain, x + dxx(nuc), y);
211         else
212                 drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
213
214         if (hasUp())
215                 up().draw(pain, x + dx1(nuc), y - dy1(nuc));
216
217         if (hasDown())
218                 down().draw(pain, x + dx0(nuc), y + dy0(nuc));
219 }
220
221
222 bool MathScriptInset::hasLimits(MathInset const * nuc) const
223 {
224         // obviuos cases
225         if (limits_ == 1)
226                 return true;
227         if (limits_ == -1)
228                 return false;
229
230         // we can only display limits if the nucleus wants some 
231         if (!nuc)
232                 return false;
233         if (!nuc->isScriptable())
234                 return false;
235         
236         // per default \int has limits beside the \int even in displayed formulas
237         if (nuc->asSymbolInset())
238                 if (nuc->asSymbolInset()->name().find("int") != string::npos)
239                         return false;
240
241         // assume "real" limits for everything else
242         return true;
243 }
244
245
246 void MathScriptInset::removeEmptyScripts()
247 {
248         for (int i = 0; i <= 1; ++i)
249                 if (script_[i] && !cell(i).size())
250                         script_[i] = false;
251 }
252
253
254 void MathScriptInset::removeScript(bool up)
255 {
256         cell(up).clear();
257         script_[up] = false;
258 }
259
260
261 bool MathScriptInset::has(bool up) const
262 {
263         return script_[up];
264 }
265
266
267 bool MathScriptInset::hasUp() const
268 {
269         return script_[1];
270 }
271
272
273 bool MathScriptInset::hasDown() const
274 {
275         return script_[0];
276 }
277
278
279 bool MathScriptInset::idxRight(MathInset::idx_type &,
280                                  MathInset::pos_type &) const
281 {
282         return false;
283 }
284
285
286 bool MathScriptInset::idxLeft(MathInset::idx_type &,
287                                 MathInset::pos_type &) const
288 {
289         return false;
290 }
291
292
293 void MathScriptInset::write(WriteStream & os) const
294 {  
295         //lyxerr << "unexpected call to MathScriptInset::write()\n";
296         write2(0, os);
297 }
298
299
300 void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
301 {
302         if (nuc) {
303                 os << nuc;
304                 if (nuc->takesLimits()) {
305                         if (limits_ == -1)
306                                 os << "\\nolimits ";
307                         if (limits_ == 1)
308                                 os << "\\limits ";
309                 }
310         } else
311                         if (os.firstitem())
312                                 lyxerr << "suppressing {} \n";
313                         else
314                                 os << "{}";
315
316         if (hasDown() && down().data_.size())
317                 os << "_{" << down().data_ << '}';
318
319         if (hasUp() && up().data_.size())
320                 os << "^{" << up().data_ << '}';
321 }
322
323
324 void MathScriptInset::normalize(NormalStream & os) const
325 {  
326         //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
327         normalize2(0, os);
328 }
329
330
331 void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
332 {
333         bool d = hasDown() && down().data_.size();
334         bool u = hasUp() && up().data_.size();
335
336         if (u) 
337                 os << "[sup ";
338         if (d)
339                 os << "[sub ";
340         
341         if (nuc)
342                 os << nuc << ' ';
343         else
344                 os << "[par]";
345
346         if (d)
347                 os << down().data_ << ']';
348         if (u) 
349                 os << up().data_ << ']';
350 }
351
352
353 void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
354 {
355         if (nuc)
356                 os << nuc;
357         if (hasDown() && down().data_.size())
358                 os << '[' << down().data_ << ']';
359         if (hasUp() && up().data_.size())
360                 os << "^(" << up().data_ << ')';
361 }
362
363
364 void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
365 {
366         bool d = hasDown() && down().data_.size();
367         bool u = hasUp() && up().data_.size();
368
369         if (u && d)
370                 os << MTag("msubsup");
371         else if (u)
372                 os << MTag("msup");
373         else if (d)
374                 os << MTag("msub");
375
376         if (nuc)
377                 os << nuc;
378         else
379                 os << "<mrow/>";
380
381         if (u && d)
382                 os << down().data_ << up().data_ << ETag("msubsup");
383         else if (u)
384                 os << up().data_ << ETag("msup");
385         else if (d)
386                 os << down().data_ << ETag("msub");
387 }
388
389
390 void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
391 {
392         if (nuc)
393                 os << nuc;
394         if (hasDown() && down().data_.size())
395                 os << '[' << down().data_ << ']';
396         if (hasUp() && up().data_.size())
397                 os << "^(" << up().data_ << ')';
398 }
399
400