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/MathMacro.h"
28 #include "support/ExceptionMessage.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
39 CursorSlice::CursorSlice()
40 : inset_(0), idx_(0), pit_(0), pos_(0)
44 CursorSlice::CursorSlice(Inset & p)
45 : inset_(&p), idx_(0), pit_(0), pos_(0)
51 MathData & CursorSlice::cell() const
53 return inset_->asInsetMath()->cell(idx_);
57 Paragraph & CursorSlice::paragraph() const
59 return text()->getPar(pit_);
63 pos_type CursorSlice::lastpos() const
66 InsetMath const * math = inset_->asInsetMath();
67 bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs();
68 return math ? (paramless_macro ? 0 : cell().size())
69 : (text()->empty() ? 0 : paragraph().size());
73 pit_type CursorSlice::lastpit() const
75 if (inset_->inMathed())
77 return text()->paragraphs().size() - 1;
81 CursorSlice::row_type CursorSlice::row() const
83 LASSERT(inset_, return 0);
84 return inset_->row(idx_);
88 CursorSlice::col_type CursorSlice::col() const
90 LASSERT(inset_, return 0);
91 return inset_->col(idx_);
95 void CursorSlice::forwardPos()
97 // move on one position if possible
98 if (pos_ < lastpos()) {
99 //lyxerr << "... next pos" << endl;
104 // otherwise move on one paragraph if possible
105 if (pit_ < lastpit()) {
106 //lyxerr << "... next par" << endl;
112 // otherwise move on one cell
113 //lyxerr << "... next idx" << endl;
115 LASSERT(idx_ < nargs(), return);
123 void CursorSlice::forwardIdx()
125 LASSERT(idx_ < nargs(), return);
133 void CursorSlice::backwardPos()
157 bool CursorSlice::at_cell_end() const
159 return pit_ == lastpit() && pos_ == lastpos();
163 bool CursorSlice::at_cell_begin() const
165 return pit_ == 0 && pos_ == 0;
169 bool CursorSlice::at_end() const
171 return idx_ == lastidx() && at_cell_end();
175 bool CursorSlice::at_begin() const
177 return idx_ == 0 && at_cell_begin();
181 bool operator==(CursorSlice const & p, CursorSlice const & q)
183 return p.inset_ == q.inset_
190 bool operator!=(CursorSlice const & p, CursorSlice const & q)
192 return p.inset_ != q.inset_
199 bool operator<(CursorSlice const & p, CursorSlice const & q)
201 if (p.inset_ != q.inset_) {
202 LYXERR0("can't compare cursor and anchor in different insets\n"
203 << "p: " << p << '\n' << "q: " << q);
204 // It should be safe to continue, just registering the error.
205 LASSERT(false, return false);
207 if (p.idx_ != q.idx_)
208 return p.idx_ < q.idx_;
209 if (p.pit_ != q.pit_)
210 return p.pit_ < q.pit_;
211 return p.pos_ < q.pos_;
215 bool operator>(CursorSlice const & p, CursorSlice const & q)
221 bool operator<=(CursorSlice const & p, CursorSlice const & q)
227 ostream & operator<<(ostream & os, CursorSlice const & item)
230 << "inset: " << (void *)item.inset_
231 // << " text: " << item.text()
232 << " idx: " << item.idx_
233 << " par: " << item.pit_
234 << " pos: " << item.pos_
235 // << " x: " << item.inset_->x()
236 // << " y: " << item.inset_->y()