]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
move forwardIdx to CursorSlice from DocIterator, as it was the only one able to trave...
[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
108         BOOST_ASSERT(idx() < nargs());
109
110         ++idx();
111         pit() = 0;
112         pos() = 0;
113 }
114
115
116 void CursorSlice::forwardIdx()
117 {
118         BOOST_ASSERT(idx() < nargs());
119
120         ++idx();
121         pit() = 0;
122         pos() = 0;
123 }
124
125
126 void CursorSlice::backwardPos()
127 {
128         if (pos() != 0) {
129                 --pos();
130                 return;
131         }
132
133         if (pit() != 0) {
134                 --pit();
135                 pos() = lastpos();
136                 return;
137         }
138
139         if (idx() != 0) {
140                 --idx();
141                 pit() = lastpit();
142                 pos() = lastpos();
143                 return;
144         }
145
146         BOOST_ASSERT(false);
147 }
148
149
150 bool CursorSlice::at_end() const 
151 {
152         return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
153 }
154
155
156 bool CursorSlice::at_begin() const
157 {
158         return idx() == 0 && pit() == 0 && pos() == 0;
159 }
160
161
162 bool operator==(CursorSlice const & p, CursorSlice const & q)
163 {
164         return &p.inset() == &q.inset()
165                && p.idx() == q.idx()
166                && p.pit() == q.pit()
167                && p.pos() == q.pos();
168 }
169
170
171 bool operator!=(CursorSlice const & p, CursorSlice const & q)
172 {
173         return &p.inset() != &q.inset()
174                || p.idx() != q.idx()
175                || p.pit() != q.pit()
176                || p.pos() != q.pos();
177 }
178
179
180 bool operator<(CursorSlice const & p, CursorSlice const & q)
181 {
182         if (&p.inset() != &q.inset()) {
183                 lyxerr << "can't compare cursor and anchor in different insets\n"
184                        << "p: " << p << '\n' << "q: " << q << endl;
185                 BOOST_ASSERT(false);
186         }
187         if (p.idx() != q.idx())
188                 return p.idx() < q.idx();
189         if (p.pit() != q.pit())
190                 return p.pit() < q.pit();
191         return p.pos() < q.pos();
192 }
193
194
195 bool operator>(CursorSlice const & p, CursorSlice const & q)
196 {
197         return q < p;
198 }
199
200
201 bool operator<=(CursorSlice const & p, CursorSlice const & q)
202 {
203         return !(q < p);
204 }
205
206
207 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
208 {
209         return os
210            << "inset: " << &item.inset()
211 //         << " text: " << item.text()
212            << " idx: " << item.idx()
213            << " par: " << item.pit()
214            << " pos: " << item.pos()
215 //         << " x: " << item.inset().x()
216 //         << " y: " << item.inset().y()
217 ;
218 }
219
220
221 } // namespace lyx