]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
20a0331c8c7383679e6872c95c537645ce74780f
[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 #include "lyxtext.h"
19 #include "paragraph.h"
20
21 #include "mathed/math_inset.h"
22 #include "mathed/math_data.h"
23
24 #include "insets/updatableinset.h"
25
26
27 #include <boost/assert.hpp>
28
29 using std::endl;
30
31
32 CursorSlice::CursorSlice()
33         : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
34 {}
35
36
37 CursorSlice::CursorSlice(InsetBase * p)
38         : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
39 {
40         ///BOOST_ASSERT(inset_);
41 }
42
43
44 size_t CursorSlice::nargs() const
45 {
46         return inset_->nargs();
47 }
48
49
50 size_t CursorSlice::nrows() const
51 {
52         return inset_->nrows();
53 }
54
55
56 size_t CursorSlice::ncols() const
57 {
58         return inset_->ncols();
59 }
60
61
62 CursorSlice::idx_type CursorSlice::idx() const
63 {
64         return idx_;
65 }
66
67
68 CursorSlice::idx_type & CursorSlice::idx()
69 {
70         return idx_;
71 }
72
73
74 CursorSlice::par_type CursorSlice::par() const
75 {
76         return par_;
77 }
78
79
80 CursorSlice::par_type & CursorSlice::par()
81 {
82         return par_;
83 }
84
85
86 CursorSlice::pos_type CursorSlice::pos() const
87 {
88         return pos_;
89 }
90
91
92 CursorSlice::pos_type & CursorSlice::pos()
93 {
94         return pos_;
95 }
96
97
98 CursorSlice::pos_type CursorSlice::lastpos() const
99 {
100         return (inset_ && inset_->asMathInset()) ? cell().size() : paragraph().size();
101 }
102
103
104 bool CursorSlice::boundary() const
105 {
106         return boundary_;
107 }
108
109
110 bool & CursorSlice::boundary()
111 {
112         return boundary_;
113 }
114
115
116 CursorSlice::row_type CursorSlice::row() const
117 {
118         BOOST_ASSERT(asMathInset());
119         return asMathInset()->row(idx_);
120 }
121
122
123 CursorSlice::col_type CursorSlice::col() const
124 {
125         BOOST_ASSERT(asMathInset());
126         return asMathInset()->col(idx_);
127 }
128
129
130 MathInset * CursorSlice::asMathInset() const
131 {
132         return inset_ ? inset_->asMathInset() : 0;
133 }
134
135
136 UpdatableInset * CursorSlice::asUpdatableInset() const
137 {
138         return inset_ ? inset_->asUpdatableInset() : 0;
139 }
140
141
142 MathArray & CursorSlice::cell() const
143 {
144         BOOST_ASSERT(asMathInset());
145         return asMathInset()->cell(idx_);
146 }
147
148
149 LyXText * CursorSlice::text() const
150 {
151         return inset_ ? inset_->getText(idx_) : 0;
152 }
153
154
155 Paragraph & CursorSlice::paragraph()
156 {
157         // access to the main lyx text must be handled in the cursor
158         BOOST_ASSERT(text());
159         return *text()->getPar(par_);
160 }
161
162
163 Paragraph const & CursorSlice::paragraph() const
164 {
165         // access to the main lyx text must be handled in the cursor
166         BOOST_ASSERT(text());
167         return *text()->getPar(par_);
168 }
169
170
171 bool operator==(CursorSlice const & p, CursorSlice const & q)
172 {
173         return p.inset_ == q.inset_
174                && p.idx_ == q.idx_
175                && p.par_ == q.par_
176                && p.pos_ == q.pos_;
177 }
178
179
180 bool operator!=(CursorSlice const & p, CursorSlice const & q)
181 {
182         return p.inset_ != q.inset_
183                || p.idx_ != q.idx_
184                || p.par_ != q.par_
185                || p.pos_ != q.pos_;
186 }
187
188
189 bool operator<(CursorSlice const & p, CursorSlice const & q)
190 {
191         if (p.inset_ != q.inset_) {
192                 lyxerr << "can't compare cursor and anchor in different insets\n"
193                        << "p: " << p << '\n' << "q: " << q << endl;
194                 return true;
195         }
196         if (p.idx_ != q.idx_)
197                 return p.idx_ < q.idx_;
198         if (p.par_ != q.par_)
199                 return p.par_ < q.par_;
200         return p.pos_ < q.pos_;
201 }
202
203
204 bool operator>(CursorSlice const & p, CursorSlice const & q)
205 {
206         return q < p;
207 }
208
209
210 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
211 {
212         os
213 //         << "inset: " << item.inset_
214 //         << " text: " << item.text()
215            << " idx: " << item.idx_
216            << " par: " << item.par_
217            << " pos: " << item.pos_
218 //         << " x: " << item.inset_->x()
219 //         << " y: " << item.inset_->y()
220 ;
221         return os;
222 }