]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
some renaming + safety stuff
[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 CursorSlice::idx_type CursorSlice::idx() const
49 {
50         return idx_;
51 }
52
53
54 void CursorSlice::par(par_type par)
55 {
56         par_ = par;
57 }
58
59
60 CursorSlice::par_type CursorSlice::par() const
61 {
62         return par_;
63 }
64
65
66 void CursorSlice::pos(pos_type pos)
67 {
68         pos_ = pos;
69 }
70
71
72 CursorSlice::pos_type CursorSlice::pos() const
73 {
74         return pos_;
75 }
76
77
78 void CursorSlice::boundary(bool boundary)
79 {
80         boundary_ = boundary;
81 }
82
83
84 bool CursorSlice::boundary() const
85 {
86         return boundary_;
87 }
88
89
90 MathInset * CursorSlice::asMathInset() const
91 {
92         return inset_ ? inset_->asMathInset() : 0;
93 }
94
95
96 UpdatableInset * CursorSlice::asUpdatableInset() const
97 {
98         return inset_ ? inset_->asUpdatableInset() : 0;
99 }
100
101
102 void CursorSlice::cell(CursorSlice::idx_type idx) const
103 {
104         BOOST_ASSERT(asMathInset());
105         asMathInset()->cell(idx);
106 }
107
108
109 MathArray & CursorSlice::cell() const
110 {
111         BOOST_ASSERT(asMathInset());
112         return asMathInset()->cell(idx_);
113 }
114
115
116 void CursorSlice::getScreenPos(int & x, int & y) const
117 {
118         BOOST_ASSERT(asMathInset());
119         asMathInset()->getScreenPos(idx_, pos_, x, y);
120 }
121
122
123 LyXText * CursorSlice::text() const
124 {
125         return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
126 }
127
128
129 bool operator==(CursorSlice const & p, CursorSlice const & q)
130 {
131         return p.inset_ == q.inset_
132                && p.idx_ == q.idx_
133                && p.par_ == q.par_
134                && p.pos_ == q.pos_;
135 }
136
137
138 bool operator!=(CursorSlice const & p, CursorSlice const & q)
139 {
140         return p.inset_ != q.inset_
141                || p.idx_ != q.idx_
142                || p.par_ != q.par_
143                || p.pos_ != q.pos_;
144 }
145
146
147 bool operator<(CursorSlice const & p, CursorSlice const & q)
148 {
149         if (p.inset_ != q.inset_) {
150                 lyxerr << "can't compare cursor and anchor in different insets\n"
151                        << "p: " << p << '\n' << "q: " << q << endl;
152                 return true;
153         }
154         if (p.idx_ != q.idx_)
155                 return p.idx_ < q.idx_;
156         if (p.par_ != q.par_)
157                 return p.par_ < q.par_;
158         return p.pos_ < q.pos_;
159 }
160
161
162 bool operator>(CursorSlice const & p, CursorSlice const & q)
163 {
164         return q < p;
165 }
166
167
168 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
169 {
170         os << " inset: " << item.inset_
171            << " text: " << item.text()
172            << " idx: " << item.idx_
173            << " par: " << item.par_
174            << " pos: " << item.pos_
175 //         << " x: " << item.inset_->x()
176 //         << " y: " << item.inset_->y()
177 ;
178         return os;
179 }
180
181
182
183
184 void increment(CursorBase & it)
185 {
186         CursorSlice & top = it.back();
187         MathArray   & ar  = top.asMathInset()->cell(top.idx_);
188
189         // move into the current inset if possible
190         // it is impossible for pos() == size()!
191         MathInset * n = 0;
192         if (top.pos_ != ar.size())
193                 n = (ar.begin() + top.pos_)->nucleus();
194         if (n && n->isActive()) {
195                 it.push_back(CursorSlice(n));
196                 return;
197         }
198
199         // otherwise move on one cell back if possible
200         if (top.pos_ < ar.size()) {
201                 // pos() == size() is valid!
202                 ++top.pos_;
203                 return;
204         }
205
206         // otherwise try to move on one cell if possible
207         while (top.idx_ + 1 < top.asMathInset()->nargs()) {
208                 // idx() == nargs() is _not_ valid!
209                 ++top.idx_;
210                 if (top.asMathInset()->validCell(top.idx_)) {
211                         top.pos_ = 0;
212                         return;
213                 }
214         }
215
216         // otherwise leave array, move on one back
217         // this might yield pos() == size(), but that's a ok.
218         it.pop_back();
219         // it certainly invalidates top
220         ++it.back().pos_;
221 }
222
223
224 CursorBase ibegin(InsetBase * p)
225 {
226         CursorBase it;
227         it.push_back(CursorSlice(p));
228         return it;
229 }
230
231
232 CursorBase iend(InsetBase * p)
233 {
234         CursorBase it;
235         it.push_back(CursorSlice(p));
236         CursorSlice & top = it.back();
237         top.idx_ = top.asMathInset()->nargs() - 1;
238         top.pos_ = top.asMathInset()->cell(top.idx_).size();
239         return it;
240 }