]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
prevent crash in cursorX when row cache is empty
[lyx.git] / src / cursor_slice.C
1 /**
2  * \file cursor_slice.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "cursor_slice.h"
14 #include "debug.h"
15
16 #include "mathed/math_inset.h"
17 #include "insets/updatableinset.h"
18
19 #include <boost/assert.hpp>
20
21 using std::endl;
22
23
24 CursorSlice::CursorSlice()
25         : inset_(0), idx_(0), par_(0), pos_(0)
26 {}
27
28
29 CursorSlice::CursorSlice(InsetBase * p)
30         : inset_(p), idx_(0), par_(0), pos_(0)
31 {
32         ///BOOST_ASSERT(inset_);
33 }
34
35
36 MathInset * CursorSlice::asMathInset() const
37 {
38         return static_cast<MathInset *>(const_cast<InsetBase *>(inset_));
39 }
40
41
42 UpdatableInset * CursorSlice::asUpdatableInset() const
43 {
44         return static_cast<UpdatableInset *>(const_cast<InsetBase *>(inset_));
45 }
46
47
48 MathArray & CursorSlice::cell(CursorSlice::idx_type idx) const
49 {
50         BOOST_ASSERT(inset_);
51         return asMathInset()->cell(idx);
52 }
53
54
55 MathArray & CursorSlice::cell() const
56 {
57         BOOST_ASSERT(inset_);
58         return asMathInset()->cell(idx_);
59 }
60
61
62 void CursorSlice::getPos(int & x, int & y) const
63 {
64         asMathInset()->getPos(idx_, pos_, x, y);
65 }
66
67
68 void CursorSlice::setPos(int pos)
69 {
70         pos_ = pos;
71 }
72
73
74 LyXText * CursorSlice::text() const
75 {
76         return asUpdatableInset()->getText(idx_);
77 }
78
79
80 bool operator==(CursorSlice const & p, CursorSlice const & q)
81 {
82         return p.inset_ == q.inset_
83                && p.idx_ == q.idx_
84                && p.par_ == q.par_
85                && p.pos_ == q.pos_;
86 }
87
88
89 bool operator!=(CursorSlice const & p, CursorSlice const & q)
90 {
91         return p.inset_ != q.inset_
92                || p.idx_ != q.idx_
93                || p.par_ != q.par_
94                || p.pos_ != q.pos_;
95 }
96
97
98 bool operator<(CursorSlice const & p, CursorSlice const & q)
99 {
100         if (p.inset_ != q.inset_) {
101                 lyxerr << "can't compare cursor and anchor in different insets\n"
102                        << "p: " << p << '\n' << "q: " << q << endl;
103                 return true;
104         }
105         if (p.idx_ != q.idx_)
106                 return p.idx_ < q.idx_;
107         if (p.par_ != q.par_)
108                 return p.par_ < q.par_;
109         return p.pos_ < q.pos_;
110 }
111
112
113 //std::ostream & operator<<(std::ostream & os, CursorSlice const & p)
114 //{
115 //      os << "(par: " << p.inset_ << " idx: " << p.idx_ << " pos: " << p.pos_ << ')';
116 //      return os;
117 //}
118
119
120 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
121 {
122         os << " inset: " << item.inset_
123 //         << " text: " << item.text()
124            << " idx: " << item.idx_
125 //         << " par: " << item.par_
126 //         << " pos: " << item.pos_
127 //         << " x: " << item.inset_->x()
128 //         << " y: " << item.inset_->y()
129 ;
130         return os;
131 }
132
133