]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
* "Goto label" in reference dialog works with master and child documents
[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         /// are we in regexp-mode ?
128         bool inRegexped() const;
129
130         //
131         // math-specific part
132         //
133         /// return the mathed cell this cursor is in
134         MathData & cell() const;
135         /// the mathatom left of the cursor
136         MathAtom & prevAtom() const;
137         /// the mathatom right of the cursor
138         MathAtom & nextAtom() const;
139
140         // text-specific part
141         //
142         /// the paragraph we're in in text mode.
143         /// \warning only works within text!
144         Paragraph & paragraph() const;
145         /// the paragraph we're in in any case.
146         /// This method will give the containing paragraph even
147         /// if not in text mode (ex: in mathed).
148         Paragraph & innerParagraph() const;
149         /// return the inner text slice.
150         CursorSlice const & innerTextSlice() const;
151         ///
152         Text * text() const;
153         /// the containing inset or the cell, respectively
154         Inset * realInset() const;
155         ///
156         Inset * innerInsetOfType(int code) const;
157         ///
158         Text * innerText() const;
159
160         //
161         // elementary moving
162         //
163         /**
164          * move on one logical position, descend into nested insets
165          * including collapsed insets
166          */
167         void forwardPos();
168         /**
169          * move on one logical position, descend into nested insets
170          * skip collapsed insets
171          */
172         void forwardPosIgnoreCollapsed();
173         /// move on one physical character or inset
174         void forwardChar();
175         /// move on one paragraph
176         void forwardPar();
177         /// move on one inset
178         void forwardInset();
179         /// move backward one logical position
180         void backwardPos();
181         /// move backward one physical character or inset
182         void backwardChar();
183         /// move backward one paragraph
184         void backwardPar();
185         /// move backward one inset
186         /// FIXME: This is not implemented!
187         //void backwardInset();
188
189         /// are we some 'extension' (i.e. deeper nested) of the given iterator
190         bool hasPart(DocIterator const & it) const;
191
192         /// output
193         friend std::ostream &
194         operator<<(std::ostream & os, DocIterator const & cur);
195         friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
196         ///
197         friend bool operator==(DocIterator const &, DocIterator const &);
198         friend bool operator<(DocIterator const &, DocIterator const &);
199         friend bool operator>(DocIterator const &, DocIterator const &);
200         friend bool operator<=(DocIterator const &, DocIterator const &);
201         ///
202         friend class StableDocIterator;
203 //protected:
204         ///
205         void clear() { slices_.clear(); }
206         ///
207         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
208         ///
209         void pop_back() { slices_.pop_back(); }
210         /// recompute the inset parts of the cursor from the document data
211         void updateInsets(Inset * inset);
212         /// fix DocIterator in circumstances that should never happen.
213         /// \return true if the DocIterator was fixed.
214         bool fixIfBroken();
215
216         /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
217         int find(MathData const & cell) const;
218         /// find index of CursorSlice with inset() == inset (or -1 of not found)
219         int find(Inset const * inset) const;
220         /// cut off CursorSlices with index > above and store cut off slices in cut.
221         void cutOff(int above, std::vector<CursorSlice> & cut);
222         /// cut off CursorSlices with index > above
223         void cutOff(int above);
224         /// push CursorSlices on top
225         void append(std::vector<CursorSlice> const & x);
226         /// push one CursorSlice on top and set its index and position
227         void append(idx_type idx, pos_type pos);
228
229 private:
230         friend class InsetIterator;
231         friend DocIterator doc_iterator_begin(Inset & inset);
232         friend DocIterator doc_iterator_end(Inset & inset);
233         ///
234         explicit DocIterator(Inset & inset);
235         /**
236          * Normally, when the cursor is at position i, it is painted *before*
237          * the character at position i. However, what if we want the cursor 
238          * painted *after* position i? That's what boundary_ is for: if
239          * boundary_==true, the cursor is painted *after* position i-1, instead
240          * of before position i.
241          *
242          * Note 1: Usually, after i-1 or before i are actually the same place!
243          * However, this is not the case when i-1 and i are not painted 
244          * contiguously, and in these cases we sometimes do want to have control
245          * over whether to paint before i or after i-1.
246          * Some concrete examples of where this happens:
247          * a. i-1 at the end of one row, i at the beginning of next row
248          * b. in bidi text, at transitions between RTL and LTR or vice versa
249          *
250          * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i, 
251          * couldn't boundary_==true mean: *after* i? 
252          * Well, the reason is this: cursor position is not used only for 
253          * painting the cursor, but it also affects other things, for example:
254          * where the next insertion will be placed (it is inserted at the current
255          * position, pushing anything at the current position and beyond forward).
256          * Now, when the current position is i and boundary_==true, insertion would
257          * happen *before* i. If the cursor, however, were painted *after* i, that
258          * would be very unnatural...
259          */
260         bool boundary_;
261         ///
262         std::vector<CursorSlice> const & internalData() const {
263                 return slices_;
264         }
265         ///
266         std::vector<CursorSlice> slices_;
267         ///
268         Inset * inset_;
269 };
270
271
272 DocIterator doc_iterator_begin(Inset & inset);
273 DocIterator doc_iterator_end(Inset & inset);
274
275
276 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
277 {
278         return di1.slices_ == di2.slices_;
279 }
280
281
282 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
283 {
284         return !(di1 == di2);
285 }
286
287
288 inline
289 bool operator<(DocIterator const & p, DocIterator const & q)
290 {
291         size_t depth = std::min(p.depth(), q.depth());
292         for (size_t i = 0 ; i < depth ; ++i) {
293                 if (p[i] != q[i])
294                         return p[i] < q[i];
295         }
296         return p.depth() < q.depth();
297 }
298
299
300 inline  
301 bool operator>(DocIterator const & p, DocIterator const & q)
302 {
303         return q < p;
304 }
305
306
307 inline  
308 bool operator<=(DocIterator const & p, DocIterator const & q)
309 {
310         return !(q < p);
311 }
312
313
314 inline  
315 bool operator>=(DocIterator const & p, DocIterator const & q)
316 {
317         return !(p < q);
318 }
319
320
321 // The difference to a ('non stable') DocIterator is the removed
322 // (overwritten by 0...) part of the CursorSlice data items. So this thing
323 // is suitable for external storage, but not for iteration as such.
324
325 class StableDocIterator {
326 public:
327         ///
328         StableDocIterator() {}
329         /// non-explicit intended
330         StableDocIterator(const DocIterator & it);
331         ///
332         DocIterator asDocIterator(Inset * start) const;
333         ///
334         size_t size() const { return data_.size(); }
335         ///  return the position within the paragraph
336         pos_type pos() const { return data_.back().pos(); }
337         ///  return the position within the paragraph
338         pos_type & pos() { return data_.back().pos(); }
339         ///
340         friend std::ostream &
341         operator<<(std::ostream & os, StableDocIterator const & cur);
342         ///
343         friend std::istream &
344         operator>>(std::istream & is, StableDocIterator & cur);
345         ///
346         friend bool
347         operator==(StableDocIterator const &, StableDocIterator const &);
348 private:
349         std::vector<CursorSlice> data_;
350 };
351
352
353 } // namespace lyx
354
355 #endif