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 drawStr(pi, pi.base.font, 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 // per default \int has limits beside the \int even in displayed formulas
296 if (nuc().back()->asSymbolInset())
297 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
300 // assume "real" limits for everything else
305 void MathScriptInset::removeScript(bool up)
307 lyxerr << "MathScriptInset::removeScript: 1 up: " << up << endl;
309 lyxerr << "MathScriptInset::removeScript: a up: " << up << endl;
310 if (up == cell_1_is_up_)
312 lyxerr << "MathScriptInset::removeScript: b up: " << up << endl;
313 } else if (nargs() == 3) {
315 swap(cells_[1], cells_[2]);
316 cell_1_is_up_ = false;
318 cell_1_is_up_ = true;
322 lyxerr << "MathScriptInset::removeScript: 2 up: " << up << endl;
326 bool MathScriptInset::has(bool up) const
328 return idxOfScript(up);
332 bool MathScriptInset::hasUp() const
334 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
335 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
336 return idxOfScript(true);
340 bool MathScriptInset::hasDown() const
342 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
343 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
344 return idxOfScript(false);
348 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
353 return (cell_1_is_up_ == up) ? 1 : 0;
362 bool MathScriptInset::idxRight(LCursor &) const
368 bool MathScriptInset::idxLeft(LCursor &) const
374 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
377 if (cur.idx() == 0) {
378 // don't go up/down if there is no cell in this direction
381 // go up/down only if in the last position
382 // or in the first position of something with displayed limits
383 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
384 cur.idx() = idxOfScript(up);
392 if (has(up) && cur.idx() == idxOfScript(true)) {
393 // can't go further up
396 // otherwise go to last position in the nucleus
398 cur.pos() = cur.lastpos();
403 if (has(up) && cur.idx() == idxOfScript(false)) {
404 // can't go further down
407 // otherwise go to last position in the nucleus
409 cur.pos() = cur.lastpos();
417 void MathScriptInset::write(WriteStream & os) const
421 //if (nuc().back()->takesLimits()) {
429 lyxerr[Debug::MATHED] << "suppressing {} when writing"
435 if (hasDown() && down().size())
436 os << "_{" << down() << '}';
438 if (hasUp() && up().size())
439 os << "^{" << up() << '}';
441 if (lock_ && !os.latex())
446 void MathScriptInset::normalize(NormalStream & os) const
448 bool d = hasDown() && down().size();
449 bool u = hasUp() && up().size();
464 os << down() << ' ' << up() << ']';
472 void MathScriptInset::maple(MapleStream & os) const
476 if (hasDown() && down().size())
477 os << '[' << down() << ']';
478 if (hasUp() && up().size())
479 os << "^(" << up() << ')';
483 void MathScriptInset::mathematica(MathematicaStream & os) const
485 bool d = hasDown() && down().size();
486 bool u = hasUp() && up().size();
490 os << "Subscript[" << nuc();
496 os << "^(" << up() << ')';
500 os << ',' << down() << ']';
505 void MathScriptInset::mathmlize(MathMLStream & os) const
507 bool d = hasDown() && down().size();
508 bool u = hasUp() && up().size();
511 os << MTag("msubsup");
523 os << down() << up() << ETag("msubsup");
525 os << up() << ETag("msup");
527 os << down() << ETag("msub");
531 void MathScriptInset::octave(OctaveStream & os) const
535 if (hasDown() && down().size())
536 os << '[' << down() << ']';
537 if (hasUp() && up().size())
538 os << "^(" << up() << ')';
542 void MathScriptInset::infoize(std::ostream & os) const
548 void MathScriptInset::infoize2(std::ostream & os) const
551 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
555 void MathScriptInset::notifyCursorLeaves(LCursor & cur)
557 MathNestInset::notifyCursorLeaves(cur);
559 lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl;
561 // remove empty scripts if possible
563 if (nargs() > 2 && cur.idx() == 2 && cell(2).empty()) {
564 // must be a subscript...
566 } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
567 // could be either subscript or super script
568 removeScript(cell_1_is_up_);
572 lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl;
576 void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
578 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
580 if (cmd.action == LFUN_MATH_LIMITS) {
581 if (!cmd.argument.empty()) {
582 if (cmd.argument == "limits")
584 else if (cmd.argument == "nolimits")
588 } else if (limits_ == 0)
589 limits_ = hasLimits() ? -1 : 1;
595 MathNestInset::priv_dispatch(cur, cmd);