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"
19 #include "funcrequest.h"
20 #include "support/LAssert.h"
22 using namespace lyx::support;
29 MathScriptInset::MathScriptInset()
30 : MathNestInset(3), limits_(0)
37 MathScriptInset::MathScriptInset(bool up)
38 : MathNestInset(3), limits_(0)
45 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
46 : MathNestInset(3), limits_(0)
50 cell(2).push_back(at);
55 auto_ptr<InsetBase> MathScriptInset::clone() const
57 return auto_ptr<InsetBase>(new MathScriptInset(*this));
61 MathScriptInset const * MathScriptInset::asScriptInset() const
67 MathScriptInset * MathScriptInset::asScriptInset()
73 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
81 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
89 MathArray const & MathScriptInset::down() const
95 MathArray & MathScriptInset::down()
101 MathArray const & MathScriptInset::up() const
107 MathArray & MathScriptInset::up()
113 void MathScriptInset::ensure(bool up)
119 MathArray const & MathScriptInset::nuc() const
125 MathArray & MathScriptInset::nuc()
131 int MathScriptInset::dy0() const
136 int des = down().ascent();
145 int MathScriptInset::dy1() const
150 int asc = up().descent();
160 int MathScriptInset::dx0() const
163 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
167 int MathScriptInset::dx1() const
170 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
174 int MathScriptInset::dxx() const
176 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
180 int MathScriptInset::nwid() const
182 return nuc().size() ? nuc().width() : 2;
186 int MathScriptInset::nasc() const
188 return nuc().size() ? nuc().ascent() : 5;
192 int MathScriptInset::ndes() const
194 return nuc().size() ? nuc().descent() : 0;
198 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
201 ScriptChanger dummy(mi.base);
208 dim_.wid = max(dim_.wid, up().width());
210 dim_.wid = max(dim_.wid, down().width());
213 dim_.wid = max(dim_.wid, up().width());
215 dim_.wid = max(dim_.wid, down().width());
218 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
219 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
225 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
228 nuc().draw(pi, x + dxx(), y);
230 nuc().setXY(x + dxx(), y);
232 drawStr(pi, pi.base.font, x + dxx(), y, ".");
234 ScriptChanger dummy(pi.base);
236 up().draw(pi, x + dx1(), y - dy1());
238 down().draw(pi, x + dx0(), y + dy0());
239 drawMarkers(pi, x, y);
243 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
246 up().metricsT(mi, dim);
248 down().metricsT(mi, dim);
249 nuc().metricsT(mi, dim);
253 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
256 nuc().drawT(pain, x + dxx(), y);
258 up().drawT(pain, x + dx1(), y - dy1());
260 down().drawT(pain, x + dx0(), y + dy0());
265 bool MathScriptInset::hasLimits() const
273 // we can only display limits if the nucleus wants some
276 if (!nuc().back()->isScriptable())
279 // per default \int has limits beside the \int even in displayed formulas
280 if (nuc().back()->asSymbolInset())
281 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
284 // assume "real" limits for everything else
289 void MathScriptInset::removeScript(bool up)
296 bool MathScriptInset::has(bool up) const
302 bool MathScriptInset::hasUp() const
308 bool MathScriptInset::hasDown() const
314 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
320 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
326 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
330 // if we are 'up' we can't go further up
333 // otherwise go to last base position
335 pos = cell(2).size();
339 // if we are 'down' we can't go further down
343 pos = cell(2).size();
348 // don't go up/down if there is no cell.
351 // go up/down only if in the last position
352 // or in the first position of something with displayed limits
353 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
364 void MathScriptInset::write(WriteStream & os) const
368 //if (nuc().back()->takesLimits()) {
376 lyxerr[Debug::MATHED] << "suppressing {} when writing"
382 if (hasDown() && down().size())
383 os << "_{" << down() << '}';
385 if (hasUp() && up().size())
386 os << "^{" << up() << '}';
388 if (lock_ && !os.latex())
393 void MathScriptInset::normalize(NormalStream & os) const
395 bool d = hasDown() && down().size();
396 bool u = hasUp() && up().size();
411 os << down() << ' ' << up() << ']';
419 void MathScriptInset::maple(MapleStream & os) const
423 if (hasDown() && down().size())
424 os << '[' << down() << ']';
425 if (hasUp() && up().size())
426 os << "^(" << up() << ')';
430 void MathScriptInset::mathematica(MathematicaStream & os) const
432 bool d = hasDown() && down().size();
433 bool u = hasUp() && up().size();
437 os << "Subscript[" << nuc();
443 os << "^(" << up() << ')';
447 os << ',' << down() << ']';
451 void MathScriptInset::mathmlize( MathMLStream & os) const
453 bool d = hasDown() && down().size();
454 bool u = hasUp() && up().size();
457 os << MTag("msubsup");
469 os << down() << up() << ETag("msubsup");
471 os << up() << ETag("msup");
473 os << down() << ETag("msub");
477 void MathScriptInset::octave(OctaveStream & os) const
481 if (hasDown() && down().size())
482 os << '[' << down() << ']';
483 if (hasUp() && up().size())
484 os << "^(" << up() << ')';
488 void MathScriptInset::infoize(std::ostream & os) const
494 void MathScriptInset::infoize2(std::ostream & os) const
497 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
501 void MathScriptInset::notifyCursorLeaves(idx_type idx)
503 MathNestInset::notifyCursorLeaves(idx);
505 // remove empty scripts if possible
506 if (idx != 2 && script_[idx] && cell(idx).empty()) {
508 script_[idx] = false;
513 dispatch_result MathScriptInset::dispatch
514 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
516 if (cmd.action == LFUN_MATH_LIMITS) {
517 if (!cmd.argument.empty()) {
518 if (cmd.argument == "limits")
520 else if (cmd.argument == "nolimits")
524 } else if (limits_ == 0)
525 limits_ = (hasLimits()) ? -1 : 1;
531 return MathNestInset::dispatch(cmd, idx, pos);