]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
* src/LyXRC.{cpp,h}:
[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 "CursorSlice.h"
16
17 #include <vector>
18
19
20 namespace lyx {
21
22 class Text;
23 class MathAtom;
24 class Paragraph;
25
26
27 // The public inheritance should go in favour of a suitable data member
28 // (or maybe private inheritance) at some point of time.
29 class DocIterator // : public std::vector<CursorSlice>
30 {
31 public:
32         /// type for cell number in inset
33         typedef CursorSlice::idx_type idx_type;
34         /// type for row indices
35         typedef CursorSlice::row_type row_type;
36         /// type for col indices
37         typedef CursorSlice::col_type col_type;
38
39 public:
40         ///
41         DocIterator();
42         ///
43         explicit DocIterator(Inset & inset);
44
45         /// access slice at position \p i
46         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
47         /// access slice at position \p i
48         CursorSlice & operator[](size_t i) { return slices_[i]; }
49         /// chop a few slices from the iterator
50         void resize(size_t i) { slices_.resize(i); }
51
52         /// is the iterator valid?
53         operator const void*() const { return empty() ? 0 : this; }
54         /// is this iterator invalid?
55         bool operator!() const { return empty(); }
56
57         /// does this iterator have any content?
58         bool empty() const { return slices_.empty(); }
59
60         //
61         // access to slice at tip
62         //
63         /// access to tip
64         CursorSlice & top() { return slices_.back(); }
65         /// access to tip
66         CursorSlice const & top() const { return slices_.back(); }
67         /// access to outermost slice
68         CursorSlice & bottom() { return slices_.front(); }
69         /// access to outermost slice
70         CursorSlice const & bottom() const { return slices_.front(); }
71         /// how many nested insets do we have?
72         size_t depth() const { return slices_.size(); }
73         /// the containing inset
74         Inset & inset() const { return top().inset(); }
75         /// return the cell of the inset this cursor is in
76         idx_type idx() const { return top().idx(); }
77         /// return the cell of the inset this cursor is in
78         idx_type & idx() { return top().idx(); }
79         /// return the last possible cell in this inset
80         idx_type lastidx() const;
81         /// return the paragraph this cursor is in
82         pit_type pit() const { return top().pit(); }
83         /// return the paragraph this cursor is in
84         pit_type & pit() { return top().pit(); }
85         /// return the last possible paragraph in this inset
86         pit_type lastpit() const;
87         /// return the position within the paragraph
88         pos_type pos() const { return top().pos(); }
89         /// return the position within the paragraph
90         pos_type & pos() { return top().pos(); }
91         /// return the last position within the paragraph
92         pos_type lastpos() const;
93
94         /// return the number of embedded cells
95         size_t nargs() const;
96         /// return the number of embedded cells
97         size_t ncols() const;
98         /// return the number of embedded cells
99         size_t nrows() const;
100         /// return the grid row of the top cell
101         row_type row() const;
102         /// return the last row of the top grid
103         row_type lastrow() const { return nrows() - 1; }
104         /// return the grid column of the top cell
105         col_type col() const;
106         /// return the last column of the top grid
107         col_type lastcol() const { return ncols() - 1; }
108         /// the inset just behind the cursor
109         Inset * nextInset() const;
110         /// the inset just in front of the cursor
111         Inset * prevInset() const;
112         ///
113         bool boundary() const { return boundary_; }
114         ///
115         void boundary(bool b) { boundary_ = b; }
116
117         // the two methods below have been inlined out because of
118         // profiling results under linux when opening a document.
119         /// are we in mathed?.
120         bool inMathed() const
121         { return !empty() && inset().inMathed(); }
122         /// are we in texted?.
123         bool inTexted() const
124         { return !empty() && !inset().inMathed(); }
125
126         //
127         // math-specific part
128         //
129         /// return the mathed cell this cursor is in
130         MathData & cell() const;
131         /// the mathatom left of the cursor
132         MathAtom & prevAtom() const;
133         /// the mathatom right of the cursor
134         MathAtom & nextAtom() const;
135
136         // text-specific part
137         //
138         /// the paragraph we're in in text mode.
139         /// \warning only works within text!
140         Paragraph & paragraph() const;
141         /// the paragraph we're in in any case.
142         /// This method will give the containing paragraph even
143         /// if not in text mode (ex: in mathed).
144         Paragraph & innerParagraph() const;
145         /// return the inner text slice.
146         CursorSlice const & innerTextSlice() const;
147         ///
148         Text * text() const;
149         /// the containing inset or the cell, respectively
150         Inset * realInset() const;
151         ///
152         Inset * innerInsetOfType(int code) const;
153         ///
154         Text * innerText() const;
155
156         //
157         // elementary moving
158         //
159         /**
160          * move on one logical position, descend into nested insets
161          * skip collapsed insets if \p ignorecollapsed is true
162          */
163         void forwardPos(bool ignorecollapsed = false);
164         /// move on one physical character or inset
165         void forwardChar();
166         /// move on one paragraph
167         void forwardPar();
168         /// move on one inset
169         void forwardInset();
170         /// move backward one logical position
171         void backwardPos();
172         /// move backward one physical character or inset
173         void backwardChar();
174         /// move backward one paragraph
175         void backwardPar();
176         /// move backward one inset
177         /// FIXME: This is not implemented!
178         //void backwardInset();
179
180         /// are we some 'extension' (i.e. deeper nested) of the given iterator
181         bool hasPart(DocIterator const & it) const;
182
183         /// output
184         friend std::ostream &
185         operator<<(std::ostream & os, DocIterator const & cur);
186         ///
187         friend bool operator==(DocIterator const &, DocIterator const &);
188         friend bool operator<(DocIterator const &, DocIterator const &);
189         friend bool operator>(DocIterator const &, DocIterator const &);
190         friend bool operator<=(DocIterator const &, DocIterator const &);
191         ///
192         friend class StableDocIterator;
193 //protected:
194         ///
195         void clear() { slices_.clear(); }
196         ///
197         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
198         ///
199         void pop_back() { slices_.pop_back(); }
200         /// recompute the inset parts of the cursor from the document data
201         void updateInsets(Inset * inset);
202         /// fix DocIterator in circumstances that should never happen.
203         /// \return true if the DocIterator was fixed.
204         bool fixIfBroken();
205
206         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
207         idx_type find(MathData const & cell) const;
208         /// find index of CursorSlice with inset() == inset (or -1 of not found)
209         idx_type find(InsetMath const * inset) const;
210         /// cut off CursorSlices with index > above and store cut off slices in cut
211         void cutOff(idx_type above, std::vector<CursorSlice> & cut);
212         /// cut off CursorSlices with index > above
213         void cutOff(idx_type above);
214         /// push CursorSlices on top
215         void append(std::vector<CursorSlice> const & x);
216         /// push one CursorSlice on top and set its index and position
217         void append(idx_type idx, pos_type pos);
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         Inset * inset_;
244 };
245
246
247 DocIterator doc_iterator_begin(Inset & inset);
248 DocIterator doc_iterator_end(Inset & 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 // (overwritten 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(Inset * 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