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"
23 #include <boost/assert.hpp>
32 MathScriptInset::MathScriptInset()
33 : MathNestInset(1), cell_1_is_up_(false), limits_(0)
37 MathScriptInset::MathScriptInset(bool up)
38 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
42 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
43 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
45 BOOST_ASSERT(nargs() >= 1);
46 cell(0).push_back(at);
50 auto_ptr<InsetBase> MathScriptInset::doClone() const
52 return auto_ptr<InsetBase>(new MathScriptInset(*this));
56 MathScriptInset const * MathScriptInset::asScriptInset() const
62 MathScriptInset * MathScriptInset::asScriptInset()
68 bool MathScriptInset::idxFirst(LCursor & cur) const
76 bool MathScriptInset::idxLast(LCursor & cur) const
79 cur.pos() = nuc().size();
84 MathArray const & MathScriptInset::down() const
88 BOOST_ASSERT(nargs() > 1);
93 MathArray & MathScriptInset::down()
97 BOOST_ASSERT(nargs() > 1);
102 MathArray const & MathScriptInset::up() const
104 BOOST_ASSERT(nargs() > 1);
109 MathArray & MathScriptInset::up()
111 BOOST_ASSERT(nargs() > 1);
116 void MathScriptInset::ensure(bool up)
119 // just nucleus so far
120 cells_.push_back(MathArray());
122 } else if (nargs() == 2 && !has(up)) {
124 cells_.push_back(cell(1));
127 cells_.push_back(MathArray());
133 MathArray const & MathScriptInset::nuc() const
139 MathArray & MathScriptInset::nuc()
145 int MathScriptInset::dy0() const
150 int des = down().ascent();
159 int MathScriptInset::dy1() const
164 int asc = up().descent();
174 int MathScriptInset::dx0() const
176 BOOST_ASSERT(hasDown());
177 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
181 int MathScriptInset::dx1() const
183 BOOST_ASSERT(hasUp());
184 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
188 int MathScriptInset::dxx() const
190 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
194 int MathScriptInset::nwid() const
196 return nuc().size() ? nuc().width() : 2;
200 int MathScriptInset::nasc() const
202 return nuc().size() ? nuc().ascent() : 5;
206 int MathScriptInset::ndes() const
208 return nuc().size() ? nuc().descent() : 0;
212 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
215 ScriptChanger dummy(mi.base);
224 dim.wid = max(dim.wid, up().width());
226 dim.wid = max(dim.wid, down().width());
229 dim.wid = max(dim.wid, up().width());
231 dim.wid = max(dim.wid, down().width());
234 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
235 dim.des = dy0() + (hasDown() ? down().descent() : 0);
241 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
244 nuc().draw(pi, x + dxx(), y);
246 nuc().setXY(x + dxx(), y);
247 if (editing(pi.base.bv))
248 pi.draw(x + dxx(), y, ".");
250 ScriptChanger dummy(pi.base);
252 up().draw(pi, x + dx1(), y - dy1());
254 down().draw(pi, x + dx0(), y + dy0());
255 drawMarkers(pi, x, y);
259 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
262 up().metricsT(mi, dim);
264 down().metricsT(mi, dim);
265 nuc().metricsT(mi, dim);
269 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
272 nuc().drawT(pain, x + dxx(), y);
274 up().drawT(pain, x + dx1(), y - dy1());
276 down().drawT(pain, x + dx0(), y + dy0());
281 bool MathScriptInset::hasLimits() const
289 // we can only display limits if the nucleus wants some
292 if (!nuc().back()->isScriptable())
295 if (nuc().back()->asSymbolInset()) {
296 // \intop is an alias for \int\limits, \ointop == \oint\limits
297 if (nuc().back()->asSymbolInset()->name().find("intop") != string::npos)
299 // per default \int has limits beside the \int even in displayed formulas
300 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
304 // assume "real" limits for everything else
309 void MathScriptInset::removeScript(bool up)
312 if (up == cell_1_is_up_)
314 } else if (nargs() == 3) {
316 swap(cells_[1], cells_[2]);
317 cell_1_is_up_ = false;
319 cell_1_is_up_ = true;
323 lyxerr << "MathScriptInset::removeScript: 2 up: " << up << endl;
327 bool MathScriptInset::has(bool up) const
329 return idxOfScript(up);
333 bool MathScriptInset::hasUp() const
335 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
336 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
337 return idxOfScript(true);
341 bool MathScriptInset::hasDown() const
343 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
344 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
345 return idxOfScript(false);
349 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
354 return (cell_1_is_up_ == up) ? 1 : 0;
363 bool MathScriptInset::idxRight(LCursor &) const
369 bool MathScriptInset::idxLeft(LCursor &) const
375 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
378 if (cur.idx() == 0) {
379 // don't go up/down if there is no cell in this direction
382 // go up/down only if in the last position
383 // or in the first position of something with displayed limits
384 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
385 cur.idx() = idxOfScript(up);
393 if (has(up) && cur.idx() == idxOfScript(true)) {
394 // can't go further up
397 // otherwise go to last position in the nucleus
399 cur.pos() = cur.lastpos();
404 if (has(up) && cur.idx() == idxOfScript(false)) {
405 // can't go further down
408 // otherwise go to last position in the nucleus
410 cur.pos() = cur.lastpos();
418 void MathScriptInset::write(WriteStream & os) const
422 //if (nuc().back()->takesLimits()) {
430 lyxerr[Debug::MATHED] << "suppressing {} when writing"
436 if (hasDown() && down().size())
437 os << "_{" << down() << '}';
439 if (hasUp() && up().size())
440 os << "^{" << up() << '}';
442 if (lock_ && !os.latex())
447 void MathScriptInset::normalize(NormalStream & os) const
449 bool d = hasDown() && down().size();
450 bool u = hasUp() && up().size();
465 os << down() << ' ' << up() << ']';
473 void MathScriptInset::maple(MapleStream & os) const
477 if (hasDown() && down().size())
478 os << '[' << down() << ']';
479 if (hasUp() && up().size())
480 os << "^(" << up() << ')';
484 void MathScriptInset::mathematica(MathematicaStream & os) const
486 bool d = hasDown() && down().size();
487 bool u = hasUp() && up().size();
491 os << "Subscript[" << nuc();
497 os << "^(" << up() << ')';
501 os << ',' << down() << ']';
506 void MathScriptInset::mathmlize(MathMLStream & os) const
508 bool d = hasDown() && down().size();
509 bool u = hasUp() && up().size();
512 os << MTag("msubsup");
524 os << down() << up() << ETag("msubsup");
526 os << up() << ETag("msup");
528 os << down() << ETag("msub");
532 void MathScriptInset::octave(OctaveStream & os) const
536 if (hasDown() && down().size())
537 os << '[' << down() << ']';
538 if (hasUp() && up().size())
539 os << "^(" << up() << ')';
543 void MathScriptInset::infoize(std::ostream & os) const
549 void MathScriptInset::infoize2(std::ostream & os) const
552 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
556 void MathScriptInset::notifyCursorLeaves(LCursor & cur)
558 MathNestInset::notifyCursorLeaves(cur);
560 //lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl;
562 // remove empty scripts if possible
564 // Case of two scripts. In this case, 1 = super, 2 = sub
565 if (cur.idx() == 2 && cell(2).empty()) {
566 // must be a subscript...
568 } else if (cur.idx() == 1 && cell(1).empty()) {
569 // must be a superscript...
572 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
573 // could be either subscript or super script
574 removeScript(cell_1_is_up_);
577 //lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
581 void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd)
583 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
585 if (cmd.action == LFUN_MATH_LIMITS) {
586 if (!cmd.argument.empty()) {
587 if (cmd.argument == "limits")
589 else if (cmd.argument == "nolimits")
593 } else if (limits_ == 0)
594 limits_ = hasLimits() ? -1 : 1;
600 MathNestInset::doDispatch(cur, cmd);