]> git.lyx.org Git - lyx.git/blobdiff - src/CursorSlice.cpp
adjust
[lyx.git] / src / CursorSlice.cpp
index 8b64734403d012e090548e0fa35171e173001b6f..3d6e8794b42fbf9b30ca2e571234bd6ead42db1f 100644 (file)
 #include <config.h>
 
 #include "CursorSlice.h"
+
 #include "debug.h"
 #include "Text.h"
 #include "Paragraph.h"
 
+#include "insets/Inset.h"
+
 #include "mathed/InsetMath.h"
 #include "mathed/MathData.h"
 
 #include <boost/assert.hpp>
 
+using std::endl;
+
 
 namespace lyx {
 
-using std::endl;
-
 
 CursorSlice::CursorSlice()
        : inset_(0), idx_(0), pit_(0), pos_(0)
@@ -47,22 +50,24 @@ MathData & CursorSlice::cell() const
 }
 
 
-Paragraph & CursorSlice::paragraph()
+Paragraph & CursorSlice::paragraph() const
 {
        return text()->getPar(pit_);
 }
 
 
-Paragraph const & CursorSlice::paragraph() const
+pos_type CursorSlice::lastpos() const
 {
-       return text()->getPar(pit_);
+       BOOST_ASSERT(inset_);
+       return inset_->asInsetMath() ? cell().size() : paragraph().size();
 }
 
 
-pos_type CursorSlice::lastpos() const
+pit_type CursorSlice::lastpit() const
 {
-       BOOST_ASSERT(inset_);
-       return inset_->asInsetMath() ? cell().size() : paragraph().size();
+       if (inset().inMathed())
+               return 0;
+       return text()->paragraphs().size() - 1;
 }
 
 
@@ -80,9 +85,83 @@ CursorSlice::col_type CursorSlice::col() const
 }
 
 
+void CursorSlice::forwardPos()
+{
+       //  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;
+}
+
+
+void CursorSlice::forwardIdx()
+{
+       BOOST_ASSERT(idx() < nargs());
+
+       ++idx();
+       pit() = 0;
+       pos() = 0;
+}
+
+
+void CursorSlice::backwardPos()
+{
+       if (pos() != 0) {
+               --pos();
+               return;
+       }
+
+       if (pit() != 0) {
+               --pit();
+               pos() = lastpos();
+               return;
+       }
+
+       if (idx() != 0) {
+               --idx();
+               pit() = lastpit();
+               pos() = lastpos();
+               return;
+       }
+
+       BOOST_ASSERT(false);
+}
+
+
+bool CursorSlice::at_end() const 
+{
+       return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
+}
+
+
+bool CursorSlice::at_begin() const
+{
+       return idx() == 0 && pit() == 0 && pos() == 0;
+}
+
+
 bool operator==(CursorSlice const & p, CursorSlice const & q)
 {
-       return p.inset_ == q.inset_
+       return &p.inset() == &q.inset()
               && p.idx() == q.idx()
               && p.pit() == q.pit()
               && p.pos() == q.pos();
@@ -91,7 +170,7 @@ bool operator==(CursorSlice const & p, CursorSlice const & q)
 
 bool operator!=(CursorSlice const & p, CursorSlice const & q)
 {
-       return p.inset_ != q.inset_
+       return &p.inset() != &q.inset()
               || p.idx() != q.idx()
               || p.pit() != q.pit()
               || p.pos() != q.pos();