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"
20 #include "ParagraphList.h"
22 #include "support/debug.h"
24 #include "mathed/InsetMath.h"
25 #include "mathed/InsetMathMacro.h"
26 #include "mathed/MathData.h"
28 #include "support/lassert.h"
37 CursorSlice::CursorSlice()
38 : inset_(nullptr), 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 InsetMath const * math = inset_->asInsetMath();
65 bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs();
66 return math ? (paramless_macro ? 0 : cell().size())
67 : (text()->empty() ? 0 : paragraph().size());
71 pit_type CursorSlice::lastpit() const
73 if (inset_->inMathed())
75 return text()->paragraphs().size() - 1;
79 row_type CursorSlice::row() const
81 LASSERT(inset_, return 0);
82 return inset_->row(idx_);
86 col_type CursorSlice::col() const
88 LASSERT(inset_, return 0);
89 return inset_->col(idx_);
93 void CursorSlice::setPitPos(pit_type pit, pos_type pos)
95 LASSERT(pit != int(text()->paragraphs().size()), return);
99 // Now some strict checking. None of these should happen, but
100 // we're scaredy-cats
102 LYXERR0("Don't like -1!");
106 if (pos > paragraph().size()) {
107 LYXERR0("Don't like 1, pos: " << pos
108 << " size: " << paragraph().size()
115 void CursorSlice::forwardPos()
117 // move on one position if possible
118 if (pos_ < lastpos()) {
119 //lyxerr << "... next pos" << endl;
124 // otherwise move on one paragraph if possible
125 if (pit_ < lastpit()) {
126 //lyxerr << "... next par" << endl;
132 // otherwise move on one cell
133 //lyxerr << "... next idx" << endl;
135 LASSERT(idx_ < nargs(), return);
143 void CursorSlice::forwardIdx()
145 LASSERT(idx_ < nargs(), return);
153 void CursorSlice::backwardPos()
177 bool CursorSlice::at_cell_end() const
179 return pit_ == lastpit() && pos_ == lastpos();
183 bool CursorSlice::at_cell_begin() const
185 return pit_ == 0 && pos_ == 0;
189 bool CursorSlice::at_end() const
191 return idx_ == lastidx() && at_cell_end();
195 bool CursorSlice::at_begin() const
197 return idx_ == 0 && at_cell_begin();
201 bool operator==(CursorSlice const & p, CursorSlice const & q)
203 return p.inset_ == q.inset_
210 bool operator!=(CursorSlice const & p, CursorSlice const & q)
212 return p.inset_ != q.inset_
219 bool operator<(CursorSlice const & p, CursorSlice const & q)
221 if (p.inset_ != q.inset_) {
222 LYXERR0("can't compare cursor and anchor in different insets\n"
223 << "p: " << p << '\n' << "q: " << q);
224 // It should be safe to continue, just registering the error.
225 LASSERT(false, return false);
227 if (p.idx_ != q.idx_)
228 return p.idx_ < q.idx_;
229 if (p.pit_ != q.pit_)
230 return p.pit_ < q.pit_;
231 return p.pos_ < q.pos_;
235 bool operator>(CursorSlice const & p, CursorSlice const & q)
241 bool operator<=(CursorSlice const & p, CursorSlice const & q)
247 ostream & operator<<(ostream & os, CursorSlice const & item)
250 << "inset: " << (void *)item.inset_
251 // << " text: " << item.text()
252 << " idx: " << item.idx_
253 << " par: " << item.pit_
254 << " pos: " << item.pos_
255 // << " x: " << item.inset_->x()
256 // << " y: " << item.inset_->y()