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;
349 bool MathScriptInset::idxRight(LCursor &) const
355 bool MathScriptInset::idxLeft(LCursor &) const
361 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
364 if (cur.idx() == 0) {
365 // don't go up/down if there is no cell in this direction
368 // go up/down only if in the last position
369 // or in the first position of something with displayed limits
370 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
371 cur.idx() = idxOfScript(up);
379 if (cur.idx() == idxOfScript(true)) {
380 // can't go further up
383 // otherwise go to last position in the nucleus
385 cur.pos() = cur.lastpos();
390 if (cur.idx() == idxOfScript(false)) {
391 // can't go further down
394 // otherwise go to last position in the nucleus
396 cur.pos() = cur.lastpos();
404 void MathScriptInset::write(WriteStream & os) const
408 //if (nuc().back()->takesLimits()) {
416 lyxerr[Debug::MATHED] << "suppressing {} when writing"
422 if (hasDown() && down().size())
423 os << "_{" << down() << '}';
425 if (hasUp() && up().size())
426 os << "^{" << up() << '}';
428 if (lock_ && !os.latex())
433 void MathScriptInset::normalize(NormalStream & os) const
435 bool d = hasDown() && down().size();
436 bool u = hasUp() && up().size();
451 os << down() << ' ' << up() << ']';
459 void MathScriptInset::maple(MapleStream & os) const
463 if (hasDown() && down().size())
464 os << '[' << down() << ']';
465 if (hasUp() && up().size())
466 os << "^(" << up() << ')';
470 void MathScriptInset::mathematica(MathematicaStream & os) const
472 bool d = hasDown() && down().size();
473 bool u = hasUp() && up().size();
477 os << "Subscript[" << nuc();
483 os << "^(" << up() << ')';
487 os << ',' << down() << ']';
492 void MathScriptInset::mathmlize(MathMLStream & os) const
494 bool d = hasDown() && down().size();
495 bool u = hasUp() && up().size();
498 os << MTag("msubsup");
510 os << down() << up() << ETag("msubsup");
512 os << up() << ETag("msup");
514 os << down() << ETag("msub");
518 void MathScriptInset::octave(OctaveStream & os) const
522 if (hasDown() && down().size())
523 os << '[' << down() << ']';
524 if (hasUp() && up().size())
525 os << "^(" << up() << ')';
529 void MathScriptInset::infoize(std::ostream & os) const
535 void MathScriptInset::infoize2(std::ostream & os) const
538 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
542 void MathScriptInset::notifyCursorLeaves(idx_type idx)
544 MathNestInset::notifyCursorLeaves(idx);
546 // remove empty scripts if possible
547 if (idx == 2 && cell(2).empty()) {
548 removeScript(false); // must be a subscript...
549 } else if (idx == 1 && cell(1).empty()) {
551 cell_1_is_up_ = false;
554 } else if (nargs() == 1) {
561 void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
563 //lyxerr << "MathScriptInset: request: " << cmd << std::endl;
565 if (cmd.action == LFUN_MATH_LIMITS) {
566 if (!cmd.argument.empty()) {
567 if (cmd.argument == "limits")
569 else if (cmd.argument == "nolimits")
573 } else if (limits_ == 0)
574 limits_ = hasLimits() ? -1 : 1;
580 MathNestInset::priv_dispatch(cur, cmd);