4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "CursorSlice.h"
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>
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;
44 explicit DocIterator(Inset & inset);
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); }
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(); }
58 /// does this iterator have any content?
59 bool empty() const { return slices_.empty(); }
62 // access to slice at tip
65 CursorSlice & top() { return slices_.back(); }
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;
95 /// return the number of embedded cells
97 /// return the number of embedded cells
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;
114 bool boundary() const { return boundary_; }
116 void boundary(bool b) { boundary_ = b; }
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(); }
128 // math-specific part
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;
137 // text-specific part
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;
150 /// the containing inset or the cell, respectively
151 Inset * realInset() const;
153 Inset * innerInsetOfType(int code) const;
155 Text * innerText() const;
161 * move on one logical position, descend into nested insets
162 * including collapsed insets
166 * move on one logical position, descend into nested insets
167 * skip collapsed insets
169 void forwardPosIgnoreCollapsed();
170 /// move on one physical character or inset
172 /// move on one paragraph
174 /// move on one inset
176 /// move backward one logical position
178 /// move backward one physical character or inset
180 /// move backward one paragraph
182 /// move backward one inset
183 /// FIXME: This is not implemented!
184 //void backwardInset();
186 /// are we some 'extension' (i.e. deeper nested) of the given iterator
187 bool hasPart(DocIterator const & it) const;
190 friend std::ostream &
191 operator<<(std::ostream & os, DocIterator const & cur);
192 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
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 &);
199 friend class StableDocIterator;
202 void clear() { slices_.clear(); }
204 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
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.
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);
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
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.
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
244 std::vector<CursorSlice> const & internalData() const {
248 std::vector<CursorSlice> slices_;
254 DocIterator doc_iterator_begin(Inset & inset);
255 DocIterator doc_iterator_end(Inset & inset);
258 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
260 return di1.slices_ == di2.slices_;
264 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
266 return !(di1 == di2);
271 bool operator<(DocIterator const & p, DocIterator const & q)
273 size_t depth = std::min(p.depth(), q.depth());
274 for (size_t i = 0 ; i < depth ; ++i) {
278 return p.depth() < q.depth();
283 bool operator>(DocIterator const & p, DocIterator const & q)
290 bool operator<=(DocIterator const & p, DocIterator const & q)
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.
300 class StableDocIterator {
303 StableDocIterator() {}
304 /// non-explicit intended
305 StableDocIterator(const DocIterator & it);
307 DocIterator asDocIterator(Inset * start) const;
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(); }
315 friend std::ostream &
316 operator<<(std::ostream & os, StableDocIterator const & cur);
318 friend std::istream &
319 operator>>(std::istream & is, StableDocIterator & cur);
322 operator==(StableDocIterator const &, StableDocIterator const &);
324 std::vector<CursorSlice> data_;