]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
next ones
[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(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_) << endl;
445         //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
446         return idxOfScript(true);
447 }
448
449
450 bool InsetMathScript::hasDown() const
451 {
452         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
453         //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
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::idxRight(Cursor &) const
473 {
474         return false;
475 }
476
477
478 bool InsetMathScript::idxLeft(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                                               << endl;
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 << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
662 }
663
664
665 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
666 {
667         InsetMathNest::notifyCursorLeaves(cur);
668
669         //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
670
671         // remove empty scripts if possible
672         if (nargs() > 2) {
673                 // Case of two scripts. In this case, 1 = super, 2 = sub
674                 if (cur.idx() == 2 && cell(2).empty()) {
675                         // must be a subscript...
676                         recordUndoInset(cur);
677                         removeScript(false);
678                         return true;
679                 } else if (cur.idx() == 1 && cell(1).empty()) {
680                         // must be a superscript...
681                         recordUndoInset(cur);
682                         removeScript(true);
683                         return true;
684                 }
685         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
686                 // could be either subscript or super script
687                 recordUndoInset(cur);
688                 removeScript(cell_1_is_up_);
689                 // Let the script inset commit suicide. This is
690                 // modelled on Cursor.pullArg(), but tries not to
691                 // invoke notifyCursorLeaves again and does not touch
692                 // cur (since the top slice will be deleted
693                 // afterwards))
694                 MathData ar = cell(0);
695                 Cursor tmpcur = cur;
696                 tmpcur.pop();
697                 tmpcur.cell().erase(tmpcur.pos());
698                 tmpcur.cell().insert(tmpcur.pos(), ar);
699                 return true;
700         }
701
702         //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
703         return false;
704 }
705
706
707 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
708 {
709         //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
710
711         if (cmd.action == LFUN_MATH_LIMITS) {
712                 if (!cmd.argument().empty()) {
713                         if (cmd.argument() == "limits")
714                                 limits_ = 1;
715                         else if (cmd.argument() == "nolimits")
716                                 limits_ = -1;
717                         else
718                                 limits_ = 0;
719                 } else if (limits_ == 0)
720                         limits_ = hasLimits() ? -1 : 1;
721                 else
722                         limits_ = 0;
723                 return;
724         }
725
726         InsetMathNest::doDispatch(cur, cmd);
727 }
728
729
730 } // namespace lyx