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"
21 #include <boost/assert.hpp>
30 MathScriptInset::MathScriptInset()
31 : MathNestInset(3), limits_(0)
38 MathScriptInset::MathScriptInset(bool up)
39 : MathNestInset(3), limits_(0)
46 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
47 : MathNestInset(3), limits_(0)
51 cell(2).push_back(at);
56 auto_ptr<InsetBase> MathScriptInset::clone() const
58 return auto_ptr<InsetBase>(new MathScriptInset(*this));
62 MathScriptInset const * MathScriptInset::asScriptInset() const
68 MathScriptInset * MathScriptInset::asScriptInset()
74 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
82 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
90 MathArray const & MathScriptInset::down() const
96 MathArray & MathScriptInset::down()
102 MathArray const & MathScriptInset::up() const
108 MathArray & MathScriptInset::up()
114 void MathScriptInset::ensure(bool up)
120 MathArray const & MathScriptInset::nuc() const
126 MathArray & MathScriptInset::nuc()
132 int MathScriptInset::dy0() const
137 int des = down().ascent();
146 int MathScriptInset::dy1() const
151 int asc = up().descent();
161 int MathScriptInset::dx0() const
163 BOOST_ASSERT(hasDown());
164 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
168 int MathScriptInset::dx1() const
170 BOOST_ASSERT(hasUp());
171 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
175 int MathScriptInset::dxx() const
177 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
181 int MathScriptInset::nwid() const
183 return nuc().size() ? nuc().width() : 2;
187 int MathScriptInset::nasc() const
189 return nuc().size() ? nuc().ascent() : 5;
193 int MathScriptInset::ndes() const
195 return nuc().size() ? nuc().descent() : 0;
199 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
202 ScriptChanger dummy(mi.base);
209 dim_.wid = max(dim_.wid, up().width());
211 dim_.wid = max(dim_.wid, down().width());
214 dim_.wid = max(dim_.wid, up().width());
216 dim_.wid = max(dim_.wid, down().width());
219 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
220 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
226 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
229 nuc().draw(pi, x + dxx(), y);
231 nuc().setXY(x + dxx(), y);
233 drawStr(pi, pi.base.font, x + dxx(), y, ".");
235 ScriptChanger dummy(pi.base);
237 up().draw(pi, x + dx1(), y - dy1());
239 down().draw(pi, x + dx0(), y + dy0());
240 drawMarkers(pi, x, y);
244 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
247 up().metricsT(mi, dim);
249 down().metricsT(mi, dim);
250 nuc().metricsT(mi, dim);
254 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
257 nuc().drawT(pain, x + dxx(), y);
259 up().drawT(pain, x + dx1(), y - dy1());
261 down().drawT(pain, x + dx0(), y + dy0());
266 bool MathScriptInset::hasLimits() const
274 // we can only display limits if the nucleus wants some
277 if (!nuc().back()->isScriptable())
280 // per default \int has limits beside the \int even in displayed formulas
281 if (nuc().back()->asSymbolInset())
282 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
285 // assume "real" limits for everything else
290 void MathScriptInset::removeScript(bool up)
297 bool MathScriptInset::has(bool up) const
303 bool MathScriptInset::hasUp() const
309 bool MathScriptInset::hasDown() const
315 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
321 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
327 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
331 // if we are 'up' we can't go further up
334 // otherwise go to last base position
336 pos = cell(2).size();
340 // if we are 'down' we can't go further down
344 pos = cell(2).size();
349 // don't go up/down if there is no cell.
352 // go up/down only if in the last position
353 // or in the first position of something with displayed limits
354 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
365 void MathScriptInset::write(WriteStream & os) const
369 //if (nuc().back()->takesLimits()) {
377 lyxerr[Debug::MATHED] << "suppressing {} when writing"
383 if (hasDown() && down().size())
384 os << "_{" << down() << '}';
386 if (hasUp() && up().size())
387 os << "^{" << up() << '}';
389 if (lock_ && !os.latex())
394 void MathScriptInset::normalize(NormalStream & os) const
396 bool d = hasDown() && down().size();
397 bool u = hasUp() && up().size();
412 os << down() << ' ' << up() << ']';
420 void MathScriptInset::maple(MapleStream & os) const
424 if (hasDown() && down().size())
425 os << '[' << down() << ']';
426 if (hasUp() && up().size())
427 os << "^(" << up() << ')';
431 void MathScriptInset::mathematica(MathematicaStream & os) const
433 bool d = hasDown() && down().size();
434 bool u = hasUp() && up().size();
438 os << "Subscript[" << nuc();
444 os << "^(" << up() << ')';
448 os << ',' << down() << ']';
452 void MathScriptInset::mathmlize( MathMLStream & os) const
454 bool d = hasDown() && down().size();
455 bool u = hasUp() && up().size();
458 os << MTag("msubsup");
470 os << down() << up() << ETag("msubsup");
472 os << up() << ETag("msup");
474 os << down() << ETag("msub");
478 void MathScriptInset::octave(OctaveStream & os) const
482 if (hasDown() && down().size())
483 os << '[' << down() << ']';
484 if (hasUp() && up().size())
485 os << "^(" << up() << ')';
489 void MathScriptInset::infoize(std::ostream & os) const
495 void MathScriptInset::infoize2(std::ostream & os) const
498 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
502 void MathScriptInset::notifyCursorLeaves(idx_type idx)
504 MathNestInset::notifyCursorLeaves(idx);
506 // remove empty scripts if possible
507 if (idx != 2 && script_[idx] && cell(idx).empty()) {
509 script_[idx] = false;
514 dispatch_result MathScriptInset::dispatch
515 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
517 if (cmd.action == LFUN_MATH_LIMITS) {
518 if (!cmd.argument.empty()) {
519 if (cmd.argument == "limits")
521 else if (cmd.argument == "nolimits")
525 } else if (limits_ == 0)
526 limits_ = (hasLimits()) ? -1 : 1;
532 return MathNestInset::dispatch(cmd, idx, pos);