2 * \file CursorSlice.cpp
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 "CursorSlice.h"
20 #include "Paragraph.h"
22 #include "insets/Inset.h"
24 #include "mathed/InsetMath.h"
25 #include "mathed/MathData.h"
27 #include <boost/assert.hpp>
35 CursorSlice::CursorSlice()
36 : inset_(0), idx_(0), pit_(0), pos_(0)
40 CursorSlice::CursorSlice(Inset & p)
41 : inset_(&p), idx_(0), pit_(0), pos_(0)
47 MathData & CursorSlice::cell() const
49 return inset_->asInsetMath()->cell(idx_);
53 Paragraph & CursorSlice::paragraph() const
55 return text()->getPar(pit_);
59 pos_type CursorSlice::lastpos() const
62 return inset_->asInsetMath() ? cell().size() : paragraph().size();
66 pit_type CursorSlice::lastpit() const
68 if (inset().inMathed())
70 return text()->paragraphs().size() - 1;
74 CursorSlice::row_type CursorSlice::row() const
76 BOOST_ASSERT(asInsetMath());
77 return asInsetMath()->row(idx_);
81 CursorSlice::col_type CursorSlice::col() const
83 BOOST_ASSERT(asInsetMath());
84 return asInsetMath()->col(idx_);
88 void CursorSlice::forwardPos()
90 // move on one position if possible
91 if (pos() < lastpos()) {
92 //lyxerr << "... next pos" << endl;
97 // otherwise move on one paragraph if possible
98 if (pit() < lastpit()) {
99 //lyxerr << "... next par" << endl;
105 // otherwise move on one cell
106 //lyxerr << "... next idx" << endl;
113 void CursorSlice::backwardPos()
137 bool CursorSlice::at_end() const
139 return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
143 bool CursorSlice::at_begin() const
145 return idx() == 0 && pit() == 0 && pos() == 0;
149 bool operator==(CursorSlice const & p, CursorSlice const & q)
151 return &p.inset() == &q.inset()
152 && p.idx() == q.idx()
153 && p.pit() == q.pit()
154 && p.pos() == q.pos();
158 bool operator!=(CursorSlice const & p, CursorSlice const & q)
160 return &p.inset() != &q.inset()
161 || p.idx() != q.idx()
162 || p.pit() != q.pit()
163 || p.pos() != q.pos();
167 bool operator<(CursorSlice const & p, CursorSlice const & q)
169 if (&p.inset() != &q.inset()) {
170 lyxerr << "can't compare cursor and anchor in different insets\n"
171 << "p: " << p << '\n' << "q: " << q << endl;
174 if (p.idx() != q.idx())
175 return p.idx() < q.idx();
176 if (p.pit() != q.pit())
177 return p.pit() < q.pit();
178 return p.pos() < q.pos();
182 bool operator>(CursorSlice const & p, CursorSlice const & q)
188 bool operator<=(CursorSlice const & p, CursorSlice const & q)
194 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
197 << "inset: " << &item.inset()
198 // << " text: " << item.text()
199 << " idx: " << item.idx()
200 << " par: " << item.pit()
201 << " pos: " << item.pos()
202 // << " x: " << item.inset().x()
203 // << " y: " << item.inset().y()