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::dy0() const
154 int des = down().ascent();
163 int InsetMathScript::dy1() const
168 int asc = up().descent();
178 int InsetMathScript::dx0() const
180 BOOST_ASSERT(hasDown());
181 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
185 int InsetMathScript::dx1() const
187 BOOST_ASSERT(hasUp());
188 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
192 int InsetMathScript::dxx() const
194 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
198 int InsetMathScript::nwid() const
200 return nuc().size() ? nuc().width() : 2;
204 int InsetMathScript::nasc() const
206 return nuc().size() ? nuc().ascent() : 5;
210 int InsetMathScript::ndes() const
212 return nuc().size() ? nuc().descent() : 0;
216 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
219 ScriptChanger dummy(mi.base);
228 dim.wid = max(dim.wid, up().width());
230 dim.wid = max(dim.wid, down().width());
233 dim.wid = max(dim.wid, up().width());
235 dim.wid = max(dim.wid, down().width());
238 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
239 dim.des = dy0() + (hasDown() ? down().descent() : 0);
245 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
248 nuc().draw(pi, x + dxx(), y);
250 nuc().setXY(*pi.base.bv, x + dxx(), y);
251 if (editing(pi.base.bv))
252 pi.draw(x + dxx(), y, char_type('.'));
254 ScriptChanger dummy(pi.base);
256 up().draw(pi, x + dx1(), y - dy1());
258 down().draw(pi, x + dx0(), y + dy0());
259 drawMarkers(pi, x, y);
263 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
266 up().metricsT(mi, dim);
268 down().metricsT(mi, dim);
269 nuc().metricsT(mi, dim);
273 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
276 nuc().drawT(pain, x + dxx(), y);
278 up().drawT(pain, x + dx1(), y - dy1());
280 down().drawT(pain, x + dx0(), y + dy0());
285 bool InsetMathScript::hasLimits() const
293 // we can only display limits if the nucleus wants some
296 if (!nuc().back()->isScriptable())
299 if (nuc().back()->asSymbolInset()) {
300 // \intop is an alias for \int\limits, \ointop == \oint\limits
301 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
303 // per default \int has limits beside the \int even in displayed formulas
304 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
308 // assume "real" limits for everything else
313 void InsetMathScript::removeScript(bool up)
316 if (up == cell_1_is_up_)
318 } else if (nargs() == 3) {
320 swap(cells_[1], cells_[2]);
321 cell_1_is_up_ = false;
323 cell_1_is_up_ = true;
330 bool InsetMathScript::has(bool up) const
332 return idxOfScript(up);
336 bool InsetMathScript::hasUp() const
338 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
339 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
340 return idxOfScript(true);
344 bool InsetMathScript::hasDown() const
346 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
347 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
348 return idxOfScript(false);
352 InsetBase::idx_type InsetMathScript::idxOfScript(bool up) const
357 return (cell_1_is_up_ == up) ? 1 : 0;
366 bool InsetMathScript::idxRight(LCursor &) const
372 bool InsetMathScript::idxLeft(LCursor &) const
378 bool InsetMathScript::idxUpDown(LCursor & cur, bool up) const
381 if (cur.idx() == 0) {
382 // don't go up/down if there is no cell in this direction
385 // go up/down only if in the last position
386 // or in the first position of something with displayed limits
387 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
388 cur.idx() = idxOfScript(up);
396 if (has(up) && cur.idx() == idxOfScript(true)) {
397 // can't go further up
400 // otherwise go to last position in the nucleus
402 cur.pos() = cur.lastpos();
407 if (has(up) && cur.idx() == idxOfScript(false)) {
408 // can't go further down
411 // otherwise go to last position in the nucleus
413 cur.pos() = cur.lastpos();
421 void InsetMathScript::write(WriteStream & os) const
424 if (nuc().size() == 1
425 && ! nuc().begin()->nucleus()->asScriptInset())
428 os << '{' << nuc() << '}';
429 //if (nuc().back()->takesLimits()) {
437 lyxerr[Debug::MATHED] << "suppressing {} when writing"
443 if (hasDown() /*&& down().size()*/)
444 os << "_{" << down() << '}';
446 if (hasUp() /*&& up().size()*/)
447 os << "^{" << up() << '}';
449 if (lock_ && !os.latex())
454 void InsetMathScript::normalize(NormalStream & os) const
456 bool d = hasDown() && down().size();
457 bool u = hasUp() && up().size();
472 os << down() << ' ' << up() << ']';
480 void InsetMathScript::maple(MapleStream & os) const
484 if (hasDown() && down().size())
485 os << '[' << down() << ']';
486 if (hasUp() && up().size())
487 os << "^(" << up() << ')';
491 void InsetMathScript::mathematica(MathematicaStream & os) const
493 bool d = hasDown() && down().size();
494 bool u = hasUp() && up().size();
498 os << "Subscript[" << nuc();
504 os << "^(" << up() << ')';
508 os << ',' << down() << ']';
513 void InsetMathScript::mathmlize(MathStream & os) const
515 bool d = hasDown() && down().size();
516 bool u = hasUp() && up().size();
519 os << MTag("msubsup");
531 os << down() << up() << ETag("msubsup");
533 os << up() << ETag("msup");
535 os << down() << ETag("msub");
539 void InsetMathScript::octave(OctaveStream & os) const
543 if (hasDown() && down().size())
544 os << '[' << down() << ']';
545 if (hasUp() && up().size())
546 os << "^(" << up() << ')';
550 void InsetMathScript::infoize(odocstream & os) const
556 void InsetMathScript::infoize2(odocstream & os) const
559 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
563 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
565 InsetMathNest::notifyCursorLeaves(cur);
567 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
569 // remove empty scripts if possible
571 // Case of two scripts. In this case, 1 = super, 2 = sub
572 if (cur.idx() == 2 && cell(2).empty()) {
573 // must be a subscript...
574 recordUndoInset(cur);
577 } else if (cur.idx() == 1 && cell(1).empty()) {
578 // must be a superscript...
579 recordUndoInset(cur);
583 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
584 // could be either subscript or super script
585 recordUndoInset(cur);
586 removeScript(cell_1_is_up_);
587 // Let the script inset commit suicide. This is
588 // modelled on LCursor.pullArg(), but tries not to
589 // invoke notifyCursorLeaves again and does not touch
590 // cur (since the top slice will be deleted
592 MathArray ar = cell(0);
593 LCursor tmpcur = cur;
595 tmpcur.cell().erase(tmpcur.pos());
596 tmpcur.cell().insert(tmpcur.pos(), ar);
600 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
605 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
607 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
609 if (cmd.action == LFUN_MATH_LIMITS) {
610 if (!cmd.argument().empty()) {
611 if (cmd.argument() == "limits")
613 else if (cmd.argument() == "nolimits")
617 } else if (limits_ == 0)
618 limits_ = hasLimits() ? -1 : 1;
624 InsetMathNest::doDispatch(cur, cmd);