]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
Audit all the LASSERT calls, and try to do something sensible at
[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_, _("Invalid initialization of CursorSlice!"));
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_, _("Cursor slice not properly initialized!"));
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: This should only ever be called from an InsetMath.
84         // Should we crash in release mode, though, or try to continue?
85         LASSERT(asInsetMath(), /**/);
86         return asInsetMath()->row(idx_);
87 }
88
89
90 CursorSlice::col_type CursorSlice::col() const
91 {
92         // LASSERT: This should only ever be called from an InsetMath.
93         // Should we crash in release mode, though, or try to continue?
94         LASSERT(asInsetMath(), /**/);
95         return asInsetMath()->col(idx_);
96 }
97
98
99 void CursorSlice::forwardPos()
100 {
101         //  move on one position if possible
102         if (pos_ < lastpos()) {
103                 //lyxerr << "... next pos" << endl;
104                 ++pos_;
105                 return;
106         }
107
108         // otherwise move on one paragraph if possible
109         if (pit_ < lastpit()) {
110                 //lyxerr << "... next par" << endl;
111                 ++pit_;
112                 pos_ = 0;
113                 return;
114         }
115
116         // otherwise move on one cell
117         //lyxerr << "... next idx" << endl;
118
119         LASSERT(idx_ < nargs(), return);
120
121         ++idx_;
122         pit_ = 0;
123         pos_ = 0;
124 }
125
126
127 void CursorSlice::forwardIdx()
128 {
129         LASSERT(idx_ < nargs(), return);
130
131         ++idx_;
132         pit_ = 0;
133         pos_ = 0;
134 }
135
136
137 void CursorSlice::backwardPos()
138 {
139         if (pos_ != 0) {
140                 --pos_;
141                 return;
142         }
143
144         if (pit_ != 0) {
145                 --pit_;
146                 pos_ = lastpos();
147                 return;
148         }
149
150         if (idx_ != 0) {
151                 --idx_;
152                 pit_ = lastpit();
153                 pos_ = lastpos();
154                 return;
155         }
156
157         LATTEST(false);
158 }
159
160
161 bool CursorSlice::at_end() const 
162 {
163         return idx_ == lastidx() && pit_ == lastpit() && pos_ == lastpos();
164 }
165
166
167 bool CursorSlice::at_begin() const
168 {
169         return idx_ == 0 && pit_ == 0 && pos_ == 0;
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         return p.inset_ != q.inset_
185                || p.idx_ != q.idx_
186                || p.pit_ != q.pit_
187                || p.pos_ != q.pos_;
188 }
189
190
191 bool operator<(CursorSlice const & p, CursorSlice const & q)
192 {
193         if (p.inset_ != q.inset_) {
194                 LYXERR0("can't compare cursor and anchor in different insets\n"
195                        << "p: " << p << '\n' << "q: " << q);
196                 // It should be safe to continue, just registering the error.
197                 LASSERT(false, return false);
198         }
199         if (p.idx_ != q.idx_)
200                 return p.idx_ < q.idx_;
201         if (p.pit_ != q.pit_)
202                 return p.pit_ < q.pit_;
203         return p.pos_ < q.pos_;
204 }
205
206
207 bool operator>(CursorSlice const & p, CursorSlice const & q)
208 {
209         return q < p;
210 }
211
212
213 bool operator<=(CursorSlice const & p, CursorSlice const & q)
214 {
215         return !(q < p);
216 }
217
218
219 ostream & operator<<(ostream & os, CursorSlice const & item)
220 {
221         return os
222            << "inset: " << (void *)item.inset_
223 //         << " text: " << item.text()
224            << " idx: " << item.idx_
225            << " par: " << item.pit_
226            << " pos: " << item.pos_
227 //         << " x: " << item.inset_->x()
228 //         << " y: " << item.inset_->y()
229 ;
230 }
231
232
233 } // namespace lyx