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