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)
311 lyxerr << "MathScriptInset::removeScript: 1 up: " << up << endl;
313 lyxerr << "MathScriptInset::removeScript: a up: " << up << endl;
314 if (up == cell_1_is_up_)
316 lyxerr << "MathScriptInset::removeScript: b up: " << up << endl;
317 } else if (nargs() == 3) {
319 swap(cells_[1], cells_[2]);
320 cell_1_is_up_ = false;
322 cell_1_is_up_ = true;
326 lyxerr << "MathScriptInset::removeScript: 2 up: " << up << endl;
330 bool MathScriptInset::has(bool up) const
332 return idxOfScript(up);
336 bool MathScriptInset::hasUp() const
338 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
339 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
340 return idxOfScript(true);
344 bool MathScriptInset::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 MathScriptInset::idxOfScript(bool up) const
357 return (cell_1_is_up_ == up) ? 1 : 0;
366 bool MathScriptInset::idxRight(LCursor &) const
372 bool MathScriptInset::idxLeft(LCursor &) const
378 bool MathScriptInset::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 MathScriptInset::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 MathScriptInset::normalize(NormalStream & os) const
452 bool d = hasDown() && down().size();
453 bool u = hasUp() && up().size();
468 os << down() << ' ' << up() << ']';
476 void MathScriptInset::maple(MapleStream & os) const
480 if (hasDown() && down().size())
481 os << '[' << down() << ']';
482 if (hasUp() && up().size())
483 os << "^(" << up() << ')';
487 void MathScriptInset::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 MathScriptInset::mathmlize(MathMLStream & 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 MathScriptInset::octave(OctaveStream & os) const
539 if (hasDown() && down().size())
540 os << '[' << down() << ']';
541 if (hasUp() && up().size())
542 os << "^(" << up() << ')';
546 void MathScriptInset::infoize(std::ostream & os) const
552 void MathScriptInset::infoize2(std::ostream & os) const
555 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
559 void MathScriptInset::notifyCursorLeaves(LCursor & cur)
561 MathNestInset::notifyCursorLeaves(cur);
563 //lyxerr << "MathScriptInset::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...
571 } else if (cur.idx() == 1 && cell(1).empty()) {
572 // must be a superscript...
575 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
576 // could be either subscript or super script
577 removeScript(cell_1_is_up_);
580 //lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
584 void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd)
586 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
588 if (cmd.action == LFUN_MATH_LIMITS) {
589 if (!cmd.argument.empty()) {
590 if (cmd.argument == "limits")
592 else if (cmd.argument == "nolimits")
596 } else if (limits_ == 0)
597 limits_ = hasLimits() ? -1 : 1;
603 MathNestInset::doDispatch(cur, cmd);