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 if there is no cell.
338 // go up/down only if in the last position
339 // or in the first position of something with displayed limits
340 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
351 void MathScriptInset::write(WriteStream & os) const
355 //if (nuc().back()->takesLimits()) {
363 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
368 if (hasDown() && down().size())
369 os << "_{" << down() << '}';
371 if (hasUp() && up().size())
372 os << "^{" << up() << '}';
374 if (lock_ && !os.latex())
379 void MathScriptInset::normalize(NormalStream & os) const
381 bool d = hasDown() && down().size();
382 bool u = hasUp() && up().size();
397 os << down() << ' ' << up() << ']';
405 void MathScriptInset::maplize(MapleStream & os) const
409 if (hasDown() && down().size())
410 os << '[' << down() << ']';
411 if (hasUp() && up().size())
412 os << "^(" << up() << ')';
416 void MathScriptInset::mathematicize(MathematicaStream & os) const
418 bool d = hasDown() && down().size();
419 bool u = hasUp() && up().size();
423 os << "Subscript[" << nuc();
429 os << "^(" << up() << ')';
433 os << ',' << down() << ']';
437 void MathScriptInset::mathmlize( MathMLStream & os) const
439 bool d = hasDown() && down().size();
440 bool u = hasUp() && up().size();
443 os << MTag("msubsup");
455 os << down() << up() << ETag("msubsup");
457 os << up() << ETag("msup");
459 os << down() << ETag("msub");
463 void MathScriptInset::octavize(OctaveStream & os) const
467 if (hasDown() && down().size())
468 os << '[' << down() << ']';
469 if (hasUp() && up().size())
470 os << "^(" << up() << ')';
474 void MathScriptInset::infoize(std::ostream & os) const
480 void MathScriptInset::infoize2(std::ostream & os) const
483 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
487 void MathScriptInset::notifyCursorLeaves(idx_type idx)
489 MathNestInset::notifyCursorLeaves(idx);
491 // remove empty scripts if possible
492 if (idx != 2 && script_[idx] && cell(idx).empty()) {
494 script_[idx] = false;
499 MathInset::result_type MathScriptInset::dispatch
500 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
502 if (cmd.action == LFUN_MATH_LIMITS) {
503 if (!cmd.argument.empty()) {
504 if (cmd.argument == "limits")
506 else if (cmd.argument == "nolimits")
510 } else if (limits_ == 0)
511 limits_ = (hasLimits()) ? -1 : 1;
517 return MathNestInset::dispatch(cmd, idx, pos);