]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
* DocIterator.h (forwardPosNoDescend): remove method
[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()
54 {
55         return text()->getPar(pit_);
56 }
57
58
59 Paragraph const & CursorSlice::paragraph() const
60 {
61         return text()->getPar(pit_);
62 }
63
64
65 pos_type CursorSlice::lastpos() const
66 {
67         BOOST_ASSERT(inset_);
68         return inset_->asInsetMath() ? cell().size() : paragraph().size();
69 }
70
71
72 pit_type CursorSlice::lastpit() const
73 {
74         if (inset().inMathed())
75                 return 0;
76         return text()->paragraphs().size() - 1;
77 }
78
79
80 CursorSlice::row_type CursorSlice::row() const
81 {
82         BOOST_ASSERT(asInsetMath());
83         return asInsetMath()->row(idx_);
84 }
85
86
87 CursorSlice::col_type CursorSlice::col() const
88 {
89         BOOST_ASSERT(asInsetMath());
90         return asInsetMath()->col(idx_);
91 }
92
93
94 void CursorSlice::forwardPos()
95 {
96         //  move on one position if possible
97         if (pos() < lastpos()) {
98                 //lyxerr << "... next pos" << endl;
99                 ++pos();
100                 return;
101         }
102
103         // otherwise move on one paragraph if possible
104         if (pit() < lastpit()) {
105                 //lyxerr << "... next par" << endl;
106                 ++pit();
107                 pos() = 0;
108                 return;
109         }
110
111         // otherwise try to move on one cell if possible
112         if (idx() < lastidx()) {
113                 //lyxerr << "... next idx" << endl;
114                 ++idx();
115                 pit() = 0;
116                 pos() = 0;
117                 return;
118         }
119         BOOST_ASSERT(false);
120 }
121
122
123 void CursorSlice::backwardPos()
124 {
125         if (pos() != 0) {
126                 --pos();
127                 return;
128         }
129
130         if (pit() != 0) {
131                 --pit();
132                 pos() = lastpos();
133                 return;
134         }
135
136         if (idx() != 0) {
137                 --idx();
138                 pit() = lastpit();
139                 pos() = lastpos();
140                 return;
141         }
142
143         BOOST_ASSERT(false);
144 }
145
146
147 bool CursorSlice::at_end() const 
148 {
149         return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
150 }
151
152
153 bool CursorSlice::at_begin() const
154 {
155         return idx() == 0 && pit() == 0 && pos() == 0;
156 }
157
158
159 bool operator==(CursorSlice const & p, CursorSlice const & q)
160 {
161         return &p.inset() == &q.inset()
162                && p.idx() == q.idx()
163                && p.pit() == q.pit()
164                && p.pos() == q.pos();
165 }
166
167
168 bool operator!=(CursorSlice const & p, CursorSlice const & q)
169 {
170         return &p.inset() != &q.inset()
171                || p.idx() != q.idx()
172                || p.pit() != q.pit()
173                || p.pos() != q.pos();
174 }
175
176
177 bool operator<(CursorSlice const & p, CursorSlice const & q)
178 {
179         if (&p.inset() != &q.inset()) {
180                 lyxerr << "can't compare cursor and anchor in different insets\n"
181                        << "p: " << p << '\n' << "q: " << q << endl;
182                 BOOST_ASSERT(false);
183         }
184         if (p.idx() != q.idx())
185                 return p.idx() < q.idx();
186         if (p.pit() != q.pit())
187                 return p.pit() < q.pit();
188         return p.pos() < q.pos();
189 }
190
191
192 bool operator>(CursorSlice const & p, CursorSlice const & q)
193 {
194         return q < p;
195 }
196
197
198 bool operator<=(CursorSlice const & p, CursorSlice const & q)
199 {
200         return !(q < p);
201 }
202
203
204 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
205 {
206         return os
207            << "inset: " << &item.inset()
208 //         << " text: " << item.text()
209            << " idx: " << item.idx()
210            << " par: " << item.pit()
211            << " pos: " << item.pos()
212 //         << " x: " << item.inset().x()
213 //         << " y: " << item.inset().y()
214 ;
215 }
216
217
218 } // namespace lyx