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