2 #include "math_scriptinset.h"
3 #include "math_support.h"
4 #include "math_symbolinset.h"
5 #include "math_mathmlstream.h"
6 #include "funcrequest.h"
7 #include "support/LAssert.h"
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 MathInset * 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
147 lyx::Assert(hasDown());
148 return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
152 int MathScriptInset::dx1() const
154 lyx::Assert(hasUp());
155 return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
159 int MathScriptInset::dxx() const
161 return hasLimits() ? (dim_.w - 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) const
186 ScriptChanger dummy(mi.base);
193 dim_.w = max(dim_.w, up().width());
195 dim_.w = max(dim_.w, down().width());
198 dim_.w = max(dim_.w, up().width());
200 dim_.w = max(dim_.w, down().width());
203 dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
204 dim_.d = dy0() + (hasDown() ? down().descent() : 0);
209 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
212 nuc().draw(pi, x + dxx(), y);
214 nuc().setXY(x + dxx(), y);
216 drawStr(pi, pi.base.font, x + dxx(), y, ".");
218 ScriptChanger dummy(pi.base);
220 up().draw(pi, x + dx1(), y - dy1());
222 down().draw(pi, x + dx0(), y + dy0());
223 drawMarkers2(pi, x, y);
227 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
237 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
240 nuc().drawT(pain, x + dxx(), y);
242 up().drawT(pain, x + dx1(), y - dy1());
244 down().drawT(pain, x + dx0(), y + dy0());
249 bool MathScriptInset::hasLimits() const
257 // we can only display limits if the nucleus wants some
260 if (!nuc().back()->isScriptable())
263 // per default \int has limits beside the \int even in displayed formulas
264 if (nuc().back()->asSymbolInset())
265 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
268 // assume "real" limits for everything else
273 void MathScriptInset::removeScript(bool up)
280 bool MathScriptInset::has(bool up) const
286 bool MathScriptInset::hasUp() const
292 bool MathScriptInset::hasDown() const
298 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
304 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
310 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
314 // if we are 'up' we can't go further up
317 // otherwise go to last base position
319 pos = cell(2).size();
323 // if we are 'down' we can't go further down
327 pos = cell(2).size();
332 // don't go up/down if there is no cell.
335 // go up/down only if in the last position
336 // or in the first position of something with displayed limits
337 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
348 void MathScriptInset::write(WriteStream & os) const
352 //if (nuc().back()->takesLimits()) {
360 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
365 if (hasDown() && down().size())
366 os << "_{" << down() << '}';
368 if (hasUp() && up().size())
369 os << "^{" << up() << '}';
371 if (lock_ && !os.latex())
376 void MathScriptInset::normalize(NormalStream & os) const
378 bool d = hasDown() && down().size();
379 bool u = hasUp() && up().size();
394 os << down() << ' ' << up() << ']';
402 void MathScriptInset::maple(MapleStream & os) const
406 if (hasDown() && down().size())
407 os << '[' << down() << ']';
408 if (hasUp() && up().size())
409 os << "^(" << up() << ')';
413 void MathScriptInset::mathematica(MathematicaStream & os) const
415 bool d = hasDown() && down().size();
416 bool u = hasUp() && up().size();
420 os << "Subscript[" << nuc();
426 os << "^(" << up() << ')';
430 os << ',' << down() << ']';
434 void MathScriptInset::mathmlize( MathMLStream & os) const
436 bool d = hasDown() && down().size();
437 bool u = hasUp() && up().size();
440 os << MTag("msubsup");
452 os << down() << up() << ETag("msubsup");
454 os << up() << ETag("msup");
456 os << down() << ETag("msub");
460 void MathScriptInset::octave(OctaveStream & os) const
464 if (hasDown() && down().size())
465 os << '[' << down() << ']';
466 if (hasUp() && up().size())
467 os << "^(" << up() << ')';
471 void MathScriptInset::infoize(std::ostream & os) const
477 void MathScriptInset::infoize2(std::ostream & os) const
480 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
484 void MathScriptInset::notifyCursorLeaves(idx_type idx)
486 MathNestInset::notifyCursorLeaves(idx);
488 // remove empty scripts if possible
489 if (idx != 2 && script_[idx] && cell(idx).empty()) {
491 script_[idx] = false;
496 dispatch_result MathScriptInset::dispatch
497 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
499 if (cmd.action == LFUN_MATH_LIMITS) {
500 if (!cmd.argument.empty()) {
501 if (cmd.argument == "limits")
503 else if (cmd.argument == "nolimits")
507 } else if (limits_ == 0)
508 limits_ = (hasLimits()) ? -1 : 1;
514 return MathNestInset::dispatch(cmd, idx, pos);