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