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