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 cell(0).push_back(at);
50 auto_ptr<InsetBase> MathScriptInset::clone() 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
86 return nargs() == 2 ? cell(2) : cell(1);
90 MathArray & MathScriptInset::down()
92 return nargs() == 2 ? cell(2) : cell(1);
96 MathArray const & MathScriptInset::up() const
102 MathArray & MathScriptInset::up()
108 void MathScriptInset::ensure(bool up)
111 // just nucleus so far
112 cells_.push_back(MathArray());
114 } else if (nargs() == 2 && !has(up)) {
116 cells_.push_back(cell(1));
119 cells_.push_back(MathArray());
125 MathArray const & MathScriptInset::nuc() const
131 MathArray & MathScriptInset::nuc()
137 int MathScriptInset::dy0() const
142 int des = down().ascent();
151 int MathScriptInset::dy1() const
156 int asc = up().descent();
166 int MathScriptInset::dx0() const
168 BOOST_ASSERT(hasDown());
169 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
173 int MathScriptInset::dx1() const
175 BOOST_ASSERT(hasUp());
176 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
180 int MathScriptInset::dxx() const
182 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
186 int MathScriptInset::nwid() const
188 return nuc().size() ? nuc().width() : 2;
192 int MathScriptInset::nasc() const
194 return nuc().size() ? nuc().ascent() : 5;
198 int MathScriptInset::ndes() const
200 return nuc().size() ? nuc().descent() : 0;
204 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
207 ScriptChanger dummy(mi.base);
216 dim.wid = max(dim.wid, up().width());
218 dim.wid = max(dim.wid, down().width());
221 dim.wid = max(dim.wid, up().width());
223 dim.wid = max(dim.wid, down().width());
226 dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
227 dim.des = dy0() + (hasDown() ? down().descent() : 0);
233 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
236 nuc().draw(pi, x + dxx(), y);
238 nuc().setXY(x + dxx(), y);
239 if (editing(pi.base.bv))
240 drawStr(pi, pi.base.font, x + dxx(), y, ".");
242 ScriptChanger dummy(pi.base);
244 up().draw(pi, x + dx1(), y - dy1());
246 down().draw(pi, x + dx0(), y + dy0());
247 drawMarkers(pi, x, y);
251 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
254 up().metricsT(mi, dim);
256 down().metricsT(mi, dim);
257 nuc().metricsT(mi, dim);
261 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
264 nuc().drawT(pain, x + dxx(), y);
266 up().drawT(pain, x + dx1(), y - dy1());
268 down().drawT(pain, x + dx0(), y + dy0());
273 bool MathScriptInset::hasLimits() const
281 // we can only display limits if the nucleus wants some
284 if (!nuc().back()->isScriptable())
287 // per default \int has limits beside the \int even in displayed formulas
288 if (nuc().back()->asSymbolInset())
289 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
292 // assume "real" limits for everything else
297 void MathScriptInset::removeScript(bool up)
300 if (up == cell_1_is_up_)
302 } else if (nargs() == 3) {
304 swap(cells_[1], cells_[2]);
305 cell_1_is_up_ = false;
307 cell_1_is_up_ = true;
313 bool MathScriptInset::has(bool up) const
315 return idxOfScript(up);
319 bool MathScriptInset::hasUp() const
321 //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
322 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
323 return idxOfScript(true);
327 bool MathScriptInset::hasDown() const
329 //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
330 //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
331 return idxOfScript(false);
335 InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const
340 return cell_1_is_up_ == up ? 1 : 0;
347 bool MathScriptInset::idxRight(LCursor &) const
353 bool MathScriptInset::idxLeft(LCursor &) const
359 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
362 if (cur.idx() == 0) {
363 // don't go up/down if there is no cell in this direction
366 // go up/down only if in the last position
367 // or in the first position of something with displayed limits
368 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
369 cur.idx() = idxOfScript(up);
377 if (cur.idx() == idxOfScript(true)) {
378 // can't go further up
381 // otherwise go to last position in the nucleus
383 cur.pos() = cur.lastpos();
388 if (cur.idx() == idxOfScript(false)) {
389 // can't go further down
392 // otherwise go to last position in the nucleus
394 cur.pos() = cur.lastpos();
402 void MathScriptInset::write(WriteStream & os) const
406 //if (nuc().back()->takesLimits()) {
414 lyxerr[Debug::MATHED] << "suppressing {} when writing"
420 if (hasDown() && down().size())
421 os << "_{" << down() << '}';
423 if (hasUp() && up().size())
424 os << "^{" << up() << '}';
426 if (lock_ && !os.latex())
431 void MathScriptInset::normalize(NormalStream & os) const
433 bool d = hasDown() && down().size();
434 bool u = hasUp() && up().size();
449 os << down() << ' ' << up() << ']';
457 void MathScriptInset::maple(MapleStream & os) const
461 if (hasDown() && down().size())
462 os << '[' << down() << ']';
463 if (hasUp() && up().size())
464 os << "^(" << up() << ')';
468 void MathScriptInset::mathematica(MathematicaStream & os) const
470 bool d = hasDown() && down().size();
471 bool u = hasUp() && up().size();
475 os << "Subscript[" << nuc();
481 os << "^(" << up() << ')';
485 os << ',' << down() << ']';
490 void MathScriptInset::mathmlize(MathMLStream & os) const
492 bool d = hasDown() && down().size();
493 bool u = hasUp() && up().size();
496 os << MTag("msubsup");
508 os << down() << up() << ETag("msubsup");
510 os << up() << ETag("msup");
512 os << down() << ETag("msub");
516 void MathScriptInset::octave(OctaveStream & os) const
520 if (hasDown() && down().size())
521 os << '[' << down() << ']';
522 if (hasUp() && up().size())
523 os << "^(" << up() << ')';
527 void MathScriptInset::infoize(std::ostream & os) const
533 void MathScriptInset::infoize2(std::ostream & os) const
536 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
540 void MathScriptInset::notifyCursorLeaves(idx_type idx)
542 MathNestInset::notifyCursorLeaves(idx);
544 // remove empty scripts if possible
545 if (idx == 2 && cell(2).empty()) {
546 removeScript(false); // must be a subscript...
547 } else if (idx == 1 && cell(1).empty()) {
549 cell_1_is_up_ = false;
552 } else if (nargs() == 1) {
559 void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
561 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
563 if (cmd.action == LFUN_MATH_LIMITS) {
564 if (!cmd.argument.empty()) {
565 if (cmd.argument == "limits")
567 else if (cmd.argument == "nolimits")
571 } else if (limits_ == 0)
572 limits_ = hasLimits() ? -1 : 1;
578 MathNestInset::priv_dispatch(cur, cmd);