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);
51 auto_ptr<InsetBase> MathScriptInset::clone() const
53 return auto_ptr<InsetBase>(new MathScriptInset(*this));
57 MathScriptInset const * MathScriptInset::asScriptInset() const
63 MathScriptInset * MathScriptInset::asScriptInset()
69 bool MathScriptInset::idxFirst(LCursor & cur) const
77 bool MathScriptInset::idxLast(LCursor & cur) const
80 cur.pos() = nuc().size();
85 MathArray const & MathScriptInset::down() const
89 BOOST_ASSERT(nargs() > 1);
94 MathArray & MathScriptInset::down()
98 BOOST_ASSERT(nargs() > 1);
103 MathArray const & MathScriptInset::up() const
105 BOOST_ASSERT(nargs() > 1);
110 MathArray & MathScriptInset::up()
112 BOOST_ASSERT(nargs() > 1);
117 void MathScriptInset::ensure(bool up)
120 // just nucleus so far
121 cells_.push_back(MathArray());
123 } else if (nargs() == 2 && !has(up)) {
125 cells_.push_back(cell(1));
128 cells_.push_back(MathArray());
134 MathArray const & MathScriptInset::nuc() const
140 MathArray & MathScriptInset::nuc()
146 int MathScriptInset::dy0() const
151 int des = down().ascent();
160 int MathScriptInset::dy1() const
165 int asc = up().descent();
175 int MathScriptInset::dx0() const
177 BOOST_ASSERT(hasDown());
178 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
182 int MathScriptInset::dx1() const
184 BOOST_ASSERT(hasUp());
185 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
189 int MathScriptInset::dxx() const
191 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
195 int MathScriptInset::nwid() const
197 return nuc().size() ? nuc().width() : 2;
201 int MathScriptInset::nasc() const
203 return nuc().size() ? nuc().ascent() : 5;
207 int MathScriptInset::ndes() const
209 return nuc().size() ? nuc().descent() : 0;
213 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
216 ScriptChanger dummy(mi.base);
225 dim.wid = max(dim.wid, up().width());
227 dim.wid = max(dim.wid, down().width());
230 dim.wid = max(dim.wid, up().width());
232 dim.wid = max(dim.wid, down().width());
235 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
236 dim.des = dy0() + (hasDown() ? down().descent() : 0);
242 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
245 nuc().draw(pi, x + dxx(), y);
247 nuc().setXY(x + dxx(), y);
248 if (editing(pi.base.bv))
249 drawStr(pi, pi.base.font, x + dxx(), y, ".");
251 ScriptChanger dummy(pi.base);
253 up().draw(pi, x + dx1(), y - dy1());
255 down().draw(pi, x + dx0(), y + dy0());
256 drawMarkers(pi, x, y);
260 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
263 up().metricsT(mi, dim);
265 down().metricsT(mi, dim);
266 nuc().metricsT(mi, dim);
270 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
273 nuc().drawT(pain, x + dxx(), y);
275 up().drawT(pain, x + dx1(), y - dy1());
277 down().drawT(pain, x + dx0(), y + dy0());
282 bool MathScriptInset::hasLimits() const
290 // we can only display limits if the nucleus wants some
293 if (!nuc().back()->isScriptable())
296 // per default \int has limits beside the \int even in displayed formulas
297 if (nuc().back()->asSymbolInset())
298 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
301 // assume "real" limits for everything else
306 void MathScriptInset::removeScript(bool up)
308 lyxerr << "MathNestInset::removeScript: 1 up: " << up << endl;
310 lyxerr << "MathNestInset::removeScript: a up: " << up << endl;
311 if (up == cell_1_is_up_)
313 lyxerr << "MathNestInset::removeScript: b up: " << up << endl;
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 << "MathNestInset::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 // remove empty scripts if possible
561 if (nargs() > 2 && cur.idx() == 2 && cell(2).empty()) {
562 // must be a subscript...
564 // sanitize cursor, even if this slice will be removed immediately
567 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
568 // could be either subscript or super script
569 removeScript(cell_1_is_up_);
570 // sanitize cursor, even if this slice will be removed immediately
577 void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
579 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
581 if (cmd.action == LFUN_MATH_LIMITS) {
582 if (!cmd.argument.empty()) {
583 if (cmd.argument == "limits")
585 else if (cmd.argument == "nolimits")
589 } else if (limits_ == 0)
590 limits_ = hasLimits() ? -1 : 1;
596 MathNestInset::priv_dispatch(cur, cmd);