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(BufferView & bv) const
77 CursorSlice & cur = cursorTip(bv);
84 bool MathScriptInset::idxLast(BufferView & bv) const
86 CursorSlice & cur = cursorTip(bv);
88 cur.pos() = nuc().size();
93 MathArray const & MathScriptInset::down() const
99 MathArray & MathScriptInset::down()
105 MathArray const & MathScriptInset::up() const
111 MathArray & MathScriptInset::up()
117 void MathScriptInset::ensure(bool up)
123 MathArray const & MathScriptInset::nuc() const
129 MathArray & MathScriptInset::nuc()
135 int MathScriptInset::dy0() const
140 int des = down().ascent();
149 int MathScriptInset::dy1() const
154 int asc = up().descent();
164 int MathScriptInset::dx0() const
166 BOOST_ASSERT(hasDown());
167 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
171 int MathScriptInset::dx1() const
173 BOOST_ASSERT(hasUp());
174 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
178 int MathScriptInset::dxx() const
180 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
184 int MathScriptInset::nwid() const
186 return nuc().size() ? nuc().width() : 2;
190 int MathScriptInset::nasc() const
192 return nuc().size() ? nuc().ascent() : 5;
196 int MathScriptInset::ndes() const
198 return nuc().size() ? nuc().descent() : 0;
202 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
205 ScriptChanger dummy(mi.base);
212 dim_.wid = max(dim_.wid, up().width());
214 dim_.wid = max(dim_.wid, down().width());
217 dim_.wid = max(dim_.wid, up().width());
219 dim_.wid = max(dim_.wid, down().width());
222 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
223 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
229 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
232 nuc().draw(pi, x + dxx(), y);
234 nuc().setXY(x + dxx(), y);
236 drawStr(pi, pi.base.font, x + dxx(), y, ".");
238 ScriptChanger dummy(pi.base);
240 up().draw(pi, x + dx1(), y - dy1());
242 down().draw(pi, x + dx0(), y + dy0());
243 drawMarkers(pi, x, y);
247 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
250 up().metricsT(mi, dim);
252 down().metricsT(mi, dim);
253 nuc().metricsT(mi, dim);
257 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
260 nuc().drawT(pain, x + dxx(), y);
262 up().drawT(pain, x + dx1(), y - dy1());
264 down().drawT(pain, x + dx0(), y + dy0());
269 bool MathScriptInset::hasLimits() const
277 // we can only display limits if the nucleus wants some
280 if (!nuc().back()->isScriptable())
283 // per default \int has limits beside the \int even in displayed formulas
284 if (nuc().back()->asSymbolInset())
285 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
288 // assume "real" limits for everything else
293 void MathScriptInset::removeScript(bool up)
300 bool MathScriptInset::has(bool up) const
306 bool MathScriptInset::hasUp() const
312 bool MathScriptInset::hasDown() const
318 bool MathScriptInset::idxRight(BufferView &) const
324 bool MathScriptInset::idxLeft(BufferView &) const
330 bool MathScriptInset::idxUpDown(BufferView & bv, bool up, int) const
332 CursorSlice & cur = cursorTip(bv);
333 if (cur.idx() == 1) {
334 // if we are 'up' we can't go further up
337 // otherwise go to last base position
339 cur.pos() = cur.lastpos();
342 else if (cur.idx() == 0) {
343 // if we are 'down' we can't go further down
347 cur.pos() = cur.lastpos();
352 // don't go up/down if there is no cell.
355 // go up/down only if in the last position
356 // or in the first position of something with displayed limits
357 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
368 void MathScriptInset::write(WriteStream & os) const
372 //if (nuc().back()->takesLimits()) {
380 lyxerr[Debug::MATHED] << "suppressing {} when writing"
386 if (hasDown() && down().size())
387 os << "_{" << down() << '}';
389 if (hasUp() && up().size())
390 os << "^{" << up() << '}';
392 if (lock_ && !os.latex())
397 void MathScriptInset::normalize(NormalStream & os) const
399 bool d = hasDown() && down().size();
400 bool u = hasUp() && up().size();
415 os << down() << ' ' << up() << ']';
423 void MathScriptInset::maple(MapleStream & os) const
427 if (hasDown() && down().size())
428 os << '[' << down() << ']';
429 if (hasUp() && up().size())
430 os << "^(" << up() << ')';
434 void MathScriptInset::mathematica(MathematicaStream & os) const
436 bool d = hasDown() && down().size();
437 bool u = hasUp() && up().size();
441 os << "Subscript[" << nuc();
447 os << "^(" << up() << ')';
451 os << ',' << down() << ']';
455 void MathScriptInset::mathmlize( MathMLStream & os) const
457 bool d = hasDown() && down().size();
458 bool u = hasUp() && up().size();
461 os << MTag("msubsup");
473 os << down() << up() << ETag("msubsup");
475 os << up() << ETag("msup");
477 os << down() << ETag("msub");
481 void MathScriptInset::octave(OctaveStream & os) const
485 if (hasDown() && down().size())
486 os << '[' << down() << ']';
487 if (hasUp() && up().size())
488 os << "^(" << up() << ')';
492 void MathScriptInset::infoize(std::ostream & os) const
498 void MathScriptInset::infoize2(std::ostream & os) const
501 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
505 void MathScriptInset::notifyCursorLeaves(idx_type idx)
507 MathNestInset::notifyCursorLeaves(idx);
509 // remove empty scripts if possible
510 if (idx != 2 && script_[idx] && cell(idx).empty()) {
512 script_[idx] = false;
518 MathScriptInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
520 if (cmd.action == LFUN_MATH_LIMITS) {
521 if (!cmd.argument.empty()) {
522 if (cmd.argument == "limits")
524 else if (cmd.argument == "nolimits")
528 } else if (limits_ == 0)
529 limits_ = hasLimits() ? -1 : 1;
532 return DispatchResult(true, true);
535 return MathNestInset::priv_dispatch(bv, cmd);