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_support.h"
15 #include "math_symbolinset.h"
16 #include "math_mathmlstream.h"
17 #include "funcrequest.h"
18 #include "support/LAssert.h"
21 using namespace lyx::support;
28 MathScriptInset::MathScriptInset()
29 : MathNestInset(3), limits_(0)
36 MathScriptInset::MathScriptInset(bool up)
37 : MathNestInset(3), limits_(0)
44 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
45 : MathNestInset(3), limits_(0)
49 cell(2).push_back(at);
54 auto_ptr<InsetBase> MathScriptInset::clone() const
56 return auto_ptr<InsetBase>(new MathScriptInset(*this));
60 MathScriptInset const * MathScriptInset::asScriptInset() const
66 MathScriptInset * MathScriptInset::asScriptInset()
72 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
80 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
88 MathArray const & MathScriptInset::down() const
94 MathArray & MathScriptInset::down()
100 MathArray const & MathScriptInset::up() const
106 MathArray & MathScriptInset::up()
112 void MathScriptInset::ensure(bool up)
118 MathArray const & MathScriptInset::nuc() const
124 MathArray & MathScriptInset::nuc()
130 int MathScriptInset::dy0() const
135 int des = down().ascent();
144 int MathScriptInset::dy1() const
149 int asc = up().descent();
159 int MathScriptInset::dx0() const
162 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
166 int MathScriptInset::dx1() const
169 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
173 int MathScriptInset::dxx() const
175 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
179 int MathScriptInset::nwid() const
181 return nuc().size() ? nuc().width() : 2;
185 int MathScriptInset::nasc() const
187 return nuc().size() ? nuc().ascent() : 5;
191 int MathScriptInset::ndes() const
193 return nuc().size() ? nuc().descent() : 0;
197 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
200 ScriptChanger dummy(mi.base);
207 dim_.wid = max(dim_.wid, up().width());
209 dim_.wid = max(dim_.wid, down().width());
212 dim_.wid = max(dim_.wid, up().width());
214 dim_.wid = max(dim_.wid, down().width());
217 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
218 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
224 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
227 nuc().draw(pi, x + dxx(), y);
229 nuc().setXY(x + dxx(), y);
231 drawStr(pi, pi.base.font, x + dxx(), y, ".");
233 ScriptChanger dummy(pi.base);
235 up().draw(pi, x + dx1(), y - dy1());
237 down().draw(pi, x + dx0(), y + dy0());
238 drawMarkers(pi, x, y);
242 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
245 up().metricsT(mi, dim);
247 down().metricsT(mi, dim);
248 nuc().metricsT(mi, dim);
252 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
255 nuc().drawT(pain, x + dxx(), y);
257 up().drawT(pain, x + dx1(), y - dy1());
259 down().drawT(pain, x + dx0(), y + dy0());
264 bool MathScriptInset::hasLimits() const
272 // we can only display limits if the nucleus wants some
275 if (!nuc().back()->isScriptable())
278 // per default \int has limits beside the \int even in displayed formulas
279 if (nuc().back()->asSymbolInset())
280 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
283 // assume "real" limits for everything else
288 void MathScriptInset::removeScript(bool up)
295 bool MathScriptInset::has(bool up) const
301 bool MathScriptInset::hasUp() const
307 bool MathScriptInset::hasDown() const
313 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
319 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
325 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
329 // if we are 'up' we can't go further up
332 // otherwise go to last base position
334 pos = cell(2).size();
338 // if we are 'down' we can't go further down
342 pos = cell(2).size();
347 // don't go up/down if there is no cell.
350 // go up/down only if in the last position
351 // or in the first position of something with displayed limits
352 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
363 void MathScriptInset::write(WriteStream & os) const
367 //if (nuc().back()->takesLimits()) {
375 lyxerr[Debug::MATHED] << "suppressing {} when writing"
381 if (hasDown() && down().size())
382 os << "_{" << down() << '}';
384 if (hasUp() && up().size())
385 os << "^{" << up() << '}';
387 if (lock_ && !os.latex())
392 void MathScriptInset::normalize(NormalStream & os) const
394 bool d = hasDown() && down().size();
395 bool u = hasUp() && up().size();
410 os << down() << ' ' << up() << ']';
418 void MathScriptInset::maple(MapleStream & os) const
422 if (hasDown() && down().size())
423 os << '[' << down() << ']';
424 if (hasUp() && up().size())
425 os << "^(" << up() << ')';
429 void MathScriptInset::mathematica(MathematicaStream & os) const
431 bool d = hasDown() && down().size();
432 bool u = hasUp() && up().size();
436 os << "Subscript[" << nuc();
442 os << "^(" << up() << ')';
446 os << ',' << down() << ']';
450 void MathScriptInset::mathmlize( MathMLStream & os) const
452 bool d = hasDown() && down().size();
453 bool u = hasUp() && up().size();
456 os << MTag("msubsup");
468 os << down() << up() << ETag("msubsup");
470 os << up() << ETag("msup");
472 os << down() << ETag("msub");
476 void MathScriptInset::octave(OctaveStream & os) const
480 if (hasDown() && down().size())
481 os << '[' << down() << ']';
482 if (hasUp() && up().size())
483 os << "^(" << up() << ')';
487 void MathScriptInset::infoize(std::ostream & os) const
493 void MathScriptInset::infoize2(std::ostream & os) const
496 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
500 void MathScriptInset::notifyCursorLeaves(idx_type idx)
502 MathNestInset::notifyCursorLeaves(idx);
504 // remove empty scripts if possible
505 if (idx != 2 && script_[idx] && cell(idx).empty()) {
507 script_[idx] = false;
512 dispatch_result MathScriptInset::dispatch
513 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
515 if (cmd.action == LFUN_MATH_LIMITS) {
516 if (!cmd.argument.empty()) {
517 if (cmd.argument == "limits")
519 else if (cmd.argument == "nolimits")
523 } else if (limits_ == 0)
524 limits_ = (hasLimits()) ? -1 : 1;
530 return MathNestInset::dispatch(cmd, idx, pos);