2 * \file math_scriptinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_scriptinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_support.h"
17 #include "math_symbolinset.h"
19 #include "funcrequest.h"
20 #include <boost/assert.hpp>
27 MathScriptInset::MathScriptInset()
28 : MathNestInset(3), limits_(0)
35 MathScriptInset::MathScriptInset(bool up)
36 : MathNestInset(3), limits_(0)
43 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
44 : MathNestInset(3), limits_(0)
48 cell(2).push_back(at);
53 auto_ptr<InsetBase> MathScriptInset::clone() const
55 return auto_ptr<InsetBase>(new MathScriptInset(*this));
59 MathScriptInset const * MathScriptInset::asScriptInset() const
65 MathScriptInset * MathScriptInset::asScriptInset()
71 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
79 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
87 MathArray const & MathScriptInset::down() const
93 MathArray & MathScriptInset::down()
99 MathArray const & MathScriptInset::up() const
105 MathArray & MathScriptInset::up()
111 void MathScriptInset::ensure(bool up)
117 MathArray const & MathScriptInset::nuc() const
123 MathArray & MathScriptInset::nuc()
129 int MathScriptInset::dy0() const
134 int des = down().ascent();
143 int MathScriptInset::dy1() const
148 int asc = up().descent();
158 int MathScriptInset::dx0() const
160 BOOST_ASSERT(hasDown());
161 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
165 int MathScriptInset::dx1() const
167 BOOST_ASSERT(hasUp());
168 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
172 int MathScriptInset::dxx() const
174 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
178 int MathScriptInset::nwid() const
180 return nuc().size() ? nuc().width() : 2;
184 int MathScriptInset::nasc() const
186 return nuc().size() ? nuc().ascent() : 5;
190 int MathScriptInset::ndes() const
192 return nuc().size() ? nuc().descent() : 0;
196 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
199 ScriptChanger dummy(mi.base);
206 dim_.wid = max(dim_.wid, up().width());
208 dim_.wid = max(dim_.wid, down().width());
211 dim_.wid = max(dim_.wid, up().width());
213 dim_.wid = max(dim_.wid, down().width());
216 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
217 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
223 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
226 nuc().draw(pi, x + dxx(), y);
228 nuc().setXY(x + dxx(), y);
230 drawStr(pi, pi.base.font, x + dxx(), y, ".");
232 ScriptChanger dummy(pi.base);
234 up().draw(pi, x + dx1(), y - dy1());
236 down().draw(pi, x + dx0(), y + dy0());
237 drawMarkers(pi, x, y);
241 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
244 up().metricsT(mi, dim);
246 down().metricsT(mi, dim);
247 nuc().metricsT(mi, dim);
251 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
254 nuc().drawT(pain, x + dxx(), y);
256 up().drawT(pain, x + dx1(), y - dy1());
258 down().drawT(pain, x + dx0(), y + dy0());
263 bool MathScriptInset::hasLimits() const
271 // we can only display limits if the nucleus wants some
274 if (!nuc().back()->isScriptable())
277 // per default \int has limits beside the \int even in displayed formulas
278 if (nuc().back()->asSymbolInset())
279 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
282 // assume "real" limits for everything else
287 void MathScriptInset::removeScript(bool up)
294 bool MathScriptInset::has(bool up) const
300 bool MathScriptInset::hasUp() const
306 bool MathScriptInset::hasDown() const
312 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
318 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
324 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
328 // if we are 'up' we can't go further up
331 // otherwise go to last base position
333 pos = cell(2).size();
337 // if we are 'down' we can't go further down
341 pos = cell(2).size();
346 // don't go up/down if there is no cell.
349 // go up/down only if in the last position
350 // or in the first position of something with displayed limits
351 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
362 void MathScriptInset::write(WriteStream & os) const
366 //if (nuc().back()->takesLimits()) {
374 lyxerr[Debug::MATHED] << "suppressing {} when writing"
380 if (hasDown() && down().size())
381 os << "_{" << down() << '}';
383 if (hasUp() && up().size())
384 os << "^{" << up() << '}';
386 if (lock_ && !os.latex())
391 void MathScriptInset::normalize(NormalStream & os) const
393 bool d = hasDown() && down().size();
394 bool u = hasUp() && up().size();
409 os << down() << ' ' << up() << ']';
417 void MathScriptInset::maple(MapleStream & os) const
421 if (hasDown() && down().size())
422 os << '[' << down() << ']';
423 if (hasUp() && up().size())
424 os << "^(" << up() << ')';
428 void MathScriptInset::mathematica(MathematicaStream & os) const
430 bool d = hasDown() && down().size();
431 bool u = hasUp() && up().size();
435 os << "Subscript[" << nuc();
441 os << "^(" << up() << ')';
445 os << ',' << down() << ']';
449 void MathScriptInset::mathmlize( MathMLStream & os) const
451 bool d = hasDown() && down().size();
452 bool u = hasUp() && up().size();
455 os << MTag("msubsup");
467 os << down() << up() << ETag("msubsup");
469 os << up() << ETag("msup");
471 os << down() << ETag("msub");
475 void MathScriptInset::octave(OctaveStream & os) const
479 if (hasDown() && down().size())
480 os << '[' << down() << ']';
481 if (hasUp() && up().size())
482 os << "^(" << up() << ')';
486 void MathScriptInset::infoize(std::ostream & os) const
492 void MathScriptInset::infoize2(std::ostream & os) const
495 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
499 void MathScriptInset::notifyCursorLeaves(idx_type idx)
501 MathNestInset::notifyCursorLeaves(idx);
503 // remove empty scripts if possible
504 if (idx != 2 && script_[idx] && cell(idx).empty()) {
506 script_[idx] = false;
511 dispatch_result MathScriptInset::dispatch
512 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
514 if (cmd.action == LFUN_MATH_LIMITS) {
515 if (!cmd.argument.empty()) {
516 if (cmd.argument == "limits")
518 else if (cmd.argument == "nolimits")
522 } else if (limits_ == 0)
523 limits_ = (hasLimits()) ? -1 : 1;
529 return MathNestInset::dispatch(cmd, idx, pos);