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