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