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"
19 #include "Paragraph.h"
21 #include "support/debug.h"
23 #include "insets/Inset.h"
25 #include "mathed/InsetMath.h"
26 #include "mathed/MathData.h"
28 #include <boost/assert.hpp>
37 CursorSlice::CursorSlice()
38 : inset_(0), idx_(0), pit_(0), pos_(0)
42 CursorSlice::CursorSlice(Inset & p)
43 : inset_(&p), idx_(0), pit_(0), pos_(0)
49 MathData & CursorSlice::cell() const
51 return inset_->asInsetMath()->cell(idx_);
55 Paragraph & CursorSlice::paragraph() const
57 return text()->getPar(pit_);
61 pos_type CursorSlice::lastpos() const
64 return inset_->asInsetMath() ? cell().size()
65 : (text()->empty() ? 0 : paragraph().size());
69 pit_type CursorSlice::lastpit() const
71 if (inset().inMathed())
73 return text()->paragraphs().size() - 1;
77 CursorSlice::row_type CursorSlice::row() const
79 BOOST_ASSERT(asInsetMath());
80 return asInsetMath()->row(idx_);
84 CursorSlice::col_type CursorSlice::col() const
86 BOOST_ASSERT(asInsetMath());
87 return asInsetMath()->col(idx_);
91 void CursorSlice::forwardPos()
93 // move on one position if possible
94 if (pos() < lastpos()) {
95 //lyxerr << "... next pos" << endl;
100 // otherwise move on one paragraph if possible
101 if (pit() < lastpit()) {
102 //lyxerr << "... next par" << endl;
108 // otherwise move on one cell
109 //lyxerr << "... next idx" << endl;
111 BOOST_ASSERT(idx() < nargs());
119 void CursorSlice::forwardIdx()
121 BOOST_ASSERT(idx() < nargs());
129 void CursorSlice::backwardPos()
153 bool CursorSlice::at_end() const
155 return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
159 bool CursorSlice::at_begin() const
161 return idx() == 0 && pit() == 0 && pos() == 0;
165 bool operator==(CursorSlice const & p, CursorSlice const & q)
167 return &p.inset() == &q.inset()
168 && p.idx() == q.idx()
169 && p.pit() == q.pit()
170 && p.pos() == q.pos();
174 bool operator!=(CursorSlice const & p, CursorSlice const & q)
176 return &p.inset() != &q.inset()
177 || p.idx() != q.idx()
178 || p.pit() != q.pit()
179 || p.pos() != q.pos();
183 bool operator<(CursorSlice const & p, CursorSlice const & q)
185 if (&p.inset() != &q.inset()) {
186 LYXERR0("can't compare cursor and anchor in different insets\n"
187 << "p: " << p << '\n' << "q: " << q);
190 if (p.idx() != q.idx())
191 return p.idx() < q.idx();
192 if (p.pit() != q.pit())
193 return p.pit() < q.pit();
194 return p.pos() < q.pos();
198 bool operator>(CursorSlice const & p, CursorSlice const & q)
204 bool operator<=(CursorSlice const & p, CursorSlice const & q)
210 ostream & operator<<(ostream & os, CursorSlice const & item)
213 << "inset: " << (void *)&item.inset()
214 // << " text: " << item.text()
215 << " idx: " << item.idx()
216 << " par: " << item.pit()
217 << " pos: " << item.pos()
218 // << " x: " << item.inset().x()
219 // << " y: " << item.inset().y()