3 #include "math_scriptinset.h"
4 #include "math_support.h"
5 #include "math_symbolinset.h"
6 #include "math_mathmlstream.h"
7 #include "funcrequest.h"
8 #include "support/LAssert.h"
11 using namespace lyx::support;
18 MathScriptInset::MathScriptInset()
19 : MathNestInset(3), limits_(0)
26 MathScriptInset::MathScriptInset(bool up)
27 : MathNestInset(3), limits_(0)
34 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
35 : MathNestInset(3), limits_(0)
39 cell(2).push_back(at);
44 auto_ptr<InsetBase> MathScriptInset::clone() const
46 return auto_ptr<InsetBase>(new MathScriptInset(*this));
50 MathScriptInset const * MathScriptInset::asScriptInset() const
56 MathScriptInset * MathScriptInset::asScriptInset()
62 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
70 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
78 MathArray const & MathScriptInset::down() const
84 MathArray & MathScriptInset::down()
90 MathArray const & MathScriptInset::up() const
96 MathArray & MathScriptInset::up()
102 void MathScriptInset::ensure(bool up)
108 MathArray const & MathScriptInset::nuc() const
114 MathArray & MathScriptInset::nuc()
120 int MathScriptInset::dy0() const
125 int des = down().ascent();
134 int MathScriptInset::dy1() const
139 int asc = up().descent();
149 int MathScriptInset::dx0() const
152 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
156 int MathScriptInset::dx1() const
159 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
163 int MathScriptInset::dxx() const
165 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
169 int MathScriptInset::nwid() const
171 return nuc().size() ? nuc().width() : 2;
175 int MathScriptInset::nasc() const
177 return nuc().size() ? nuc().ascent() : 5;
181 int MathScriptInset::ndes() const
183 return nuc().size() ? nuc().descent() : 0;
187 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
190 ScriptChanger dummy(mi.base);
197 dim_.wid = max(dim_.wid, up().width());
199 dim_.wid = max(dim_.wid, down().width());
202 dim_.wid = max(dim_.wid, up().width());
204 dim_.wid = max(dim_.wid, down().width());
207 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
208 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
214 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
217 nuc().draw(pi, x + dxx(), y);
219 nuc().setXY(x + dxx(), y);
221 drawStr(pi, pi.base.font, x + dxx(), y, ".");
223 ScriptChanger dummy(pi.base);
225 up().draw(pi, x + dx1(), y - dy1());
227 down().draw(pi, x + dx0(), y + dy0());
228 drawMarkers(pi, x, y);
232 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
235 up().metricsT(mi, dim);
237 down().metricsT(mi, dim);
238 nuc().metricsT(mi, dim);
242 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
245 nuc().drawT(pain, x + dxx(), y);
247 up().drawT(pain, x + dx1(), y - dy1());
249 down().drawT(pain, x + dx0(), y + dy0());
254 bool MathScriptInset::hasLimits() const
262 // we can only display limits if the nucleus wants some
265 if (!nuc().back()->isScriptable())
268 // per default \int has limits beside the \int even in displayed formulas
269 if (nuc().back()->asSymbolInset())
270 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
273 // assume "real" limits for everything else
278 void MathScriptInset::removeScript(bool up)
285 bool MathScriptInset::has(bool up) const
291 bool MathScriptInset::hasUp() const
297 bool MathScriptInset::hasDown() const
303 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
309 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
315 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
319 // if we are 'up' we can't go further up
322 // otherwise go to last base position
324 pos = cell(2).size();
328 // if we are 'down' we can't go further down
332 pos = cell(2).size();
337 // don't go up/down if there is no cell.
340 // go up/down only if in the last position
341 // or in the first position of something with displayed limits
342 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
353 void MathScriptInset::write(WriteStream & os) const
357 //if (nuc().back()->takesLimits()) {
365 lyxerr[Debug::MATHED] << "suppressing {} when writing"
371 if (hasDown() && down().size())
372 os << "_{" << down() << '}';
374 if (hasUp() && up().size())
375 os << "^{" << up() << '}';
377 if (lock_ && !os.latex())
382 void MathScriptInset::normalize(NormalStream & os) const
384 bool d = hasDown() && down().size();
385 bool u = hasUp() && up().size();
400 os << down() << ' ' << up() << ']';
408 void MathScriptInset::maple(MapleStream & os) const
412 if (hasDown() && down().size())
413 os << '[' << down() << ']';
414 if (hasUp() && up().size())
415 os << "^(" << up() << ')';
419 void MathScriptInset::mathematica(MathematicaStream & os) const
421 bool d = hasDown() && down().size();
422 bool u = hasUp() && up().size();
426 os << "Subscript[" << nuc();
432 os << "^(" << up() << ')';
436 os << ',' << down() << ']';
440 void MathScriptInset::mathmlize( MathMLStream & os) const
442 bool d = hasDown() && down().size();
443 bool u = hasUp() && up().size();
446 os << MTag("msubsup");
458 os << down() << up() << ETag("msubsup");
460 os << up() << ETag("msup");
462 os << down() << ETag("msub");
466 void MathScriptInset::octave(OctaveStream & os) const
470 if (hasDown() && down().size())
471 os << '[' << down() << ']';
472 if (hasUp() && up().size())
473 os << "^(" << up() << ')';
477 void MathScriptInset::infoize(std::ostream & os) const
483 void MathScriptInset::infoize2(std::ostream & os) const
486 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
490 void MathScriptInset::notifyCursorLeaves(idx_type idx)
492 MathNestInset::notifyCursorLeaves(idx);
494 // remove empty scripts if possible
495 if (idx != 2 && script_[idx] && cell(idx).empty()) {
497 script_[idx] = false;
502 dispatch_result MathScriptInset::dispatch
503 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
505 if (cmd.action == LFUN_MATH_LIMITS) {
506 if (!cmd.argument.empty()) {
507 if (cmd.argument == "limits")
509 else if (cmd.argument == "nolimits")
513 } else if (limits_ == 0)
514 limits_ = (hasLimits()) ? -1 : 1;
520 return MathNestInset::dispatch(cmd, idx, pos);