]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
Trivial fixes to some warnings thrown up by MSVS.Net 2003.
[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         ///
168         InsetBase * innerInsetOfType(int code) const;
169         ///
170         LyXText * innerText();
171         ///
172         LyXText const * innerText() const;
173
174         //
175         // elementary moving
176         //
177         /// move on one logical position, do not descend into nested insets
178         void forwardPosNoDescend();
179         /// move on one logical position, descend into nested insets
180         void forwardPos();
181         /// move on one physical character or inset
182         void forwardChar();
183         /// move on one paragraph
184         void forwardPar();
185         /// move on one cell
186         void forwardIdx();
187         /// move on one inset
188         void forwardInset();
189         /// move backward one logical position
190         void backwardPos();
191         /// move backward one physical character or inset
192         void backwardChar();
193         /// move backward one paragraph
194         void backwardPar();
195         /// move backward one cell
196         void backwardIdx();
197         /// move backward one inset
198         void backwardInset();
199
200         /// are we some 'extension' (i.e. deeper nested) of the given iterator
201         bool hasPart(DocIterator const & it) const;
202
203         /// output
204         friend std::ostream &
205         operator<<(std::ostream & os, DocIterator const & cur);
206         ///
207         friend bool operator==(DocIterator const &, DocIterator const &);
208         ///
209         friend class StableDocIterator;
210 //protected:
211         ///
212         void clear() { slices_.clear(); }
213         ///
214         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
215         ///
216         void pop_back() { slices_.pop_back(); }
217         /// recompute the inset parts of the cursor from the document data
218         void updateInsets(InsetBase * inset);
219
220 private:
221         /**
222          * When the cursor position is i, is the cursor after the i-th char
223          * or before the i+1-th char ? Normally, these two interpretations are
224          * equivalent, except when the fonts of the i-th and i+1-th char
225          * differ.
226          * We use boundary_ to distinguish between the two options:
227          * If boundary_=true, then the cursor is after the i-th char
228          * and if boundary_=false, then the cursor is before the i+1-th char.
229          *
230          * We currently use the boundary only when the language direction of
231          * the i-th char is different than the one of the i+1-th char.
232          * In this case it is important to distinguish between the two
233          * cursor interpretations, in order to give a reasonable behavior to
234          * the user.
235          */
236         bool boundary_;
237         ///
238         std::vector<CursorSlice> const & internalData() const {
239                 return slices_;
240         }
241         ///
242         std::vector<CursorSlice> slices_;
243         ///
244         InsetBase * inset_;
245 };
246
247
248 DocIterator doc_iterator_begin(InsetBase & inset);
249 DocIterator doc_iterator_end(InsetBase & inset);
250
251
252 inline
253 bool operator==(DocIterator const & di1, DocIterator const & di2)
254 {
255         return di1.slices_ == di2.slices_;
256 }
257
258
259 inline
260 bool operator!=(DocIterator const & di1, DocIterator const & di2)
261 {
262         return !(di1 == di2);
263 }
264
265
266 // The difference to a ('non stable') DocIterator is the removed
267 // (overwritte by 0...) part of the CursorSlice data items. So this thing
268 // is suitable for external storage, but not for iteration as such.
269
270 class StableDocIterator {
271 public:
272         ///
273         StableDocIterator() {}
274         /// non-explicit intended
275         StableDocIterator(const DocIterator & it);
276         ///
277         DocIterator asDocIterator(InsetBase * start) const;
278         ///
279         size_t size() const { return data_.size(); }
280         /// type for cursor positions within a cell
281         typedef CursorSlice::pos_type pos_type;
282         ///  return the position within the paragraph
283         pos_type pos() const { return data_.back().pos(); }
284         ///  return the position within the paragraph
285         pos_type & pos() { return data_.back().pos(); }
286         ///
287         friend std::ostream &
288         operator<<(std::ostream & os, StableDocIterator const & cur);
289         ///
290         friend std::istream &
291         operator>>(std::istream & is, StableDocIterator & cur);
292         ///
293         friend bool
294         operator==(StableDocIterator const &, StableDocIterator const &);
295 private:
296         std::vector<CursorSlice> data_;
297 };
298
299 #endif