]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
remove fake constness in CursorSlice
[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 try to move on one cell if possible
106         if (idx() < lastidx()) {
107                 //lyxerr << "... next idx" << endl;
108                 ++idx();
109                 pit() = 0;
110                 pos() = 0;
111                 return;
112         }
113         BOOST_ASSERT(false);
114 }
115
116
117 void CursorSlice::backwardPos()
118 {
119         if (pos() != 0) {
120                 --pos();
121                 return;
122         }
123
124         if (pit() != 0) {
125                 --pit();
126                 pos() = lastpos();
127                 return;
128         }
129
130         if (idx() != 0) {
131                 --idx();
132                 pit() = lastpit();
133                 pos() = lastpos();
134                 return;
135         }
136
137         BOOST_ASSERT(false);
138 }
139
140
141 bool CursorSlice::at_end() const 
142 {
143         return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
144 }
145
146
147 bool CursorSlice::at_begin() const
148 {
149         return idx() == 0 && pit() == 0 && pos() == 0;
150 }
151
152
153 bool operator==(CursorSlice const & p, CursorSlice const & q)
154 {
155         return &p.inset() == &q.inset()
156                && p.idx() == q.idx()
157                && p.pit() == q.pit()
158                && p.pos() == q.pos();
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         if (&p.inset() != &q.inset()) {
174                 lyxerr << "can't compare cursor and anchor in different insets\n"
175                        << "p: " << p << '\n' << "q: " << q << endl;
176                 BOOST_ASSERT(false);
177         }
178         if (p.idx() != q.idx())
179                 return p.idx() < q.idx();
180         if (p.pit() != q.pit())
181                 return p.pit() < q.pit();
182         return p.pos() < q.pos();
183 }
184
185
186 bool operator>(CursorSlice const & p, CursorSlice const & q)
187 {
188         return q < p;
189 }
190
191
192 bool operator<=(CursorSlice const & p, CursorSlice const & q)
193 {
194         return !(q < p);
195 }
196
197
198 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
199 {
200         return os
201            << "inset: " << &item.inset()
202 //         << " text: " << item.text()
203            << " idx: " << item.idx()
204            << " par: " << item.pit()
205            << " pos: " << item.pos()
206 //         << " x: " << item.inset().x()
207 //         << " y: " << item.inset().y()
208 ;
209 }
210
211
212 } // namespace lyx