5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "funcrequest.h"
10 #include "support/LAssert.h"
17 MathScriptInset::MathScriptInset()
18 : MathNestInset(3), limits_(0)
25 MathScriptInset::MathScriptInset(bool up)
26 : MathNestInset(3), limits_(0)
33 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
34 : MathNestInset(3), limits_(0)
38 cell(2).push_back(at);
43 MathInset * MathScriptInset::clone() const
45 return new MathScriptInset(*this);
49 MathScriptInset const * MathScriptInset::asScriptInset() const
55 MathScriptInset * MathScriptInset::asScriptInset()
61 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
69 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
77 MathArray const & MathScriptInset::down() const
83 MathArray & MathScriptInset::down()
89 MathArray const & MathScriptInset::up() const
95 MathArray & MathScriptInset::up()
101 void MathScriptInset::ensure(bool up)
107 MathArray const & MathScriptInset::nuc() const
113 MathArray & MathScriptInset::nuc()
119 int MathScriptInset::dy0() const
124 int des = down().ascent();
133 int MathScriptInset::dy1() const
138 int asc = up().descent();
148 int MathScriptInset::dx0() const
150 lyx::Assert(hasDown());
151 return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
155 int MathScriptInset::dx1() const
157 lyx::Assert(hasUp());
158 return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
162 int MathScriptInset::dxx() const
164 return hasLimits() ? (dim_.w - nwid()) / 2 : 0;
168 int MathScriptInset::nwid() const
170 return nuc().size() ? nuc().width() : 2;
174 int MathScriptInset::nasc() const
176 return nuc().size() ? nuc().ascent() : 5;
180 int MathScriptInset::ndes() const
182 return nuc().size() ? nuc().descent() : 0;
186 void MathScriptInset::metrics(MathMetricsInfo & mi) const
189 MathScriptChanger dummy(mi.base);
196 dim_.w = max(dim_.w, up().width());
198 dim_.w = max(dim_.w, down().width());
201 dim_.w = max(dim_.w, up().width());
203 dim_.w = max(dim_.w, down().width());
206 dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
207 dim_.d = dy0() + (hasDown() ? down().descent() : 0);
212 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
215 nuc().draw(pi, x + dxx(), y);
217 nuc().setXY(x + dxx(), y);
219 drawStr(pi, pi.base.font, x + dxx(), y, ".");
221 MathScriptChanger dummy(pi.base);
223 up().draw(pi, x + dx1(), y - dy1());
225 down().draw(pi, x + dx0(), y + dy0());
226 drawMarkers2(pi, x, y);
230 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
240 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
243 nuc().drawT(pain, x + dxx(), y);
245 up().drawT(pain, x + dx1(), y - dy1());
247 down().drawT(pain, x + dx0(), y + dy0());
252 bool MathScriptInset::hasLimits() const
260 // we can only display limits if the nucleus wants some
263 if (!nuc().back()->isScriptable())
266 // per default \int has limits beside the \int even in displayed formulas
267 if (nuc().back()->asSymbolInset())
268 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
271 // assume "real" limits for everything else
276 void MathScriptInset::removeScript(bool up)
283 bool MathScriptInset::has(bool up) const
289 bool MathScriptInset::hasUp() const
295 bool MathScriptInset::hasDown() const
301 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
307 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
313 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
317 // if we are 'up' we can't go further up
320 // otherwise go to last base position
322 pos = cell(2).size();
326 // if we are 'down' we can't go further down
330 pos = cell(2).size();
335 // don't go up/down unless in last position
336 if (pos != cell(2).size())
338 // don't go up/down if there is no cell.
341 // otherwise move into the first position
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::maplize(MapleStream & os) const
407 if (hasDown() && down().size())
408 os << '[' << down() << ']';
409 if (hasUp() && up().size())
410 os << "^(" << up() << ')';
414 void MathScriptInset::mathematicize(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::octavize(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 MathInset::result_type 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);