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"
26 MathScriptInset::MathScriptInset()
27 : MathNestInset(3), limits_(0)
34 MathScriptInset::MathScriptInset(bool up)
35 : MathNestInset(3), limits_(0)
42 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
43 : MathNestInset(3), limits_(0)
47 cell(2).push_back(at);
52 auto_ptr<InsetBase> MathScriptInset::clone() const
54 return auto_ptr<InsetBase>(new MathScriptInset(*this));
58 MathScriptInset const * MathScriptInset::asScriptInset() const
64 MathScriptInset * MathScriptInset::asScriptInset()
70 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
78 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
86 MathArray const & MathScriptInset::down() const
92 MathArray & MathScriptInset::down()
98 MathArray const & MathScriptInset::up() const
104 MathArray & MathScriptInset::up()
110 void MathScriptInset::ensure(bool up)
116 MathArray const & MathScriptInset::nuc() const
122 MathArray & MathScriptInset::nuc()
128 int MathScriptInset::dy0() const
133 int des = down().ascent();
142 int MathScriptInset::dy1() const
147 int asc = up().descent();
157 int MathScriptInset::dx0() const
159 BOOST_ASSERT(hasDown());
160 return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
164 int MathScriptInset::dx1() const
166 BOOST_ASSERT(hasUp());
167 return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
171 int MathScriptInset::dxx() const
173 return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
177 int MathScriptInset::nwid() const
179 return nuc().size() ? nuc().width() : 2;
183 int MathScriptInset::nasc() const
185 return nuc().size() ? nuc().ascent() : 5;
189 int MathScriptInset::ndes() const
191 return nuc().size() ? nuc().descent() : 0;
195 void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
198 ScriptChanger dummy(mi.base);
205 dim_.wid = max(dim_.wid, up().width());
207 dim_.wid = max(dim_.wid, down().width());
210 dim_.wid = max(dim_.wid, up().width());
212 dim_.wid = max(dim_.wid, down().width());
215 dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
216 dim_.des = dy0() + (hasDown() ? down().descent() : 0);
222 void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
225 nuc().draw(pi, x + dxx(), y);
227 nuc().setXY(x + dxx(), y);
229 drawStr(pi, pi.base.font, x + dxx(), y, ".");
231 ScriptChanger dummy(pi.base);
233 up().draw(pi, x + dx1(), y - dy1());
235 down().draw(pi, x + dx0(), y + dy0());
236 drawMarkers(pi, x, y);
240 void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
243 up().metricsT(mi, dim);
245 down().metricsT(mi, dim);
246 nuc().metricsT(mi, dim);
250 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
253 nuc().drawT(pain, x + dxx(), y);
255 up().drawT(pain, x + dx1(), y - dy1());
257 down().drawT(pain, x + dx0(), y + dy0());
262 bool MathScriptInset::hasLimits() const
270 // we can only display limits if the nucleus wants some
273 if (!nuc().back()->isScriptable())
276 // per default \int has limits beside the \int even in displayed formulas
277 if (nuc().back()->asSymbolInset())
278 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
281 // assume "real" limits for everything else
286 void MathScriptInset::removeScript(bool up)
293 bool MathScriptInset::has(bool up) const
299 bool MathScriptInset::hasUp() const
305 bool MathScriptInset::hasDown() const
311 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
317 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
323 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
327 // if we are 'up' we can't go further up
330 // otherwise go to last base position
332 pos = cell(2).size();
336 // if we are 'down' we can't go further down
340 pos = cell(2).size();
345 // don't go up/down if there is no cell.
348 // go up/down only if in the last position
349 // or in the first position of something with displayed limits
350 if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
361 void MathScriptInset::write(WriteStream & os) const
365 //if (nuc().back()->takesLimits()) {
373 lyxerr[Debug::MATHED] << "suppressing {} when writing"
379 if (hasDown() && down().size())
380 os << "_{" << down() << '}';
382 if (hasUp() && up().size())
383 os << "^{" << up() << '}';
385 if (lock_ && !os.latex())
390 void MathScriptInset::normalize(NormalStream & os) const
392 bool d = hasDown() && down().size();
393 bool u = hasUp() && up().size();
408 os << down() << ' ' << up() << ']';
416 void MathScriptInset::maple(MapleStream & os) const
420 if (hasDown() && down().size())
421 os << '[' << down() << ']';
422 if (hasUp() && up().size())
423 os << "^(" << up() << ')';
427 void MathScriptInset::mathematica(MathematicaStream & os) const
429 bool d = hasDown() && down().size();
430 bool u = hasUp() && up().size();
434 os << "Subscript[" << nuc();
440 os << "^(" << up() << ')';
444 os << ',' << down() << ']';
448 void MathScriptInset::mathmlize( MathMLStream & os) const
450 bool d = hasDown() && down().size();
451 bool u = hasUp() && up().size();
454 os << MTag("msubsup");
466 os << down() << up() << ETag("msubsup");
468 os << up() << ETag("msup");
470 os << down() << ETag("msub");
474 void MathScriptInset::octave(OctaveStream & os) const
478 if (hasDown() && down().size())
479 os << '[' << down() << ']';
480 if (hasUp() && up().size())
481 os << "^(" << up() << ')';
485 void MathScriptInset::infoize(std::ostream & os) const
491 void MathScriptInset::infoize2(std::ostream & os) const
494 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
498 void MathScriptInset::notifyCursorLeaves(idx_type idx)
500 MathNestInset::notifyCursorLeaves(idx);
502 // remove empty scripts if possible
503 if (idx != 2 && script_[idx] && cell(idx).empty()) {
505 script_[idx] = false;
510 dispatch_result MathScriptInset::dispatch
511 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
513 if (cmd.action == LFUN_MATH_LIMITS) {
514 if (!cmd.argument.empty()) {
515 if (cmd.argument == "limits")
517 else if (cmd.argument == "nolimits")
521 } else if (limits_ == 0)
522 limits_ = (hasLimits()) ? -1 : 1;
528 return MathNestInset::dispatch(cmd, idx, pos);