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(3), limits_(0)
40 MathScriptInset::MathScriptInset(bool up)
41 : MathNestInset(3), limits_(0)
48 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
49 : MathNestInset(3), limits_(0)
53 cell(2).push_back(at);
58 auto_ptr<InsetBase> MathScriptInset::clone() const
60 return auto_ptr<InsetBase>(new MathScriptInset(*this));
64 MathScriptInset const * MathScriptInset::asScriptInset() const
70 MathScriptInset * MathScriptInset::asScriptInset()
76 bool MathScriptInset::idxFirst(LCursor & cur) const
84 bool MathScriptInset::idxLast(LCursor & cur) const
87 cur.pos() = nuc().size();
92 MathArray const & MathScriptInset::down() const
98 MathArray & MathScriptInset::down()
104 MathArray const & MathScriptInset::up() const
110 MathArray & MathScriptInset::up()
116 void MathScriptInset::ensure(bool up)
122 MathArray const & MathScriptInset::nuc() const
128 MathArray & MathScriptInset::nuc()
134 int MathScriptInset::dy0() const
139 int des = down().ascent();
148 int MathScriptInset::dy1() const
153 int asc = up().descent();
163 int MathScriptInset::dx0() const
165 BOOST_ASSERT(hasDown());
166 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
170 int MathScriptInset::dx1() const
172 BOOST_ASSERT(hasUp());
173 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
177 int MathScriptInset::dxx() const
179 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
183 int MathScriptInset::nwid() const
185 return nuc().size() ? nuc().width() : 2;
189 int MathScriptInset::nasc() const
191 return nuc().size() ? nuc().ascent() : 5;
195 int MathScriptInset::ndes() const
197 return nuc().size() ? nuc().descent() : 0;
201 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
204 ScriptChanger dummy(mi.base);
211 dim_.wid = max(dim_.wid, up().width());
213 dim_.wid = max(dim_.wid, down().width());
216 dim_.wid = max(dim_.wid, up().width());
218 dim_.wid = max(dim_.wid, down().width());
221 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
222 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
228 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
231 nuc().draw(pi, x + dxx(), y);
233 nuc().setXY(x + dxx(), y);
234 if (editing(pi.base.bv))
235 drawStr(pi, pi.base.font, x + dxx(), y, ".");
237 ScriptChanger dummy(pi.base);
239 up().draw(pi, x + dx1(), y - dy1());
241 down().draw(pi, x + dx0(), y + dy0());
242 drawMarkers(pi, x, y);
246 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
249 up().metricsT(mi, dim);
251 down().metricsT(mi, dim);
252 nuc().metricsT(mi, dim);
256 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
259 nuc().drawT(pain, x + dxx(), y);
261 up().drawT(pain, x + dx1(), y - dy1());
263 down().drawT(pain, x + dx0(), y + dy0());
268 bool MathScriptInset::hasLimits() const
276 // we can only display limits if the nucleus wants some
279 if (!nuc().back()->isScriptable())
282 // per default \int has limits beside the \int even in displayed formulas
283 if (nuc().back()->asSymbolInset())
284 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
287 // assume "real" limits for everything else
292 void MathScriptInset::removeScript(bool up)
299 bool MathScriptInset::has(bool up) const
305 bool MathScriptInset::hasUp() const
311 bool MathScriptInset::hasDown() const
317 bool MathScriptInset::idxRight(LCursor &) const
323 bool MathScriptInset::idxLeft(LCursor &) const
329 bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
331 if (cur.idx() == 1) {
332 // if we are 'up' we can't go further up
335 // otherwise go to last base position
337 cur.pos() = cur.lastpos();
340 else if (cur.idx() == 0) {
341 // if we are 'down' we can't go further down
345 cur.pos() = cur.lastpos();
350 // don't go up/down if there is no cell.
353 // go up/down only if in the last position
354 // or in the first position of something with displayed limits
355 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
366 void MathScriptInset::write(WriteStream & os) const
370 //if (nuc().back()->takesLimits()) {
378 lyxerr[Debug::MATHED] << "suppressing {} when writing"
384 if (hasDown() && down().size())
385 os << "_{" << down() << '}';
387 if (hasUp() && up().size())
388 os << "^{" << up() << '}';
390 if (lock_ && !os.latex())
395 void MathScriptInset::normalize(NormalStream & os) const
397 bool d = hasDown() && down().size();
398 bool u = hasUp() && up().size();
413 os << down() << ' ' << up() << ']';
421 void MathScriptInset::maple(MapleStream & os) const
425 if (hasDown() && down().size())
426 os << '[' << down() << ']';
427 if (hasUp() && up().size())
428 os << "^(" << up() << ')';
432 void MathScriptInset::mathematica(MathematicaStream & os) const
434 bool d = hasDown() && down().size();
435 bool u = hasUp() && up().size();
439 os << "Subscript[" << nuc();
445 os << "^(" << up() << ')';
449 os << ',' << down() << ']';
453 void MathScriptInset::mathmlize( MathMLStream & os) const
455 bool d = hasDown() && down().size();
456 bool u = hasUp() && up().size();
459 os << MTag("msubsup");
471 os << down() << up() << ETag("msubsup");
473 os << up() << ETag("msup");
475 os << down() << ETag("msub");
479 void MathScriptInset::octave(OctaveStream & os) const
483 if (hasDown() && down().size())
484 os << '[' << down() << ']';
485 if (hasUp() && up().size())
486 os << "^(" << up() << ')';
490 void MathScriptInset::infoize(std::ostream & os) const
496 void MathScriptInset::infoize2(std::ostream & os) const
499 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
503 void MathScriptInset::notifyCursorLeaves(idx_type idx)
505 MathNestInset::notifyCursorLeaves(idx);
507 // remove empty scripts if possible
508 if (idx != 2 && script_[idx] && cell(idx).empty()) {
510 script_[idx] = false;
516 MathScriptInset::priv_dispatch(LCursor & bv, FuncRequest const & cmd)
518 if (cmd.action == LFUN_MATH_LIMITS) {
519 if (!cmd.argument.empty()) {
520 if (cmd.argument == "limits")
522 else if (cmd.argument == "nolimits")
526 } else if (limits_ == 0)
527 limits_ = hasLimits() ? -1 : 1;
530 return DispatchResult(true, true);
533 return MathNestInset::priv_dispatch(bv, cmd);