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: This should only ever be called from an InsetMath.
84 // Should we crash in release mode, though, or try to continue?
85 LASSERT(asInsetMath(), /**/);
86 return asInsetMath()->row(idx_);
90 CursorSlice::col_type CursorSlice::col() const
92 // LASSERT: This should only ever be called from an InsetMath.
93 // Should we crash in release mode, though, or try to continue?
94 LASSERT(asInsetMath(), /**/);
95 return asInsetMath()->col(idx_);
99 void CursorSlice::forwardPos()
101 // move on one position if possible
102 if (pos_ < lastpos()) {
103 //lyxerr << "... next pos" << endl;
108 // otherwise move on one paragraph if possible
109 if (pit_ < lastpit()) {
110 //lyxerr << "... next par" << endl;
116 // otherwise move on one cell
117 //lyxerr << "... next idx" << endl;
119 LASSERT(idx_ < nargs(), return);
127 void CursorSlice::forwardIdx()
129 LASSERT(idx_ < nargs(), return);
137 void CursorSlice::backwardPos()
161 bool CursorSlice::at_end() const
163 return idx_ == lastidx() && pit_ == lastpit() && pos_ == lastpos();
167 bool CursorSlice::at_begin() const
169 return idx_ == 0 && pit_ == 0 && pos_ == 0;
173 bool operator==(CursorSlice const & p, CursorSlice const & q)
175 return p.inset_ == q.inset_
182 bool operator!=(CursorSlice const & p, CursorSlice const & q)
184 return p.inset_ != q.inset_
191 bool operator<(CursorSlice const & p, CursorSlice const & q)
193 if (p.inset_ != q.inset_) {
194 LYXERR0("can't compare cursor and anchor in different insets\n"
195 << "p: " << p << '\n' << "q: " << q);
196 // It should be safe to continue, just registering the error.
197 LASSERT(false, return false);
199 if (p.idx_ != q.idx_)
200 return p.idx_ < q.idx_;
201 if (p.pit_ != q.pit_)
202 return p.pit_ < q.pit_;
203 return p.pos_ < q.pos_;
207 bool operator>(CursorSlice const & p, CursorSlice const & q)
213 bool operator<=(CursorSlice const & p, CursorSlice const & q)
219 ostream & operator<<(ostream & os, CursorSlice const & item)
222 << "inset: " << (void *)item.inset_
223 // << " text: " << item.text()
224 << " idx: " << item.idx_
225 << " par: " << item.pit_
226 << " pos: " << item.pos_
227 // << " x: " << item.inset_->x()
228 // << " y: " << item.inset_->y()