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
425 //if (nuc().back()->takesLimits()) {
433 lyxerr[Debug::MATHED] << "suppressing {} when writing"
439 if (hasDown() /*&& down().size()*/)
440 os << "_{" << down() << '}';
442 if (hasUp() /*&& up().size()*/)
443 os << "^{" << up() << '}';
445 if (lock_ && !os.latex())
450 void InsetMathScript::normalize(NormalStream & os) const
452 bool d = hasDown() && down().size();
453 bool u = hasUp() && up().size();
468 os << down() << ' ' << up() << ']';
476 void InsetMathScript::maple(MapleStream & os) const
480 if (hasDown() && down().size())
481 os << '[' << down() << ']';
482 if (hasUp() && up().size())
483 os << "^(" << up() << ')';
487 void InsetMathScript::mathematica(MathematicaStream & os) const
489 bool d = hasDown() && down().size();
490 bool u = hasUp() && up().size();
494 os << "Subscript[" << nuc();
500 os << "^(" << up() << ')';
504 os << ',' << down() << ']';
509 void InsetMathScript::mathmlize(MathStream & os) const
511 bool d = hasDown() && down().size();
512 bool u = hasUp() && up().size();
515 os << MTag("msubsup");
527 os << down() << up() << ETag("msubsup");
529 os << up() << ETag("msup");
531 os << down() << ETag("msub");
535 void InsetMathScript::octave(OctaveStream & os) const
539 if (hasDown() && down().size())
540 os << '[' << down() << ']';
541 if (hasUp() && up().size())
542 os << "^(" << up() << ')';
546 void InsetMathScript::infoize(odocstream & os) const
552 void InsetMathScript::infoize2(odocstream & os) const
555 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
559 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
561 InsetMathNest::notifyCursorLeaves(cur);
563 //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
565 // remove empty scripts if possible
567 // Case of two scripts. In this case, 1 = super, 2 = sub
568 if (cur.idx() == 2 && cell(2).empty()) {
569 // must be a subscript...
570 recordUndoInset(cur);
573 } else if (cur.idx() == 1 && cell(1).empty()) {
574 // must be a superscript...
575 recordUndoInset(cur);
579 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
580 // could be either subscript or super script
581 recordUndoInset(cur);
582 removeScript(cell_1_is_up_);
583 // Let the script inset commit suicide. This is
584 // modelled on LCursor.pullArg(), but tries not to
585 // invoke notifyCursorLeaves again and does not touch
586 // cur (since the top slice will be deleted
588 MathArray ar = cell(0);
589 LCursor tmpcur = cur;
591 tmpcur.cell().erase(tmpcur.pos());
592 tmpcur.cell().insert(tmpcur.pos(), ar);
596 //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
601 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
603 //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
605 if (cmd.action == LFUN_MATH_LIMITS) {
606 if (!cmd.argument().empty()) {
607 if (cmd.argument() == "limits")
609 else if (cmd.argument() == "nolimits")
613 } else if (limits_ == 0)
614 limits_ = hasLimits() ? -1 : 1;
620 InsetMathNest::doDispatch(cur, cmd);