]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
5e157ef9072c05d79134608758d7c18141989d9e
[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 class InsetIterator;
27
28
29 // The public inheritance should go in favour of a suitable data member
30 // (or maybe private inheritance) at some point of time.
31 class DocIterator // : public std::vector<CursorSlice>
32 {
33 public:
34         /// type for cell number in inset
35         typedef CursorSlice::idx_type idx_type;
36         /// type for row indices
37         typedef CursorSlice::row_type row_type;
38         /// type for col indices
39         typedef CursorSlice::col_type col_type;
40
41 public:
42         ///
43         DocIterator();
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         /// is this the end position?
60         bool atEnd() const { return slices_.empty(); }
61
62         //
63         // access to slice at tip
64         //
65         /// access to tip
66         CursorSlice & top() { return slices_.back(); }
67         /// access to tip
68         CursorSlice const & top() const { return slices_.back(); }
69         /// access to outermost slice
70         CursorSlice & bottom() { return slices_.front(); }
71         /// access to outermost slice
72         CursorSlice const & bottom() const { return slices_.front(); }
73         /// how many nested insets do we have?
74         size_t depth() const { return slices_.size(); }
75         /// the containing inset
76         Inset & inset() const { return top().inset(); }
77         /// return the cell of the inset this cursor is in
78         idx_type idx() const { return top().idx(); }
79         /// return the cell of the inset this cursor is in
80         idx_type & idx() { return top().idx(); }
81         /// return the last possible cell in this inset
82         idx_type lastidx() const;
83         /// return the paragraph this cursor is in
84         pit_type pit() const { return top().pit(); }
85         /// return the paragraph this cursor is in
86         pit_type & pit() { return top().pit(); }
87         /// return the last possible paragraph in this inset
88         pit_type lastpit() const;
89         /// return the position within the paragraph
90         pos_type pos() const { return top().pos(); }
91         /// return the position within the paragraph
92         pos_type & pos() { return top().pos(); }
93         /// return the last position within the paragraph
94         pos_type lastpos() const;
95
96         /// return the number of embedded cells
97         size_t nargs() const;
98         /// return the number of embedded cells
99         size_t ncols() const;
100         /// return the number of embedded cells
101         size_t nrows() const;
102         /// return the grid row of the top cell
103         row_type row() const;
104         /// return the last row of the top grid
105         row_type lastrow() const { return nrows() - 1; }
106         /// return the grid column of the top cell
107         col_type col() const;
108         /// return the last column of the top grid
109         col_type lastcol() const { return ncols() - 1; }
110         /// the inset just behind the cursor
111         Inset * nextInset() const;
112         /// the inset just in front of the cursor
113         Inset * prevInset() const;
114         ///
115         bool boundary() const { return boundary_; }
116         ///
117         void boundary(bool b) { boundary_ = b; }
118
119         // the two methods below have been inlined out because of
120         // profiling results under linux when opening a document.
121         /// are we in mathed?.
122         bool inMathed() const
123         { return !empty() && inset().inMathed(); }
124         /// are we in texted?.
125         bool inTexted() const
126         { return !empty() && !inset().inMathed(); }
127
128         //
129         // math-specific part
130         //
131         /// return the mathed cell this cursor is in
132         MathData & cell() const;
133         /// the mathatom left of the cursor
134         MathAtom & prevAtom() const;
135         /// the mathatom right of the cursor
136         MathAtom & nextAtom() const;
137
138         // text-specific part
139         //
140         /// the paragraph we're in in text mode.
141         /// \warning only works within text!
142         Paragraph & paragraph() const;
143         /// the paragraph we're in in any case.
144         /// This method will give the containing paragraph even
145         /// if not in text mode (ex: in mathed).
146         Paragraph & innerParagraph() const;
147         /// return the inner text slice.
148         CursorSlice const & innerTextSlice() const;
149         ///
150         Text * text() const;
151         /// the containing inset or the cell, respectively
152         Inset * realInset() const;
153         ///
154         Inset * innerInsetOfType(int code) const;
155         ///
156         Text * innerText() const;
157
158         //
159         // elementary moving
160         //
161         /**
162          * move on one logical position, descend into nested insets
163          * including collapsed insets
164          */
165         void forwardPos();
166         /**
167          * move on one logical position, descend into nested insets
168          * skip collapsed insets
169          */
170         void forwardPosIgnoreCollapsed();
171         /// move on one physical character or inset
172         void forwardChar();
173         /// move on one paragraph
174         void forwardPar();
175         /// move on one inset
176         void forwardInset();
177         /// move backward one logical position
178         void backwardPos();
179         /// move backward one physical character or inset
180         void backwardChar();
181         /// move backward one paragraph
182         void backwardPar();
183         /// move backward one inset
184         /// FIXME: This is not implemented!
185         //void backwardInset();
186
187         /// are we some 'extension' (i.e. deeper nested) of the given iterator
188         bool hasPart(DocIterator const & it) const;
189
190         /// output
191         friend std::ostream &
192         operator<<(std::ostream & os, DocIterator const & cur);
193         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
194         ///
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         friend bool operator<=(DocIterator const &, DocIterator const &);
199         ///
200         friend class StableDocIterator;
201 //protected:
202         ///
203         void clear() { slices_.clear(); }
204         ///
205         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
206         ///
207         void pop_back() { slices_.pop_back(); }
208         /// recompute the inset parts of the cursor from the document data
209         void updateInsets(Inset * inset);
210         /// fix DocIterator in circumstances that should never happen.
211         /// \return true if the DocIterator was fixed.
212         bool fixIfBroken();
213
214         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
215         int find(MathData const & cell) const;
216         /// find index of CursorSlice with inset() == inset (or -1 of not found)
217         int find(Inset const * inset) const;
218         /// cut off CursorSlices with index > above and store cut off slices in cut.
219         void cutOff(int above, std::vector<CursorSlice> & cut);
220         /// cut off CursorSlices with index > above
221         void cutOff(int above);
222         /// push CursorSlices on top
223         void append(std::vector<CursorSlice> const & x);
224         /// push one CursorSlice on top and set its index and position
225         void append(idx_type idx, pos_type pos);
226
227 private:
228         friend class InsetIterator;
229         friend DocIterator doc_iterator_begin(Inset & inset);
230         friend DocIterator doc_iterator_end(Inset & inset);
231         ///
232         explicit DocIterator(Inset & inset);
233         /**
234          * Normally, when the cursor is at position i, it is painted *before*
235          * the character at position i. However, what if we want the cursor 
236          * painted *after* position i? That's what boundary_ is for: if
237          * boundary_==true, the cursor is painted *after* position i-1, instead
238          * of before position i.
239          *
240          * Note 1: Usually, after i-1 or before i are actually the same place!
241          * However, this is not the case when i-1 and i are not painted 
242          * contiguously, and in these cases we sometimes do want to have control
243          * over whether to paint before i or after i-1.
244          * Some concrete examples of where this happens:
245          * a. i-1 at the end of one row, i at the beginning of next row
246          * b. in bidi text, at transitions between RTL and LTR or vice versa
247          *
248          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
249          * couldn't boundary_==true mean: *after* i? 
250          * Well, the reason is this: cursor position is not used only for 
251          * painting the cursor, but it also affects other things, for example:
252          * where the next insertion will be placed (it is inserted at the current
253          * position, pushing anything at the current position and beyond forward).
254          * Now, when the current position is i and boundary_==true, insertion would
255          * happen *before* i. If the cursor, however, were painted *after* i, that
256          * would be very unnatural...
257          */
258         bool boundary_;
259         ///
260         std::vector<CursorSlice> const & internalData() const {
261                 return slices_;
262         }
263         ///
264         std::vector<CursorSlice> slices_;
265         ///
266         Inset * inset_;
267 };
268
269
270 DocIterator doc_iterator_begin(Inset & inset);
271 DocIterator doc_iterator_end(Inset & inset);
272
273
274 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
275 {
276         return di1.slices_ == di2.slices_;
277 }
278
279
280 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
281 {
282         return !(di1 == di2);
283 }
284
285
286 inline
287 bool operator<(DocIterator const & p, DocIterator const & q)
288 {
289         size_t depth = std::min(p.depth(), q.depth());
290         for (size_t i = 0 ; i < depth ; ++i) {
291                 if (p[i] != q[i])
292                         return p[i] < q[i];
293         }
294         return p.depth() < q.depth();
295 }
296
297
298 inline  
299 bool operator>(DocIterator const & p, DocIterator const & q)
300 {
301         return q < p;
302 }
303
304
305 inline  
306 bool operator<=(DocIterator const & p, DocIterator const & q)
307 {
308         return !(q < p);
309 }
310
311
312 inline  
313 bool operator>=(DocIterator const & p, DocIterator const & q)
314 {
315         return !(p < q);
316 }
317
318
319 // The difference to a ('non stable') DocIterator is the removed
320 // (overwritten by 0...) part of the CursorSlice data items. So this thing
321 // is suitable for external storage, but not for iteration as such.
322
323 class StableDocIterator {
324 public:
325         ///
326         StableDocIterator() {}
327         /// non-explicit intended
328         StableDocIterator(const DocIterator & it);
329         ///
330         DocIterator asDocIterator(Inset * start) const;
331         ///
332         size_t size() const { return data_.size(); }
333         ///  return the position within the paragraph
334         pos_type pos() const { return data_.back().pos(); }
335         ///  return the position within the paragraph
336         pos_type & pos() { return data_.back().pos(); }
337         ///
338         friend std::ostream &
339         operator<<(std::ostream & os, StableDocIterator const & cur);
340         ///
341         friend std::istream &
342         operator>>(std::istream & is, StableDocIterator & cur);
343         ///
344         friend bool
345         operator==(StableDocIterator const &, StableDocIterator const &);
346 private:
347         std::vector<CursorSlice> data_;
348 };
349
350
351 } // namespace lyx
352
353 #endif