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 "MathMLStream.h"
16 #include "MathSupport.h"
17 #include "InsetMathSymbol.h"
18 #include "dispatchresult.h"
21 #include "funcrequest.h"
24 #include <boost/assert.hpp>
35 InsetMathScript::InsetMathScript()
36 : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
40 InsetMathScript::InsetMathScript(bool up)
41 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
45 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
46 : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
48 BOOST_ASSERT(nargs() >= 1);
49 cell(0).push_back(at);
53 auto_ptr<InsetBase> InsetMathScript::doClone() const
55 return auto_ptr<InsetBase>(new InsetMathScript(*this));
59 InsetMathScript const * InsetMathScript::asScriptInset() const
65 InsetMathScript * InsetMathScript::asScriptInset()
71 bool InsetMathScript::idxFirst(LCursor & cur) const
79 bool InsetMathScript::idxLast(LCursor & cur) const
82 cur.pos() = nuc().size();
87 MathArray const & InsetMathScript::down() const
91 BOOST_ASSERT(nargs() > 1);
96 MathArray & InsetMathScript::down()
100 BOOST_ASSERT(nargs() > 1);
105 MathArray const & InsetMathScript::up() const
107 BOOST_ASSERT(nargs() > 1);
112 MathArray & InsetMathScript::up()
114 BOOST_ASSERT(nargs() > 1);
119 void InsetMathScript::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 & InsetMathScript::nuc() const
142 MathArray & InsetMathScript::nuc()
148 int InsetMathScript::dy0() const
153 int des = down().ascent();
162 int InsetMathScript::dy1() const
167 int asc = up().descent();
177 int InsetMathScript::dx0() const
179 BOOST_ASSERT(hasDown());
180 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
184 int InsetMathScript::dx1() const
186 BOOST_ASSERT(hasUp());
187 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
191 int InsetMathScript::dxx() const
193 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
197 int InsetMathScript::nwid() const
199 return nuc().size() ? nuc().width() : 2;
203 int InsetMathScript::nasc() const
205 return nuc().size() ? nuc().ascent() : 5;
209 int InsetMathScript::ndes() const
211 return nuc().size() ? nuc().descent() : 0;
215 void InsetMathScript::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 InsetMathScript::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 InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
265 up().metricsT(mi, dim);
267 down().metricsT(mi, dim);
268 nuc().metricsT(mi, dim);
272 void InsetMathScript::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 InsetMathScript::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 InsetMathScript::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 InsetMathScript::has(bool up) const
331 return idxOfScript(up);
335 bool InsetMathScript::hasUp() const
337 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
338 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
339 return idxOfScript(true);
343 bool InsetMathScript::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 InsetMathScript::idxOfScript(bool up) const
356 return (cell_1_is_up_ == up) ? 1 : 0;
365 bool InsetMathScript::idxRight(LCursor &) const
371 bool InsetMathScript::idxLeft(LCursor &) const
377 bool InsetMathScript::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 InsetMathScript::write(WriteStream & os) const
423 if (nuc().size() == 1
424 && ! nuc().begin()->nucleus()->asScriptInset())
427 os << '{' << nuc() << '}';
428 //if (nuc().back()->takesLimits()) {
436 lyxerr[Debug::MATHED] << "suppressing {} when writing"
442 if (hasDown() /*&& down().size()*/)
443 os << "_{" << down() << '}';
445 if (hasUp() /*&& up().size()*/)
446 os << "^{" << up() << '}';
448 if (lock_ && !os.latex())
453 void InsetMathScript::normalize(NormalStream & os) const
455 bool d = hasDown() && down().size();
456 bool u = hasUp() && up().size();
471 os << down() << ' ' << up() << ']';
479 void InsetMathScript::maple(MapleStream & os) const
483 if (hasDown() && down().size())
484 os << '[' << down() << ']';
485 if (hasUp() && up().size())
486 os << "^(" << up() << ')';
490 void InsetMathScript::mathematica(MathematicaStream & os) const
492 bool d = hasDown() && down().size();
493 bool u = hasUp() && up().size();
497 os << "Subscript[" << nuc();
503 os << "^(" << up() << ')';
507 os << ',' << down() << ']';
512 void InsetMathScript::mathmlize(MathMLStream & os) const
514 bool d = hasDown() && down().size();
515 bool u = hasUp() && up().size();
518 os << MTag("msubsup");
530 os << down() << up() << ETag("msubsup");
532 os << up() << ETag("msup");
534 os << down() << ETag("msub");
538 void InsetMathScript::octave(OctaveStream & os) const
542 if (hasDown() && down().size())
543 os << '[' << down() << ']';
544 if (hasUp() && up().size())
545 os << "^(" << up() << ')';
549 void InsetMathScript::infoize(std::ostream & os) const
555 void InsetMathScript::infoize2(std::ostream & os) const
558 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
562 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
564 InsetMathNest::notifyCursorLeaves(cur);
566 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
568 // remove empty scripts if possible
570 // Case of two scripts. In this case, 1 = super, 2 = sub
571 if (cur.idx() == 2 && cell(2).empty()) {
572 // must be a subscript...
573 recordUndoInset(cur);
576 } else if (cur.idx() == 1 && cell(1).empty()) {
577 // must be a superscript...
578 recordUndoInset(cur);
582 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
583 // could be either subscript or super script
584 recordUndoInset(cur);
585 removeScript(cell_1_is_up_);
586 // Let the script inset commit suicide. This is
587 // modelled on LCursor.pullArg(), but tries not to
588 // invoke notifyCursorLeaves again and does not touch
589 // cur (since the top slice will be deleted
591 MathArray ar = cell(0);
592 LCursor tmpcur = cur;
594 tmpcur.cell().erase(tmpcur.pos());
595 tmpcur.cell().insert(tmpcur.pos(), ar);
599 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
604 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
606 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
608 if (cmd.action == LFUN_MATH_LIMITS) {
609 if (!cmd.argument().empty()) {
610 if (cmd.argument() == "limits")
612 else if (cmd.argument() == "nolimits")
616 } else if (limits_ == 0)
617 limits_ = hasLimits() ? -1 : 1;
623 InsetMathNest::doDispatch(cur, cmd);