]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
shut up compiler warning message
[lyx.git] / src / mathed / math_scriptinset.C
1 /**
2  * \file math_scriptinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_scriptinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_support.h"
17 #include "math_symbolinset.h"
18 #include "dispatchresult.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "funcrequest.h"
22
23 #include <boost/assert.hpp>
24
25 using std::string;
26 using std::max;
27 using std::auto_ptr;
28 using std::endl;
29
30
31
32 MathScriptInset::MathScriptInset()
33         : MathNestInset(1), cell_1_is_up_(false), limits_(0)
34 {}
35
36
37 MathScriptInset::MathScriptInset(bool up)
38         : MathNestInset(2), cell_1_is_up_(up), limits_(0)
39 {}
40
41
42 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
43         : MathNestInset(2), cell_1_is_up_(up), limits_(0)
44 {
45         BOOST_ASSERT(nargs() >= 1);
46         cell(0).push_back(at);
47 }
48
49
50 auto_ptr<InsetBase> MathScriptInset::doClone() const
51 {
52         return auto_ptr<InsetBase>(new MathScriptInset(*this));
53 }
54
55
56 MathScriptInset const * MathScriptInset::asScriptInset() const
57 {
58         return this;
59 }
60
61
62 MathScriptInset * MathScriptInset::asScriptInset()
63 {
64         return this;
65 }
66
67
68 bool MathScriptInset::idxFirst(LCursor & cur) const
69 {
70         cur.idx() = 0;
71         cur.pos() = 0;
72         return true;
73 }
74
75
76 bool MathScriptInset::idxLast(LCursor & cur) const
77 {
78         cur.idx() = 0;
79         cur.pos() = nuc().size();
80         return true;
81 }
82
83
84 MathArray const & MathScriptInset::down() const
85 {
86         if (nargs() == 3)
87                 return cell(2);
88         BOOST_ASSERT(nargs() > 1);
89         return cell(1);
90 }
91
92
93 MathArray & MathScriptInset::down()
94 {
95         if (nargs() == 3)
96                 return cell(2);
97         BOOST_ASSERT(nargs() > 1);
98         return cell(1);
99 }
100
101
102 MathArray const & MathScriptInset::up() const
103 {
104         BOOST_ASSERT(nargs() > 1);
105         return cell(1);
106 }
107
108
109 MathArray & MathScriptInset::up()
110 {
111         BOOST_ASSERT(nargs() > 1);
112         return cell(1);
113 }
114
115
116 void MathScriptInset::ensure(bool up)
117 {
118         if (nargs() == 1) {
119                 // just nucleus so far
120                 cells_.push_back(MathArray());
121                 cell_1_is_up_ = up;
122         } else if (nargs() == 2 && !has(up)) {
123                 if (up) {
124                         cells_.push_back(cell(1));
125                         cell(1).clear();
126                 } else {
127                         cells_.push_back(MathArray());
128                 }
129         }
130 }
131
132
133 MathArray const & MathScriptInset::nuc() const
134 {
135         return cell(0);
136 }
137
138
139 MathArray & MathScriptInset::nuc()
140 {
141         return cell(0);
142 }
143
144
145 int MathScriptInset::dy0() const
146 {
147         int nd = ndes();
148         if (!hasDown())
149                 return nd;
150         int des = down().ascent();
151         if (hasLimits())
152                 des += nd + 2;
153         else
154                 des = max(des, nd);
155         return des;
156 }
157
158
159 int MathScriptInset::dy1() const
160 {
161         int na = nasc();
162         if (!hasUp())
163                 return na;
164         int asc = up().descent();
165         if (hasLimits())
166                 asc += na + 2;
167         else
168                 asc = max(asc, na);
169         asc = max(asc, 5);
170         return asc;
171 }
172
173
174 int MathScriptInset::dx0() const
175 {
176         BOOST_ASSERT(hasDown());
177         return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
178 }
179
180
181 int MathScriptInset::dx1() const
182 {
183         BOOST_ASSERT(hasUp());
184         return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
185 }
186
187
188 int MathScriptInset::dxx() const
189 {
190         return hasLimits() ? (dim_.wid - nwid()) / 2  :  0;
191 }
192
193
194 int MathScriptInset::nwid() const
195 {
196         return nuc().size() ? nuc().width() : 2;
197 }
198
199
200 int MathScriptInset::nasc() const
201 {
202         return nuc().size() ? nuc().ascent() : 5;
203 }
204
205
206 int MathScriptInset::ndes() const
207 {
208         return nuc().size() ? nuc().descent() : 0;
209 }
210
211
212 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
213 {
214         cell(0).metrics(mi);
215         ScriptChanger dummy(mi.base);
216         if (nargs() > 1)
217                 cell(1).metrics(mi);
218         if (nargs() > 2)
219                 cell(2).metrics(mi);
220         dim.wid = 0;
221         if (hasLimits()) {
222                 dim.wid = nwid();
223                 if (hasUp())
224                         dim.wid = max(dim.wid, up().width());
225                 if (hasDown())
226                         dim.wid = max(dim.wid, down().width());
227         } else {
228                 if (hasUp())
229                         dim.wid = max(dim.wid, up().width());
230                 if (hasDown())
231                         dim.wid = max(dim.wid, down().width());
232                 dim.wid += nwid();
233         }
234         dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
235         dim.des = dy0() + (hasDown() ? down().descent() : 0);
236         metricsMarkers(dim);
237         dim_ = dim;
238 }
239
240
241 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
242 {
243         if (nuc().size())
244                 nuc().draw(pi, x + dxx(), y);
245         else {
246                 nuc().setXY(x + dxx(), y);
247                 if (editing(pi.base.bv))
248                         pi.draw(x + dxx(), y, ".");
249         }
250         ScriptChanger dummy(pi.base);
251         if (hasUp())
252                 up().draw(pi, x + dx1(), y - dy1());
253         if (hasDown())
254                 down().draw(pi, x + dx0(), y + dy0());
255         drawMarkers(pi, x, y);
256 }
257
258
259 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
260 {
261         if (hasUp())
262                 up().metricsT(mi, dim);
263         if (hasDown())
264                 down().metricsT(mi, dim);
265         nuc().metricsT(mi, dim);
266 }
267
268
269 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
270 {
271         if (nuc().size())
272                 nuc().drawT(pain, x + dxx(), y);
273         if (hasUp())
274                 up().drawT(pain, x + dx1(), y - dy1());
275         if (hasDown())
276                 down().drawT(pain, x + dx0(), y + dy0());
277 }
278
279
280
281 bool MathScriptInset::hasLimits() const
282 {
283         // obvious cases
284         if (limits_ == 1)
285                 return true;
286         if (limits_ == -1)
287                 return false;
288
289         // we can only display limits if the nucleus wants some
290         if (!nuc().size())
291                 return false;
292         if (!nuc().back()->isScriptable())
293                 return false;
294
295         if (nuc().back()->asSymbolInset()) {
296                 // \intop is an alias for \int\limits, \ointop == \oint\limits
297                 if (nuc().back()->asSymbolInset()->name().find("intop") != string::npos)
298                         return true;
299                 // per default \int has limits beside the \int even in displayed formulas
300                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
301                         return false;
302         }
303
304         // assume "real" limits for everything else
305         return true;
306 }
307
308
309 void MathScriptInset::removeScript(bool up)
310 {
311         lyxerr << "MathScriptInset::removeScript: 1 up: " << up << endl;
312         if (nargs() == 2) {
313                 lyxerr << "MathScriptInset::removeScript: a up: " << up << endl;
314                 if (up == cell_1_is_up_)
315                         cells_.pop_back();
316                 lyxerr << "MathScriptInset::removeScript: b up: " << up << endl;
317         } else if (nargs() == 3) {
318                 if (up == true) {
319                         swap(cells_[1], cells_[2]);
320                         cell_1_is_up_ = false;
321                 } else {
322                         cell_1_is_up_ = true;
323                 }
324                 cells_.pop_back();
325         }
326         lyxerr << "MathScriptInset::removeScript: 2 up: " << up << endl;
327 }
328
329
330 bool MathScriptInset::has(bool up) const
331 {
332         return idxOfScript(up);
333 }
334
335
336 bool MathScriptInset::hasUp() const
337 {
338         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
339         //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
340         return idxOfScript(true);
341 }
342
343
344 bool MathScriptInset::hasDown() const
345 {
346         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
347         //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
348         return idxOfScript(false);
349 }
350
351
352 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
353 {
354         if (nargs() == 1)
355                 return 0;
356         if (nargs() == 2)
357                 return (cell_1_is_up_ == up) ? 1 : 0;
358         if (nargs() == 3)
359                 return up ? 1 : 2;
360         BOOST_ASSERT(false);
361         // Silence compiler
362         return 0;
363 }
364
365
366 bool MathScriptInset::idxRight(LCursor &) const
367 {
368         return false;
369 }
370
371
372 bool MathScriptInset::idxLeft(LCursor &) const
373 {
374         return false;
375 }
376
377
378 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
379 {
380         // in nucleus?
381         if (cur.idx() == 0) {
382                 // don't go up/down if there is no cell in this direction
383                 if (!has(up))
384                         return false;
385                 // go up/down only if in the last position
386                 // or in the first position of something with displayed limits
387                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
388                         cur.idx() = idxOfScript(up);
389                         cur.pos() = 0;
390                         return true;
391                 }
392                 return false;
393         }
394
395         // Are we 'up'?
396         if (has(up) && cur.idx() == idxOfScript(true)) {
397                 // can't go further up
398                 if (up)
399                         return false;
400                 // otherwise go to last position in the nucleus
401                 cur.idx() = 0;
402                 cur.pos() = cur.lastpos();
403                 return true;
404         }
405
406         // Are we 'down'?
407         if (has(up) && cur.idx() == idxOfScript(false)) {
408                 // can't go further down
409                 if (!up)
410                         return false;
411                 // otherwise go to last position in the nucleus
412                 cur.idx() = 0;
413                 cur.pos() = cur.lastpos();
414                 return true;
415         }
416
417         return false;
418 }
419
420
421 void MathScriptInset::write(WriteStream & os) const
422 {
423         if (nuc().size()) {
424                 os << nuc();
425                 //if (nuc().back()->takesLimits()) {
426                         if (limits_ == -1)
427                                 os << "\\nolimits ";
428                         if (limits_ == 1)
429                                 os << "\\limits ";
430                 //}
431         } else {
432                 if (os.firstitem())
433                         lyxerr[Debug::MATHED] << "suppressing {} when writing"
434                                               << endl;
435                 else
436                         os << "{}";
437         }
438
439         if (hasDown() && down().size())
440                 os << "_{" << down() << '}';
441
442         if (hasUp() && up().size())
443                 os << "^{" << up() << '}';
444
445         if (lock_ && !os.latex())
446                 os << "\\lyxlock ";
447 }
448
449
450 void MathScriptInset::normalize(NormalStream & os) const
451 {
452         bool d = hasDown() && down().size();
453         bool u = hasUp() && up().size();
454
455         if (u && d)
456                 os << "[subsup ";
457         else if (u)
458                 os << "[sup ";
459         else if (d)
460                 os << "[sub ";
461
462         if (nuc().size())
463                 os << nuc() << ' ';
464         else
465                 os << "[par]";
466
467         if (u && d)
468                 os << down() << ' ' << up() << ']';
469         else if (d)
470                 os << down() << ']';
471         else if (u)
472                 os << up() << ']';
473 }
474
475
476 void MathScriptInset::maple(MapleStream & os) const
477 {
478         if (nuc().size())
479                 os << nuc();
480         if (hasDown() && down().size())
481                 os << '[' << down() << ']';
482         if (hasUp() && up().size())
483                 os << "^(" << up() << ')';
484 }
485
486
487 void MathScriptInset::mathematica(MathematicaStream & os) const
488 {
489         bool d = hasDown() && down().size();
490         bool u = hasUp() && up().size();
491
492         if (nuc().size()) {
493                 if (d)
494                         os << "Subscript[" << nuc();
495                 else
496                         os << nuc();
497         }
498
499         if (u)
500                 os << "^(" << up() << ')';
501
502         if (nuc().size()) {
503                 if (d)
504                         os << ',' << down() << ']';
505         }
506 }
507
508
509 void MathScriptInset::mathmlize(MathMLStream & os) const
510 {
511         bool d = hasDown() && down().size();
512         bool u = hasUp() && up().size();
513
514         if (u && d)
515                 os << MTag("msubsup");
516         else if (u)
517                 os << MTag("msup");
518         else if (d)
519                 os << MTag("msub");
520
521         if (nuc().size())
522                 os << nuc();
523         else
524                 os << "<mrow/>";
525
526         if (u && d)
527                 os << down() << up() << ETag("msubsup");
528         else if (u)
529                 os << up() << ETag("msup");
530         else if (d)
531                 os << down() << ETag("msub");
532 }
533
534
535 void MathScriptInset::octave(OctaveStream & os) const
536 {
537         if (nuc().size())
538                 os << nuc();
539         if (hasDown() && down().size())
540                 os << '[' << down() << ']';
541         if (hasUp() && up().size())
542                 os << "^(" << up() << ')';
543 }
544
545
546 void MathScriptInset::infoize(std::ostream & os) const
547 {
548         os << "Scripts";
549 }
550
551
552 void MathScriptInset::infoize2(std::ostream & os) const
553 {
554         if (limits_)
555                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
556 }
557
558
559 void MathScriptInset::notifyCursorLeaves(LCursor & cur)
560 {
561         MathNestInset::notifyCursorLeaves(cur);
562
563         //lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl;
564
565         // remove empty scripts if possible
566         if (nargs() > 2) {
567                 // Case of two scripts. In this case, 1 = super, 2 = sub
568                 if (cur.idx() == 2 && cell(2).empty()) {
569                         // must be a subscript...
570                         removeScript(false);
571                 } else if (cur.idx() == 1 && cell(1).empty()) {
572                         // must be a superscript...
573                         removeScript(true);
574                 }
575         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
576                 // could be either subscript or super script
577                 removeScript(cell_1_is_up_);
578         }
579
580         //lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
581 }
582
583
584 void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd)
585 {
586         //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
587
588         if (cmd.action == LFUN_MATH_LIMITS) {
589                 if (!cmd.argument.empty()) {
590                         if (cmd.argument == "limits")
591                                 limits_ = 1;
592                         else if (cmd.argument == "nolimits")
593                                 limits_ = -1;
594                         else
595                                 limits_ = 0;
596                 } else if (limits_ == 0)
597                         limits_ = hasLimits() ? -1 : 1;
598                 else
599                         limits_ = 0;
600                 return;
601         }
602
603         MathNestInset::doDispatch(cur, cmd);
604 }