X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCursorSlice.cpp;h=0ff99882ec0ceedd1e8f60ca435fc562dcf2893b;hb=8db4a8dd4663cdd6a3cf67777ea2538a380d0dd2;hp=2b1164e99d69f963e54e92621d77f24a93b5526a;hpb=d075cb25b89b7bd2ac3d293c9db7c825d58770dd;p=lyx.git diff --git a/src/CursorSlice.cpp b/src/CursorSlice.cpp index 2b1164e99d..0ff99882ec 100644 --- a/src/CursorSlice.cpp +++ b/src/CursorSlice.cpp @@ -25,12 +25,11 @@ #include "mathed/MathData.h" #include -#include +using std::endl; -namespace lyx { -using std::endl; +namespace lyx { CursorSlice::CursorSlice() @@ -42,87 +41,118 @@ CursorSlice::CursorSlice(Inset & p) : inset_(&p), idx_(0), pit_(0), pos_(0) { BOOST_ASSERT(inset_); - boost::signal * destroyed_signal = inset_->destroyedSignal(); - if (destroyed_signal) - inset_connection_ = destroyed_signal->connect( - boost::bind(&CursorSlice::invalidate, this)); } -CursorSlice::CursorSlice(CursorSlice const & cs) +MathData & CursorSlice::cell() const { - operator=(cs); + return inset_->asInsetMath()->cell(idx_); } -CursorSlice::~CursorSlice() +Paragraph & CursorSlice::paragraph() { - inset_connection_.disconnect(); + return text()->getPar(pit_); } -CursorSlice & CursorSlice::operator=(CursorSlice const & cs) +Paragraph const & CursorSlice::paragraph() const { - inset_ = cs.inset_; - idx_ = cs.idx_; - pit_ = cs.pit_; - pos_ = cs.pos_; - if (inset_ && inset_->destroyedSignal()) { - inset_connection_ = inset_->destroyedSignal()->connect( - boost::bind(&CursorSlice::invalidate, this)); - } - return *this; + return text()->getPar(pit_); } -void CursorSlice::invalidate() +pos_type CursorSlice::lastpos() const { - inset_ = 0; + BOOST_ASSERT(inset_); + return inset_->asInsetMath() ? cell().size() : paragraph().size(); } -bool CursorSlice::isValid() const +pit_type CursorSlice::lastpit() const { - return inset_ != 0; + if (inset().inMathed()) + return 0; + return text()->paragraphs().size() - 1; } -MathData & CursorSlice::cell() const +CursorSlice::row_type CursorSlice::row() const { - return inset_->asInsetMath()->cell(idx_); + BOOST_ASSERT(asInsetMath()); + return asInsetMath()->row(idx_); } -Paragraph & CursorSlice::paragraph() +CursorSlice::col_type CursorSlice::col() const { - return text()->getPar(pit_); + BOOST_ASSERT(asInsetMath()); + return asInsetMath()->col(idx_); } -Paragraph const & CursorSlice::paragraph() const +void CursorSlice::forwardPos() { - return text()->getPar(pit_); + // move on one position if possible + if (pos() < lastpos()) { + //lyxerr << "... next pos" << endl; + ++pos(); + return; + } + + // otherwise move on one paragraph if possible + if (pit() < lastpit()) { + //lyxerr << "... next par" << endl; + ++pit(); + pos() = 0; + return; + } + + // otherwise try to move on one cell if possible + if (idx() < lastidx()) { + //lyxerr << "... next idx" << endl; + ++idx(); + pit() = 0; + pos() = 0; + return; + } + BOOST_ASSERT(false); } -pos_type CursorSlice::lastpos() const +void CursorSlice::backwardPos() { - BOOST_ASSERT(inset_); - return inset_->asInsetMath() ? cell().size() : paragraph().size(); + if (pos() != 0) { + --pos(); + return; + } + + if (pit() != 0) { + --pit(); + pos() = lastpos(); + return; + } + + if (idx() != 0) { + --idx(); + pit() = lastpit(); + pos() = lastpos(); + return; + } + + BOOST_ASSERT(false); } -CursorSlice::row_type CursorSlice::row() const +bool CursorSlice::at_end() const { - BOOST_ASSERT(asInsetMath()); - return asInsetMath()->row(idx_); + return idx() == lastidx() && pit() == lastpit() && pos() == lastpos(); } -CursorSlice::col_type CursorSlice::col() const +bool CursorSlice::at_begin() const { - BOOST_ASSERT(asInsetMath()); - return asInsetMath()->col(idx_); + return idx() == 0 && pit() == 0 && pos() == 0; }