2 * \file InsetMathScript.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathScript.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
17 #include "InsetMathSymbol.h"
18 #include "dispatchresult.h"
21 #include "funcrequest.h"
24 #include <boost/assert.hpp>
36 InsetMathScript::InsetMathScript()
37 : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
41 InsetMathScript::InsetMathScript(bool up)
42 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
46 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
47 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
49 BOOST_ASSERT(nargs() >= 1);
50 cell(0).push_back(at);
54 auto_ptr<InsetBase> InsetMathScript::doClone() const
56 return auto_ptr<InsetBase>(new InsetMathScript(*this));
60 InsetMathScript const * InsetMathScript::asScriptInset() const
66 InsetMathScript * InsetMathScript::asScriptInset()
72 bool InsetMathScript::idxFirst(LCursor & cur) const
80 bool InsetMathScript::idxLast(LCursor & cur) const
83 cur.pos() = nuc().size();
88 MathArray const & InsetMathScript::down() const
92 BOOST_ASSERT(nargs() > 1);
97 MathArray & InsetMathScript::down()
101 BOOST_ASSERT(nargs() > 1);
106 MathArray const & InsetMathScript::up() const
108 BOOST_ASSERT(nargs() > 1);
113 MathArray & InsetMathScript::up()
115 BOOST_ASSERT(nargs() > 1);
120 void InsetMathScript::ensure(bool up)
123 // just nucleus so far
124 cells_.push_back(MathArray());
126 } else if (nargs() == 2 && !has(up)) {
128 cells_.push_back(cell(1));
131 cells_.push_back(MathArray());
137 MathArray const & InsetMathScript::nuc() const
143 MathArray & InsetMathScript::nuc()
149 int InsetMathScript::dy01(int asc, int des, int what) const
154 dasc = down().ascent();
155 slevel = nuc().slevel();
156 int ascdrop = dasc - slevel;
157 int desdrop = des + nuc().sshift();
158 int mindes = nuc().mindes();
159 des = max(desdrop, ascdrop);
160 des = max(mindes, des);
163 int minasc = nuc().minasc();
164 int ascdrop = asc - up().mindes();
165 int udes = up().descent();
166 asc = udes + nuc().sshift();
167 asc = max(ascdrop, asc);
168 asc = max(minasc, asc);
170 int del = asc - udes - dasc;
171 if (del + des <= 2) {
173 del = slevel - asc + udes;
181 return what ? asc : des;
185 int InsetMathScript::dy0() const
190 int des = down().ascent();
195 des = dy01(na, nd, 0);
201 int InsetMathScript::dy1() const
206 int asc = up().descent();
211 asc = dy01(na, nd, 1);
218 int InsetMathScript::dx0() const
220 BOOST_ASSERT(hasDown());
221 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
225 int InsetMathScript::dx1() const
227 BOOST_ASSERT(hasUp());
228 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid() + nker();
232 int InsetMathScript::dxx() const
234 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
238 int InsetMathScript::nwid() const
240 return nuc().size() ? nuc().width() : 2;
244 int InsetMathScript::nasc() const
246 return nuc().size() ? nuc().ascent() : 5;
250 int InsetMathScript::ndes() const
252 return nuc().size() ? nuc().descent() : 0;
256 int InsetMathScript::nker() const
259 int kerning = nuc().kerning();
260 return kerning > 0 ? kerning : 0;
266 bool InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
269 ScriptChanger dummy(mi.base);
278 dim.wid = max(dim.wid, up().width());
280 dim.wid = max(dim.wid, down().width());
283 dim.wid = max(dim.wid, nker() + up().width());
285 dim.wid = max(dim.wid, down().width());
290 int asc = dy1() + up().ascent();
291 dim.asc = max(na, asc);
296 int des = dy0() + down().descent();
297 dim.des = max(nd, des);
308 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
311 nuc().draw(pi, x + dxx(), y);
313 nuc().setXY(*pi.base.bv, x + dxx(), y);
314 if (editing(pi.base.bv))
315 pi.draw(x + dxx(), y, char_type('.'));
317 ScriptChanger dummy(pi.base);
319 up().draw(pi, x + dx1(), y - dy1());
321 down().draw(pi, x + dx0(), y + dy0());
322 drawMarkers(pi, x, y);
326 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
329 up().metricsT(mi, dim);
331 down().metricsT(mi, dim);
332 nuc().metricsT(mi, dim);
336 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
339 nuc().drawT(pain, x + dxx(), y);
341 up().drawT(pain, x + dx1(), y - dy1());
343 down().drawT(pain, x + dx0(), y + dy0());
348 bool InsetMathScript::hasLimits() const
356 // we can only display limits if the nucleus wants some
359 if (!nuc().back()->isScriptable())
362 if (nuc().back()->asSymbolInset()) {
363 // \intop is an alias for \int\limits, \ointop == \oint\limits
364 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
366 // per default \int has limits beside the \int even in displayed formulas
367 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
371 // assume "real" limits for everything else
376 void InsetMathScript::removeScript(bool up)
379 if (up == cell_1_is_up_)
381 } else if (nargs() == 3) {
383 swap(cells_[1], cells_[2]);
384 cell_1_is_up_ = false;
386 cell_1_is_up_ = true;
393 bool InsetMathScript::has(bool up) const
395 return idxOfScript(up);
399 bool InsetMathScript::hasUp() const
401 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
402 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
403 return idxOfScript(true);
407 bool InsetMathScript::hasDown() const
409 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
410 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
411 return idxOfScript(false);
415 InsetBase::idx_type InsetMathScript::idxOfScript(bool up) const
420 return (cell_1_is_up_ == up) ? 1 : 0;
429 bool InsetMathScript::idxRight(LCursor &) const
435 bool InsetMathScript::idxLeft(LCursor &) const
441 bool InsetMathScript::idxUpDown(LCursor & cur, bool up) const
444 if (cur.idx() == 0) {
445 // don't go up/down if there is no cell in this direction
448 // go up/down only if in the last position
449 // or in the first position of something with displayed limits
450 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
451 cur.idx() = idxOfScript(up);
459 if (has(up) && cur.idx() == idxOfScript(true)) {
460 // can't go further up
463 // otherwise go to last position in the nucleus
465 cur.pos() = cur.lastpos();
470 if (has(up) && cur.idx() == idxOfScript(false)) {
471 // can't go further down
474 // otherwise go to last position in the nucleus
476 cur.pos() = cur.lastpos();
484 void InsetMathScript::write(WriteStream & os) const
488 //if (nuc().back()->takesLimits()) {
496 lyxerr[Debug::MATHED] << "suppressing {} when writing"
502 if (hasDown() /*&& down().size()*/)
503 os << "_{" << down() << '}';
505 if (hasUp() /*&& up().size()*/)
506 os << "^{" << up() << '}';
508 if (lock_ && !os.latex())
513 void InsetMathScript::normalize(NormalStream & os) const
515 bool d = hasDown() && down().size();
516 bool u = hasUp() && up().size();
531 os << down() << ' ' << up() << ']';
539 void InsetMathScript::maple(MapleStream & os) const
543 if (hasDown() && down().size())
544 os << '[' << down() << ']';
545 if (hasUp() && up().size())
546 os << "^(" << up() << ')';
550 void InsetMathScript::mathematica(MathematicaStream & os) const
552 bool d = hasDown() && down().size();
553 bool u = hasUp() && up().size();
557 os << "Subscript[" << nuc();
563 os << "^(" << up() << ')';
567 os << ',' << down() << ']';
572 void InsetMathScript::mathmlize(MathStream & os) const
574 bool d = hasDown() && down().size();
575 bool u = hasUp() && up().size();
578 os << MTag("msubsup");
590 os << down() << up() << ETag("msubsup");
592 os << up() << ETag("msup");
594 os << down() << ETag("msub");
598 void InsetMathScript::octave(OctaveStream & os) const
602 if (hasDown() && down().size())
603 os << '[' << down() << ']';
604 if (hasUp() && up().size())
605 os << "^(" << up() << ')';
609 void InsetMathScript::infoize(odocstream & os) const
615 void InsetMathScript::infoize2(odocstream & os) const
618 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
622 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
624 InsetMathNest::notifyCursorLeaves(cur);
626 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
628 // remove empty scripts if possible
630 // Case of two scripts. In this case, 1 = super, 2 = sub
631 if (cur.idx() == 2 && cell(2).empty()) {
632 // must be a subscript...
633 recordUndoInset(cur);
636 } else if (cur.idx() == 1 && cell(1).empty()) {
637 // must be a superscript...
638 recordUndoInset(cur);
642 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
643 // could be either subscript or super script
644 recordUndoInset(cur);
645 removeScript(cell_1_is_up_);
646 // Let the script inset commit suicide. This is
647 // modelled on LCursor.pullArg(), but tries not to
648 // invoke notifyCursorLeaves again and does not touch
649 // cur (since the top slice will be deleted
651 MathArray ar = cell(0);
652 LCursor tmpcur = cur;
654 tmpcur.cell().erase(tmpcur.pos());
655 tmpcur.cell().insert(tmpcur.pos(), ar);
659 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
664 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
666 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
668 if (cmd.action == LFUN_MATH_LIMITS) {
669 if (!cmd.argument().empty()) {
670 if (cmd.argument() == "limits")
672 else if (cmd.argument() == "nolimits")
676 } else if (limits_ == 0)
677 limits_ = hasLimits() ? -1 : 1;
683 InsetMathNest::doDispatch(cur, cmd);