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 bool & CursorSlice::boundary()
140 CursorSlice::row_type CursorSlice::row() const
142 BOOST_ASSERT(asMathInset());
143 return asMathInset()->row(idx_);
147 CursorSlice::col_type CursorSlice::col() const
149 BOOST_ASSERT(asMathInset());
150 return asMathInset()->col(idx_);
154 MathInset * CursorSlice::asMathInset() const
156 return inset_ ? inset_->asMathInset() : 0;
160 UpdatableInset * CursorSlice::asUpdatableInset() const
162 return inset_ ? inset_->asUpdatableInset() : 0;
166 MathArray & CursorSlice::cell() const
168 BOOST_ASSERT(asMathInset());
169 return asMathInset()->cell(idx_);
173 LyXText * CursorSlice::text() const
175 return inset_ ? inset_->getText(idx_) : 0;
179 Paragraph & CursorSlice::paragraph()
181 // access to the main lyx text must be handled in the cursor
182 BOOST_ASSERT(text());
183 return *text()->getPar(par_);
187 Paragraph const & CursorSlice::paragraph() const
189 // access to the main lyx text must be handled in the cursor
190 BOOST_ASSERT(text());
191 return *text()->getPar(par_);
195 bool operator==(CursorSlice const & p, CursorSlice const & q)
197 return p.inset_ == q.inset_
204 bool operator!=(CursorSlice const & p, CursorSlice const & q)
206 return p.inset_ != q.inset_
213 bool operator<(CursorSlice const & p, CursorSlice const & q)
215 if (p.inset_ != q.inset_) {
216 lyxerr << "can't compare cursor and anchor in different insets\n"
217 << "p: " << p << '\n' << "q: " << q << endl;
220 if (p.idx_ != q.idx_)
221 return p.idx_ < q.idx_;
222 if (p.par_ != q.par_)
223 return p.par_ < q.par_;
224 return p.pos_ < q.pos_;
228 bool operator>(CursorSlice const & p, CursorSlice const & q)
234 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
236 os << "inset: " << item.inset_
237 << " text: " << item.text()
238 << " idx: " << item.idx_
239 << " par: " << item.par_
240 << " pos: " << item.pos_
241 // << " x: " << item.inset_->x()
242 // << " y: " << item.inset_->y()