]> git.lyx.org Git - features.git/blob - src/cursor_slice.C
Part of IU.
[features.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 CursorSlice::row_type CursorSlice::row() const
135 {
136         BOOST_ASSERT(asMathInset());
137         return asMathInset()->row(idx_);
138 }
139
140
141 CursorSlice::col_type CursorSlice::col() const
142 {
143         BOOST_ASSERT(asMathInset());
144         return asMathInset()->col(idx_);
145 }
146
147
148 MathInset * CursorSlice::asMathInset() const
149 {
150         return inset_ ? inset_->asMathInset() : 0;
151 }
152
153
154 UpdatableInset * CursorSlice::asUpdatableInset() const
155 {
156         return inset_ ? inset_->asUpdatableInset() : 0;
157 }
158
159
160 MathArray & CursorSlice::cell() const
161 {
162         BOOST_ASSERT(asMathInset());
163         return asMathInset()->cell(idx_);
164 }
165
166
167 void CursorSlice::getScreenPos(int & x, int & y) const
168 {
169         BOOST_ASSERT(asMathInset());
170         asMathInset()->getScreenPos(idx_, pos_, x, y);
171 }
172
173
174 LyXText * CursorSlice::text() const
175 {
176         return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
177 }
178
179
180 Paragraph & CursorSlice::paragraph()
181 {
182         // access to the main lyx text must be handled in the cursor
183         BOOST_ASSERT(text());
184         return *text()->getPar(par_);
185 }
186
187
188 Paragraph const & CursorSlice::paragraph() const
189 {
190         // access to the main lyx text must be handled in the cursor
191         BOOST_ASSERT(text());
192         return *text()->getPar(par_);
193 }
194
195
196 bool operator==(CursorSlice const & p, CursorSlice const & q)
197 {
198         return p.inset_ == q.inset_
199                && p.idx_ == q.idx_
200                && p.par_ == q.par_
201                && p.pos_ == q.pos_;
202 }
203
204
205 bool operator!=(CursorSlice const & p, CursorSlice const & q)
206 {
207         return p.inset_ != q.inset_
208                || p.idx_ != q.idx_
209                || p.par_ != q.par_
210                || p.pos_ != q.pos_;
211 }
212
213
214 bool operator<(CursorSlice const & p, CursorSlice const & q)
215 {
216         if (p.inset_ != q.inset_) {
217                 lyxerr << "can't compare cursor and anchor in different insets\n"
218                        << "p: " << p << '\n' << "q: " << q << endl;
219                 return true;
220         }
221         if (p.idx_ != q.idx_)
222                 return p.idx_ < q.idx_;
223         if (p.par_ != q.par_)
224                 return p.par_ < q.par_;
225         return p.pos_ < q.pos_;
226 }
227
228
229 bool operator>(CursorSlice const & p, CursorSlice const & q)
230 {
231         return q < p;
232 }
233
234
235 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
236 {
237         os << "inset: " << item.inset_
238 //         << " text: " << item.text()
239            << " idx: " << item.idx_
240            << " par: " << item.par_
241            << " pos: " << item.pos_
242 //         << " x: " << item.inset_->x()
243 //         << " y: " << item.inset_->y()
244 ;
245         return os;
246 }