]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
Don't store row and col in cursor, add some documentation about idx
[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 {
59                 return slices_[i];
60         }
61
62         /// access slice at position \p i
63         CursorSlice & operator[](size_t i) {
64                 return slices_[i];
65         }
66
67         // What is the point of this function?
68         void resize(size_t i) { slices_.resize(i); }
69
70         /// is the iterator valid?
71         operator const void*() const { return empty() ? 0 : this; }
72         /// is this iterator invalid?
73         bool operator!() const { return empty(); }
74
75         /// does this iterator have any content?
76         bool empty() const { return slices_.empty(); }
77
78         //
79         // access to slice at tip
80         //
81         /// access to tip
82         CursorSlice & top() { return slices_.back(); }
83         /// access to tip
84         CursorSlice const & top() const { return slices_.back(); }
85         /// access to outermost slice
86         CursorSlice & bottom() { return slices_.front(); }
87         /// access to outermost slice
88         CursorSlice const & bottom() const { return slices_.front(); }
89         /// how many nested insets do we have?
90         size_t depth() const { return slices_.size(); }
91         /// the containing inset
92         InsetBase & inset() const { return top().inset(); }
93         /// return the cell of the inset this cursor is in
94         idx_type idx() const { return top().idx(); }
95         /// return the cell of the inset this cursor is in
96         idx_type & idx() { return top().idx(); }
97         /// return the last possible cell in this inset
98         idx_type lastidx() const;
99         /// return the paragraph this cursor is in
100         pit_type pit() const { return top().pit(); }
101         /// return the paragraph this cursor is in
102         pit_type & pit() { return top().pit(); }
103         /// return the last possible paragraph in this inset
104         pit_type lastpit() const;
105         /// return the position within the paragraph
106         pos_type pos() const { return top().pos(); }
107         /// return the position within the paragraph
108         pos_type & pos() { return top().pos(); }
109         /// return the last position within the paragraph
110         pos_type lastpos() const;
111
112         /// return the number of embedded cells
113         size_t nargs() const;
114         /// return the number of embedded cells
115         size_t ncols() const;
116         /// return the number of embedded cells
117         size_t nrows() const;
118         /// return the grid row of the top cell
119         row_type row() const;
120         /// return the last row of the top grid
121         row_type lastrow() const { return nrows() - 1; }
122         /// return the grid column of the top cell
123         col_type col() const;
124         /// return the last column of the top grid
125         col_type lastcol() const { return ncols() - 1; }
126         /// the inset just behind the cursor
127         InsetBase * nextInset();
128         /// the inset just in front of the cursor
129         InsetBase * prevInset();
130         /// the inset just in front of the cursor
131         InsetBase const * prevInset() const;
132
133         /// are we in mathed?
134         bool inMathed() const;
135         /// are we in texted?
136         bool inTexted() const;
137
138         //
139         // math-specific part
140         //
141         /// return the mathed cell this cursor is in
142         MathArray const & cell() const;
143         /// return the mathed cell this cursor is in
144         MathArray & cell();
145         /// the mathatom left of the cursor
146         MathAtom const & prevAtom() const;
147         /// the mathatom left of the cursor
148         MathAtom & prevAtom();
149         /// the mathatom right of the cursor
150         MathAtom const & nextAtom() const;
151         /// the mathatom right of the cursor
152         MathAtom & nextAtom();
153
154         //
155         // text-specific part
156         //
157         /// \sa boundary_
158         bool boundary() const { return top().boundary(); }
159         /// \sa boundary_
160         bool & boundary() { return top().boundary(); }
161         /// the paragraph we're in
162         Paragraph & paragraph();
163         /// the paragraph we're in
164         Paragraph const & paragraph() const;
165         /// the row in the paragraph we're in
166         Row & textRow();
167         /// the row in the paragraph we're in
168         Row const & textRow() const;
169         ///
170         LyXText * text();
171         ///
172         LyXText const * text() const;
173         ///
174         InsetBase * innerInsetOfType(int code) const;
175         ///
176         LyXText * innerText();
177         ///
178         LyXText const * innerText() const;
179
180         //
181         // elementary moving
182         //
183         /// move on one logical position, do not descend into nested insets
184         void forwardPosNoDescend();
185         /// move on one logical position, descend into nested insets
186         void forwardPos();
187         /// move on one physical character or inset
188         void forwardChar();
189         /// move on one paragraph
190         void forwardPar();
191         /// move on one cell
192         void forwardIdx();
193         /// move on one inset
194         void forwardInset();
195         /// move backward one logical position
196         void backwardPos();
197         /// move backward one physical character or inset
198         void backwardChar();
199         /// move backward one paragraph
200         void backwardPar();
201         /// move backward one cell
202         void backwardIdx();
203         /// move backward one inset
204         void backwardInset();
205
206         /// are we some 'extension' (i.e. deeper nested) of the given iterator
207         bool hasPart(DocIterator const & it) const;
208
209         /// output
210         friend std::ostream &
211         operator<<(std::ostream & os, DocIterator const & cur);
212         friend bool operator==(DocIterator const &, DocIterator const &);
213         friend class StableDocIterator;
214 protected:
215         void clear() { slices_.clear(); }
216         void push_back(CursorSlice const & sl) {
217                 slices_.push_back(sl);
218         }
219         void pop_back() {
220                 slices_.pop_back();
221         }
222 private:
223         std::vector<CursorSlice> const & internalData() const {
224                 return slices_;
225         }
226         std::vector<CursorSlice> slices_;
227         InsetBase * inset_;
228 };
229
230
231 DocIterator doc_iterator_begin(InsetBase & inset);
232 DocIterator doc_iterator_end(InsetBase & inset);
233
234
235 inline
236 bool operator==(DocIterator const & di1, DocIterator const & di2)
237 {
238         return di1.slices_ == di2.slices_;
239 }
240
241
242 inline
243 bool operator!=(DocIterator const & di1, DocIterator const & di2)
244 {
245         return !(di1 == di2);
246 }
247
248
249 // The difference to a ('non stable') DocIterator is the removed
250 // (overwritte by 0...) part of the CursorSlice data items. So this thing
251 // is suitable for external storage, but not for iteration as such.
252
253 class StableDocIterator {
254 public:
255         ///
256         StableDocIterator() {}
257         /// non-explicit intended
258         StableDocIterator(const DocIterator & it);
259         ///
260         DocIterator asDocIterator(InsetBase * start) const;
261         ///
262         size_t size() const { return data_.size(); }
263         ///
264         friend std::ostream &
265         operator<<(std::ostream & os, StableDocIterator const & cur);
266         ///
267         friend std::istream &
268         operator>>(std::istream & is, StableDocIterator & cur);
269         ///
270         friend bool
271         operator==(StableDocIterator const &, StableDocIterator const &);
272 private:
273         std::vector<CursorSlice> data_;
274 };
275
276 #endif