]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[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         /// is the iterator valid?
58         operator const void*() const { return empty() ? 0 : this; }
59         /// is this iterator invalid?
60         bool operator!() const { return empty(); }
61
62         //
63         // access to slice at tip
64         //
65         /// access to tip
66         CursorSlice & top() { return back(); }
67         /// access to tip
68         CursorSlice const & top() const { return back(); }
69         /// access to outermost slice
70         CursorSlice & bottom() { return front(); }
71         /// access to  outermost slicetip
72         CursorSlice const & bottom() const { return front(); }
73         /// how many nested insets do we have?
74         size_t depth() const { return size(); }
75         /// the containing inset
76         InsetBase & inset() const { return back().inset(); }
77         /// return the cell of the inset this cursor is in
78         idx_type idx() const { return back().idx(); }
79         /// return the cell of the inset this cursor is in
80         idx_type & idx() { return back().idx(); }
81         /// return the last possible cell in this inset
82         idx_type lastidx() const;
83         /// return the paragraph this cursor is in
84         pit_type pit() const { return back().pit(); }
85         /// return the paragraph this cursor is in
86         pit_type & pit() { return back().pit(); }
87         /// return the last possible paragraph in this inset
88         pit_type lastpit() const;
89         /// return the position within the paragraph
90         pos_type pos() const { return back().pos(); }
91         /// return the position within the paragraph
92         pos_type & pos() { return back().pos(); }
93         /// return the last position within the paragraph
94         pos_type lastpos() const;
95
96         /// return the number of embedded cells
97         size_t nargs() const;
98         /// return the number of embedded cells
99         size_t ncols() const;
100         /// return the number of embedded cells
101         size_t nrows() const;
102         /// return the grid row of the top cell
103         row_type row() const;
104         /// return the last row of the top grid
105         row_type lastrow() const { return nrows() - 1; }
106         /// return the grid column of the top cell
107         col_type col() const;
108         /// return the last column of the top grid
109         col_type lastcol() const { return ncols() - 1; }
110         /// the inset just behind the cursor
111         InsetBase * nextInset();
112         /// the inset just in front of the cursor
113         InsetBase * prevInset();
114         /// the inset just in front of the cursor
115         InsetBase const * prevInset() const;
116
117         /// are we in mathed?
118         bool inMathed() const;
119         /// are we in texted?
120         bool inTexted() const;
121
122         //
123         // math-specific part
124         //
125         /// return the mathed cell this cursor is in
126         MathArray const & cell() const;
127         /// return the mathed cell this cursor is in
128         MathArray & cell();
129         /// the mathatom left of the cursor
130         MathAtom const & prevAtom() const;
131         /// the mathatom left of the cursor
132         MathAtom & prevAtom();
133         /// the mathatom right of the cursor
134         MathAtom const & nextAtom() const;
135         /// the mathatom right of the cursor
136         MathAtom & nextAtom();
137
138         //
139         // text-specific part
140         //
141         /// see comment for boundary_ below
142         bool boundary() const { return top().boundary(); }
143         /// see comment for boundary_ below
144         bool & boundary() { return top().boundary(); }
145         /// the paragraph we're in
146         Paragraph & paragraph();
147         /// the paragraph we're in
148         Paragraph const & paragraph() const;
149         /// the row in the paragraph we're in
150         Row & textRow();
151         /// the row in the paragraph we're in
152         Row const & textRow() const;
153         ///
154         LyXText * text() const;
155         ///
156         InsetBase * innerInsetOfType(int code) const;
157         ///
158         LyXText * innerText() const;
159
160         //
161         // elementary moving
162         //
163         /// move on one logical position
164         void forwardPos();
165         /// move on one physical character or inset
166         void forwardChar();
167         /// move on one paragraph
168         void forwardPar();
169         /// move on one cell
170         void forwardIdx();
171         /// move on one inset
172         void forwardInset();
173         /// move backward one logical position
174         void backwardPos();
175         /// move backward one physical character or inset
176         void backwardChar();
177         /// move backward one paragraph
178         void backwardPar();
179         /// move backward one cell
180         void backwardIdx();
181         /// move backward one inset
182         void backwardInset();
183
184         /// are we some 'extension' (i.e. deeper nested) of the given iterator
185         bool hasPart(DocIterator const & it) const;
186
187         /// output
188         friend std::ostream &
189         operator<<(std::ostream & os, DocIterator const & cur);
190 private:
191         InsetBase * inset_;
192 };
193
194
195 DocIterator doc_iterator_begin(InsetBase & inset);
196 DocIterator doc_iterator_end(InsetBase & inset);
197
198
199 // The difference to a ('non stable') DocIterator is the removed
200 // (overwritte by 0...) part of the CursorSlice data items. So this thing
201 // is suitable for external storage, but not for iteration as such.
202
203 class StableDocIterator {
204 public:
205         ///
206         StableDocIterator() {}
207         /// non-explicit intended
208         StableDocIterator(const DocIterator & it);
209         ///
210         DocIterator asDocIterator(InsetBase * start) const;
211         ///
212         size_t size() const { return data_.size(); }
213         ///
214         friend std::ostream &
215         operator<<(std::ostream & os, StableDocIterator const & cur);
216         ///
217         friend std::istream &
218         operator>>(std::istream & is, StableDocIterator & cur);
219 private:
220         std::vector<CursorSlice> data_;
221 };
222
223 bool operator==(StableDocIterator const &, StableDocIterator const &);
224
225 #endif