3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
11 * Full author contact details are available in file CREDITS.
16 #include "cursor_slice.h"
19 #include "mathed/math_inset.h"
20 #include "mathed/math_data.h"
22 #include "insets/updatableinset.h"
25 #include <boost/assert.hpp>
30 CursorSlice::CursorSlice()
31 : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
35 CursorSlice::CursorSlice(InsetBase * p)
36 : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
38 ///BOOST_ASSERT(inset_);
42 void CursorSlice::idx(idx_type idx)
48 CursorSlice::idx_type CursorSlice::idx() const
54 void CursorSlice::par(par_type par)
60 CursorSlice::par_type CursorSlice::par() const
66 void CursorSlice::pos(pos_type pos)
72 CursorSlice::pos_type CursorSlice::pos() const
78 void CursorSlice::boundary(bool boundary)
84 bool CursorSlice::boundary() const
90 MathInset * CursorSlice::asMathInset() const
92 return static_cast<MathInset *>(const_cast<InsetBase *>(inset_));
96 UpdatableInset * CursorSlice::asUpdatableInset() const
98 return static_cast<UpdatableInset *>(const_cast<InsetBase *>(inset_));
102 MathArray & CursorSlice::cell(CursorSlice::idx_type idx) const
104 BOOST_ASSERT(inset_);
105 BOOST_ASSERT(asMathInset());
106 return asMathInset()->cell(idx);
110 MathArray & CursorSlice::cell() const
112 BOOST_ASSERT(inset_);
113 return asMathInset()->cell(idx_);
117 void CursorSlice::getPos(int & x, int & y) const
119 BOOST_ASSERT(inset_);
120 asMathInset()->getPos(idx_, pos_, x, y);
124 void CursorSlice::setPos(int pos)
130 LyXText * CursorSlice::text() const
132 return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
136 bool operator==(CursorSlice const & p, CursorSlice const & q)
138 return p.inset_ == q.inset_
145 bool operator!=(CursorSlice const & p, CursorSlice const & q)
147 return p.inset_ != q.inset_
154 bool operator<(CursorSlice const & p, CursorSlice const & q)
156 if (p.inset_ != q.inset_) {
157 lyxerr << "can't compare cursor and anchor in different insets\n"
158 << "p: " << p << '\n' << "q: " << q << endl;
161 if (p.idx_ != q.idx_)
162 return p.idx_ < q.idx_;
163 if (p.par_ != q.par_)
164 return p.par_ < q.par_;
165 return p.pos_ < q.pos_;
169 bool operator>(CursorSlice const & p, CursorSlice const & q)
175 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
177 os << " inset: " << item.inset_
178 << " text: " << item.text()
179 << " idx: " << item.idx_
180 << " par: " << item.par_
181 << " pos: " << item.pos_
182 // << " x: " << item.inset_->x()
183 // << " y: " << item.inset_->y()
191 void increment(CursorBase & it)
193 CursorSlice & top = it.back();
194 MathArray & ar = top.asMathInset()->cell(top.idx_);
196 // move into the current inset if possible
197 // it is impossible for pos() == size()!
199 if (top.pos_ != ar.size())
200 n = (ar.begin() + top.pos_)->nucleus();
201 if (n && n->isActive()) {
202 it.push_back(CursorSlice(n));
206 // otherwise move on one cell back if possible
207 if (top.pos_ < ar.size()) {
208 // pos() == size() is valid!
213 // otherwise try to move on one cell if possible
214 while (top.idx_ + 1 < top.asMathInset()->nargs()) {
215 // idx() == nargs() is _not_ valid!
217 if (top.asMathInset()->validCell(top.idx_)) {
223 // otherwise leave array, move on one back
224 // this might yield pos() == size(), but that's a ok.
226 // it certainly invalidates top
231 CursorBase ibegin(InsetBase * p)
234 it.push_back(CursorSlice(p));
239 CursorBase iend(InsetBase * p)
242 it.push_back(CursorSlice(p));
243 CursorSlice & top = it.back();
244 top.idx_ = top.asMathInset()->nargs() - 1;
245 top.pos_ = top.asMathInset()->cell(top.idx_).size();