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>
34 MathScriptInset::MathScriptInset()
35 : MathNestInset(1), cell_1_is_up_(false), limits_(0)
39 MathScriptInset::MathScriptInset(bool up)
40 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
44 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
45 : MathNestInset(2), cell_1_is_up_(up), limits_(0)
47 BOOST_ASSERT(nargs() >= 1);
48 cell(0).push_back(at);
52 auto_ptr<InsetBase> MathScriptInset::doClone() const
54 return auto_ptr<InsetBase>(new MathScriptInset(*this));
58 MathScriptInset const * MathScriptInset::asScriptInset() const
64 MathScriptInset * MathScriptInset::asScriptInset()
70 bool MathScriptInset::idxFirst(LCursor & cur) const
78 bool MathScriptInset::idxLast(LCursor & cur) const
81 cur.pos() = nuc().size();
86 MathArray const & MathScriptInset::down() const
90 BOOST_ASSERT(nargs() > 1);
95 MathArray & MathScriptInset::down()
99 BOOST_ASSERT(nargs() > 1);
104 MathArray const & MathScriptInset::up() const
106 BOOST_ASSERT(nargs() > 1);
111 MathArray & MathScriptInset::up()
113 BOOST_ASSERT(nargs() > 1);
118 void MathScriptInset::ensure(bool up)
121 // just nucleus so far
122 cells_.push_back(MathArray());
124 } else if (nargs() == 2 && !has(up)) {
126 cells_.push_back(cell(1));
129 cells_.push_back(MathArray());
135 MathArray const & MathScriptInset::nuc() const
141 MathArray & MathScriptInset::nuc()
147 int MathScriptInset::dy0() const
152 int des = down().ascent();
161 int MathScriptInset::dy1() const
166 int asc = up().descent();
176 int MathScriptInset::dx0() const
178 BOOST_ASSERT(hasDown());
179 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
183 int MathScriptInset::dx1() const
185 BOOST_ASSERT(hasUp());
186 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
190 int MathScriptInset::dxx() const
192 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
196 int MathScriptInset::nwid() const
198 return nuc().size() ? nuc().width() : 2;
202 int MathScriptInset::nasc() const
204 return nuc().size() ? nuc().ascent() : 5;
208 int MathScriptInset::ndes() const
210 return nuc().size() ? nuc().descent() : 0;
214 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
217 ScriptChanger dummy(mi.base);
226 dim.wid = max(dim.wid, up().width());
228 dim.wid = max(dim.wid, down().width());
231 dim.wid = max(dim.wid, up().width());
233 dim.wid = max(dim.wid, down().width());
236 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
237 dim.des = dy0() + (hasDown() ? down().descent() : 0);
243 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
246 nuc().draw(pi, x + dxx(), y);
248 nuc().setXY(x + dxx(), y);
249 if (editing(pi.base.bv))
250 pi.draw(x + dxx(), y, char_type('.'));
252 ScriptChanger dummy(pi.base);
254 up().draw(pi, x + dx1(), y - dy1());
256 down().draw(pi, x + dx0(), y + dy0());
257 drawMarkers(pi, x, y);
261 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
264 up().metricsT(mi, dim);
266 down().metricsT(mi, dim);
267 nuc().metricsT(mi, dim);
271 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
274 nuc().drawT(pain, x + dxx(), y);
276 up().drawT(pain, x + dx1(), y - dy1());
278 down().drawT(pain, x + dx0(), y + dy0());
283 bool MathScriptInset::hasLimits() const
291 // we can only display limits if the nucleus wants some
294 if (!nuc().back()->isScriptable())
297 if (nuc().back()->asSymbolInset()) {
298 // \intop is an alias for \int\limits, \ointop == \oint\limits
299 if (nuc().back()->asSymbolInset()->name().find("intop") != string::npos)
301 // per default \int has limits beside the \int even in displayed formulas
302 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
306 // assume "real" limits for everything else
311 void MathScriptInset::removeScript(bool up)
314 if (up == cell_1_is_up_)
316 } else if (nargs() == 3) {
318 swap(cells_[1], cells_[2]);
319 cell_1_is_up_ = false;
321 cell_1_is_up_ = true;
328 bool MathScriptInset::has(bool up) const
330 return idxOfScript(up);
334 bool MathScriptInset::hasUp() const
336 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
337 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
338 return idxOfScript(true);
342 bool MathScriptInset::hasDown() const
344 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
345 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
346 return idxOfScript(false);
350 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
355 return (cell_1_is_up_ == up) ? 1 : 0;
364 bool MathScriptInset::idxRight(LCursor &) const
370 bool MathScriptInset::idxLeft(LCursor &) const
376 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
379 if (cur.idx() == 0) {
380 // don't go up/down if there is no cell in this direction
383 // go up/down only if in the last position
384 // or in the first position of something with displayed limits
385 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
386 cur.idx() = idxOfScript(up);
394 if (has(up) && cur.idx() == idxOfScript(true)) {
395 // can't go further up
398 // otherwise go to last position in the nucleus
400 cur.pos() = cur.lastpos();
405 if (has(up) && cur.idx() == idxOfScript(false)) {
406 // can't go further down
409 // otherwise go to last position in the nucleus
411 cur.pos() = cur.lastpos();
419 void MathScriptInset::write(WriteStream & os) const
423 //if (nuc().back()->takesLimits()) {
431 lyxerr[Debug::MATHED] << "suppressing {} when writing"
437 if (hasDown() && down().size())
438 os << "_{" << down() << '}';
440 if (hasUp() && up().size())
441 os << "^{" << up() << '}';
443 if (lock_ && !os.latex())
448 void MathScriptInset::normalize(NormalStream & os) const
450 bool d = hasDown() && down().size();
451 bool u = hasUp() && up().size();
466 os << down() << ' ' << up() << ']';
474 void MathScriptInset::maple(MapleStream & os) const
478 if (hasDown() && down().size())
479 os << '[' << down() << ']';
480 if (hasUp() && up().size())
481 os << "^(" << up() << ')';
485 void MathScriptInset::mathematica(MathematicaStream & os) const
487 bool d = hasDown() && down().size();
488 bool u = hasUp() && up().size();
492 os << "Subscript[" << nuc();
498 os << "^(" << up() << ')';
502 os << ',' << down() << ']';
507 void MathScriptInset::mathmlize(MathMLStream & os) const
509 bool d = hasDown() && down().size();
510 bool u = hasUp() && up().size();
513 os << MTag("msubsup");
525 os << down() << up() << ETag("msubsup");
527 os << up() << ETag("msup");
529 os << down() << ETag("msub");
533 void MathScriptInset::octave(OctaveStream & os) const
537 if (hasDown() && down().size())
538 os << '[' << down() << ']';
539 if (hasUp() && up().size())
540 os << "^(" << up() << ')';
544 void MathScriptInset::infoize(std::ostream & os) const
550 void MathScriptInset::infoize2(std::ostream & os) const
553 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
557 void MathScriptInset::notifyCursorLeaves(LCursor & cur)
559 MathNestInset::notifyCursorLeaves(cur);
561 //lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl;
563 // remove empty scripts if possible
565 // Case of two scripts. In this case, 1 = super, 2 = sub
566 if (cur.idx() == 2 && cell(2).empty()) {
567 // must be a subscript...
569 } else if (cur.idx() == 1 && cell(1).empty()) {
570 // must be a superscript...
573 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
574 // could be either subscript or super script
575 removeScript(cell_1_is_up_);
578 //lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
582 void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd)
584 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
586 if (cmd.action == LFUN_MATH_LIMITS) {
587 if (!cmd.argument.empty()) {
588 if (cmd.argument == "limits")
590 else if (cmd.argument == "nolimits")
594 } else if (limits_ == 0)
595 limits_ = hasLimits() ? -1 : 1;
601 MathNestInset::doDispatch(cur, cmd);