]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
f4e6fb43a99fa3d01bb90ca4495965d94c4bbc4c
[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 "mathed/math_data.h"
21
22 #include "insets/updatableinset.h"
23
24
25 #include <boost/assert.hpp>
26
27 using std::endl;
28
29
30 CursorSlice::CursorSlice()
31         : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
32 {}
33
34
35 CursorSlice::CursorSlice(InsetBase * p)
36         : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
37 {
38         ///BOOST_ASSERT(inset_);
39 }
40
41
42 void CursorSlice::idx(idx_type idx)
43 {
44         idx_ = idx;
45 }
46
47
48 size_t CursorSlice::nargs() const
49 {
50         return inset_->nargs();
51 }
52
53
54 size_t CursorSlice::ncols() const
55 {
56         return inset_->ncols();
57 }
58
59
60 CursorSlice::idx_type CursorSlice::idx() const
61 {
62         return idx_;
63 }
64
65
66 CursorSlice::idx_type & CursorSlice::idx()
67 {
68         return idx_;
69 }
70
71
72 void CursorSlice::par(par_type par)
73 {
74         par_ = par;
75 }
76
77
78 CursorSlice::par_type CursorSlice::par() const
79 {
80         return par_;
81 }
82
83
84 CursorSlice::par_type & CursorSlice::par()
85 {
86         return par_;
87 }
88
89
90 void CursorSlice::pos(pos_type pos)
91 {
92         pos_ = pos;
93 }
94
95
96 CursorSlice::pos_type CursorSlice::pos() const
97 {
98         return pos_;
99 }
100
101
102 CursorSlice::pos_type & CursorSlice::pos()
103 {
104         return pos_;
105 }
106
107
108 CursorSlice::pos_type CursorSlice::lastpos() const
109 {
110         BOOST_ASSERT(inset_);
111 #warning implement me for texted, too.
112         return inset_->asMathInset() ? cell().size() : 0;
113 }
114
115
116 void CursorSlice::boundary(bool boundary)
117 {
118         boundary_ = boundary;
119 }
120
121
122 bool CursorSlice::boundary() const
123 {
124         return boundary_;
125 }
126
127
128 CursorSlice::row_type CursorSlice::row() const
129 {
130         BOOST_ASSERT(asMathInset());
131         return asMathInset()->row(idx_);
132 }
133
134
135 CursorSlice::col_type CursorSlice::col() const
136 {
137         BOOST_ASSERT(asMathInset());
138         return asMathInset()->col(idx_);
139 }
140
141
142 MathInset * CursorSlice::asMathInset() const
143 {
144         return inset_ ? inset_->asMathInset() : 0;
145 }
146
147
148 UpdatableInset * CursorSlice::asUpdatableInset() const
149 {
150         return inset_ ? inset_->asUpdatableInset() : 0;
151 }
152
153
154 MathArray & CursorSlice::cell() const
155 {
156         BOOST_ASSERT(asMathInset());
157         return asMathInset()->cell(idx_);
158 }
159
160
161 void CursorSlice::getScreenPos(int & x, int & y) const
162 {
163         BOOST_ASSERT(asMathInset());
164         asMathInset()->getScreenPos(idx_, pos_, x, y);
165 }
166
167
168 LyXText * CursorSlice::text() const
169 {
170         return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
171 }
172
173
174 bool operator==(CursorSlice const & p, CursorSlice const & q)
175 {
176         return p.inset_ == q.inset_
177                && p.idx_ == q.idx_
178                && p.par_ == q.par_
179                && p.pos_ == q.pos_;
180 }
181
182
183 bool operator!=(CursorSlice const & p, CursorSlice const & q)
184 {
185         return p.inset_ != q.inset_
186                || p.idx_ != q.idx_
187                || p.par_ != q.par_
188                || p.pos_ != q.pos_;
189 }
190
191
192 bool operator<(CursorSlice const & p, CursorSlice const & q)
193 {
194         if (p.inset_ != q.inset_) {
195                 lyxerr << "can't compare cursor and anchor in different insets\n"
196                        << "p: " << p << '\n' << "q: " << q << endl;
197                 return true;
198         }
199         if (p.idx_ != q.idx_)
200                 return p.idx_ < q.idx_;
201         if (p.par_ != q.par_)
202                 return p.par_ < q.par_;
203         return p.pos_ < q.pos_;
204 }
205
206
207 bool operator>(CursorSlice const & p, CursorSlice const & q)
208 {
209         return q < p;
210 }
211
212
213 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
214 {
215         os << " inset: " << item.inset_
216            << " text: " << item.text()
217            << " idx: " << item.idx_
218            << " par: " << item.par_
219            << " pos: " << item.pos_
220 //         << " x: " << item.inset_->x()
221 //         << " y: " << item.inset_->y()
222 ;
223         return os;
224 }
225
226
227
228
229 void increment(CursorBase & it)
230 {
231         CursorSlice & top = it.back();
232         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
233
234         // move into the current inset if possible
235         // it is impossible for pos() == size()!
236         MathInset * n = 0;
237         if (top.pos_ != ar.size())
238                 n = (ar.begin() + top.pos_)->nucleus();
239         if (n && n->isActive()) {
240                 it.push_back(CursorSlice(n));
241                 return;
242         }
243
244         // otherwise move on one cell back if possible
245         if (top.pos_ < ar.size()) {
246                 // pos() == size() is valid!
247                 ++top.pos_;
248                 return;
249         }
250
251         // otherwise try to move on one cell if possible
252         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
253                 // idx() == nargs() is _not_ valid!
254                 ++top.idx_;
255                 if (top.asMathInset()->validCell(top.idx_)) {
256                         top.pos_ = 0;
257                         return;
258                 }
259         }
260
261         // otherwise leave array, move on one back
262         // this might yield pos() == size(), but that's a ok.
263         it.pop_back();
264         // it certainly invalidates top
265         ++it.back().pos_;
266 }
267
268
269 CursorBase ibegin(InsetBase * p)
270 {
271         CursorBase it;
272         it.push_back(CursorSlice(p));
273         return it;
274 }
275
276
277 CursorBase iend(InsetBase * p)
278 {
279         CursorBase it;
280         it.push_back(CursorSlice(p));
281         CursorSlice & top = it.back();
282         top.idx_ = top.asMathInset()->nargs() - 1;
283         top.pos_ = top.asMathInset()->cell(top.idx_).size();
284         return it;
285 }