X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fcursor_slice.C;h=f891844e196547906fa8ac0d54d414290325c0ab;hb=32d281cba0a2e4d0e8425a34a1a8d1f5e7251412;hp=ce9b4605c392a04c5f669785ee775dfaa67b286c;hpb=f7b38dbdc55ed671af3a7f78c931a92b4c431f27;p=lyx.git diff --git a/src/cursor_slice.C b/src/cursor_slice.C index ce9b4605c3..f891844e19 100644 --- a/src/cursor_slice.C +++ b/src/cursor_slice.C @@ -15,6 +15,8 @@ #include "cursor_slice.h" #include "debug.h" +#include "lyxtext.h" +#include "paragraph.h" #include "mathed/math_inset.h" #include "mathed/math_data.h" @@ -32,130 +34,129 @@ CursorSlice::CursorSlice() {} -CursorSlice::CursorSlice(InsetBase * p) - : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false) +CursorSlice::CursorSlice(InsetBase & p) + : inset_(&p), idx_(0), par_(0), pos_(0), boundary_(false) { - ///BOOST_ASSERT(inset_); + BOOST_ASSERT(inset_); } -void CursorSlice::idx(idx_type idx) +size_t CursorSlice::nargs() const { - idx_ = idx; + BOOST_ASSERT(inset_); + return inset_->nargs(); } -CursorSlice::idx_type CursorSlice::idx() const +size_t CursorSlice::nrows() const { - return idx_; + BOOST_ASSERT(inset_); + return inset_->nrows(); } -void CursorSlice::par(par_type par) +size_t CursorSlice::ncols() const { - par_ = par; + BOOST_ASSERT(inset_); + return inset_->ncols(); } -CursorSlice::par_type CursorSlice::par() const +CursorSlice::pos_type CursorSlice::lastpos() const { - return par_; + BOOST_ASSERT(inset_); + return inset_->asMathInset() ? cell().size() : paragraph().size(); } -void CursorSlice::pos(pos_type pos) +CursorSlice::row_type CursorSlice::row() const { - pos_ = pos; -} - - -CursorSlice::pos_type CursorSlice::pos() const -{ - return pos_; -} - - -void CursorSlice::boundary(bool boundary) -{ - boundary_ = boundary; + BOOST_ASSERT(asMathInset()); + return asMathInset()->row(idx_); } -bool CursorSlice::boundary() const +CursorSlice::col_type CursorSlice::col() const { - return boundary_; + BOOST_ASSERT(asMathInset()); + return asMathInset()->col(idx_); } MathInset * CursorSlice::asMathInset() const { - return inset_ ? inset_->asMathInset() : 0; + BOOST_ASSERT(inset_); + return inset_->asMathInset(); } UpdatableInset * CursorSlice::asUpdatableInset() const { - return inset_ ? inset_->asUpdatableInset() : 0; + BOOST_ASSERT(inset_); + return inset_->asUpdatableInset(); } -void CursorSlice::cell(CursorSlice::idx_type idx) const +MathArray & CursorSlice::cell() const { BOOST_ASSERT(asMathInset()); - asMathInset()->cell(idx); + return asMathInset()->cell(idx_); } -MathArray & CursorSlice::cell() const +LyXText * CursorSlice::text() const { - BOOST_ASSERT(asMathInset()); - return asMathInset()->cell(idx_); + BOOST_ASSERT(inset_); + return inset_->getText(idx_); } -void CursorSlice::getScreenPos(int & x, int & y) const +Paragraph & CursorSlice::paragraph() { - BOOST_ASSERT(asMathInset()); - asMathInset()->getScreenPos(idx_, pos_, x, y); + // access to the main lyx text must be handled in the cursor + BOOST_ASSERT(text()); + return text()->getPar(par_); } -LyXText * CursorSlice::text() const +Paragraph const & CursorSlice::paragraph() const { - return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0; + // access to the main lyx text must be handled in the cursor + BOOST_ASSERT(text()); + return text()->getPar(par_); } bool operator==(CursorSlice const & p, CursorSlice const & q) { - return p.inset_ == q.inset_ - && p.idx_ == q.idx_ - && p.par_ == q.par_ - && p.pos_ == q.pos_; + return &p.inset() == &q.inset() + && p.idx() == q.idx() + && p.par() == q.par() + && p.pos() == q.pos(); } bool operator!=(CursorSlice const & p, CursorSlice const & q) { - return p.inset_ != q.inset_ - || p.idx_ != q.idx_ - || p.par_ != q.par_ - || p.pos_ != q.pos_; + return &p.inset() != &q.inset() + || p.idx() != q.idx() + || p.par() != q.par() + || p.pos() != q.pos(); } bool operator<(CursorSlice const & p, CursorSlice const & q) { - if (p.inset_ != q.inset_) { + if (&p.inset() != &q.inset()) { lyxerr << "can't compare cursor and anchor in different insets\n" << "p: " << p << '\n' << "q: " << q << endl; - return true; + BOOST_ASSERT(false); } - if (p.idx_ != q.idx_) - return p.idx_ < q.idx_; - if (p.par_ != q.par_) - return p.par_ < q.par_; - return p.pos_ < q.pos_; + if (p.idx() != q.idx()) + return p.idx() < q.idx(); + if (p.par() != q.par()) + return p.par() < q.par(); + return p.pos() < q.pos(); } @@ -165,76 +166,21 @@ bool operator>(CursorSlice const & p, CursorSlice const & q) } -std::ostream & operator<<(std::ostream & os, CursorSlice const & item) +bool operator<=(CursorSlice const & p, CursorSlice const & q) { - os << " inset: " << item.inset_ - << " text: " << item.text() - << " idx: " << item.idx_ - << " par: " << item.par_ - << " pos: " << item.pos_ -// << " x: " << item.inset_->x() -// << " y: " << item.inset_->y() -; - return os; + return !(q < p); } - - -void increment(CursorBase & it) -{ - CursorSlice & top = it.back(); - MathArray & ar = top.asMathInset()->cell(top.idx_); - - // move into the current inset if possible - // it is impossible for pos() == size()! - MathInset * n = 0; - if (top.pos_ != ar.size()) - n = (ar.begin() + top.pos_)->nucleus(); - if (n && n->isActive()) { - it.push_back(CursorSlice(n)); - return; - } - - // otherwise move on one cell back if possible - if (top.pos_ < ar.size()) { - // pos() == size() is valid! - ++top.pos_; - return; - } - - // otherwise try to move on one cell if possible - while (top.idx_ + 1 < top.asMathInset()->nargs()) { - // idx() == nargs() is _not_ valid! - ++top.idx_; - if (top.asMathInset()->validCell(top.idx_)) { - top.pos_ = 0; - return; - } - } - - // otherwise leave array, move on one back - // this might yield pos() == size(), but that's a ok. - it.pop_back(); - // it certainly invalidates top - ++it.back().pos_; -} - - -CursorBase ibegin(InsetBase * p) -{ - CursorBase it; - it.push_back(CursorSlice(p)); - return it; -} - - -CursorBase iend(InsetBase * p) +std::ostream & operator<<(std::ostream & os, CursorSlice const & item) { - CursorBase it; - it.push_back(CursorSlice(p)); - CursorSlice & top = it.back(); - top.idx_ = top.asMathInset()->nargs() - 1; - top.pos_ = top.asMathInset()->cell(top.idx_).size(); - return it; + return os + << "inset: " << &item.inset() +// << " text: " << item.text() + << " idx: " << item.idx() + << " par: " << item.par() + << " pos: " << item.pos() +// << " x: " << item.inset().x() +// << " y: " << item.inset().y() +; }