1 #include "math_scriptinset.h"
2 #include "math_support.h"
3 #include "math_symbolinset.h"
4 #include "math_mathmlstream.h"
5 #include "funcrequest.h"
6 #include "support/LAssert.h"
9 using namespace lyx::support;
14 MathScriptInset::MathScriptInset()
15 : MathNestInset(3), limits_(0)
22 MathScriptInset::MathScriptInset(bool up)
23 : MathNestInset(3), limits_(0)
30 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
31 : MathNestInset(3), limits_(0)
35 cell(2).push_back(at);
40 InsetBase * MathScriptInset::clone() const
42 return new MathScriptInset(*this);
46 MathScriptInset const * MathScriptInset::asScriptInset() const
52 MathScriptInset * MathScriptInset::asScriptInset()
58 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
66 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
74 MathArray const & MathScriptInset::down() const
80 MathArray & MathScriptInset::down()
86 MathArray const & MathScriptInset::up() const
92 MathArray & MathScriptInset::up()
98 void MathScriptInset::ensure(bool up)
104 MathArray const & MathScriptInset::nuc() const
110 MathArray & MathScriptInset::nuc()
116 int MathScriptInset::dy0() const
121 int des = down().ascent();
130 int MathScriptInset::dy1() const
135 int asc = up().descent();
145 int MathScriptInset::dx0() const
148 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
152 int MathScriptInset::dx1() const
155 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
159 int MathScriptInset::dxx() const
161 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
165 int MathScriptInset::nwid() const
167 return nuc().size() ? nuc().width() : 2;
171 int MathScriptInset::nasc() const
173 return nuc().size() ? nuc().ascent() : 5;
177 int MathScriptInset::ndes() const
179 return nuc().size() ? nuc().descent() : 0;
183 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
186 ScriptChanger dummy(mi.base);
193 dim_.wid = max(dim_.wid, up().width());
195 dim_.wid = max(dim_.wid, down().width());
198 dim_.wid = max(dim_.wid, up().width());
200 dim_.wid = max(dim_.wid, down().width());
203 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
204 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
210 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
213 nuc().draw(pi, x + dxx(), y);
215 nuc().setXY(x + dxx(), y);
217 drawStr(pi, pi.base.font, x + dxx(), y, ".");
219 ScriptChanger dummy(pi.base);
221 up().draw(pi, x + dx1(), y - dy1());
223 down().draw(pi, x + dx0(), y + dy0());
224 drawMarkers(pi, x, y);
228 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
231 up().metricsT(mi, dim);
233 down().metricsT(mi, dim);
234 nuc().metricsT(mi, dim);
238 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
241 nuc().drawT(pain, x + dxx(), y);
243 up().drawT(pain, x + dx1(), y - dy1());
245 down().drawT(pain, x + dx0(), y + dy0());
250 bool MathScriptInset::hasLimits() const
258 // we can only display limits if the nucleus wants some
261 if (!nuc().back()->isScriptable())
264 // per default \int has limits beside the \int even in displayed formulas
265 if (nuc().back()->asSymbolInset())
266 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
269 // assume "real" limits for everything else
274 void MathScriptInset::removeScript(bool up)
281 bool MathScriptInset::has(bool up) const
287 bool MathScriptInset::hasUp() const
293 bool MathScriptInset::hasDown() const
299 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
305 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
311 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
315 // if we are 'up' we can't go further up
318 // otherwise go to last base position
320 pos = cell(2).size();
324 // if we are 'down' we can't go further down
328 pos = cell(2).size();
333 // don't go up/down if there is no cell.
336 // go up/down only if in the last position
337 // or in the first position of something with displayed limits
338 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
349 void MathScriptInset::write(WriteStream & os) const
353 //if (nuc().back()->takesLimits()) {
361 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
366 if (hasDown() && down().size())
367 os << "_{" << down() << '}';
369 if (hasUp() && up().size())
370 os << "^{" << up() << '}';
372 if (lock_ && !os.latex())
377 void MathScriptInset::normalize(NormalStream & os) const
379 bool d = hasDown() && down().size();
380 bool u = hasUp() && up().size();
395 os << down() << ' ' << up() << ']';
403 void MathScriptInset::maple(MapleStream & os) const
407 if (hasDown() && down().size())
408 os << '[' << down() << ']';
409 if (hasUp() && up().size())
410 os << "^(" << up() << ')';
414 void MathScriptInset::mathematica(MathematicaStream & os) const
416 bool d = hasDown() && down().size();
417 bool u = hasUp() && up().size();
421 os << "Subscript[" << nuc();
427 os << "^(" << up() << ')';
431 os << ',' << down() << ']';
435 void MathScriptInset::mathmlize( MathMLStream & os) const
437 bool d = hasDown() && down().size();
438 bool u = hasUp() && up().size();
441 os << MTag("msubsup");
453 os << down() << up() << ETag("msubsup");
455 os << up() << ETag("msup");
457 os << down() << ETag("msub");
461 void MathScriptInset::octave(OctaveStream & os) const
465 if (hasDown() && down().size())
466 os << '[' << down() << ']';
467 if (hasUp() && up().size())
468 os << "^(" << up() << ')';
472 void MathScriptInset::infoize(std::ostream & os) const
478 void MathScriptInset::infoize2(std::ostream & os) const
481 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
485 void MathScriptInset::notifyCursorLeaves(idx_type idx)
487 MathNestInset::notifyCursorLeaves(idx);
489 // remove empty scripts if possible
490 if (idx != 2 && script_[idx] && cell(idx).empty()) {
492 script_[idx] = false;
497 dispatch_result MathScriptInset::dispatch
498 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
500 if (cmd.action == LFUN_MATH_LIMITS) {
501 if (!cmd.argument.empty()) {
502 if (cmd.argument == "limits")
504 else if (cmd.argument == "nolimits")
508 } else if (limits_ == 0)
509 limits_ = (hasLimits()) ? -1 : 1;
515 return MathNestInset::dispatch(cmd, idx, pos);