]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
potential compile fix for Kornel
[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 "support/debug.h"
22 #include "FuncRequest.h"
23
24 #include <boost/assert.hpp>
25
26 #include <ostream>
27
28
29 namespace lyx {
30
31 using std::string;
32 using std::max;
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(BufferView const & bv, 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                 Dimension const & dimdown = down().dimension(bv);
179                 dasc = dimdown.ascent();
180                 slevel = nuc().slevel();
181                 int ascdrop = dasc - slevel;
182                 int desdrop = isCharBox ? 0 : des + nuc().sshift();
183                 int mindes = nuc().mindes();
184                 des = max(desdrop, ascdrop);
185                 des = max(mindes, des);
186         }
187         if (hasUp()) {
188                 Dimension const & dimup = up().dimension(bv);
189                 int minasc = nuc().minasc();
190                 int ascdrop = isCharBox ? 0 : asc - up().mindes();
191                 int udes = dimup.descent();
192                 asc = udes + nuc().sshift();
193                 asc = max(ascdrop, asc);
194                 asc = max(minasc, asc);
195                 if (hasDown()) {
196                         int del = asc - udes - dasc;
197                         if (del + des <= 2) {
198                                 int newdes = 2 - del;
199                                 del = slevel - asc + udes;
200                                 if (del > 0) {
201                                         asc += del;
202                                         newdes -= del;
203                                 }
204                                 des = max(des, newdes);
205                         }
206                 }
207         }
208         return what ? asc : des;
209 }
210
211
212 int InsetMathScript::dy0(BufferView const & bv) const
213 {
214         int nd = ndes(bv);
215         if (!hasDown())
216                 return nd;
217         int des = down().dimension(bv).ascent();
218         if (hasLimits())
219                 des += nd + 2;
220         else {
221                 int na = nasc(bv);
222                 des = dy01(bv, na, nd, 0);
223         }
224         return des;
225 }
226
227
228 int InsetMathScript::dy1(BufferView const & bv) const
229 {
230         int na = nasc(bv);
231         if (!hasUp())
232                 return na;
233         int asc = up().dimension(bv).descent();
234         if (hasLimits())
235                 asc += na + 2;
236         else {
237                 int nd = ndes(bv);
238                 asc = dy01(bv, na, nd, 1);
239         }
240         asc = max(asc, 5);
241         return asc;
242 }
243
244
245 int InsetMathScript::dx0(BufferView const & bv) const
246 {
247         BOOST_ASSERT(hasDown());
248         Dimension const dim = dimension(bv);
249         return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
250 }
251
252
253 int InsetMathScript::dx1(BufferView const & bv) const
254 {
255         BOOST_ASSERT(hasUp());
256         Dimension const dim = dimension(bv);
257         return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker();
258 }
259
260
261 int InsetMathScript::dxx(BufferView const & bv) const
262 {
263         Dimension const dim = dimension(bv);
264         return hasLimits() ? (dim.wid - nwid(bv)) / 2  :  0;
265 }
266
267
268 int InsetMathScript::nwid(BufferView const & bv) const
269 {
270         return nuc().size() ? nuc().dimension(bv).width() : 2;
271 }
272
273
274 int InsetMathScript::nasc(BufferView const & bv) const
275 {
276         return nuc().size() ? nuc().dimension(bv).ascent() : 5;
277 }
278
279
280 int InsetMathScript::ndes(BufferView const & bv) const
281 {
282         return nuc().size() ? nuc().dimension(bv).descent() : 0;
283 }
284
285
286 int InsetMathScript::nker() const
287 {
288         if (nuc().size()) {
289                 int kerning = nuc().kerning();
290                 return kerning > 0 ? kerning : 0;
291         }
292         return 0;
293 }
294
295
296 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
297 {
298         Dimension dim0;
299         Dimension dim1;
300         Dimension dim2;
301         cell(0).metrics(mi, dim0);
302         ScriptChanger dummy(mi.base);
303         if (nargs() > 1)
304                 cell(1).metrics(mi, dim1);
305         if (nargs() > 2)
306                 cell(2).metrics(mi, dim2);
307
308         dim.wid = 0;
309         BufferView & bv = *mi.base.bv;
310         // FIXME: data copying... not very efficient.
311         Dimension dimup;
312         Dimension dimdown;
313         if (hasUp())
314                 dimup = up().dimension(bv);
315         if (hasDown())
316                 dimdown = down().dimension(bv);
317
318         if (hasLimits()) {
319                 dim.wid = nwid(bv);
320                 if (hasUp())
321                         dim.wid = max(dim.wid, dimup.width());
322                 if (hasDown())
323                         dim.wid = max(dim.wid, dimdown.width());
324         } else {
325                 if (hasUp())
326                         dim.wid = max(dim.wid, nker() + dimup.width());
327                 if (hasDown())
328                         dim.wid = max(dim.wid, dimdown.width());
329                 dim.wid += nwid(bv);
330         }
331         int na = nasc(bv);
332         if (hasUp()) {
333                 int asc = dy1(bv) + dimup.ascent();
334                 dim.asc = max(na, asc);
335         } else
336                 dim.asc = na;
337         int nd = ndes(bv);
338         if (hasDown()) {
339                 int des = dy0(bv) + dimdown.descent();
340                 dim.des = max(nd, des);
341         } else
342                 dim.des = nd;
343         metricsMarkers(dim);
344         // Cache the inset dimension. 
345         setDimCache(mi, dim);
346 }
347
348
349 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
350 {
351         BufferView & bv = *pi.base.bv;
352         if (nuc().size())
353                 nuc().draw(pi, x + dxx(bv), y);
354         else {
355                 nuc().setXY(bv, x + dxx(bv), y);
356                 if (editing(&bv))
357                         pi.draw(x + dxx(bv), y, char_type('.'));
358         }
359         ScriptChanger dummy(pi.base);
360         if (hasUp())
361                 up().draw(pi, x + dx1(bv), y - dy1(bv));
362         if (hasDown())
363                 down().draw(pi, x + dx0(bv), y + dy0(bv));
364         drawMarkers(pi, x, y);
365 }
366
367
368 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
369 {
370         if (hasUp())
371                 up().metricsT(mi, dim);
372         if (hasDown())
373                 down().metricsT(mi, dim);
374         nuc().metricsT(mi, dim);
375 }
376
377
378 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
379 {
380         // FIXME: BROKEN
381         if (nuc().size())
382                 nuc().drawT(pain, x + 1, y);
383         if (hasUp())
384                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
385         if (hasDown())
386                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
387 }
388
389
390
391 bool InsetMathScript::hasLimits() const
392 {
393         // obvious cases
394         if (limits_ == 1)
395                 return true;
396         if (limits_ == -1)
397                 return false;
398
399         // we can only display limits if the nucleus wants some
400         if (!nuc().size())
401                 return false;
402         if (!nuc().back()->isScriptable())
403                 return false;
404
405         if (nuc().back()->asSymbolInset()) {
406                 // \intop is an alias for \int\limits, \ointop == \oint\limits
407                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
408                         return true;
409                 // per default \int has limits beside the \int even in displayed formulas
410                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
411                         return false;
412         }
413
414         // assume "real" limits for everything else
415         return true;
416 }
417
418
419 void InsetMathScript::removeScript(bool up)
420 {
421         if (nargs() == 2) {
422                 if (up == cell_1_is_up_)
423                         cells_.pop_back();
424         } else if (nargs() == 3) {
425                 if (up == true) {
426                         swap(cells_[1], cells_[2]);
427                         cell_1_is_up_ = false;
428                 } else {
429                         cell_1_is_up_ = true;
430                 }
431                 cells_.pop_back();
432         }
433 }
434
435
436 bool InsetMathScript::has(bool up) const
437 {
438         return idxOfScript(up);
439 }
440
441
442 bool InsetMathScript::hasUp() const
443 {
444         //lyxerr << "1up: " << bool(cell_1_is_up_));
445         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
446         return idxOfScript(true);
447 }
448
449
450 bool InsetMathScript::hasDown() const
451 {
452         //LYXERR0("1up: " << bool(cell_1_is_up_));
453         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
454         return idxOfScript(false);
455 }
456
457
458 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
459 {
460         if (nargs() == 1)
461                 return 0;
462         if (nargs() == 2)
463                 return (cell_1_is_up_ == up) ? 1 : 0;
464         if (nargs() == 3)
465                 return up ? 1 : 2;
466         BOOST_ASSERT(false);
467         // Silence compiler
468         return 0;
469 }
470
471
472 bool InsetMathScript::idxForward(Cursor &) const
473 {
474         return false;
475 }
476
477
478 bool InsetMathScript::idxBackward(Cursor &) const
479 {
480         return false;
481 }
482
483
484 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
485 {
486         // in nucleus?
487         if (cur.idx() == 0) {
488                 // don't go up/down if there is no cell in this direction
489                 if (!has(up))
490                         return false;
491                 // go up/down only if in the last position
492                 // or in the first position of something with displayed limits
493                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
494                         cur.idx() = idxOfScript(up);
495                         cur.pos() = 0;
496                         return true;
497                 }
498                 return false;
499         }
500
501         // Are we 'up'?
502         if (cur.idx() == idxOfScript(true)) {
503                 // can't go further up
504                 if (up)
505                         return false;
506                 // otherwise go to last position in the nucleus
507                 cur.idx() = 0;
508                 cur.pos() = cur.lastpos();
509                 return true;
510         }
511
512         // Are we 'down'?
513         if (cur.idx() == idxOfScript(false)) {
514                 // can't go further down
515                 if (!up)
516                         return false;
517                 // otherwise go to last position in the nucleus
518                 cur.idx() = 0;
519                 cur.pos() = cur.lastpos();
520                 return true;
521         }
522
523         return false;
524 }
525
526
527 void InsetMathScript::write(WriteStream & os) const
528 {
529         if (nuc().size()) {
530                 os << nuc();
531                 //if (nuc().back()->takesLimits()) {
532                         if (limits_ == -1)
533                                 os << "\\nolimits ";
534                         if (limits_ == 1)
535                                 os << "\\limits ";
536                 //}
537         } else {
538                 if (os.firstitem())
539                         LYXERR(Debug::MATHED, "suppressing {} when writing");
540                 else
541                         os << "{}";
542         }
543
544         if (hasDown() /*&& down().size()*/)
545                 os << "_{" << down() << '}';
546
547         if (hasUp() /*&& up().size()*/)
548                 os << "^{" << up() << '}';
549
550         if (lock_ && !os.latex())
551                 os << "\\lyxlock ";
552 }
553
554
555 void InsetMathScript::normalize(NormalStream & os) const
556 {
557         bool d = hasDown() && down().size();
558         bool u = hasUp() && up().size();
559
560         if (u && d)
561                 os << "[subsup ";
562         else if (u)
563                 os << "[sup ";
564         else if (d)
565                 os << "[sub ";
566
567         if (nuc().size())
568                 os << nuc() << ' ';
569         else
570                 os << "[par]";
571
572         if (u && d)
573                 os << down() << ' ' << up() << ']';
574         else if (d)
575                 os << down() << ']';
576         else if (u)
577                 os << up() << ']';
578 }
579
580
581 void InsetMathScript::maple(MapleStream & os) const
582 {
583         if (nuc().size())
584                 os << nuc();
585         if (hasDown() && down().size())
586                 os << '[' << down() << ']';
587         if (hasUp() && up().size())
588                 os << "^(" << up() << ')';
589 }
590
591
592 void InsetMathScript::mathematica(MathematicaStream & os) const
593 {
594         bool d = hasDown() && down().size();
595         bool u = hasUp() && up().size();
596
597         if (nuc().size()) {
598                 if (d)
599                         os << "Subscript[" << nuc();
600                 else
601                         os << nuc();
602         }
603
604         if (u)
605                 os << "^(" << up() << ')';
606
607         if (nuc().size()) {
608                 if (d)
609                         os << ',' << down() << ']';
610         }
611 }
612
613
614 void InsetMathScript::mathmlize(MathStream & os) const
615 {
616         bool d = hasDown() && down().size();
617         bool u = hasUp() && up().size();
618
619         if (u && d)
620                 os << MTag("msubsup");
621         else if (u)
622                 os << MTag("msup");
623         else if (d)
624                 os << MTag("msub");
625
626         if (nuc().size())
627                 os << nuc();
628         else
629                 os << "<mrow/>";
630
631         if (u && d)
632                 os << down() << up() << ETag("msubsup");
633         else if (u)
634                 os << up() << ETag("msup");
635         else if (d)
636                 os << down() << ETag("msub");
637 }
638
639
640 void InsetMathScript::octave(OctaveStream & os) const
641 {
642         if (nuc().size())
643                 os << nuc();
644         if (hasDown() && down().size())
645                 os << '[' << down() << ']';
646         if (hasUp() && up().size())
647                 os << "^(" << up() << ')';
648 }
649
650
651 void InsetMathScript::infoize(odocstream & os) const
652 {
653         os << "Scripts";
654 }
655
656
657 void InsetMathScript::infoize2(odocstream & os) const
658 {
659         if (limits_)
660                 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
661 }
662
663
664 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
665 {
666         InsetMathNest::notifyCursorLeaves(cur);
667
668         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
669
670         // remove empty scripts if possible
671         if (nargs() > 2) {
672                 // Case of two scripts. In this case, 1 = super, 2 = sub
673                 if (cur.idx() == 2 && cell(2).empty()) {
674                         // must be a subscript...
675                         cur.recordUndoInset();
676                         removeScript(false);
677                         return true;
678                 } else if (cur.idx() == 1 && cell(1).empty()) {
679                         // must be a superscript...
680                         cur.recordUndoInset();
681                         removeScript(true);
682                         return true;
683                 }
684         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
685                 // could be either subscript or super script
686                 cur.recordUndoInset();
687                 removeScript(cell_1_is_up_);
688                 // Let the script inset commit suicide. This is
689                 // modelled on Cursor.pullArg(), but tries not to
690                 // invoke notifyCursorLeaves again and does not touch
691                 // cur (since the top slice will be deleted
692                 // afterwards))
693                 MathData ar = cell(0);
694                 Cursor tmpcur = cur;
695                 tmpcur.pop();
696                 tmpcur.cell().erase(tmpcur.pos());
697                 tmpcur.cell().insert(tmpcur.pos(), ar);
698                 return true;
699         }
700
701         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
702         return false;
703 }
704
705
706 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
707 {
708         //LYXERR("InsetMathScript: request: " << cmd);
709
710         if (cmd.action == LFUN_MATH_LIMITS) {
711                 if (!cmd.argument().empty()) {
712                         if (cmd.argument() == "limits")
713                                 limits_ = 1;
714                         else if (cmd.argument() == "nolimits")
715                                 limits_ = -1;
716                         else
717                                 limits_ = 0;
718                 } else if (limits_ == 0)
719                         limits_ = hasLimits() ? -1 : 1;
720                 else
721                         limits_ = 0;
722                 return;
723         }
724
725         InsetMathNest::doDispatch(cur, cmd);
726 }
727
728
729 } // namespace lyx