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