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