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 inset_ ? inset_->asMathInset() : 0;
96 UpdatableInset * CursorSlice::asUpdatableInset() const
98 return inset_ ? inset_->asUpdatableInset() : 0;
102 void CursorSlice::cell(CursorSlice::idx_type idx) const
104 BOOST_ASSERT(asMathInset());
105 asMathInset()->cell(idx);
109 MathArray & CursorSlice::cell() const
111 BOOST_ASSERT(asMathInset());
112 return asMathInset()->cell(idx_);
116 void CursorSlice::getScreenPos(int & x, int & y) const
118 BOOST_ASSERT(asMathInset());
119 asMathInset()->getScreenPos(idx_, pos_, x, y);
123 LyXText * CursorSlice::text() const
125 return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
129 bool operator==(CursorSlice const & p, CursorSlice const & q)
131 return p.inset_ == q.inset_
138 bool operator!=(CursorSlice const & p, CursorSlice const & q)
140 return p.inset_ != q.inset_
147 bool operator<(CursorSlice const & p, CursorSlice const & q)
149 if (p.inset_ != q.inset_) {
150 lyxerr << "can't compare cursor and anchor in different insets\n"
151 << "p: " << p << '\n' << "q: " << q << endl;
154 if (p.idx_ != q.idx_)
155 return p.idx_ < q.idx_;
156 if (p.par_ != q.par_)
157 return p.par_ < q.par_;
158 return p.pos_ < q.pos_;
162 bool operator>(CursorSlice const & p, CursorSlice const & q)
168 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
170 os << " inset: " << item.inset_
171 << " text: " << item.text()
172 << " idx: " << item.idx_
173 << " par: " << item.par_
174 << " pos: " << item.pos_
175 // << " x: " << item.inset_->x()
176 // << " y: " << item.inset_->y()
184 void increment(CursorBase & it)
186 CursorSlice & top = it.back();
187 MathArray & ar = top.asMathInset()->cell(top.idx_);
189 // move into the current inset if possible
190 // it is impossible for pos() == size()!
192 if (top.pos_ != ar.size())
193 n = (ar.begin() + top.pos_)->nucleus();
194 if (n && n->isActive()) {
195 it.push_back(CursorSlice(n));
199 // otherwise move on one cell back if possible
200 if (top.pos_ < ar.size()) {
201 // pos() == size() is valid!
206 // otherwise try to move on one cell if possible
207 while (top.idx_ + 1 < top.asMathInset()->nargs()) {
208 // idx() == nargs() is _not_ valid!
210 if (top.asMathInset()->validCell(top.idx_)) {
216 // otherwise leave array, move on one back
217 // this might yield pos() == size(), but that's a ok.
219 // it certainly invalidates top
224 CursorBase ibegin(InsetBase * p)
227 it.push_back(CursorSlice(p));
232 CursorBase iend(InsetBase * p)
235 it.push_back(CursorSlice(p));
236 CursorSlice & top = it.back();
237 top.idx_ = top.asMathInset()->nargs() - 1;
238 top.pos_ = top.asMathInset()->cell(top.idx_).size();