]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
Fix bug #6264: Broken undo of removed script in mathed when both scripts are present.
[lyx.git] / src / mathed / InsetMathScript.cpp
1 /**
2  * \file InsetMathScript.cpp
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 "BufferView.h"
14 #include "Cursor.h"
15 #include "DispatchResult.h"
16 #include "FuncRequest.h"
17 #include "InsetMathFont.h"
18 #include "InsetMathScript.h"
19 #include "InsetMathSymbol.h"
20 #include "MathData.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
23
24 #include "support/debug.h"
25
26 #include "support/lassert.h"
27
28 #include <ostream>
29
30 using namespace std;
31
32 namespace lyx {
33
34
35 InsetMathScript::InsetMathScript()
36         : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
37 {}
38
39
40 InsetMathScript::InsetMathScript(bool up)
41         : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
42 {}
43
44
45 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
46         : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
47 {
48         LASSERT(nargs() >= 1, /**/);
49         cell(0).push_back(at);
50 }
51
52
53 Inset * InsetMathScript::clone() const
54 {
55         return new InsetMathScript(*this);
56 }
57
58
59 InsetMathScript const * InsetMathScript::asScriptInset() const
60 {
61         return this;
62 }
63
64
65 InsetMathScript * InsetMathScript::asScriptInset()
66 {
67         return this;
68 }
69
70
71 bool InsetMathScript::idxFirst(Cursor & cur) const
72 {
73         cur.idx() = 0;
74         cur.pos() = 0;
75         return true;
76 }
77
78
79 bool InsetMathScript::idxLast(Cursor & cur) const
80 {
81         cur.idx() = 0;
82         cur.pos() = nuc().size();
83         return true;
84 }
85
86
87 MathData const & InsetMathScript::down() const
88 {
89         if (nargs() == 3)
90                 return cell(2);
91         LASSERT(nargs() > 1, /**/);
92         return cell(1);
93 }
94
95
96 MathData & InsetMathScript::down()
97 {
98         if (nargs() == 3)
99                 return cell(2);
100         LASSERT(nargs() > 1, /**/);
101         return cell(1);
102 }
103
104
105 MathData const & InsetMathScript::up() const
106 {
107         LASSERT(nargs() > 1, /**/);
108         return cell(1);
109 }
110
111
112 MathData & InsetMathScript::up()
113 {
114         LASSERT(nargs() > 1, /**/);
115         return cell(1);
116 }
117
118
119 void InsetMathScript::ensure(bool up)
120 {
121         if (nargs() == 1) {
122                 // just nucleus so far
123                 cells_.push_back(MathData());
124                 cell_1_is_up_ = up;
125         } else if (nargs() == 2 && !has(up)) {
126                 if (up) {
127                         cells_.push_back(cell(1));
128                         cell(1).clear();
129                 } else {
130                         cells_.push_back(MathData());
131                 }
132         }
133 }
134
135
136 MathData const & InsetMathScript::nuc() const
137 {
138         return cell(0);
139 }
140
141
142 MathData & InsetMathScript::nuc()
143 {
144         return cell(0);
145 }
146
147
148 namespace {
149
150 bool isAlphaSymbol(MathAtom const & at)
151 {
152         if (at->asCharInset() ||
153                         (at->asSymbolInset() &&
154                          at->asSymbolInset()->isOrdAlpha()))
155                 return true;
156
157         if (at->asFontInset()) {
158                 MathData const & ar = at->asFontInset()->cell(0);
159                 for (size_t i = 0; i < ar.size(); ++i) {
160                         if (!(ar[i]->asCharInset() ||
161                                         (ar[i]->asSymbolInset() &&
162                                          ar[i]->asSymbolInset()->isOrdAlpha())))
163                                 return false;
164                 }
165                 return true;
166         }
167         return false;
168 }
169
170 } // namespace anon
171
172
173 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
174 {
175         int dasc = 0;
176         int slevel = 0;
177         bool isCharBox = nuc().size() ? isAlphaSymbol(nuc().back()) : false;
178         if (hasDown()) {
179                 Dimension const & dimdown = down().dimension(bv);
180                 dasc = dimdown.ascent();
181                 slevel = nuc().slevel();
182                 int ascdrop = dasc - slevel;
183                 int desdrop = isCharBox ? 0 : des + nuc().sshift();
184                 int mindes = nuc().mindes();
185                 des = max(desdrop, ascdrop);
186                 des = max(mindes, des);
187         }
188         if (hasUp()) {
189                 Dimension const & dimup = up().dimension(bv);
190                 int minasc = nuc().minasc();
191                 int ascdrop = isCharBox ? 0 : asc - up().mindes();
192                 int udes = dimup.descent();
193                 asc = udes + nuc().sshift();
194                 asc = max(ascdrop, asc);
195                 asc = max(minasc, asc);
196                 if (hasDown()) {
197                         int del = asc - udes - dasc;
198                         if (del + des <= 2) {
199                                 int newdes = 2 - del;
200                                 del = slevel - asc + udes;
201                                 if (del > 0) {
202                                         asc += del;
203                                         newdes -= del;
204                                 }
205                                 des = max(des, newdes);
206                         }
207                 }
208         }
209         return what ? asc : des;
210 }
211
212
213 int InsetMathScript::dy0(BufferView const & bv) const
214 {
215         int nd = ndes(bv);
216         if (!hasDown())
217                 return nd;
218         int des = down().dimension(bv).ascent();
219         if (hasLimits())
220                 des += nd + 2;
221         else {
222                 int na = nasc(bv);
223                 des = dy01(bv, na, nd, 0);
224         }
225         return des;
226 }
227
228
229 int InsetMathScript::dy1(BufferView const & bv) const
230 {
231         int na = nasc(bv);
232         if (!hasUp())
233                 return na;
234         int asc = up().dimension(bv).descent();
235         if (hasLimits())
236                 asc += na + 2;
237         else {
238                 int nd = ndes(bv);
239                 asc = dy01(bv, na, nd, 1);
240         }
241         asc = max(asc, 5);
242         return asc;
243 }
244
245
246 int InsetMathScript::dx0(BufferView const & bv) const
247 {
248         LASSERT(hasDown(), /**/);
249         Dimension const dim = dimension(bv);
250         return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
251 }
252
253
254 int InsetMathScript::dx1(BufferView const & bv) const
255 {
256         LASSERT(hasUp(), /**/);
257         Dimension const dim = dimension(bv);
258         return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
259 }
260
261
262 int InsetMathScript::dxx(BufferView const & bv) const
263 {
264         Dimension const dim = dimension(bv);
265         return hasLimits() ? (dim.wid - nwid(bv)) / 2  :  0;
266 }
267
268
269 int InsetMathScript::nwid(BufferView const & bv) const
270 {
271         return nuc().size() ? nuc().dimension(bv).width() : 2;
272 }
273
274
275 int InsetMathScript::nasc(BufferView const & bv) const
276 {
277         return nuc().size() ? nuc().dimension(bv).ascent() : 5;
278 }
279
280
281 int InsetMathScript::ndes(BufferView const & bv) const
282 {
283         return nuc().size() ? nuc().dimension(bv).descent() : 0;
284 }
285
286
287 int InsetMathScript::nker(BufferView const * bv) const
288 {
289         if (nuc().size()) {
290                 int kerning = nuc().kerning(bv);
291                 return kerning > 0 ? kerning : 0;
292         }
293         return 0;
294 }
295
296
297 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
298 {
299         Dimension dim0;
300         Dimension dim1;
301         Dimension dim2;
302         cell(0).metrics(mi, dim0);
303         ScriptChanger dummy(mi.base);
304         if (nargs() > 1)
305                 cell(1).metrics(mi, dim1);
306         if (nargs() > 2)
307                 cell(2).metrics(mi, dim2);
308
309         dim.wid = 0;
310         BufferView & bv = *mi.base.bv;
311         // FIXME: data copying... not very efficient.
312         Dimension dimup;
313         Dimension dimdown;
314         if (hasUp())
315                 dimup = up().dimension(bv);
316         if (hasDown())
317                 dimdown = down().dimension(bv);
318
319         if (hasLimits()) {
320                 dim.wid = nwid(bv);
321                 if (hasUp())
322                         dim.wid = max(dim.wid, dimup.width());
323                 if (hasDown())
324                         dim.wid = max(dim.wid, dimdown.width());
325         } else {
326                 if (hasUp())
327                         dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
328                 if (hasDown())
329                         dim.wid = max(dim.wid, dimdown.width());
330                 dim.wid += nwid(bv);
331         }
332         int na = nasc(bv);
333         if (hasUp()) {
334                 int asc = dy1(bv) + dimup.ascent();
335                 dim.asc = max(na, asc);
336         } else
337                 dim.asc = na;
338         int nd = ndes(bv);
339         if (hasDown()) {
340                 int des = dy0(bv) + dimdown.descent();
341                 dim.des = max(nd, des);
342         } else
343                 dim.des = nd;
344         metricsMarkers(dim);
345 }
346
347
348 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
349 {
350         BufferView & bv = *pi.base.bv;
351         if (nuc().size())
352                 nuc().draw(pi, x + dxx(bv), y);
353         else {
354                 nuc().setXY(bv, x + dxx(bv), y);
355                 if (editing(&bv))
356                         pi.draw(x + dxx(bv), y, char_type('.'));
357         }
358         ScriptChanger dummy(pi.base);
359         if (hasUp())
360                 up().draw(pi, x + dx1(bv), y - dy1(bv));
361         if (hasDown())
362                 down().draw(pi, x + dx0(bv), y + dy0(bv));
363         drawMarkers(pi, x, y);
364 }
365
366
367 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
368 {
369         if (hasUp())
370                 up().metricsT(mi, dim);
371         if (hasDown())
372                 down().metricsT(mi, dim);
373         nuc().metricsT(mi, dim);
374 }
375
376
377 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
378 {
379         // FIXME: BROKEN
380         if (nuc().size())
381                 nuc().drawT(pain, x + 1, y);
382         if (hasUp())
383                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
384         if (hasDown())
385                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
386 }
387
388
389
390 bool InsetMathScript::hasLimits() const
391 {
392         // obvious cases
393         if (limits_ == 1)
394                 return true;
395         if (limits_ == -1)
396                 return false;
397
398         // we can only display limits if the nucleus wants some
399         if (!nuc().size())
400                 return false;
401         if (!nuc().back()->isScriptable())
402                 return false;
403
404         if (nuc().back()->asSymbolInset()) {
405                 // \intop is an alias for \int\limits, \ointop == \oint\limits
406                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
407                         return true;
408                 // per default \int has limits beside the \int even in displayed formulas
409                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
410                         return false;
411         }
412
413         // assume "real" limits for everything else
414         return true;
415 }
416
417
418 void InsetMathScript::removeScript(bool up)
419 {
420         if (nargs() == 2) {
421                 if (up == cell_1_is_up_)
422                         cells_.pop_back();
423         } else if (nargs() == 3) {
424                 if (up == true) {
425                         swap(cells_[1], cells_[2]);
426                         cell_1_is_up_ = false;
427                 } else {
428                         cell_1_is_up_ = true;
429                 }
430                 cells_.pop_back();
431         }
432 }
433
434
435 bool InsetMathScript::has(bool up) const
436 {
437         return idxOfScript(up);
438 }
439
440
441 bool InsetMathScript::hasUp() const
442 {
443         //lyxerr << "1up: " << bool(cell_1_is_up_));
444         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
445         return idxOfScript(true);
446 }
447
448
449 bool InsetMathScript::hasDown() const
450 {
451         //LYXERR0("1up: " << bool(cell_1_is_up_));
452         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
453         return idxOfScript(false);
454 }
455
456
457 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
458 {
459         if (nargs() == 1)
460                 return 0;
461         if (nargs() == 2)
462                 return (cell_1_is_up_ == up) ? 1 : 0;
463         if (nargs() == 3)
464                 return up ? 1 : 2;
465         LASSERT(false, /**/);
466         // Silence compiler
467         return 0;
468 }
469
470
471 bool InsetMathScript::idxForward(Cursor &) const
472 {
473         return false;
474 }
475
476
477 bool InsetMathScript::idxBackward(Cursor &) const
478 {
479         return false;
480 }
481
482
483 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
484 {
485         // in nucleus?
486         if (cur.idx() == 0) {
487                 // don't go up/down if there is no cell in this direction
488                 if (!has(up))
489                         return false;
490                 // go up/down only if in the last position
491                 // or in the first position of something with displayed limits
492                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
493                         cur.idx() = idxOfScript(up);
494                         cur.pos() = 0;
495                         return true;
496                 }
497                 return false;
498         }
499
500         // Are we 'up'?
501         if (cur.idx() == idxOfScript(true)) {
502                 // can't go further up
503                 if (up)
504                         return false;
505                 // otherwise go to last position in the nucleus
506                 cur.idx() = 0;
507                 cur.pos() = cur.lastpos();
508                 return true;
509         }
510
511         // Are we 'down'?
512         if (cur.idx() == idxOfScript(false)) {
513                 // can't go further down
514                 if (!up)
515                         return false;
516                 // otherwise go to last position in the nucleus
517                 cur.idx() = 0;
518                 cur.pos() = cur.lastpos();
519                 return true;
520         }
521
522         return false;
523 }
524
525
526 void InsetMathScript::write(WriteStream & os) const
527 {
528         MathEnsurer ensurer(os);
529
530         if (nuc().size()) {
531                 os << nuc();
532                 //if (nuc().back()->takesLimits()) {
533                         if (limits_ == -1)
534                                 os << "\\nolimits ";
535                         if (limits_ == 1)
536                                 os << "\\limits ";
537                 //}
538         } else {
539                 if (os.firstitem())
540                         LYXERR(Debug::MATHED, "suppressing {} when writing");
541                 else
542                         os << "{}";
543         }
544
545         if (hasDown() /*&& down().size()*/)
546                 os << "_{" << down() << '}';
547
548         if (hasUp() /*&& up().size()*/)
549                 os << "^{" << up() << '}';
550
551         if (lock_ && !os.latex())
552                 os << "\\lyxlock ";
553 }
554
555
556 void InsetMathScript::normalize(NormalStream & os) const
557 {
558         bool d = hasDown() && down().size();
559         bool u = hasUp() && up().size();
560
561         if (u && d)
562                 os << "[subsup ";
563         else if (u)
564                 os << "[sup ";
565         else if (d)
566                 os << "[sub ";
567
568         if (nuc().size())
569                 os << nuc() << ' ';
570         else
571                 os << "[par]";
572
573         if (u && d)
574                 os << down() << ' ' << up() << ']';
575         else if (d)
576                 os << down() << ']';
577         else if (u)
578                 os << up() << ']';
579 }
580
581
582 void InsetMathScript::maple(MapleStream & os) const
583 {
584         if (nuc().size())
585                 os << nuc();
586         if (hasDown() && down().size())
587                 os << '[' << down() << ']';
588         if (hasUp() && up().size())
589                 os << "^(" << up() << ')';
590 }
591
592
593 void InsetMathScript::mathematica(MathematicaStream & os) const
594 {
595         bool d = hasDown() && down().size();
596         bool u = hasUp() && up().size();
597
598         if (nuc().size()) {
599                 if (d)
600                         os << "Subscript[" << nuc();
601                 else
602                         os << nuc();
603         }
604
605         if (u)
606                 os << "^(" << up() << ')';
607
608         if (nuc().size()) {
609                 if (d)
610                         os << ',' << down() << ']';
611         }
612 }
613
614
615 void InsetMathScript::mathmlize(MathStream & os) const
616 {
617         bool d = hasDown() && down().size();
618         bool u = hasUp() && up().size();
619
620         if (u && d)
621                 os << MTag("msubsup");
622         else if (u)
623                 os << MTag("msup");
624         else if (d)
625                 os << MTag("msub");
626
627         if (nuc().size())
628                 os << nuc();
629         else
630                 os << "<mrow/>";
631
632         if (u && d)
633                 os << down() << up() << ETag("msubsup");
634         else if (u)
635                 os << up() << ETag("msup");
636         else if (d)
637                 os << down() << ETag("msub");
638 }
639
640
641 void InsetMathScript::octave(OctaveStream & os) const
642 {
643         if (nuc().size())
644                 os << nuc();
645         if (hasDown() && down().size())
646                 os << '[' << down() << ']';
647         if (hasUp() && up().size())
648                 os << "^(" << up() << ')';
649 }
650
651
652 void InsetMathScript::infoize(odocstream & os) const
653 {
654         os << "Scripts";
655 }
656
657
658 void InsetMathScript::infoize2(odocstream & os) const
659 {
660         if (limits_)
661                 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
662 }
663
664
665 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
666 {
667         InsetMathNest::notifyCursorLeaves(old, cur);
668
669         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
670
671         // Remove empty scripts if possible:
672
673         // The case of two scripts, but only one got empty (1 = super, 2 = sub).
674         // We keep the script inset, but remove the empty script.
675         if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
676                 if (cell(2).empty()) {
677                         // must be a subscript...
678                         old.recordUndoInset();
679                         removeScript(false);
680                         cur.updateFlags(cur.disp_.update() | Update::SinglePar);
681                         return true;
682                 } else if (cell(1).empty()) {
683                         // must be a superscript...
684                         old.recordUndoInset();
685                         removeScript(true);
686                         cur.updateFlags(cur.disp_.update() | Update::SinglePar);
687                         return true;
688                 }
689         }
690         // Now the two suicide cases:
691         // * we have only one script which is empty
692         // * we have two scripts which are both empty.
693         // The script inset is removed completely.
694         if ((nargs() == 2 && cell(1).empty())
695             || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
696                 // Make undo step. We cannot use cur for this because
697                 // it does not necessarily point to us anymore. But we
698                 // should be on top of the cursor old.
699                 Cursor insetCur = old;
700                 int scriptSlice = insetCur.find(this);
701                 LASSERT(scriptSlice != -1, /**/);
702                 insetCur.cutOff(scriptSlice);
703                 insetCur.recordUndoInset();
704
705                 // Let the script inset commit suicide. This is
706                 // modelled on Cursor.pullArg(), but tries not to
707                 // invoke notifyCursorLeaves again and does not touch
708                 // cur (since the top slice will be deleted
709                 // afterwards)
710                 MathData ar = cell(0);
711                 insetCur.pop();
712                 insetCur.cell().erase(insetCur.pos());
713                 insetCur.cell().insert(insetCur.pos(), ar);
714
715                 // redraw
716                 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
717                 return true;
718         }
719
720         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
721         return false;
722 }
723
724
725 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
726 {
727         //LYXERR("InsetMathScript: request: " << cmd);
728
729         if (cmd.action == LFUN_MATH_LIMITS) {
730                 if (!cmd.argument().empty()) {
731                         if (cmd.argument() == "limits")
732                                 limits_ = 1;
733                         else if (cmd.argument() == "nolimits")
734                                 limits_ = -1;
735                         else
736                                 limits_ = 0;
737                 } else if (limits_ == 0)
738                         limits_ = hasLimits() ? -1 : 1;
739                 else
740                         limits_ = 0;
741                 return;
742         }
743
744         InsetMathNest::doDispatch(cur, cmd);
745 }
746
747
748 } // namespace lyx