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"
20 #include "funcrequest.h"
22 #include <boost/assert.hpp>
31 MathScriptInset::MathScriptInset()
32 : MathNestInset(3), limits_(0)
39 MathScriptInset::MathScriptInset(bool up)
40 : MathNestInset(3), limits_(0)
47 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
48 : MathNestInset(3), limits_(0)
52 cell(2).push_back(at);
57 auto_ptr<InsetBase> MathScriptInset::clone() const
59 return auto_ptr<InsetBase>(new MathScriptInset(*this));
63 MathScriptInset const * MathScriptInset::asScriptInset() const
69 MathScriptInset * MathScriptInset::asScriptInset()
75 bool MathScriptInset::idxFirst(LCursor & cur) const
83 bool MathScriptInset::idxLast(LCursor & cur) const
86 cur.pos() = nuc().size();
91 MathArray const & MathScriptInset::down() const
97 MathArray & MathScriptInset::down()
103 MathArray const & MathScriptInset::up() const
109 MathArray & MathScriptInset::up()
115 void MathScriptInset::ensure(bool up)
121 MathArray const & MathScriptInset::nuc() const
127 MathArray & MathScriptInset::nuc()
133 int MathScriptInset::dy0() const
138 int des = down().ascent();
147 int MathScriptInset::dy1() const
152 int asc = up().descent();
162 int MathScriptInset::dx0() const
164 BOOST_ASSERT(hasDown());
165 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
169 int MathScriptInset::dx1() const
171 BOOST_ASSERT(hasUp());
172 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
176 int MathScriptInset::dxx() const
178 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
182 int MathScriptInset::nwid() const
184 return nuc().size() ? nuc().width() : 2;
188 int MathScriptInset::nasc() const
190 return nuc().size() ? nuc().ascent() : 5;
194 int MathScriptInset::ndes() const
196 return nuc().size() ? nuc().descent() : 0;
200 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
203 ScriptChanger dummy(mi.base);
210 dim_.wid = max(dim_.wid, up().width());
212 dim_.wid = max(dim_.wid, down().width());
215 dim_.wid = max(dim_.wid, up().width());
217 dim_.wid = max(dim_.wid, down().width());
220 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
221 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
227 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
230 nuc().draw(pi, x + dxx(), y);
232 nuc().setXY(x + dxx(), y);
234 drawStr(pi, pi.base.font, x + dxx(), y, ".");
236 ScriptChanger dummy(pi.base);
238 up().draw(pi, x + dx1(), y - dy1());
240 down().draw(pi, x + dx0(), y + dy0());
241 drawMarkers(pi, x, y);
245 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
248 up().metricsT(mi, dim);
250 down().metricsT(mi, dim);
251 nuc().metricsT(mi, dim);
255 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
258 nuc().drawT(pain, x + dxx(), y);
260 up().drawT(pain, x + dx1(), y - dy1());
262 down().drawT(pain, x + dx0(), y + dy0());
267 bool MathScriptInset::hasLimits() const
275 // we can only display limits if the nucleus wants some
278 if (!nuc().back()->isScriptable())
281 // per default \int has limits beside the \int even in displayed formulas
282 if (nuc().back()->asSymbolInset())
283 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
286 // assume "real" limits for everything else
291 void MathScriptInset::removeScript(bool up)
298 bool MathScriptInset::has(bool up) const
304 bool MathScriptInset::hasUp() const
310 bool MathScriptInset::hasDown() const
316 bool MathScriptInset::idxRight(LCursor &) const
322 bool MathScriptInset::idxLeft(LCursor &) const
328 bool MathScriptInset::idxUpDown(LCursor & cur, bool up, int) const
330 if (cur.idx() == 1) {
331 // if we are 'up' we can't go further up
334 // otherwise go to last base position
336 cur.pos() = cur.lastpos();
339 else if (cur.idx() == 0) {
340 // if we are 'down' we can't go further down
344 cur.pos() = cur.lastpos();
349 // don't go up/down if there is no cell.
352 // go up/down only if in the last position
353 // or in the first position of something with displayed limits
354 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
365 void MathScriptInset::write(WriteStream & os) const
369 //if (nuc().back()->takesLimits()) {
377 lyxerr[Debug::MATHED] << "suppressing {} when writing"
383 if (hasDown() && down().size())
384 os << "_{" << down() << '}';
386 if (hasUp() && up().size())
387 os << "^{" << up() << '}';
389 if (lock_ && !os.latex())
394 void MathScriptInset::normalize(NormalStream & os) const
396 bool d = hasDown() && down().size();
397 bool u = hasUp() && up().size();
412 os << down() << ' ' << up() << ']';
420 void MathScriptInset::maple(MapleStream & os) const
424 if (hasDown() && down().size())
425 os << '[' << down() << ']';
426 if (hasUp() && up().size())
427 os << "^(" << up() << ')';
431 void MathScriptInset::mathematica(MathematicaStream & os) const
433 bool d = hasDown() && down().size();
434 bool u = hasUp() && up().size();
438 os << "Subscript[" << nuc();
444 os << "^(" << up() << ')';
448 os << ',' << down() << ']';
452 void MathScriptInset::mathmlize( MathMLStream & os) const
454 bool d = hasDown() && down().size();
455 bool u = hasUp() && up().size();
458 os << MTag("msubsup");
470 os << down() << up() << ETag("msubsup");
472 os << up() << ETag("msup");
474 os << down() << ETag("msub");
478 void MathScriptInset::octave(OctaveStream & os) const
482 if (hasDown() && down().size())
483 os << '[' << down() << ']';
484 if (hasUp() && up().size())
485 os << "^(" << up() << ')';
489 void MathScriptInset::infoize(std::ostream & os) const
495 void MathScriptInset::infoize2(std::ostream & os) const
498 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
502 void MathScriptInset::notifyCursorLeaves(idx_type idx)
504 MathNestInset::notifyCursorLeaves(idx);
506 // remove empty scripts if possible
507 if (idx != 2 && script_[idx] && cell(idx).empty()) {
509 script_[idx] = false;
515 MathScriptInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
517 if (cmd.action == LFUN_MATH_LIMITS) {
518 if (!cmd.argument.empty()) {
519 if (cmd.argument == "limits")
521 else if (cmd.argument == "nolimits")
525 } else if (limits_ == 0)
526 limits_ = hasLimits() ? -1 : 1;
529 return DispatchResult(true, true);
532 return MathNestInset::priv_dispatch(bv, cmd);