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