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 size_t CursorSlice::nargs() const
50 return inset_->nargs();
54 size_t CursorSlice::ncols() const
56 return inset_->ncols();
60 CursorSlice::idx_type CursorSlice::idx() const
66 CursorSlice::idx_type & CursorSlice::idx()
72 void CursorSlice::par(par_type par)
78 CursorSlice::par_type CursorSlice::par() const
84 CursorSlice::par_type & CursorSlice::par()
90 void CursorSlice::pos(pos_type pos)
96 CursorSlice::pos_type CursorSlice::pos() const
102 CursorSlice::pos_type & CursorSlice::pos()
108 CursorSlice::pos_type CursorSlice::lastpos() const
110 BOOST_ASSERT(inset_);
111 #warning implement me for texted, too.
112 return inset_->asMathInset() ? cell().size() : 0;
116 void CursorSlice::boundary(bool boundary)
118 boundary_ = boundary;
122 bool CursorSlice::boundary() const
128 CursorSlice::row_type CursorSlice::row() const
130 BOOST_ASSERT(asMathInset());
131 return asMathInset()->row(idx_);
135 CursorSlice::col_type CursorSlice::col() const
137 BOOST_ASSERT(asMathInset());
138 return asMathInset()->col(idx_);
142 MathInset * CursorSlice::asMathInset() const
144 return inset_ ? inset_->asMathInset() : 0;
148 UpdatableInset * CursorSlice::asUpdatableInset() const
150 return inset_ ? inset_->asUpdatableInset() : 0;
154 MathArray & CursorSlice::cell() const
156 BOOST_ASSERT(asMathInset());
157 return asMathInset()->cell(idx_);
161 void CursorSlice::getScreenPos(int & x, int & y) const
163 BOOST_ASSERT(asMathInset());
164 asMathInset()->getScreenPos(idx_, pos_, x, y);
168 LyXText * CursorSlice::text() const
170 return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
174 bool operator==(CursorSlice const & p, CursorSlice const & q)
176 return p.inset_ == q.inset_
183 bool operator!=(CursorSlice const & p, CursorSlice const & q)
185 return p.inset_ != q.inset_
192 bool operator<(CursorSlice const & p, CursorSlice const & q)
194 if (p.inset_ != q.inset_) {
195 lyxerr << "can't compare cursor and anchor in different insets\n"
196 << "p: " << p << '\n' << "q: " << q << endl;
199 if (p.idx_ != q.idx_)
200 return p.idx_ < q.idx_;
201 if (p.par_ != q.par_)
202 return p.par_ < q.par_;
203 return p.pos_ < q.pos_;
207 bool operator>(CursorSlice const & p, CursorSlice const & q)
213 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
215 os << " inset: " << item.inset_
216 << " text: " << item.text()
217 << " idx: " << item.idx_
218 << " par: " << item.par_
219 << " pos: " << item.pos_
220 // << " x: " << item.inset_->x()
221 // << " y: " << item.inset_->y()
229 void increment(CursorBase & it)
231 CursorSlice & top = it.back();
232 MathArray & ar = top.asMathInset()->cell(top.idx_);
234 // move into the current inset if possible
235 // it is impossible for pos() == size()!
237 if (top.pos_ != ar.size())
238 n = (ar.begin() + top.pos_)->nucleus();
239 if (n && n->isActive()) {
240 it.push_back(CursorSlice(n));
244 // otherwise move on one cell back if possible
245 if (top.pos_ < ar.size()) {
246 // pos() == size() is valid!
251 // otherwise try to move on one cell if possible
252 while (top.idx_ + 1 < top.asMathInset()->nargs()) {
253 // idx() == nargs() is _not_ valid!
255 if (top.asMathInset()->validCell(top.idx_)) {
261 // otherwise leave array, move on one back
262 // this might yield pos() == size(), but that's a ok.
264 // it certainly invalidates top
269 CursorBase ibegin(InsetBase * p)
272 it.push_back(CursorSlice(p));
277 CursorBase iend(InsetBase * p)
280 it.push_back(CursorSlice(p));
281 CursorSlice & top = it.back();
282 top.idx_ = top.asMathInset()->nargs() - 1;
283 top.pos_ = top.asMathInset()->cell(top.idx_).size();