]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
Fix event loop to no longer eat CPU
[lyx.git] / src / dociterator.h
1 // -*- C++ -*-
2 /**
3  * \file dociterator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DOCITERATOR_H
13 #define DOCITERATOR_H
14
15 #include "cursor_slice.h"
16
17 #include <vector>
18 #include <iosfwd>
19
20 class LyXText;
21 class MathAtom;
22 class Paragraph;
23 class Row;
24
25
26
27 // only needed for gcc 2.95, remove when support terminated
28 template <typename A, typename B>
29 bool ptr_cmp(A const * a, B const * b)
30 {
31         return a == b;
32 }
33
34
35 // The public inheritance should go in favour of a suitable data member
36 // (or maybe private inheritance) at some point of time.
37 class DocIterator // : public std::vector<CursorSlice>
38 {
39 public:
40         /// type for cell number in inset
41         typedef CursorSlice::idx_type idx_type;
42         /// type for paragraph numbers positions within a cell
43         typedef CursorSlice::pit_type pit_type;
44         /// type for cursor positions within a cell
45         typedef CursorSlice::pos_type pos_type;
46         /// type for row indices
47         typedef CursorSlice::row_type row_type;
48         /// type for col indices
49         typedef CursorSlice::col_type col_type;
50
51 public:
52         ///
53         DocIterator();
54         ///
55         explicit DocIterator(InsetBase & inset);
56
57         /// access slice at position \p i
58         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
59         /// access slice at position \p i
60         CursorSlice & operator[](size_t i) { return slices_[i]; }
61         /// chop a few slices from the iterator
62         void resize(size_t i) { slices_.resize(i); }
63
64         /// is the iterator valid?
65         operator const void*() const { return empty() ? 0 : this; }
66         /// is this iterator invalid?
67         bool operator!() const { return empty(); }
68
69         /// does this iterator have any content?
70         bool empty() const { return slices_.empty(); }
71
72         //
73         // access to slice at tip
74         //
75         /// access to tip
76         CursorSlice & top() { return slices_.back(); }
77         /// access to tip
78         CursorSlice const & top() const { return slices_.back(); }
79         /// access to outermost slice
80         CursorSlice & bottom() { return slices_.front(); }
81         /// access to outermost slice
82         CursorSlice const & bottom() const { return slices_.front(); }
83         /// how many nested insets do we have?
84         size_t depth() const { return slices_.size(); }
85         /// the containing inset
86         InsetBase & inset() const { return top().inset(); }
87         /// return the cell of the inset this cursor is in
88         idx_type idx() const { return top().idx(); }
89         /// return the cell of the inset this cursor is in
90         idx_type & idx() { return top().idx(); }
91         /// return the last possible cell in this inset
92         idx_type lastidx() const;
93         /// return the paragraph this cursor is in
94         pit_type pit() const { return top().pit(); }
95         /// return the paragraph this cursor is in
96         pit_type & pit() { return top().pit(); }
97         /// return the last possible paragraph in this inset
98         pit_type lastpit() const;
99         /// return the position within the paragraph
100         pos_type pos() const { return top().pos(); }
101         /// return the position within the paragraph
102         pos_type & pos() { return top().pos(); }
103         /// return the last position within the paragraph
104         pos_type lastpos() const;
105
106         /// return the number of embedded cells
107         size_t nargs() const;
108         /// return the number of embedded cells
109         size_t ncols() const;
110         /// return the number of embedded cells
111         size_t nrows() const;
112         /// return the grid row of the top cell
113         row_type row() const;
114         /// return the last row of the top grid
115         row_type lastrow() const { return nrows() - 1; }
116         /// return the grid column of the top cell
117         col_type col() const;
118         /// return the last column of the top grid
119         col_type lastcol() const { return ncols() - 1; }
120         /// the inset just behind the cursor
121         InsetBase * nextInset();
122         /// the inset just in front of the cursor
123         InsetBase * prevInset();
124         /// the inset just in front of the cursor
125         InsetBase const * prevInset() const;
126         ///
127         bool boundary() const { return boundary_; }
128         ///
129         void boundary(bool b) { boundary_ = b; }
130
131         /// are we in mathed?
132         bool inMathed() const;
133         /// are we in texted?
134         bool inTexted() const;
135
136         //
137         // math-specific part
138         //
139         /// return the mathed cell this cursor is in
140         MathArray const & cell() const;
141         /// return the mathed cell this cursor is in
142         MathArray & cell();
143         /// the mathatom left of the cursor
144         MathAtom const & prevAtom() const;
145         /// the mathatom left of the cursor
146         MathAtom & prevAtom();
147         /// the mathatom right of the cursor
148         MathAtom const & nextAtom() const;
149         /// the mathatom right of the cursor
150         MathAtom & nextAtom();
151
152         //
153         // text-specific part
154         //
155         /// the paragraph we're in
156         Paragraph & paragraph();
157         /// the paragraph we're in
158         Paragraph const & paragraph() const;
159         /// the row in the paragraph we're in
160         Row & textRow();
161         /// the row in the paragraph we're in
162         Row const & textRow() const;
163         ///
164         LyXText * text();
165         ///
166         LyXText const * text() const;
167         /// the containing inset or the cell, respectively
168         InsetBase * realInset() const;
169         ///
170         InsetBase * innerInsetOfType(int code) const;
171         ///
172         LyXText * innerText();
173         ///
174         LyXText const * innerText() const;
175
176         //
177         // elementary moving
178         //
179         /// move on one logical position, do not descend into nested insets
180         void forwardPosNoDescend();
181         /**
182          * move on one logical position, descend into nested insets
183          * skip collapsed insets if \p ignorecollapsed is true
184          */
185         void forwardPos(bool ignorecollapsed = false);
186         /// move on one physical character or inset
187         void forwardChar();
188         /// move on one paragraph
189         void forwardPar();
190         /// move on one cell
191         void forwardIdx();
192         /// move on one inset
193         void forwardInset();
194         /// move backward one logical position
195         void backwardPos();
196         /// move backward one physical character or inset
197         void backwardChar();
198         /// move backward one paragraph
199         void backwardPar();
200         /// move backward one cell
201         void backwardIdx();
202         /// move backward one inset
203         void backwardInset();
204
205         /// are we some 'extension' (i.e. deeper nested) of the given iterator
206         bool hasPart(DocIterator const & it) const;
207
208         /// output
209         friend std::ostream &
210         operator<<(std::ostream & os, DocIterator const & cur);
211         ///
212         friend bool operator==(DocIterator const &, DocIterator const &);
213         ///
214         friend class StableDocIterator;
215 //protected:
216         ///
217         void clear() { slices_.clear(); }
218         ///
219         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
220         ///
221         void pop_back() { slices_.pop_back(); }
222         /// recompute the inset parts of the cursor from the document data
223         void updateInsets(InsetBase * inset);
224
225 private:
226         /**
227          * When the cursor position is i, is the cursor after the i-th char
228          * or before the i+1-th char ? Normally, these two interpretations are
229          * equivalent, except when the fonts of the i-th and i+1-th char
230          * differ.
231          * We use boundary_ to distinguish between the two options:
232          * If boundary_=true, then the cursor is after the i-th char
233          * and if boundary_=false, then the cursor is before the i+1-th char.
234          *
235          * We currently use the boundary only when the language direction of
236          * the i-th char is different than the one of the i+1-th char.
237          * In this case it is important to distinguish between the two
238          * cursor interpretations, in order to give a reasonable behavior to
239          * the user.
240          */
241         bool boundary_;
242         ///
243         std::vector<CursorSlice> const & internalData() const {
244                 return slices_;
245         }
246         ///
247         std::vector<CursorSlice> slices_;
248         ///
249         InsetBase * inset_;
250 };
251
252
253 DocIterator doc_iterator_begin(InsetBase & inset);
254 DocIterator doc_iterator_end(InsetBase & inset);
255
256
257 inline
258 bool operator==(DocIterator const & di1, DocIterator const & di2)
259 {
260         return di1.slices_ == di2.slices_;
261 }
262
263
264 inline
265 bool operator!=(DocIterator const & di1, DocIterator const & di2)
266 {
267         return !(di1 == di2);
268 }
269
270
271 // The difference to a ('non stable') DocIterator is the removed
272 // (overwritte by 0...) part of the CursorSlice data items. So this thing
273 // is suitable for external storage, but not for iteration as such.
274
275 class StableDocIterator {
276 public:
277         ///
278         StableDocIterator() {}
279         /// non-explicit intended
280         StableDocIterator(const DocIterator & it);
281         ///
282         DocIterator asDocIterator(InsetBase * start) const;
283         ///
284         size_t size() const { return data_.size(); }
285         /// type for cursor positions within a cell
286         typedef CursorSlice::pos_type pos_type;
287         ///  return the position within the paragraph
288         pos_type pos() const { return data_.back().pos(); }
289         ///  return the position within the paragraph
290         pos_type & pos() { return data_.back().pos(); }
291         ///
292         friend std::ostream &
293         operator<<(std::ostream & os, StableDocIterator const & cur);
294         ///
295         friend std::istream &
296         operator>>(std::istream & is, StableDocIterator & cur);
297         ///
298         friend bool
299         operator==(StableDocIterator const &, StableDocIterator const &);
300 private:
301         std::vector<CursorSlice> data_;
302 };
303
304 #endif