]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
4feff6c4a8c3ff3bda68e01923b637f314dc0f67
[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          * Normally, when the cursor is at position i, it is painted *before*
229          * the character at position i. However, what if we want the cursor 
230          * painted *after* position i? That's what boundary_ is for: if
231          * boundary_==true, the cursor is painted *after* position i-1, instead
232          * of before position i.
233          *
234          * Note 1: Usually, after i-1 or before i are actually the same place!
235          * However, this is not the case when i-1 and i are not painted 
236          * contiguously, and in these cases we sometimes do want to have control
237          * over whether to paint before i or after i-1.
238          * Some concrete examples of where this happens:
239          * a. i-1 at the end of one row, i at the beginning of next row
240          * b. in bidi text, at transitions between RTL and LTR or vice versa
241          *
242          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
243          * couldn't boundary_==true mean: *after* i? 
244          * Well, the reason is this: cursor position is not used only for 
245          * painting the cursor, but it also affects other things, for example:
246          * where the next insertion will be placed (it is inserted at the current
247          * position, pushing anything at the current position and beyond forward).
248          * Now, when the current position is i and boundary_==true, insertion would
249          * happen *before* i. If the cursor, however, were painted *after* i, that
250          * would be very unnatural...
251          */
252         bool boundary_;
253         ///
254         std::vector<CursorSlice> const & internalData() const {
255                 return slices_;
256         }
257         ///
258         std::vector<CursorSlice> slices_;
259         ///
260         Inset * inset_;
261 };
262
263
264 DocIterator doc_iterator_begin(Inset & inset);
265 DocIterator doc_iterator_end(Inset & inset);
266
267
268 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
269 {
270         return di1.slices_ == di2.slices_;
271 }
272
273
274 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
275 {
276         return !(di1 == di2);
277 }
278
279
280 inline
281 bool operator<(DocIterator const & p, DocIterator const & q)
282 {
283         size_t depth = std::min(p.depth(), q.depth());
284         for (size_t i = 0 ; i < depth ; ++i) {
285                 if (p[i] != q[i])
286                         return p[i] < q[i];
287         }
288         return p.depth() < q.depth();
289 }
290
291
292 inline  
293 bool operator>(DocIterator const & p, DocIterator const & q)
294 {
295         return q < p;
296 }
297
298
299 inline  
300 bool operator<=(DocIterator const & p, DocIterator const & q)
301 {
302         return !(q < p);
303 }
304
305
306 // The difference to a ('non stable') DocIterator is the removed
307 // (overwritten by 0...) part of the CursorSlice data items. So this thing
308 // is suitable for external storage, but not for iteration as such.
309
310 class StableDocIterator {
311 public:
312         ///
313         StableDocIterator() {}
314         /// non-explicit intended
315         StableDocIterator(const DocIterator & it);
316         ///
317         DocIterator asDocIterator(Inset * start) const;
318         ///
319         size_t size() const { return data_.size(); }
320         ///  return the position within the paragraph
321         pos_type pos() const { return data_.back().pos(); }
322         ///  return the position within the paragraph
323         pos_type & pos() { return data_.back().pos(); }
324         ///
325         friend std::ostream &
326         operator<<(std::ostream & os, StableDocIterator const & cur);
327         ///
328         friend std::istream &
329         operator>>(std::istream & is, StableDocIterator & cur);
330         ///
331         friend bool
332         operator==(StableDocIterator const &, StableDocIterator const &);
333 private:
334         std::vector<CursorSlice> data_;
335 };
336
337
338 } // namespace lyx
339
340 #endif