]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
up/down tweaks
[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         cell(2).metrics(mi);
188         MathScriptChanger dummy(mi.base);
189         cell(0).metrics(mi);
190         cell(1).metrics(mi);
191         dim_.w = 0;
192         if (hasLimits()) {
193                 dim_.w = nwid();
194                 if (hasUp())
195                         dim_.w = max(dim_.w, up().width());
196                 if (hasDown())
197                         dim_.w = max(dim_.w, down().width());
198         } else {
199                 if (hasUp())
200                         dim_.w = max(dim_.w, up().width());
201                 if (hasDown())
202                         dim_.w = max(dim_.w, down().width());
203                 dim_.w += nwid();
204         }
205         dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
206         dim_.d = dy0() + (hasDown() ? down().descent() : 0);
207         metricsMarkers2();
208 }
209
210
211 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
212 {
213         if (nuc().size())
214                 nuc().draw(pi, x + dxx(), y);
215         else if (editing())
216                 drawStr(pi, pi.base.font, x + dxx(), y, ".");
217         MathScriptChanger dummy(pi.base);
218         if (hasUp())
219                 up().draw(pi, x + dx1(), y - dy1());
220         if (hasDown())
221                 down().draw(pi, x + dx0(), y + dy0());
222         drawMarkers2(pi, x, y);
223 }
224
225
226 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
227 {
228         if (hasUp())
229                 up().metricsT(mi);
230         if (hasDown())
231                 down().metricsT(mi);
232         nuc().metricsT(mi);
233 }
234
235
236 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
237 {
238         if (nuc().size())
239                 nuc().drawT(pain, x + dxx(), y);
240         if (hasUp())
241                 up().drawT(pain, x + dx1(), y - dy1());
242         if (hasDown())
243                 down().drawT(pain, x + dx0(), y + dy0());
244 }
245
246
247
248 bool MathScriptInset::hasLimits() const
249 {
250         // obvious cases
251         if (limits_ == 1)
252                 return true;
253         if (limits_ == -1)
254                 return false;
255
256         // we can only display limits if the nucleus wants some
257         if (!nuc().size())
258                 return false;
259         if (!nuc().back()->isScriptable())
260                 return false;
261
262         // per default \int has limits beside the \int even in displayed formulas
263         if (nuc().back()->asSymbolInset())
264                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
265                         return false;
266
267         // assume "real" limits for everything else
268         return true;
269 }
270
271
272 void MathScriptInset::removeEmptyScripts()
273 {
274         for (int i = 0; i <= 1; ++i)
275                 if (script_[i] && cell(i).size() == 0) {
276                         cell(i).clear();
277                         script_[i] = false;
278                 }
279 }
280
281
282 void MathScriptInset::removeScript(bool up)
283 {
284         cell(up).clear();
285         script_[up] = false;
286 }
287
288
289 bool MathScriptInset::has(bool up) const
290 {
291         return script_[up];
292 }
293
294
295 bool MathScriptInset::empty() const
296 {
297         return !script_[0] && !script_[1] && cell(2).empty();
298 }
299
300
301 bool MathScriptInset::hasUp() const
302 {
303         return script_[1];
304 }
305
306
307 bool MathScriptInset::hasDown() const
308 {
309         return script_[0];
310 }
311
312
313 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
314 {
315         return false;
316 }
317
318
319 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
320 {
321         return false;
322 }
323
324
325 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
326         int) const
327 {
328         if (idx == 1) {
329                 // if we are 'up' we can't go further up
330                 if (up)
331                         return false;
332                 // otherwise go to last base position
333                 idx = 2;
334                 pos = cell(2).size();   
335         }
336
337         else if (idx == 0) {
338                 // if we are 'down' we can't go further down
339                 if (!up)
340                         return false;
341                 idx = 2;
342                 pos = cell(2).size();   
343         }
344         
345         else {
346                 // in nucleus
347                 // don't go up/down unless in last position
348                 if (pos != cell(2).size())
349                         return false;   
350                 // don't go up/down if there is no cell.
351                 if (!has(up))
352                         return false;
353                 // otherwise move into the first position
354                 idx = up;
355                 pos = 0;
356         }
357         return true;
358 }
359
360
361 void MathScriptInset::write(WriteStream & os) const
362 {
363         if (nuc().size()) {
364                 os << nuc();
365                 if (nuc().back()->takesLimits()) {
366                         if (limits_ == -1)
367                                 os << "\\nolimits ";
368                         if (limits_ == 1)
369                                 os << "\\limits ";
370                 }
371         } else {
372                 if (os.firstitem())
373                         lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
374                 else
375                         os << "{}";
376         }
377
378         if (hasDown() && down().size())
379                 os << "_{" << down() << '}';
380
381         if (hasUp() && up().size())
382                 os << "^{" << up() << '}';
383
384         if (lock_ && !os.latex())
385                 os << "\\lyxlock ";
386 }
387
388
389 void MathScriptInset::normalize(NormalStream & os) const
390 {
391         bool d = hasDown() && down().size();
392         bool u = hasUp() && up().size();
393
394         if (u && d)
395                 os << "[subsup ";
396         else if (u)
397                 os << "[sup ";
398         else if (d)
399                 os << "[sub ";
400
401         if (nuc().size())
402                 os << nuc() << ' ';
403         else
404                 os << "[par]";
405
406         if (u && d)
407                 os << down() << ' ' << up() << ']';
408         else if (d)
409                 os << down() << ']';
410         else if (u)
411                 os << up() << ']';
412 }
413
414
415 void MathScriptInset::maplize(MapleStream & os) const
416 {
417         if (nuc().size())
418                 os << nuc();
419         if (hasDown() && down().size())
420                 os << '[' << down() << ']';
421         if (hasUp() && up().size())
422                 os << "^(" << up() << ')';
423 }
424
425
426 void MathScriptInset::mathematicize(MathematicaStream & os) const
427 {
428         bool d = hasDown() && down().size();
429         bool u = hasUp() && up().size();
430
431         if (nuc().size()) {
432                 if (d) 
433                         os << "Subscript[" << nuc();
434                 else
435                         os << nuc();
436         }
437
438         if (u)
439                 os << "^(" << up() << ")";
440
441         if (nuc().size())
442                 if (d)
443                         os << "," << down() << "]"; 
444 }
445
446
447 void MathScriptInset::mathmlize( MathMLStream & os) const
448 {
449         bool d = hasDown() && down().size();
450         bool u = hasUp() && up().size();
451
452         if (u && d)
453                 os << MTag("msubsup");
454         else if (u)
455                 os << MTag("msup");
456         else if (d)
457                 os << MTag("msub");
458
459         if (nuc().size())
460                 os << nuc();
461         else
462                 os << "<mrow/>";
463
464         if (u && d)
465                 os << down() << up() << ETag("msubsup");
466         else if (u)
467                 os << up() << ETag("msup");
468         else if (d)
469                 os << down() << ETag("msub");
470 }
471
472
473 void MathScriptInset::octavize(OctaveStream & os) const
474 {
475         if (nuc().size())
476                 os << nuc();
477         if (hasDown() && down().size())
478                 os << '[' << down() << ']';
479         if (hasUp() && up().size())
480                 os << "^(" << up() << ')';
481 }
482
483
484 void MathScriptInset::infoize(std::ostream & os) const
485 {
486         os << "Scripts";
487         if (limits_)
488                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
489 }