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 "paragraph.h"
21 #include "mathed/math_inset.h"
22 #include "mathed/math_data.h"
24 #include "insets/updatableinset.h"
27 #include <boost/assert.hpp>
32 CursorSlice::CursorSlice()
33 : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
37 CursorSlice::CursorSlice(InsetBase * p)
38 : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
40 ///BOOST_ASSERT(inset_);
44 void CursorSlice::idx(idx_type idx)
50 size_t CursorSlice::nargs() const
52 return inset_->nargs();
56 size_t CursorSlice::nrows() const
58 return inset_->nrows();
62 size_t CursorSlice::ncols() const
64 return inset_->ncols();
68 CursorSlice::idx_type CursorSlice::idx() const
74 CursorSlice::idx_type & CursorSlice::idx()
80 void CursorSlice::par(par_type par)
86 CursorSlice::par_type CursorSlice::par() const
92 CursorSlice::par_type & CursorSlice::par()
98 void CursorSlice::pos(pos_type pos)
104 CursorSlice::pos_type CursorSlice::pos() const
110 CursorSlice::pos_type & CursorSlice::pos()
116 CursorSlice::pos_type CursorSlice::lastpos() const
118 return (inset_ && inset_->asMathInset()) ? cell().size() : paragraph().size();
122 void CursorSlice::boundary(bool boundary)
124 boundary_ = boundary;
128 bool CursorSlice::boundary() const
134 CursorSlice::row_type CursorSlice::row() const
136 BOOST_ASSERT(asMathInset());
137 return asMathInset()->row(idx_);
141 CursorSlice::col_type CursorSlice::col() const
143 BOOST_ASSERT(asMathInset());
144 return asMathInset()->col(idx_);
148 MathInset * CursorSlice::asMathInset() const
150 return inset_ ? inset_->asMathInset() : 0;
154 UpdatableInset * CursorSlice::asUpdatableInset() const
156 return inset_ ? inset_->asUpdatableInset() : 0;
160 MathArray & CursorSlice::cell() const
162 BOOST_ASSERT(asMathInset());
163 return asMathInset()->cell(idx_);
167 void CursorSlice::getScreenPos(int & x, int & y) const
169 BOOST_ASSERT(asMathInset());
170 asMathInset()->getScreenPos(idx_, pos_, x, y);
174 LyXText * CursorSlice::text() const
176 return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
180 Paragraph & CursorSlice::paragraph()
182 // access to the main lyx text must be handled in the cursor
183 BOOST_ASSERT(text());
184 return *text()->getPar(par_);
188 Paragraph const & CursorSlice::paragraph() const
190 // access to the main lyx text must be handled in the cursor
191 BOOST_ASSERT(text());
192 return *text()->getPar(par_);
196 bool operator==(CursorSlice const & p, CursorSlice const & q)
198 return p.inset_ == q.inset_
205 bool operator!=(CursorSlice const & p, CursorSlice const & q)
207 return p.inset_ != q.inset_
214 bool operator<(CursorSlice const & p, CursorSlice const & q)
216 if (p.inset_ != q.inset_) {
217 lyxerr << "can't compare cursor and anchor in different insets\n"
218 << "p: " << p << '\n' << "q: " << q << endl;
221 if (p.idx_ != q.idx_)
222 return p.idx_ < q.idx_;
223 if (p.par_ != q.par_)
224 return p.par_ < q.par_;
225 return p.pos_ < q.pos_;
229 bool operator>(CursorSlice const & p, CursorSlice const & q)
235 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
237 os << "inset: " << item.inset_
238 // << " text: " << item.text()
239 << " idx: " << item.idx_
240 << " par: " << item.par_
241 << " pos: " << item.pos_
242 // << " x: " << item.inset_->x()
243 // << " y: " << item.inset_->y()