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