2 * \file math_scriptinset.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 "math_scriptinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_support.h"
17 #include "math_symbolinset.h"
18 #include "dispatchresult.h"
21 #include "funcrequest.h"
24 #include <boost/assert.hpp>
35 MathScriptInset::MathScriptInset()
36 : MathNestInset(1), cell_1_is_up_(false), limits_(0)
40 MathScriptInset::MathScriptInset(bool up)
41 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
45 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
46 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
48 BOOST_ASSERT(nargs() >= 1);
49 cell(0).push_back(at);
53 auto_ptr<InsetBase> MathScriptInset::doClone() const
55 return auto_ptr<InsetBase>(new MathScriptInset(*this));
59 MathScriptInset const * MathScriptInset::asScriptInset() const
65 MathScriptInset * MathScriptInset::asScriptInset()
71 bool MathScriptInset::idxFirst(LCursor & cur) const
79 bool MathScriptInset::idxLast(LCursor & cur) const
82 cur.pos() = nuc().size();
87 MathArray const & MathScriptInset::down() const
91 BOOST_ASSERT(nargs() > 1);
96 MathArray & MathScriptInset::down()
100 BOOST_ASSERT(nargs() > 1);
105 MathArray const & MathScriptInset::up() const
107 BOOST_ASSERT(nargs() > 1);
112 MathArray & MathScriptInset::up()
114 BOOST_ASSERT(nargs() > 1);
119 void MathScriptInset::ensure(bool up)
122 // just nucleus so far
123 cells_.push_back(MathArray());
125 } else if (nargs() == 2 && !has(up)) {
127 cells_.push_back(cell(1));
130 cells_.push_back(MathArray());
136 MathArray const & MathScriptInset::nuc() const
142 MathArray & MathScriptInset::nuc()
148 int MathScriptInset::dy0() const
153 int des = down().ascent();
162 int MathScriptInset::dy1() const
167 int asc = up().descent();
177 int MathScriptInset::dx0() const
179 BOOST_ASSERT(hasDown());
180 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
184 int MathScriptInset::dx1() const
186 BOOST_ASSERT(hasUp());
187 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
191 int MathScriptInset::dxx() const
193 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
197 int MathScriptInset::nwid() const
199 return nuc().size() ? nuc().width() : 2;
203 int MathScriptInset::nasc() const
205 return nuc().size() ? nuc().ascent() : 5;
209 int MathScriptInset::ndes() const
211 return nuc().size() ? nuc().descent() : 0;
215 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
218 ScriptChanger dummy(mi.base);
227 dim.wid = max(dim.wid, up().width());
229 dim.wid = max(dim.wid, down().width());
232 dim.wid = max(dim.wid, up().width());
234 dim.wid = max(dim.wid, down().width());
237 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
238 dim.des = dy0() + (hasDown() ? down().descent() : 0);
244 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
247 nuc().draw(pi, x + dxx(), y);
249 nuc().setXY(x + dxx(), y);
250 if (editing(pi.base.bv))
251 pi.draw(x + dxx(), y, char_type('.'));
253 ScriptChanger dummy(pi.base);
255 up().draw(pi, x + dx1(), y - dy1());
257 down().draw(pi, x + dx0(), y + dy0());
258 drawMarkers(pi, x, y);
262 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
265 up().metricsT(mi, dim);
267 down().metricsT(mi, dim);
268 nuc().metricsT(mi, dim);
272 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
275 nuc().drawT(pain, x + dxx(), y);
277 up().drawT(pain, x + dx1(), y - dy1());
279 down().drawT(pain, x + dx0(), y + dy0());
284 bool MathScriptInset::hasLimits() const
292 // we can only display limits if the nucleus wants some
295 if (!nuc().back()->isScriptable())
298 if (nuc().back()->asSymbolInset()) {
299 // \intop is an alias for \int\limits, \ointop == \oint\limits
300 if (nuc().back()->asSymbolInset()->name().find("intop") != string::npos)
302 // per default \int has limits beside the \int even in displayed formulas
303 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
307 // assume "real" limits for everything else
312 void MathScriptInset::removeScript(bool up)
315 if (up == cell_1_is_up_)
317 } else if (nargs() == 3) {
319 swap(cells_[1], cells_[2]);
320 cell_1_is_up_ = false;
322 cell_1_is_up_ = true;
329 bool MathScriptInset::has(bool up) const
331 return idxOfScript(up);
335 bool MathScriptInset::hasUp() const
337 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
338 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
339 return idxOfScript(true);
343 bool MathScriptInset::hasDown() const
345 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
346 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
347 return idxOfScript(false);
351 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
356 return (cell_1_is_up_ == up) ? 1 : 0;
365 bool MathScriptInset::idxRight(LCursor &) const
371 bool MathScriptInset::idxLeft(LCursor &) const
377 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
380 if (cur.idx() == 0) {
381 // don't go up/down if there is no cell in this direction
384 // go up/down only if in the last position
385 // or in the first position of something with displayed limits
386 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
387 cur.idx() = idxOfScript(up);
395 if (has(up) && cur.idx() == idxOfScript(true)) {
396 // can't go further up
399 // otherwise go to last position in the nucleus
401 cur.pos() = cur.lastpos();
406 if (has(up) && cur.idx() == idxOfScript(false)) {
407 // can't go further down
410 // otherwise go to last position in the nucleus
412 cur.pos() = cur.lastpos();
420 void MathScriptInset::write(WriteStream & os) const
424 //if (nuc().back()->takesLimits()) {
432 lyxerr[Debug::MATHED] << "suppressing {} when writing"
438 if (hasDown() /*&& down().size()*/)
439 os << "_{" << down() << '}';
441 if (hasUp() /*&& up().size()*/)
442 os << "^{" << up() << '}';
444 if (lock_ && !os.latex())
449 void MathScriptInset::normalize(NormalStream & os) const
451 bool d = hasDown() && down().size();
452 bool u = hasUp() && up().size();
467 os << down() << ' ' << up() << ']';
475 void MathScriptInset::maple(MapleStream & os) const
479 if (hasDown() && down().size())
480 os << '[' << down() << ']';
481 if (hasUp() && up().size())
482 os << "^(" << up() << ')';
486 void MathScriptInset::mathematica(MathematicaStream & os) const
488 bool d = hasDown() && down().size();
489 bool u = hasUp() && up().size();
493 os << "Subscript[" << nuc();
499 os << "^(" << up() << ')';
503 os << ',' << down() << ']';
508 void MathScriptInset::mathmlize(MathMLStream & os) const
510 bool d = hasDown() && down().size();
511 bool u = hasUp() && up().size();
514 os << MTag("msubsup");
526 os << down() << up() << ETag("msubsup");
528 os << up() << ETag("msup");
530 os << down() << ETag("msub");
534 void MathScriptInset::octave(OctaveStream & os) const
538 if (hasDown() && down().size())
539 os << '[' << down() << ']';
540 if (hasUp() && up().size())
541 os << "^(" << up() << ')';
545 void MathScriptInset::infoize(std::ostream & os) const
551 void MathScriptInset::infoize2(std::ostream & os) const
554 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
558 bool MathScriptInset::notifyCursorLeaves(LCursor & cur)
560 MathNestInset::notifyCursorLeaves(cur);
562 //lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl;
564 // remove empty scripts if possible
566 // Case of two scripts. In this case, 1 = super, 2 = sub
567 if (cur.idx() == 2 && cell(2).empty()) {
568 // must be a subscript...
569 recordUndoInset(cur);
572 } else if (cur.idx() == 1 && cell(1).empty()) {
573 // must be a superscript...
574 recordUndoInset(cur);
578 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
579 // could be either subscript or super script
580 recordUndoInset(cur);
581 removeScript(cell_1_is_up_);
582 // Let the script inset commit suicide. This is
583 // modelled on LCursor.pullArg(), but tries not to
584 // invoke notifyCursorLeaves again and does not touch
585 // cur (since the top slice will be deleted
587 MathArray ar = cell(0);
588 LCursor tmpcur = cur;
590 tmpcur.cell().erase(tmpcur.pos());
591 tmpcur.cell().insert(tmpcur.pos(), ar);
595 //lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
600 void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd)
602 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
604 if (cmd.action == LFUN_MATH_LIMITS) {
605 if (!cmd.argument.empty()) {
606 if (cmd.argument == "limits")
608 else if (cmd.argument == "nolimits")
612 } else if (limits_ == 0)
613 limits_ = hasLimits() ? -1 : 1;
619 MathNestInset::doDispatch(cur, cmd);