]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
Avoid full metrics computation with Update:FitCursor
[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 #include "ParagraphList.h"
21
22 #include "support/debug.h"
23
24 #include "mathed/InsetMath.h"
25 #include "mathed/InsetMathMacro.h"
26 #include "mathed/MathData.h"
27
28 #include "support/lassert.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 CursorSlice::CursorSlice()
38         : inset_(nullptr), 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         LBUFERR(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         LBUFERR(inset_);
64         InsetMath const * math = inset_->asInsetMath();
65         bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs();
66         return math ? (paramless_macro ? 0 : cell().size())
67                     : (text()->empty() ? 0 : paragraph().size());
68 }
69
70
71 pit_type CursorSlice::lastpit() const
72 {
73         if (inset_->inMathed())
74                 return 0;
75         return text()->paragraphs().size() - 1;
76 }
77
78
79 row_type CursorSlice::row() const
80 {
81         LASSERT(inset_, return 0);
82         return inset_->row(idx_);
83 }
84
85
86 col_type CursorSlice::col() const
87 {
88         LASSERT(inset_, return 0);
89         return inset_->col(idx_);
90 }
91
92
93 void CursorSlice::setPitPos(pit_type pit, pos_type pos)
94 {
95         LASSERT(pit != int(text()->paragraphs().size()), return);
96         pit_ = pit;
97         pos_ = pos;
98
99         // Now some strict checking. None of these should happen, but
100         // we're scaredy-cats
101         if (pos < 0) {
102                 LYXERR0("Don't like -1!");
103                 LATTEST(false);
104         }
105
106         if (pos > paragraph().size()) {
107                 LYXERR0("Don't like 1, pos: " << pos
108                                 << " size: " << paragraph().size()
109                        << " par: " << pit);
110                 LATTEST(false);
111         }
112 }
113
114
115 void CursorSlice::forwardPos()
116 {
117         //  move on one position if possible
118         if (pos_ < lastpos()) {
119                 //lyxerr << "... next pos" << endl;
120                 ++pos_;
121                 return;
122         }
123
124         // otherwise move on one paragraph if possible
125         if (pit_ < lastpit()) {
126                 //lyxerr << "... next par" << endl;
127                 ++pit_;
128                 pos_ = 0;
129                 return;
130         }
131
132         // otherwise move on one cell
133         //lyxerr << "... next idx" << endl;
134
135         LASSERT(idx_ < nargs(), return);
136
137         ++idx_;
138         pit_ = 0;
139         pos_ = 0;
140 }
141
142
143 void CursorSlice::forwardIdx()
144 {
145         LASSERT(idx_ < nargs(), return);
146
147         ++idx_;
148         pit_ = 0;
149         pos_ = 0;
150 }
151
152
153 void CursorSlice::backwardPos()
154 {
155         if (pos_ != 0) {
156                 --pos_;
157                 return;
158         }
159
160         if (pit_ != 0) {
161                 --pit_;
162                 pos_ = lastpos();
163                 return;
164         }
165
166         if (idx_ != 0) {
167                 --idx_;
168                 pit_ = lastpit();
169                 pos_ = lastpos();
170                 return;
171         }
172
173         LATTEST(false);
174 }
175
176
177 bool CursorSlice::at_cell_end() const
178 {
179         return pit_ == lastpit() && pos_ == lastpos();
180 }
181
182
183 bool CursorSlice::at_cell_begin() const
184 {
185         return pit_ == 0 && pos_ == 0;
186 }
187
188
189 bool CursorSlice::at_end() const
190 {
191         return idx_ == lastidx() && at_cell_end();
192 }
193
194
195 bool CursorSlice::at_begin() const
196 {
197         return idx_ == 0 && at_cell_begin();
198 }
199
200
201 bool operator==(CursorSlice const & p, CursorSlice const & q)
202 {
203         return p.inset_ == q.inset_
204                && p.idx_ == q.idx_
205                && p.pit_ == q.pit_
206                && p.pos_ == q.pos_;
207 }
208
209
210 bool operator!=(CursorSlice const & p, CursorSlice const & q)
211 {
212         return p.inset_ != q.inset_
213                || p.idx_ != q.idx_
214                || p.pit_ != q.pit_
215                || p.pos_ != q.pos_;
216 }
217
218
219 bool operator<(CursorSlice const & p, CursorSlice const & q)
220 {
221         if (p.inset_ != q.inset_) {
222                 LYXERR0("can't compare cursor and anchor in different insets\n"
223                        << "p: " << p << '\n' << "q: " << q);
224                 // It should be safe to continue, just registering the error.
225                 LASSERT(false, return false);
226         }
227         if (p.idx_ != q.idx_)
228                 return p.idx_ < q.idx_;
229         if (p.pit_ != q.pit_)
230                 return p.pit_ < q.pit_;
231         return p.pos_ < q.pos_;
232 }
233
234
235 bool operator>(CursorSlice const & p, CursorSlice const & q)
236 {
237         return q < p;
238 }
239
240
241 bool operator<=(CursorSlice const & p, CursorSlice const & q)
242 {
243         return !(q < p);
244 }
245
246
247 ostream & operator<<(ostream & os, CursorSlice const & item)
248 {
249         return os
250            << "inset: " << (void *)item.inset_
251 //         << " text: " << item.text()
252            << " idx: " << item.idx_
253            << " par: " << item.pit_
254            << " pos: " << item.pos_
255 //         << " x: " << item.inset_->x()
256 //         << " y: " << item.inset_->y()
257 ;
258 }
259
260
261 } // namespace lyx