]> git.lyx.org Git - lyx.git/blobdiff - src/CursorSlice.cpp
allow one past-the-end position in CursorSlice, as it eases iteration
[lyx.git] / src / CursorSlice.cpp
index 2b1164e99d69f963e54e92621d77f24a93b5526a..42c26145898231c242cb0340551fc0a7dff543ae 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,87 +41,108 @@ CursorSlice::CursorSlice(Inset & p)
        : inset_(&p), idx_(0), pit_(0), pos_(0)
 {
        BOOST_ASSERT(inset_);
-       boost::signal<void()> * 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() const
 {
-       inset_connection_.disconnect();
+       return text()->getPar(pit_);
 }
 
 
-CursorSlice & CursorSlice::operator=(CursorSlice const & cs)
+pos_type CursorSlice::lastpos() 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;
+       BOOST_ASSERT(inset_);
+       return inset_->asInsetMath() ? cell().size() : paragraph().size();
 }
 
 
-void CursorSlice::invalidate()
+pit_type CursorSlice::lastpit() const
 {
-       inset_ = 0;
+       if (inset().inMathed())
+               return 0;
+       return text()->paragraphs().size() - 1;
 }
 
 
-bool CursorSlice::isValid() const
+CursorSlice::row_type CursorSlice::row() const
 {
-       return inset_ != 0;
+       BOOST_ASSERT(asInsetMath());
+       return asInsetMath()->row(idx_);
 }
 
 
-MathData & CursorSlice::cell() const
+CursorSlice::col_type CursorSlice::col() const
 {
-       return inset_->asInsetMath()->cell(idx_);
+       BOOST_ASSERT(asInsetMath());
+       return asInsetMath()->col(idx_);
 }
 
 
-Paragraph & CursorSlice::paragraph()
+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;
+       }
 
-Paragraph const & CursorSlice::paragraph() const
-{
-       return text()->getPar(pit_);
+       // otherwise move on one cell
+       //lyxerr << "... next idx" << endl;
+       ++idx();
+       pit() = 0;
+       pos() = 0;
 }
 
 
-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;
 }