]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
merge MathArray and MathXArray classes.
[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 = 0;
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 MathArray const & MathScriptInset::down() const
77 {
78         return cell(0);
79 }
80
81
82 MathArray & MathScriptInset::down()
83 {
84         return cell(0);
85 }
86
87
88 MathArray const & MathScriptInset::up() const
89 {
90         return cell(1);
91 }
92
93
94 MathArray & MathScriptInset::up()
95 {
96         return cell(1);
97 }
98
99
100 void MathScriptInset::ensure(bool up)
101 {
102         script_[up] = true;
103 }
104
105
106 MathArray const & MathScriptInset::nuc() const
107 {
108         return cell(2);
109 }
110
111
112 MathArray & MathScriptInset::nuc()
113 {
114         return cell(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, 5);
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() : 2;
170 }
171
172
173 int MathScriptInset::nasc() const
174 {
175         return nuc().size() ? nuc().ascent() : 5;
176 }
177
178
179 int MathScriptInset::ndes() const
180 {
181         return nuc().size() ? nuc().descent() : 0;
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         metricsMarkers2();
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, pi.base.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         drawMarkers2(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();
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().size())
359                 os << "_{" << down() << '}';
360
361         if (hasUp() && up().size())
362                 os << "^{" << up() << '}';
363
364         if (lock_ && !os.latex())
365                 os << "\\lyxlock ";
366 }
367
368
369 void MathScriptInset::normalize(NormalStream & os) const
370 {
371         bool d = hasDown() && down().size();
372         bool u = hasUp() && up().size();
373
374         if (u && d)
375                 os << "[subsup ";
376         else if (u)
377                 os << "[sup ";
378         else if (d)
379                 os << "[sub ";
380
381         if (nuc().size())
382                 os << nuc() << ' ';
383         else
384                 os << "[par]";
385
386         if (u && d)
387                 os << down() << ' ' << up() << ']';
388         else if (d)
389                 os << down() << ']';
390         else if (u)
391                 os << up() << ']';
392 }
393
394
395 void MathScriptInset::maplize(MapleStream & os) const
396 {
397         if (nuc().size())
398                 os << nuc();
399         if (hasDown() && down().size())
400                 os << '[' << down() << ']';
401         if (hasUp() && up().size())
402                 os << "^(" << up() << ')';
403 }
404
405
406 void MathScriptInset::mathematicize(MathematicaStream & os) const
407 {
408         bool d = hasDown() && down().size();
409         bool u = hasUp() && up().size();
410
411         if (nuc().size()) {
412                 if (d) 
413                         os << "Subscript[" << nuc();
414                 else
415                         os << nuc();
416         }
417
418         if (u)
419                 os << "^(" << up() << ")";
420
421         if (nuc().size())
422                 if (d)
423                         os << "," << down() << "]"; 
424 }
425
426
427 void MathScriptInset::mathmlize( MathMLStream & os) const
428 {
429         bool d = hasDown() && down().size();
430         bool u = hasUp() && up().size();
431
432         if (u && d)
433                 os << MTag("msubsup");
434         else if (u)
435                 os << MTag("msup");
436         else if (d)
437                 os << MTag("msub");
438
439         if (nuc().size())
440                 os << nuc();
441         else
442                 os << "<mrow/>";
443
444         if (u && d)
445                 os << down() << up() << ETag("msubsup");
446         else if (u)
447                 os << up() << ETag("msup");
448         else if (d)
449                 os << down() << ETag("msub");
450 }
451
452
453 void MathScriptInset::octavize(OctaveStream & os) const
454 {
455         if (nuc().size())
456                 os << nuc();
457         if (hasDown() && down().size())
458                 os << '[' << down() << ']';
459         if (hasUp() && up().size())
460                 os << "^(" << up() << ')';
461 }
462
463
464 void MathScriptInset::infoize(std::ostream & os) const
465 {
466         os << "Scripts";
467         if (limits_)
468                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
469 }