]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
allow one past-the-end position in CursorSlice, as it eases iteration
[lyx.git] / src / CursorSlice.cpp
1 /**
2  * \file CursorSlice.cpp
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 "CursorSlice.h"
17
18 #include "debug.h"
19 #include "Text.h"
20 #include "Paragraph.h"
21
22 #include "insets/Inset.h"
23
24 #include "mathed/InsetMath.h"
25 #include "mathed/MathData.h"
26
27 #include <boost/assert.hpp>
28
29 using std::endl;
30
31
32 namespace lyx {
33
34
35 CursorSlice::CursorSlice()
36         : inset_(0), idx_(0), pit_(0), pos_(0)
37 {}
38
39
40 CursorSlice::CursorSlice(Inset & p)
41         : inset_(&p), idx_(0), pit_(0), pos_(0)
42 {
43         BOOST_ASSERT(inset_);
44 }
45
46
47 MathData & CursorSlice::cell() const
48 {
49         return inset_->asInsetMath()->cell(idx_);
50 }
51
52
53 Paragraph & CursorSlice::paragraph() const
54 {
55         return text()->getPar(pit_);
56 }
57
58
59 pos_type CursorSlice::lastpos() const
60 {
61         BOOST_ASSERT(inset_);
62         return inset_->asInsetMath() ? cell().size() : paragraph().size();
63 }
64
65
66 pit_type CursorSlice::lastpit() const
67 {
68         if (inset().inMathed())
69                 return 0;
70         return text()->paragraphs().size() - 1;
71 }
72
73
74 CursorSlice::row_type CursorSlice::row() const
75 {
76         BOOST_ASSERT(asInsetMath());
77         return asInsetMath()->row(idx_);
78 }
79
80
81 CursorSlice::col_type CursorSlice::col() const
82 {
83         BOOST_ASSERT(asInsetMath());
84         return asInsetMath()->col(idx_);
85 }
86
87
88 void CursorSlice::forwardPos()
89 {
90         //  move on one position if possible
91         if (pos() < lastpos()) {
92                 //lyxerr << "... next pos" << endl;
93                 ++pos();
94                 return;
95         }
96
97         // otherwise move on one paragraph if possible
98         if (pit() < lastpit()) {
99                 //lyxerr << "... next par" << endl;
100                 ++pit();
101                 pos() = 0;
102                 return;
103         }
104
105         // otherwise move on one cell
106         //lyxerr << "... next idx" << endl;
107         ++idx();
108         pit() = 0;
109         pos() = 0;
110 }
111
112
113 void CursorSlice::backwardPos()
114 {
115         if (pos() != 0) {
116                 --pos();
117                 return;
118         }
119
120         if (pit() != 0) {
121                 --pit();
122                 pos() = lastpos();
123                 return;
124         }
125
126         if (idx() != 0) {
127                 --idx();
128                 pit() = lastpit();
129                 pos() = lastpos();
130                 return;
131         }
132
133         BOOST_ASSERT(false);
134 }
135
136
137 bool CursorSlice::at_end() const 
138 {
139         return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
140 }
141
142
143 bool CursorSlice::at_begin() const
144 {
145         return idx() == 0 && pit() == 0 && pos() == 0;
146 }
147
148
149 bool operator==(CursorSlice const & p, CursorSlice const & q)
150 {
151         return &p.inset() == &q.inset()
152                && p.idx() == q.idx()
153                && p.pit() == q.pit()
154                && p.pos() == q.pos();
155 }
156
157
158 bool operator!=(CursorSlice const & p, CursorSlice const & q)
159 {
160         return &p.inset() != &q.inset()
161                || p.idx() != q.idx()
162                || p.pit() != q.pit()
163                || p.pos() != q.pos();
164 }
165
166
167 bool operator<(CursorSlice const & p, CursorSlice const & q)
168 {
169         if (&p.inset() != &q.inset()) {
170                 lyxerr << "can't compare cursor and anchor in different insets\n"
171                        << "p: " << p << '\n' << "q: " << q << endl;
172                 BOOST_ASSERT(false);
173         }
174         if (p.idx() != q.idx())
175                 return p.idx() < q.idx();
176         if (p.pit() != q.pit())
177                 return p.pit() < q.pit();
178         return p.pos() < q.pos();
179 }
180
181
182 bool operator>(CursorSlice const & p, CursorSlice const & q)
183 {
184         return q < p;
185 }
186
187
188 bool operator<=(CursorSlice const & p, CursorSlice const & q)
189 {
190         return !(q < p);
191 }
192
193
194 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
195 {
196         return os
197            << "inset: " << &item.inset()
198 //         << " text: " << item.text()
199            << " idx: " << item.idx()
200            << " par: " << item.pit()
201            << " pos: " << item.pos()
202 //         << " x: " << item.inset().x()
203 //         << " y: " << item.inset().y()
204 ;
205 }
206
207
208 } // namespace lyx