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