]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
the up/down stuff reworked
[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 using std::max;
14
15
16 MathScriptInset::MathScriptInset()
17         : MathNestInset(3), limits_(0)
18 {
19         script_[0] = false;
20         script_[1] = false;
21 }
22
23
24 MathScriptInset::MathScriptInset(bool up)
25         : MathNestInset(3), limits_(0)
26 {
27         script_[0] = !up;
28         script_[1] = up;
29 }
30
31
32 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
33         : MathNestInset(3), limits_(0)
34 {
35         script_[0] = !up;
36         script_[1] = up;
37         cell(2).push_back(at);
38 }
39
40
41
42 MathInset * MathScriptInset::clone() const
43 {
44         return new MathScriptInset(*this);
45 }
46
47
48 MathScriptInset const * MathScriptInset::asScriptInset() const
49 {
50         return this;
51 }
52
53
54 MathScriptInset * MathScriptInset::asScriptInset()
55 {
56         return this;
57 }
58
59
60 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
61 {
62         idx = 2;
63         pos = nuc().size();
64         return true;
65 }
66
67
68 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
69 {
70         idx = 2;
71         pos = nuc().size();
72         return true;
73 }
74
75
76 MathXArray const & MathScriptInset::down() const
77 {
78         return xcell(0);
79 }
80
81
82 MathXArray & MathScriptInset::down()
83 {
84         return xcell(0);
85 }
86
87
88 MathXArray const & MathScriptInset::up() const
89 {
90         return xcell(1);
91 }
92
93
94 MathXArray & MathScriptInset::up()
95 {
96         return xcell(1);
97 }
98
99
100 void MathScriptInset::ensure(bool up)
101 {
102         script_[up] = true;
103 }
104
105
106 MathXArray const & MathScriptInset::nuc() const
107 {
108         return xcell(2);
109 }
110
111
112 MathXArray & MathScriptInset::nuc()
113 {
114         return xcell(2);
115 }
116
117
118 int MathScriptInset::dy0() const
119 {
120         int nd = ndes();
121         if (!hasDown())
122                 return nd;
123         int des = down().ascent();
124         if (hasLimits())
125                 des += nd + 2;
126         else
127                 des = max(des, nd);
128         return des;
129 }
130
131
132 int MathScriptInset::dy1() const
133 {
134         int na = nasc();
135         if (!hasUp())
136                 return na;
137         int asc = up().descent();
138         if (hasLimits())
139                 asc += na + 2;
140         else
141                 asc = max(asc, na);
142         asc = max(asc, mathed_char_ascent(font_, 'I'));
143         return asc;
144 }
145
146
147 int MathScriptInset::dx0() const
148 {
149         lyx::Assert(hasDown());
150         return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
151 }
152
153
154 int MathScriptInset::dx1() const
155 {
156         lyx::Assert(hasUp());
157         return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
158 }
159
160
161 int MathScriptInset::dxx() const
162 {
163         return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
164 }
165
166
167 int MathScriptInset::nwid() const
168 {
169         return nuc().size() ? nuc().width() : mathed_char_width(font_, '.');
170 }
171
172
173 int MathScriptInset::nasc() const
174 {
175         return nuc().size() ? nuc().ascent() : mathed_char_ascent(font_, 'I');
176 }
177
178
179 int MathScriptInset::ndes() const
180 {
181         return nuc().size() ? nuc().descent() : mathed_char_descent(font_, 'I');
182 }
183
184
185 void MathScriptInset::metrics(MathMetricsInfo & mi) const
186 {
187         MathNestInset::metrics(mi);
188         MathScriptChanger dummy(mi.base);
189         dim_.w = 0;
190         if (hasLimits()) {
191                 dim_.w = nwid();
192                 if (hasUp())
193                         dim_.w = max(dim_.w, up().width());
194                 if (hasDown())
195                         dim_.w = max(dim_.w, down().width());
196         } else {
197                 if (hasUp())
198                         dim_.w = max(dim_.w, up().width());
199                 if (hasDown())
200                         dim_.w = max(dim_.w, down().width());
201                 dim_.w += nwid();
202         }
203         dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
204         dim_.d = dy0() + (hasDown() ? down().descent() : 0);
205         metricsMarkers();
206 }
207
208
209 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
210 {
211         if (nuc().size())
212                 nuc().draw(pi, x + dxx(), y);
213         else if (editing())
214                 drawStr(pi, font_, x + dxx(), y, ".");
215         MathScriptChanger dummy(pi.base);
216         if (hasUp())
217                 up().draw(pi, x + dx1(), y - dy1());
218         if (hasDown())
219                 down().draw(pi, x + dx0(), y + dy0());
220         drawMarkers(pi, x, y);
221 }
222
223
224 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
225 {
226         if (hasUp())
227                 up().metricsT(mi);
228         if (hasDown())
229                 down().metricsT(mi);
230         nuc().metricsT(mi);
231 }
232
233
234 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
235 {
236         if (nuc().size())
237                 nuc().drawT(pain, x + dxx(), y);
238         if (hasUp())
239                 up().drawT(pain, x + dx1(), y - dy1());
240         if (hasDown())
241                 down().drawT(pain, x + dx0(), y + dy0());
242 }
243
244
245
246 bool MathScriptInset::hasLimits() const
247 {
248         // obvious cases
249         if (limits_ == 1)
250                 return true;
251         if (limits_ == -1)
252                 return false;
253
254         // we can only display limits if the nucleus wants some
255         if (!nuc().size())
256                 return false;
257         if (!nuc().back()->isScriptable())
258                 return false;
259
260         // per default \int has limits beside the \int even in displayed formulas
261         if (nuc().back()->asSymbolInset())
262                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
263                         return false;
264
265         // assume "real" limits for everything else
266         return true;
267 }
268
269
270 void MathScriptInset::removeEmptyScripts()
271 {
272         for (int i = 0; i <= 1; ++i)
273                 if (script_[i] && cell(i).size() == 0) {
274                         cell(i).clear();
275                         script_[i] = false;
276                 }
277 }
278
279
280 void MathScriptInset::removeScript(bool up)
281 {
282         cell(up).clear();
283         script_[up] = false;
284 }
285
286
287 bool MathScriptInset::has(bool up) const
288 {
289         return script_[up];
290 }
291
292
293 bool MathScriptInset::empty() const
294 {
295         return !script_[0] && !script_[1] && cell(2).empty();
296 }
297
298
299 bool MathScriptInset::hasUp() const
300 {
301         return script_[1];
302 }
303
304
305 bool MathScriptInset::hasDown() const
306 {
307         return script_[0];
308 }
309
310
311 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
312 {
313         return false;
314 }
315
316
317 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
318 {
319         return false;
320 }
321
322
323 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
324         int) const
325 {
326         if ((idx == 1 && up) || (idx == 0 && !up))
327                 return false;
328
329         // in nuclues?
330         if (idx == 2) {
331                 idx = up;
332                 pos = 0;
333         } else {
334                 idx = 2;
335                 pos = cell(2).size();   
336         }
337         return true;
338 }
339
340
341 void MathScriptInset::write(WriteStream & os) const
342 {
343         if (nuc().size()) {
344                 os << nuc().data();
345                 if (nuc().back()->takesLimits()) {
346                         if (limits_ == -1)
347                                 os << "\\nolimits ";
348                         if (limits_ == 1)
349                                 os << "\\limits ";
350                 }
351         } else {
352                 if (os.firstitem())
353                         lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
354                 else
355                         os << "{}";
356         }
357
358         if (hasDown() && down().data().size())
359                 os << "_{" << down().data() << '}';
360
361         if (hasUp() && up().data().size())
362                 os << "^{" << up().data() << '}';
363 }
364
365
366 void MathScriptInset::normalize(NormalStream & os) const
367 {
368         bool d = hasDown() && down().size();
369         bool u = hasUp() && up().size();
370
371         if (u)
372                 os << "[sup ";
373         if (d)
374                 os << "[sub ";
375
376         if (nuc().size())
377                 os << nuc().data() << ' ';
378         else
379                 os << "[par]";
380
381         if (d)
382                 os << down().data() << ']';
383         if (u)
384                 os << up().data() << ']';
385 }
386
387
388 void MathScriptInset::maplize(MapleStream & os) const
389 {
390         if (nuc().size())
391                 os << nuc().data();
392         if (hasDown() && down().size())
393                 os << '[' << down().data() << ']';
394         if (hasUp() && up().size())
395                 os << "^(" << up().data() << ')';
396 }
397
398
399 void MathScriptInset::mathematicize(MathematicaStream & os) const
400 {
401         bool d = hasDown() && down().size();
402         bool u = hasUp() && up().size();
403
404         if (nuc().size()) {
405                 if (d) 
406                         os << "Subscript[" << nuc().data();
407                 else
408                         os << nuc().data();
409         }
410
411         if (u)
412                 os << "^(" << up().data() << ")";
413
414         if (nuc().size())
415                 if (d)
416                         os << "," << down().data() << "]"; 
417 }
418
419
420 void MathScriptInset::mathmlize( MathMLStream & os) const
421 {
422         bool d = hasDown() && down().data().size();
423         bool u = hasUp() && up().data().size();
424
425         if (u && d)
426                 os << MTag("msubsup");
427         else if (u)
428                 os << MTag("msup");
429         else if (d)
430                 os << MTag("msub");
431
432         if (nuc().size())
433                 os << nuc().data();
434         else
435                 os << "<mrow/>";
436
437         if (u && d)
438                 os << down().data() << up().data() << ETag("msubsup");
439         else if (u)
440                 os << up().data() << ETag("msup");
441         else if (d)
442                 os << down().data() << ETag("msub");
443 }
444
445
446 void MathScriptInset::octavize(OctaveStream & os) const
447 {
448         if (nuc().size())
449                 os << nuc().data();
450         if (hasDown() && down().size())
451                 os << '[' << down().data() << ']';
452         if (hasUp() && up().size())
453                 os << "^(" << up().data() << ')';
454 }
455
456
457 void MathScriptInset::infoize(std::ostream & os) const
458 {
459         if (limits_)
460                 os << (limits_ == 1 ? "Displayed limits" : "Inlined limits");
461 }