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>
36 CursorSlice::CursorSlice()
37 : inset_(0), idx_(0), pit_(0), pos_(0)
41 CursorSlice::CursorSlice(Inset & p)
42 : inset_(&p), idx_(0), pit_(0), pos_(0)
48 MathData & CursorSlice::cell() const
50 return inset_->asInsetMath()->cell(idx_);
54 Paragraph & CursorSlice::paragraph() const
56 return text()->getPar(pit_);
60 pos_type CursorSlice::lastpos() const
63 return inset_->asInsetMath() ? cell().size() : paragraph().size();
67 pit_type CursorSlice::lastpit() const
69 if (inset().inMathed())
71 return text()->paragraphs().size() - 1;
75 CursorSlice::row_type CursorSlice::row() const
77 BOOST_ASSERT(asInsetMath());
78 return asInsetMath()->row(idx_);
82 CursorSlice::col_type CursorSlice::col() const
84 BOOST_ASSERT(asInsetMath());
85 return asInsetMath()->col(idx_);
89 void CursorSlice::forwardPos()
91 // move on one position if possible
92 if (pos() < lastpos()) {
93 //lyxerr << "... next pos" << endl;
98 // otherwise move on one paragraph if possible
99 if (pit() < lastpit()) {
100 //lyxerr << "... next par" << endl;
106 // otherwise move on one cell
107 //lyxerr << "... next idx" << endl;
109 BOOST_ASSERT(idx() < nargs());
117 void CursorSlice::forwardIdx()
119 BOOST_ASSERT(idx() < nargs());
127 void CursorSlice::backwardPos()
151 bool CursorSlice::at_end() const
153 return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
157 bool CursorSlice::at_begin() const
159 return idx() == 0 && pit() == 0 && pos() == 0;
163 bool operator==(CursorSlice const & p, CursorSlice const & q)
165 return &p.inset() == &q.inset()
166 && p.idx() == q.idx()
167 && p.pit() == q.pit()
168 && p.pos() == q.pos();
172 bool operator!=(CursorSlice const & p, CursorSlice const & q)
174 return &p.inset() != &q.inset()
175 || p.idx() != q.idx()
176 || p.pit() != q.pit()
177 || p.pos() != q.pos();
181 bool operator<(CursorSlice const & p, CursorSlice const & q)
183 if (&p.inset() != &q.inset()) {
184 LYXERR0("can't compare cursor and anchor in different insets\n"
185 << "p: " << p << '\n' << "q: " << q);
188 if (p.idx() != q.idx())
189 return p.idx() < q.idx();
190 if (p.pit() != q.pit())
191 return p.pit() < q.pit();
192 return p.pos() < q.pos();
196 bool operator>(CursorSlice const & p, CursorSlice const & q)
202 bool operator<=(CursorSlice const & p, CursorSlice const & q)
208 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
211 << "inset: " << (void *)&item.inset()
212 // << " text: " << item.text()
213 << " idx: " << item.idx()
214 << " par: " << item.pit()
215 << " pos: " << item.pos()
216 // << " x: " << item.inset().x()
217 // << " y: " << item.inset().y()