]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
7094014d89c1cd6cc1a50256ca38ab9f313bc13f
[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 Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "cursor_slice.h"
17 #include "debug.h"
18
19 #include "mathed/math_inset.h"
20 #include "insets/updatableinset.h"
21
22 #include <boost/assert.hpp>
23
24 using std::endl;
25
26
27 CursorSlice::CursorSlice()
28         : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
29 {}
30
31
32 CursorSlice::CursorSlice(InsetBase * p)
33         : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
34 {
35         ///BOOST_ASSERT(inset_);
36 }
37
38
39 void CursorSlice::idx(idx_type idx)
40 {
41         idx_ = idx;
42 }
43
44
45 CursorSlice::idx_type CursorSlice::idx() const
46 {
47         return idx_;
48 }
49
50
51 void CursorSlice::par(par_type par)
52 {
53         par_ = par;
54 }
55
56
57 CursorSlice::par_type CursorSlice::par() const
58 {
59         return par_;
60 }
61
62
63 void CursorSlice::pos(pos_type pos)
64 {
65         pos_ = pos;
66 }
67
68
69 CursorSlice::pos_type CursorSlice::pos() const
70 {
71         return pos_;
72 }
73
74
75 void CursorSlice::boundary(bool boundary)
76 {
77         boundary_ = boundary;
78 }
79
80
81 bool CursorSlice::boundary() const
82 {
83         return boundary_;
84 }
85
86
87 MathInset * CursorSlice::asMathInset() const
88 {
89         return static_cast<MathInset *>(const_cast<InsetBase *>(inset_));
90 }
91
92
93 UpdatableInset * CursorSlice::asUpdatableInset() const
94 {
95         return static_cast<UpdatableInset *>(const_cast<InsetBase *>(inset_));
96 }
97
98
99 MathArray & CursorSlice::cell(CursorSlice::idx_type idx) const
100 {
101         BOOST_ASSERT(inset_);
102         BOOST_ASSERT(asMathInset());
103         return asMathInset()->cell(idx);
104 }
105
106
107 MathArray & CursorSlice::cell() const
108 {
109         BOOST_ASSERT(inset_);
110         return asMathInset()->cell(idx_);
111 }
112
113
114 void CursorSlice::getPos(int & x, int & y) const
115 {
116         BOOST_ASSERT(inset_);
117         asMathInset()->getPos(idx_, pos_, x, y);
118 }
119
120
121 void CursorSlice::setPos(int pos)
122 {
123         pos_ = pos;
124 }
125
126
127 LyXText * CursorSlice::text() const
128 {
129         return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
130 }
131
132
133 bool operator==(CursorSlice const & p, CursorSlice const & q)
134 {
135         return p.inset_ == q.inset_
136                && p.idx_ == q.idx_
137                && p.par_ == q.par_
138                && p.pos_ == q.pos_;
139 }
140
141
142 bool operator!=(CursorSlice const & p, CursorSlice const & q)
143 {
144         return p.inset_ != q.inset_
145                || p.idx_ != q.idx_
146                || p.par_ != q.par_
147                || p.pos_ != q.pos_;
148 }
149
150
151 bool operator<(CursorSlice const & p, CursorSlice const & q)
152 {
153         if (p.inset_ != q.inset_) {
154                 lyxerr << "can't compare cursor and anchor in different insets\n"
155                        << "p: " << p << '\n' << "q: " << q << endl;
156                 return true;
157         }
158         if (p.idx_ != q.idx_)
159                 return p.idx_ < q.idx_;
160         if (p.par_ != q.par_)
161                 return p.par_ < q.par_;
162         return p.pos_ < q.pos_;
163 }
164
165
166 bool operator>(CursorSlice const & p, CursorSlice const & q)
167 {
168         return q < p;
169 }
170
171
172 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
173 {
174         os << " inset: " << item.inset_
175            << " text: " << item.text()
176            << " idx: " << item.idx_
177            << " par: " << item.par_
178            << " pos: " << item.pos_
179 //         << " x: " << item.inset_->x()
180 //         << " y: " << item.inset_->y()
181 ;
182         return os;
183 }