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();
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 bool InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
259 ScriptChanger dummy(mi.base);
268 dim.wid = max(dim.wid, up().width());
270 dim.wid = max(dim.wid, down().width());
273 dim.wid = max(dim.wid, up().width());
275 dim.wid = max(dim.wid, down().width());
280 int asc = dy1() + up().ascent();
281 dim.asc = max(na, asc);
286 int des = dy0() + down().descent();
287 dim.des = max(nd, des);
298 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
301 nuc().draw(pi, x + dxx(), y);
303 nuc().setXY(*pi.base.bv, x + dxx(), y);
304 if (editing(pi.base.bv))
305 pi.draw(x + dxx(), y, char_type('.'));
307 ScriptChanger dummy(pi.base);
309 up().draw(pi, x + dx1(), y - dy1());
311 down().draw(pi, x + dx0(), y + dy0());
312 drawMarkers(pi, x, y);
316 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
319 up().metricsT(mi, dim);
321 down().metricsT(mi, dim);
322 nuc().metricsT(mi, dim);
326 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
329 nuc().drawT(pain, x + dxx(), y);
331 up().drawT(pain, x + dx1(), y - dy1());
333 down().drawT(pain, x + dx0(), y + dy0());
338 bool InsetMathScript::hasLimits() const
346 // we can only display limits if the nucleus wants some
349 if (!nuc().back()->isScriptable())
352 if (nuc().back()->asSymbolInset()) {
353 // \intop is an alias for \int\limits, \ointop == \oint\limits
354 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
356 // per default \int has limits beside the \int even in displayed formulas
357 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
361 // assume "real" limits for everything else
366 void InsetMathScript::removeScript(bool up)
369 if (up == cell_1_is_up_)
371 } else if (nargs() == 3) {
373 swap(cells_[1], cells_[2]);
374 cell_1_is_up_ = false;
376 cell_1_is_up_ = true;
383 bool InsetMathScript::has(bool up) const
385 return idxOfScript(up);
389 bool InsetMathScript::hasUp() const
391 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
392 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
393 return idxOfScript(true);
397 bool InsetMathScript::hasDown() const
399 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
400 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
401 return idxOfScript(false);
405 InsetBase::idx_type InsetMathScript::idxOfScript(bool up) const
410 return (cell_1_is_up_ == up) ? 1 : 0;
419 bool InsetMathScript::idxRight(LCursor &) const
425 bool InsetMathScript::idxLeft(LCursor &) const
431 bool InsetMathScript::idxUpDown(LCursor & cur, bool up) const
434 if (cur.idx() == 0) {
435 // don't go up/down if there is no cell in this direction
438 // go up/down only if in the last position
439 // or in the first position of something with displayed limits
440 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
441 cur.idx() = idxOfScript(up);
449 if (has(up) && cur.idx() == idxOfScript(true)) {
450 // can't go further up
453 // otherwise go to last position in the nucleus
455 cur.pos() = cur.lastpos();
460 if (has(up) && cur.idx() == idxOfScript(false)) {
461 // can't go further down
464 // otherwise go to last position in the nucleus
466 cur.pos() = cur.lastpos();
474 void InsetMathScript::write(WriteStream & os) const
478 //if (nuc().back()->takesLimits()) {
486 lyxerr[Debug::MATHED] << "suppressing {} when writing"
492 if (hasDown() /*&& down().size()*/)
493 os << "_{" << down() << '}';
495 if (hasUp() /*&& up().size()*/)
496 os << "^{" << up() << '}';
498 if (lock_ && !os.latex())
503 void InsetMathScript::normalize(NormalStream & os) const
505 bool d = hasDown() && down().size();
506 bool u = hasUp() && up().size();
521 os << down() << ' ' << up() << ']';
529 void InsetMathScript::maple(MapleStream & os) const
533 if (hasDown() && down().size())
534 os << '[' << down() << ']';
535 if (hasUp() && up().size())
536 os << "^(" << up() << ')';
540 void InsetMathScript::mathematica(MathematicaStream & os) const
542 bool d = hasDown() && down().size();
543 bool u = hasUp() && up().size();
547 os << "Subscript[" << nuc();
553 os << "^(" << up() << ')';
557 os << ',' << down() << ']';
562 void InsetMathScript::mathmlize(MathStream & os) const
564 bool d = hasDown() && down().size();
565 bool u = hasUp() && up().size();
568 os << MTag("msubsup");
580 os << down() << up() << ETag("msubsup");
582 os << up() << ETag("msup");
584 os << down() << ETag("msub");
588 void InsetMathScript::octave(OctaveStream & os) const
592 if (hasDown() && down().size())
593 os << '[' << down() << ']';
594 if (hasUp() && up().size())
595 os << "^(" << up() << ')';
599 void InsetMathScript::infoize(odocstream & os) const
605 void InsetMathScript::infoize2(odocstream & os) const
608 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
612 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
614 InsetMathNest::notifyCursorLeaves(cur);
616 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
618 // remove empty scripts if possible
620 // Case of two scripts. In this case, 1 = super, 2 = sub
621 if (cur.idx() == 2 && cell(2).empty()) {
622 // must be a subscript...
623 recordUndoInset(cur);
626 } else if (cur.idx() == 1 && cell(1).empty()) {
627 // must be a superscript...
628 recordUndoInset(cur);
632 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
633 // could be either subscript or super script
634 recordUndoInset(cur);
635 removeScript(cell_1_is_up_);
636 // Let the script inset commit suicide. This is
637 // modelled on LCursor.pullArg(), but tries not to
638 // invoke notifyCursorLeaves again and does not touch
639 // cur (since the top slice will be deleted
641 MathArray ar = cell(0);
642 LCursor tmpcur = cur;
644 tmpcur.cell().erase(tmpcur.pos());
645 tmpcur.cell().insert(tmpcur.pos(), ar);
649 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
654 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
656 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
658 if (cmd.action == LFUN_MATH_LIMITS) {
659 if (!cmd.argument().empty()) {
660 if (cmd.argument() == "limits")
662 else if (cmd.argument() == "nolimits")
666 } else if (limits_ == 0)
667 limits_ = hasLimits() ? -1 : 1;
673 InsetMathNest::doDispatch(cur, cmd);