X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_scriptinset.C;h=cf23765572186e00b490bb7606b1210e4e5316e8;hb=701b99ecd70ac472aa53c8b2317af44def4f9670;hp=07bf1164b7ae254b9f456eb4da1e5233ef9a5634;hpb=357a3741c0655e174ad48ded68cca90b09a158e6;p=lyx.git diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index 07bf1164b7..cf23765572 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -30,32 +30,24 @@ using std::endl; MathScriptInset::MathScriptInset() - : MathNestInset(3), limits_(0) -{ - script_[0] = false; - script_[1] = false; -} + : MathNestInset(1), cell_1_is_up_(false), limits_(0) +{} MathScriptInset::MathScriptInset(bool up) - : MathNestInset(3), limits_(0) -{ - script_[0] = !up; - script_[1] = up; -} + : MathNestInset(2), cell_1_is_up_(up), limits_(0) +{} MathScriptInset::MathScriptInset(MathAtom const & at, bool up) - : MathNestInset(3), limits_(0) + : MathNestInset(2), cell_1_is_up_(up), limits_(0) { - script_[0] = !up; - script_[1] = up; - cell(2).push_back(at); + BOOST_ASSERT(nargs() >= 1); + cell(0).push_back(at); } - -auto_ptr MathScriptInset::clone() const +auto_ptr MathScriptInset::doClone() const { return auto_ptr(new MathScriptInset(*this)); } @@ -75,7 +67,7 @@ MathScriptInset * MathScriptInset::asScriptInset() bool MathScriptInset::idxFirst(LCursor & cur) const { - cur.idx() = 2; + cur.idx() = 0; cur.pos() = 0; return true; } @@ -83,7 +75,7 @@ bool MathScriptInset::idxFirst(LCursor & cur) const bool MathScriptInset::idxLast(LCursor & cur) const { - cur.idx() = 2; + cur.idx() = 0; cur.pos() = nuc().size(); return true; } @@ -91,43 +83,62 @@ bool MathScriptInset::idxLast(LCursor & cur) const MathArray const & MathScriptInset::down() const { - return cell(0); + if (nargs() == 3) + return cell(2); + BOOST_ASSERT(nargs() > 1); + return cell(1); } MathArray & MathScriptInset::down() { - return cell(0); + if (nargs() == 3) + return cell(2); + BOOST_ASSERT(nargs() > 1); + return cell(1); } MathArray const & MathScriptInset::up() const { + BOOST_ASSERT(nargs() > 1); return cell(1); } MathArray & MathScriptInset::up() { + BOOST_ASSERT(nargs() > 1); return cell(1); } void MathScriptInset::ensure(bool up) { - script_[up] = true; + if (nargs() == 1) { + // just nucleus so far + cells_.push_back(MathArray()); + cell_1_is_up_ = up; + } else if (nargs() == 2 && !has(up)) { + if (up) { + cells_.push_back(cell(1)); + cell(1).clear(); + } else { + cells_.push_back(MathArray()); + } + } } MathArray const & MathScriptInset::nuc() const { - return cell(2); + return cell(0); } MathArray & MathScriptInset::nuc() { - return cell(2); + return cell(0); } @@ -200,10 +211,12 @@ int MathScriptInset::ndes() const void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const { - cell(2).metrics(mi); - ScriptChanger dummy(mi.base); cell(0).metrics(mi); - cell(1).metrics(mi); + ScriptChanger dummy(mi.base); + if (nargs() > 1) + cell(1).metrics(mi); + if (nargs() > 2) + cell(2).metrics(mi); dim.wid = 0; if (hasLimits()) { dim.wid = nwid(); @@ -291,26 +304,58 @@ bool MathScriptInset::hasLimits() const void MathScriptInset::removeScript(bool up) { - cell(up).clear(); - script_[up] = false; + lyxerr << "MathScriptInset::removeScript: 1 up: " << up << endl; + if (nargs() == 2) { + lyxerr << "MathScriptInset::removeScript: a up: " << up << endl; + if (up == cell_1_is_up_) + cells_.pop_back(); + lyxerr << "MathScriptInset::removeScript: b up: " << up << endl; + } else if (nargs() == 3) { + if (up == true) { + swap(cells_[1], cells_[2]); + cell_1_is_up_ = false; + } else { + cell_1_is_up_ = true; + } + cells_.pop_back(); + } + lyxerr << "MathScriptInset::removeScript: 2 up: " << up << endl; } bool MathScriptInset::has(bool up) const { - return script_[up]; + return idxOfScript(up); } bool MathScriptInset::hasUp() const { - return script_[1]; + //lyxerr << "1up: " << bool(cell_1_is_up_) << endl; + //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl; + return idxOfScript(true); } bool MathScriptInset::hasDown() const { - return script_[0]; + //lyxerr << "1up: " << bool(cell_1_is_up_) << endl; + //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl; + return idxOfScript(false); +} + + +InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const +{ + if (nargs() == 1) + return 0; + if (nargs() == 2) + return (cell_1_is_up_ == up) ? 1 : 0; + if (nargs() == 3) + return up ? 1 : 2; + BOOST_ASSERT(false); + // Silence compiler + return 0; } @@ -328,38 +373,44 @@ bool MathScriptInset::idxLeft(LCursor &) const bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const { - if (cur.idx() == 1) { - // if we are 'up' we can't go further up - if (up) - return false; - // otherwise go to last base position - cur.idx() = 2; - cur.pos() = cur.lastpos(); - } - - else if (cur.idx() == 0) { - // if we are 'down' we can't go further down - if (!up) - return false; - cur.idx() = 2; - cur.pos() = cur.lastpos(); - } - - else { - // in nucleus - // don't go up/down if there is no cell. + // in nucleus? + if (cur.idx() == 0) { + // don't go up/down if there is no cell in this direction if (!has(up)) return false; // go up/down only if in the last position // or in the first position of something with displayed limits if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) { - cur.idx() = up; + cur.idx() = idxOfScript(up); cur.pos() = 0; return true; } return false; } - return true; + + // Are we 'up'? + if (has(up) && cur.idx() == idxOfScript(true)) { + // can't go further up + if (up) + return false; + // otherwise go to last position in the nucleus + cur.idx() = 0; + cur.pos() = cur.lastpos(); + return true; + } + + // Are we 'down'? + if (has(up) && cur.idx() == idxOfScript(false)) { + // can't go further down + if (!up) + return false; + // otherwise go to last position in the nucleus + cur.idx() = 0; + cur.pos() = cur.lastpos(); + return true; + } + + return false; } @@ -501,19 +552,28 @@ void MathScriptInset::infoize2(std::ostream & os) const } -void MathScriptInset::notifyCursorLeaves(idx_type idx) +void MathScriptInset::notifyCursorLeaves(LCursor & cur) { - MathNestInset::notifyCursorLeaves(idx); + MathNestInset::notifyCursorLeaves(cur); + + lyxerr << "MathScriptInset::notifyCursorLeaves: 1 " << cur << endl; // remove empty scripts if possible - if (idx != 2 && script_[idx] && cell(idx).empty()) { - cell(idx).clear(); - script_[idx] = false; +if (1) { + if (nargs() > 2 && cur.idx() == 2 && cell(2).empty()) { + // must be a subscript... + removeScript(false); + } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) { + // could be either subscript or super script + removeScript(cell_1_is_up_); } } + lyxerr << "MathScriptInset::notifyCursorLeaves: 2 " << cur << endl; +} + -void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd) +void MathScriptInset::doDispatch(LCursor & cur, FuncRequest & cmd) { //lyxerr << "MathScriptInset: request: " << cmd << std::endl; @@ -532,5 +592,5 @@ void MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd) return; } - MathNestInset::priv_dispatch(cur, cmd); + MathNestInset::doDispatch(cur, cmd); }