]> git.lyx.org Git - lyx.git/blobdiff - src/CursorSlice.cpp
adjust
[lyx.git] / src / CursorSlice.cpp
index f8d72eea131265c0dac2861f49a7f6448bc057fa..3d6e8794b42fbf9b30ca2e571234bd6ead42db1f 100644 (file)
 #include "mathed/MathData.h"
 
 #include <boost/assert.hpp>
-#include <boost/bind.hpp>
 
+using std::endl;
 
-namespace lyx {
 
-using std::endl;
+namespace lyx {
 
 
 CursorSlice::CursorSlice()
@@ -42,80 +41,121 @@ CursorSlice::CursorSlice(Inset & p)
        : inset_(&p), idx_(0), pit_(0), pos_(0)
 {
        BOOST_ASSERT(inset_);
-       inset_->destroyed.connect(
-                       boost::bind(&CursorSlice::invalidate, this));
 }
 
 
-CursorSlice::CursorSlice(CursorSlice const & cs)
+MathData & CursorSlice::cell() const
 {
-       operator=(cs);
+       return inset_->asInsetMath()->cell(idx_);
 }
 
 
-CursorSlice & CursorSlice::operator=(CursorSlice const & cs)
+Paragraph & CursorSlice::paragraph() const
 {
-       inset_ = cs.inset_;
-       idx_ = cs.idx_;
-       pit_ = cs.pit_;
-       pos_ = cs.pos_;
-       if (inset_) {
-               BOOST_ASSERT(inset_);
-               inset_->destroyed.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 move on one cell
+       //lyxerr << "... next idx" << endl;
+
+       BOOST_ASSERT(idx() < nargs());
+
+       ++idx();
+       pit() = 0;
+       pos() = 0;
 }
 
 
-pos_type CursorSlice::lastpos() const
+void CursorSlice::forwardIdx()
 {
-       BOOST_ASSERT(inset_);
-       return inset_->asInsetMath() ? cell().size() : paragraph().size();
+       BOOST_ASSERT(idx() < nargs());
+
+       ++idx();
+       pit() = 0;
+       pos() = 0;
 }
 
 
-CursorSlice::row_type CursorSlice::row() const
+void CursorSlice::backwardPos()
 {
-       BOOST_ASSERT(asInsetMath());
-       return asInsetMath()->row(idx_);
+       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::col_type CursorSlice::col() const
+bool CursorSlice::at_end() const 
 {
-       BOOST_ASSERT(asInsetMath());
-       return asInsetMath()->col(idx_);
+       return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
+}
+
+
+bool CursorSlice::at_begin() const
+{
+       return idx() == 0 && pit() == 0 && pos() == 0;
 }