]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
Moves textRow() from DocIterator to cursor.C where it belongs.
[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
21 namespace lyx {
22
23 class LyXText;
24 class MathAtom;
25 class Paragraph;
26 class Row;
27
28
29 // only needed for gcc 2.95, remove when support terminated
30 template <typename A, typename B>
31 bool ptr_cmp(A const * a, B const * b)
32 {
33         return a == b;
34 }
35
36
37 // The public inheritance should go in favour of a suitable data member
38 // (or maybe private inheritance) at some point of time.
39 class DocIterator // : public std::vector<CursorSlice>
40 {
41 public:
42         /// type for cell number in inset
43         typedef CursorSlice::idx_type idx_type;
44         /// type for row indices
45         typedef CursorSlice::row_type row_type;
46         /// type for col indices
47         typedef CursorSlice::col_type col_type;
48
49 public:
50         ///
51         DocIterator();
52         ///
53         explicit DocIterator(InsetBase & inset);
54
55         /// access slice at position \p i
56         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
57         /// access slice at position \p i
58         CursorSlice & operator[](size_t i) { return slices_[i]; }
59         /// chop a few slices from the iterator
60         void resize(size_t i) { slices_.resize(i); }
61
62         /// is the iterator valid?
63         operator const void*() const { return empty() ? 0 : this; }
64         /// is this iterator invalid?
65         bool operator!() const { return empty(); }
66
67         /// does this iterator have any content?
68         bool empty() const { return slices_.empty(); }
69
70         //
71         // access to slice at tip
72         //
73         /// access to tip
74         CursorSlice & top() { return slices_.back(); }
75         /// access to tip
76         CursorSlice const & top() const { return slices_.back(); }
77         /// access to outermost slice
78         CursorSlice & bottom() { return slices_.front(); }
79         /// access to outermost slice
80         CursorSlice const & bottom() const { return slices_.front(); }
81         /// how many nested insets do we have?
82         size_t depth() const { return slices_.size(); }
83         /// the containing inset
84         InsetBase & inset() const { return top().inset(); }
85         /// return the cell of the inset this cursor is in
86         idx_type idx() const { return top().idx(); }
87         /// return the cell of the inset this cursor is in
88         idx_type & idx() { return top().idx(); }
89         /// return the last possible cell in this inset
90         idx_type lastidx() const;
91         /// return the paragraph this cursor is in
92         pit_type pit() const { return top().pit(); }
93         /// return the paragraph this cursor is in
94         pit_type & pit() { return top().pit(); }
95         /// return the last possible paragraph in this inset
96         pit_type lastpit() const;
97         /// return the position within the paragraph
98         pos_type pos() const { return top().pos(); }
99         /// return the position within the paragraph
100         pos_type & pos() { return top().pos(); }
101         /// return the last position within the paragraph
102         pos_type lastpos() const;
103
104         /// return the number of embedded cells
105         size_t nargs() const;
106         /// return the number of embedded cells
107         size_t ncols() const;
108         /// return the number of embedded cells
109         size_t nrows() const;
110         /// return the grid row of the top cell
111         row_type row() const;
112         /// return the last row of the top grid
113         row_type lastrow() const { return nrows() - 1; }
114         /// return the grid column of the top cell
115         col_type col() const;
116         /// return the last column of the top grid
117         col_type lastcol() const { return ncols() - 1; }
118         /// the inset just behind the cursor
119         InsetBase * nextInset();
120         /// the inset just in front of the cursor
121         InsetBase * prevInset();
122         /// the inset just in front of the cursor
123         InsetBase const * prevInset() const;
124         ///
125         bool boundary() const { return boundary_; }
126         ///
127         void boundary(bool b) { boundary_ = b; }
128
129         /// are we in mathed?
130         bool inMathed() const;
131         /// are we in texted?
132         bool inTexted() const;
133
134         //
135         // math-specific part
136         //
137         /// return the mathed cell this cursor is in
138         MathArray const & cell() const;
139         /// return the mathed cell this cursor is in
140         MathArray & cell();
141         /// the mathatom left of the cursor
142         MathAtom const & prevAtom() const;
143         /// the mathatom left of the cursor
144         MathAtom & prevAtom();
145         /// the mathatom right of the cursor
146         MathAtom const & nextAtom() const;
147         /// the mathatom right of the cursor
148         MathAtom & nextAtom();
149
150         //
151         // text-specific part
152         //
153         /// the paragraph we're in
154         Paragraph & paragraph();
155         /// the paragraph we're in
156         Paragraph const & paragraph() const;
157         ///
158         LyXText * text();
159         ///
160         LyXText const * text() const;
161         /// the containing inset or the cell, respectively
162         InsetBase * realInset() const;
163         ///
164         InsetBase * innerInsetOfType(int code) const;
165         ///
166         LyXText * innerText();
167         ///
168         LyXText const * innerText() const;
169
170         //
171         // elementary moving
172         //
173         /// move on one logical position, do not descend into nested insets
174         void forwardPosNoDescend();
175         /**
176          * move on one logical position, descend into nested insets
177          * skip collapsed insets if \p ignorecollapsed is true
178          */
179         void forwardPos(bool ignorecollapsed = false);
180         /// move on one physical character or inset
181         void forwardChar();
182         /// move on one paragraph
183         void forwardPar();
184         /// move on one cell
185         void forwardIdx();
186         /// move on one inset
187         void forwardInset();
188         /// move backward one logical position
189         void backwardPos();
190         /// move backward one physical character or inset
191         void backwardChar();
192         /// move backward one paragraph
193         void backwardPar();
194         /// move backward one cell
195         void backwardIdx();
196         /// move backward one inset
197         void backwardInset();
198
199         /// are we some 'extension' (i.e. deeper nested) of the given iterator
200         bool hasPart(DocIterator const & it) const;
201
202         /// output
203         friend std::ostream &
204         operator<<(std::ostream & os, DocIterator const & cur);
205         ///
206         friend bool operator==(DocIterator const &, DocIterator const &);
207         ///
208         friend class StableDocIterator;
209 //protected:
210         ///
211         void clear() { slices_.clear(); }
212         ///
213         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
214         ///
215         void pop_back() { slices_.pop_back(); }
216         /// recompute the inset parts of the cursor from the document data
217         void updateInsets(InsetBase * inset);
218
219 private:
220         /**
221          * When the cursor position is i, is the cursor after the i-th char
222          * or before the i+1-th char ? Normally, these two interpretations are
223          * equivalent, except when the fonts of the i-th and i+1-th char
224          * differ.
225          * We use boundary_ to distinguish between the two options:
226          * If boundary_=true, then the cursor is after the i-th char
227          * and if boundary_=false, then the cursor is before the i+1-th char.
228          *
229          * We currently use the boundary only when the language direction of
230          * the i-th char is different than the one of the i+1-th char.
231          * In this case it is important to distinguish between the two
232          * cursor interpretations, in order to give a reasonable behavior to
233          * the user.
234          */
235         bool boundary_;
236         ///
237         std::vector<CursorSlice> const & internalData() const {
238                 return slices_;
239         }
240         ///
241         std::vector<CursorSlice> slices_;
242         ///
243         InsetBase * inset_;
244 };
245
246
247 DocIterator doc_iterator_begin(InsetBase & inset);
248 DocIterator doc_iterator_end(InsetBase & inset);
249
250
251 inline
252 bool operator==(DocIterator const & di1, DocIterator const & di2)
253 {
254         return di1.slices_ == di2.slices_;
255 }
256
257
258 inline
259 bool operator!=(DocIterator const & di1, DocIterator const & di2)
260 {
261         return !(di1 == di2);
262 }
263
264
265 // The difference to a ('non stable') DocIterator is the removed
266 // (overwritte by 0...) part of the CursorSlice data items. So this thing
267 // is suitable for external storage, but not for iteration as such.
268
269 class StableDocIterator {
270 public:
271         ///
272         StableDocIterator() {}
273         /// non-explicit intended
274         StableDocIterator(const DocIterator & it);
275         ///
276         DocIterator asDocIterator(InsetBase * start) const;
277         ///
278         size_t size() const { return data_.size(); }
279         ///  return the position within the paragraph
280         pos_type pos() const { return data_.back().pos(); }
281         ///  return the position within the paragraph
282         pos_type & pos() { return data_.back().pos(); }
283         ///
284         friend std::ostream &
285         operator<<(std::ostream & os, StableDocIterator const & cur);
286         ///
287         friend std::istream &
288         operator>>(std::istream & is, StableDocIterator & cur);
289         ///
290         friend bool
291         operator==(StableDocIterator const &, StableDocIterator const &);
292 private:
293         std::vector<CursorSlice> data_;
294 };
295
296
297 } // namespace lyx
298
299 #endif