X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCursorSlice.cpp;h=b4352e3ba4b64976d54448fb9d62bfc33b8dd184;hb=d07f9eec20c8709b648a494586d4b03f75cfef47;hp=f8d72eea131265c0dac2861f49a7f6448bc057fa;hpb=3fdbf5b9c4122b5c9a122978f6374e0b9bcab010;p=lyx.git diff --git a/src/CursorSlice.cpp b/src/CursorSlice.cpp index f8d72eea13..b4352e3ba4 100644 --- a/src/CursorSlice.cpp +++ b/src/CursorSlice.cpp @@ -3,10 +3,10 @@ * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * - * \author Lars Gullik Bjønnes + * \author Lars Gullik Bjønnes * \author Matthias Ettrich - * \author André Pönitz - * \author Jürgen Vigna + * \author André Pönitz + * \author Jürgen Vigna * * Full author contact details are available in file CREDITS. */ @@ -15,22 +15,23 @@ #include "CursorSlice.h" -#include "debug.h" #include "Text.h" #include "Paragraph.h" +#include "support/debug.h" + #include "insets/Inset.h" #include "mathed/InsetMath.h" -#include "mathed/MathData.h" +#include "mathed/MathMacro.h" -#include -#include +#include "support/lassert.h" +#include -namespace lyx { +using namespace std; -using std::endl; +namespace lyx { CursorSlice::CursorSlice() @@ -41,114 +42,158 @@ CursorSlice::CursorSlice() CursorSlice::CursorSlice(Inset & p) : inset_(&p), idx_(0), pit_(0), pos_(0) { - BOOST_ASSERT(inset_); - inset_->destroyed.connect( - boost::bind(&CursorSlice::invalidate, this)); + LASSERT(inset_, /**/); } -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; + LASSERT(inset_, /**/); + InsetMath const * math = inset_->asInsetMath(); + bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs(); + return math ? (paramless_macro ? 0 : cell().size()) + : (text()->empty() ? 0 : 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_); + LASSERT(asInsetMath(), /**/); + return asInsetMath()->row(idx_); } -Paragraph & CursorSlice::paragraph() +CursorSlice::col_type CursorSlice::col() const { - return text()->getPar(pit_); + LASSERT(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; + + LASSERT(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(); + LASSERT(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; + } + + LASSERT(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; } bool operator==(CursorSlice const & p, CursorSlice const & q) { - return &p.inset() == &q.inset() - && p.idx() == q.idx() - && p.pit() == q.pit() - && p.pos() == q.pos(); + return p.inset_ == q.inset_ + && p.idx_ == q.idx_ + && p.pit_ == q.pit_ + && p.pos_ == q.pos_; } bool operator!=(CursorSlice const & p, CursorSlice const & q) { - return &p.inset() != &q.inset() - || p.idx() != q.idx() - || p.pit() != q.pit() - || p.pos() != q.pos(); + return p.inset_ != q.inset_ + || p.idx_ != q.idx_ + || p.pit_ != q.pit_ + || p.pos_ != q.pos_; } bool operator<(CursorSlice const & p, CursorSlice const & q) { - if (&p.inset() != &q.inset()) { - lyxerr << "can't compare cursor and anchor in different insets\n" - << "p: " << p << '\n' << "q: " << q << endl; - BOOST_ASSERT(false); + if (p.inset_ != q.inset_) { + LYXERR0("can't compare cursor and anchor in different insets\n" + << "p: " << p << '\n' << "q: " << q); + LASSERT(false, /**/); } - if (p.idx() != q.idx()) - return p.idx() < q.idx(); - if (p.pit() != q.pit()) - return p.pit() < q.pit(); - return p.pos() < q.pos(); + if (p.idx_ != q.idx_) + return p.idx_ < q.idx_; + if (p.pit_ != q.pit_) + return p.pit_ < q.pit_; + return p.pos_ < q.pos_; } @@ -164,16 +209,16 @@ bool operator<=(CursorSlice const & p, CursorSlice const & q) } -std::ostream & operator<<(std::ostream & os, CursorSlice const & item) +ostream & operator<<(ostream & os, CursorSlice const & item) { return os - << "inset: " << &item.inset() + << "inset: " << (void *)item.inset_ // << " text: " << item.text() - << " idx: " << item.idx() - << " par: " << item.pit() - << " pos: " << item.pos() -// << " x: " << item.inset().x() -// << " y: " << item.inset().y() + << " idx: " << item.idx_ + << " par: " << item.pit_ + << " pos: " << item.pos_ +// << " x: " << item.inset_->x() +// << " y: " << item.inset_->y() ; }