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"
18 #include "dispatchresult.h"
20 #include "funcrequest.h"
22 #include <boost/assert.hpp>
31 MathScriptInset::MathScriptInset()
32 : MathNestInset(3), limits_(0)
39 MathScriptInset::MathScriptInset(bool up)
40 : MathNestInset(3), limits_(0)
47 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
48 : MathNestInset(3), limits_(0)
52 cell(2).push_back(at);
57 auto_ptr<InsetBase> MathScriptInset::clone() const
59 return auto_ptr<InsetBase>(new MathScriptInset(*this));
63 MathScriptInset const * MathScriptInset::asScriptInset() const
69 MathScriptInset * MathScriptInset::asScriptInset()
75 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
83 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
91 MathArray const & MathScriptInset::down() const
97 MathArray & MathScriptInset::down()
103 MathArray const & MathScriptInset::up() const
109 MathArray & MathScriptInset::up()
115 void MathScriptInset::ensure(bool up)
121 MathArray const & MathScriptInset::nuc() const
127 MathArray & MathScriptInset::nuc()
133 int MathScriptInset::dy0() const
138 int des = down().ascent();
147 int MathScriptInset::dy1() const
152 int asc = up().descent();
162 int MathScriptInset::dx0() const
164 BOOST_ASSERT(hasDown());
165 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
169 int MathScriptInset::dx1() const
171 BOOST_ASSERT(hasUp());
172 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
176 int MathScriptInset::dxx() const
178 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
182 int MathScriptInset::nwid() const
184 return nuc().size() ? nuc().width() : 2;
188 int MathScriptInset::nasc() const
190 return nuc().size() ? nuc().ascent() : 5;
194 int MathScriptInset::ndes() const
196 return nuc().size() ? nuc().descent() : 0;
200 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
203 ScriptChanger dummy(mi.base);
210 dim_.wid = max(dim_.wid, up().width());
212 dim_.wid = max(dim_.wid, down().width());
215 dim_.wid = max(dim_.wid, up().width());
217 dim_.wid = max(dim_.wid, down().width());
220 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
221 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
227 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
230 nuc().draw(pi, x + dxx(), y);
232 nuc().setXY(x + dxx(), y);
234 drawStr(pi, pi.base.font, x + dxx(), y, ".");
236 ScriptChanger dummy(pi.base);
238 up().draw(pi, x + dx1(), y - dy1());
240 down().draw(pi, x + dx0(), y + dy0());
241 drawMarkers(pi, x, y);
245 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
248 up().metricsT(mi, dim);
250 down().metricsT(mi, dim);
251 nuc().metricsT(mi, dim);
255 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
258 nuc().drawT(pain, x + dxx(), y);
260 up().drawT(pain, x + dx1(), y - dy1());
262 down().drawT(pain, x + dx0(), y + dy0());
267 bool MathScriptInset::hasLimits() const
275 // we can only display limits if the nucleus wants some
278 if (!nuc().back()->isScriptable())
281 // per default \int has limits beside the \int even in displayed formulas
282 if (nuc().back()->asSymbolInset())
283 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
286 // assume "real" limits for everything else
291 void MathScriptInset::removeScript(bool up)
298 bool MathScriptInset::has(bool up) const
304 bool MathScriptInset::hasUp() const
310 bool MathScriptInset::hasDown() const
316 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
322 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
328 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
332 // if we are 'up' we can't go further up
335 // otherwise go to last base position
337 pos = cell(2).size();
341 // if we are 'down' we can't go further down
345 pos = cell(2).size();
350 // don't go up/down if there is no cell.
353 // go up/down only if in the last position
354 // or in the first position of something with displayed limits
355 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
366 void MathScriptInset::write(WriteStream & os) const
370 //if (nuc().back()->takesLimits()) {
378 lyxerr[Debug::MATHED] << "suppressing {} when writing"
384 if (hasDown() && down().size())
385 os << "_{" << down() << '}';
387 if (hasUp() && up().size())
388 os << "^{" << up() << '}';
390 if (lock_ && !os.latex())
395 void MathScriptInset::normalize(NormalStream & os) const
397 bool d = hasDown() && down().size();
398 bool u = hasUp() && up().size();
413 os << down() << ' ' << up() << ']';
421 void MathScriptInset::maple(MapleStream & os) const
425 if (hasDown() && down().size())
426 os << '[' << down() << ']';
427 if (hasUp() && up().size())
428 os << "^(" << up() << ')';
432 void MathScriptInset::mathematica(MathematicaStream & os) const
434 bool d = hasDown() && down().size();
435 bool u = hasUp() && up().size();
439 os << "Subscript[" << nuc();
445 os << "^(" << up() << ')';
449 os << ',' << down() << ']';
453 void MathScriptInset::mathmlize( MathMLStream & os) const
455 bool d = hasDown() && down().size();
456 bool u = hasUp() && up().size();
459 os << MTag("msubsup");
471 os << down() << up() << ETag("msubsup");
473 os << up() << ETag("msup");
475 os << down() << ETag("msub");
479 void MathScriptInset::octave(OctaveStream & os) const
483 if (hasDown() && down().size())
484 os << '[' << down() << ']';
485 if (hasUp() && up().size())
486 os << "^(" << up() << ')';
490 void MathScriptInset::infoize(std::ostream & os) const
496 void MathScriptInset::infoize2(std::ostream & os) const
499 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
503 void MathScriptInset::notifyCursorLeaves(idx_type idx)
505 MathNestInset::notifyCursorLeaves(idx);
507 // remove empty scripts if possible
508 if (idx != 2 && script_[idx] && cell(idx).empty()) {
510 script_[idx] = false;
516 MathScriptInset::priv_dispatch(FuncRequest const & cmd,
517 idx_type & idx, pos_type & pos)
519 if (cmd.action == LFUN_MATH_LIMITS) {
520 if (!cmd.argument.empty()) {
521 if (cmd.argument == "limits")
523 else if (cmd.argument == "nolimits")
527 } else if (limits_ == 0)
528 limits_ = (hasLimits()) ? -1 : 1;
531 return DispatchResult(true, true);
534 return MathNestInset::priv_dispatch(cmd, idx, pos);