A bit more re-organization.
[lyx.git] / src / mathed / InsetMathScript.cpp
blob8fde3ec81b329149c82e573ae68493acb24b8570
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.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
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"
24 #include "support/debug.h"
26 #include "support/lassert.h"
28 #include <ostream>
30 using namespace std;
32 namespace lyx {
35 InsetMathScript::InsetMathScript(Buffer * buf)
36 : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
40 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
41 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
45 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
46 : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
48 LASSERT(nargs() >= 1, /**/);
49 cell(0).push_back(at);
53 Inset * InsetMathScript::clone() const
55 return new InsetMathScript(*this);
59 InsetMathScript const * InsetMathScript::asScriptInset() const
61 return this;
65 InsetMathScript * InsetMathScript::asScriptInset()
67 return this;
71 bool InsetMathScript::idxFirst(Cursor & cur) const
73 cur.idx() = 0;
74 cur.pos() = 0;
75 return true;
79 bool InsetMathScript::idxLast(Cursor & cur) const
81 cur.idx() = 0;
82 cur.pos() = nuc().size();
83 return true;
87 MathData const & InsetMathScript::down() const
89 if (nargs() == 3)
90 return cell(2);
91 LASSERT(nargs() > 1, /**/);
92 return cell(1);
96 MathData & InsetMathScript::down()
98 if (nargs() == 3)
99 return cell(2);
100 LASSERT(nargs() > 1, /**/);
101 return cell(1);
105 MathData const & InsetMathScript::up() const
107 LASSERT(nargs() > 1, /**/);
108 return cell(1);
112 MathData & InsetMathScript::up()
114 LASSERT(nargs() > 1, /**/);
115 return cell(1);
119 void InsetMathScript::ensure(bool up)
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());
136 MathData const & InsetMathScript::nuc() const
138 return cell(0);
142 MathData & InsetMathScript::nuc()
144 return cell(0);
148 namespace {
150 bool isAlphaSymbol(MathAtom const & at)
152 if (at->asCharInset() ||
153 (at->asSymbolInset() &&
154 at->asSymbolInset()->isOrdAlpha()))
155 return true;
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;
165 return true;
167 return false;
170 } // namespace anon
173 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
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);
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;
205 des = max(des, newdes);
209 return what ? asc : des;
213 int InsetMathScript::dy0(BufferView const & bv) const
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);
225 return des;
229 int InsetMathScript::dy1(BufferView const & bv) const
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);
241 asc = max(asc, 5);
242 return asc;
246 int InsetMathScript::dx0(BufferView const & bv) const
248 LASSERT(hasDown(), /**/);
249 Dimension const dim = dimension(bv);
250 return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
254 int InsetMathScript::dx1(BufferView const & bv) const
256 LASSERT(hasUp(), /**/);
257 Dimension const dim = dimension(bv);
258 return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
262 int InsetMathScript::dxx(BufferView const & bv) const
264 Dimension const dim = dimension(bv);
265 return hasLimits() ? (dim.wid - nwid(bv)) / 2 : 0;
269 int InsetMathScript::nwid(BufferView const & bv) const
271 return nuc().size() ? nuc().dimension(bv).width() : 2;
275 int InsetMathScript::nasc(BufferView const & bv) const
277 return nuc().size() ? nuc().dimension(bv).ascent() : 5;
281 int InsetMathScript::ndes(BufferView const & bv) const
283 return nuc().size() ? nuc().dimension(bv).descent() : 0;
287 int InsetMathScript::nker(BufferView const * bv) const
289 if (nuc().size()) {
290 int kerning = nuc().kerning(bv);
291 return kerning > 0 ? kerning : 0;
293 return 0;
297 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
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);
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);
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);
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);
348 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
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('.'));
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);
367 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
369 if (hasUp())
370 up().metricsT(mi, dim);
371 if (hasDown())
372 down().metricsT(mi, dim);
373 nuc().metricsT(mi, dim);
377 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
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()*/);
390 bool InsetMathScript::hasLimits() const
392 // obvious cases
393 if (limits_ == 1)
394 return true;
395 if (limits_ == -1)
396 return false;
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;
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;
413 // assume "real" limits for everything else
414 return true;
418 void InsetMathScript::removeScript(bool up)
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;
430 cells_.pop_back();
435 bool InsetMathScript::has(bool up) const
437 return idxOfScript(up);
441 bool InsetMathScript::hasUp() const
443 //lyxerr << "1up: " << bool(cell_1_is_up_));
444 //lyxerr << "hasUp: " << bool(idxOfScript(true)));
445 return idxOfScript(true);
449 bool InsetMathScript::hasDown() const
451 //LYXERR0("1up: " << bool(cell_1_is_up_));
452 //LYXERR0("hasDown: " << bool(idxOfScript(false)));
453 return idxOfScript(false);
457 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
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;
471 bool InsetMathScript::idxForward(Cursor &) const
473 return false;
477 bool InsetMathScript::idxBackward(Cursor &) const
479 return false;
483 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
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;
497 return false;
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;
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;
522 return false;
526 void InsetMathScript::write(WriteStream & os) const
528 MathEnsurer ensurer(os);
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 ";
538 } else {
539 if (os.firstitem())
540 LYXERR(Debug::MATHED, "suppressing {} when writing");
541 else
542 os << "{}";
545 if (hasDown() /*&& down().size()*/)
546 os << "_{" << down() << '}';
548 if (hasUp() /*&& up().size()*/)
549 os << "^{" << up() << '}';
551 if (lock_ && !os.latex())
552 os << "\\lyxlock ";
556 void InsetMathScript::normalize(NormalStream & os) const
558 bool d = hasDown() && down().size();
559 bool u = hasUp() && up().size();
561 if (u && d)
562 os << "[subsup ";
563 else if (u)
564 os << "[sup ";
565 else if (d)
566 os << "[sub ";
568 if (nuc().size())
569 os << nuc() << ' ';
570 else
571 os << "[par]";
573 if (u && d)
574 os << down() << ' ' << up() << ']';
575 else if (d)
576 os << down() << ']';
577 else if (u)
578 os << up() << ']';
582 void InsetMathScript::maple(MapleStream & os) const
584 if (nuc().size())
585 os << nuc();
586 if (hasDown() && down().size())
587 os << '[' << down() << ']';
588 if (hasUp() && up().size())
589 os << "^(" << up() << ')';
593 void InsetMathScript::mathematica(MathematicaStream & os) const
595 bool d = hasDown() && down().size();
596 bool u = hasUp() && up().size();
598 if (nuc().size()) {
599 if (d)
600 os << "Subscript[" << nuc();
601 else
602 os << nuc();
605 if (u)
606 os << "^(" << up() << ')';
608 if (nuc().size()) {
609 if (d)
610 os << ',' << down() << ']';
615 void InsetMathScript::mathmlize(MathStream & os) const
617 bool d = hasDown() && down().size();
618 bool u = hasUp() && up().size();
620 if (u && d)
621 os << MTag("msubsup");
622 else if (u)
623 os << MTag("msup");
624 else if (d)
625 os << MTag("msub");
627 if (nuc().size())
628 os << nuc();
629 else
630 os << "<mrow/>";
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");
641 void InsetMathScript::octave(OctaveStream & os) const
643 if (nuc().size())
644 os << nuc();
645 if (hasDown() && down().size())
646 os << '[' << down() << ']';
647 if (hasUp() && up().size())
648 os << "^(" << up() << ')';
652 void InsetMathScript::infoize(odocstream & os) const
654 os << "Scripts";
658 void InsetMathScript::infoize2(odocstream & os) const
660 if (limits_)
661 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
665 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
667 InsetMathNest::notifyCursorLeaves(old, cur);
669 //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
671 // Remove empty scripts if possible:
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;
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();
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);
715 // redraw
716 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
717 return true;
720 //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
721 return false;
725 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
727 //LYXERR("InsetMathScript: request: " << cmd);
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;
744 InsetMathNest::doDispatch(cur, cmd);
748 } // namespace lyx