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"
27 // The public inheritance should go in favour of a suitable data member
28 // (or maybe private inheritance) at some point of time.
29 class DocIterator // : public std::vector<CursorSlice>
32 /// type for cell number in inset
33 typedef CursorSlice::idx_type idx_type;
34 /// type for row indices
35 typedef CursorSlice::row_type row_type;
36 /// type for col indices
37 typedef CursorSlice::col_type col_type;
43 explicit DocIterator(Inset & inset);
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); }
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(); }
57 /// does this iterator have any content?
58 bool empty() const { return slices_.empty(); }
61 // access to slice at tip
64 CursorSlice & top() { return slices_.back(); }
66 CursorSlice const & top() const { return slices_.back(); }
67 /// access to outermost slice
68 CursorSlice & bottom() { return slices_.front(); }
69 /// access to outermost slice
70 CursorSlice const & bottom() const { return slices_.front(); }
71 /// how many nested insets do we have?
72 size_t depth() const { return slices_.size(); }
73 /// the containing inset
74 Inset & inset() const { return top().inset(); }
75 /// return the cell of the inset this cursor is in
76 idx_type idx() const { return top().idx(); }
77 /// return the cell of the inset this cursor is in
78 idx_type & idx() { return top().idx(); }
79 /// return the last possible cell in this inset
80 idx_type lastidx() const;
81 /// return the paragraph this cursor is in
82 pit_type pit() const { return top().pit(); }
83 /// return the paragraph this cursor is in
84 pit_type & pit() { return top().pit(); }
85 /// return the last possible paragraph in this inset
86 pit_type lastpit() const;
87 /// return the position within the paragraph
88 pos_type pos() const { return top().pos(); }
89 /// return the position within the paragraph
90 pos_type & pos() { return top().pos(); }
91 /// return the last position within the paragraph
92 pos_type lastpos() const;
94 /// return the number of embedded cells
96 /// return the number of embedded cells
98 /// return the number of embedded cells
100 /// return the grid row of the top cell
101 row_type row() const;
102 /// return the last row of the top grid
103 row_type lastrow() const { return nrows() - 1; }
104 /// return the grid column of the top cell
105 col_type col() const;
106 /// return the last column of the top grid
107 col_type lastcol() const { return ncols() - 1; }
108 /// the inset just behind the cursor
109 Inset * nextInset() const;
110 /// the inset just in front of the cursor
111 Inset * prevInset() const;
113 bool boundary() const { return boundary_; }
115 void boundary(bool b) { boundary_ = b; }
117 // the two methods below have been inlined out because of
118 // profiling results under linux when opening a document.
119 /// are we in mathed?.
120 bool inMathed() const
121 { return !empty() && inset().inMathed(); }
122 /// are we in texted?.
123 bool inTexted() const
124 { return !empty() && !inset().inMathed(); }
127 // math-specific part
129 /// return the mathed cell this cursor is in
130 MathData & cell() const;
131 /// the mathatom left of the cursor
132 MathAtom & prevAtom() const;
133 /// the mathatom right of the cursor
134 MathAtom & nextAtom() const;
136 // text-specific part
138 /// the paragraph we're in in text mode.
139 /// \warning only works within text!
140 Paragraph & paragraph() const;
141 /// the paragraph we're in in any case.
142 /// This method will give the containing paragraph even
143 /// if not in text mode (ex: in mathed).
144 Paragraph & innerParagraph() const;
145 /// return the inner text slice.
146 CursorSlice const & innerTextSlice() const;
149 /// the containing inset or the cell, respectively
150 Inset * realInset() const;
152 Inset * innerInsetOfType(int code) const;
154 Text * innerText() const;
160 * move on one logical position, descend into nested insets
161 * skip collapsed insets if \p ignorecollapsed is true
163 void forwardPos(bool ignorecollapsed = false);
164 /// move on one physical character or inset
166 /// move on one paragraph
168 /// move on one inset
170 /// move backward one logical position
172 /// move backward one physical character or inset
174 /// move backward one paragraph
176 /// move backward one inset
177 /// FIXME: This is not implemented!
178 //void backwardInset();
180 /// are we some 'extension' (i.e. deeper nested) of the given iterator
181 bool hasPart(DocIterator const & it) const;
184 friend std::ostream &
185 operator<<(std::ostream & os, DocIterator const & cur);
187 friend bool operator==(DocIterator const &, DocIterator const &);
188 friend bool operator<(DocIterator const &, DocIterator const &);
189 friend bool operator>(DocIterator const &, DocIterator const &);
190 friend bool operator<=(DocIterator const &, DocIterator const &);
192 friend class StableDocIterator;
195 void clear() { slices_.clear(); }
197 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
199 void pop_back() { slices_.pop_back(); }
200 /// recompute the inset parts of the cursor from the document data
201 void updateInsets(Inset * inset);
202 /// fix DocIterator in circumstances that should never happen.
203 /// \return true if the DocIterator was fixed.
206 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
207 idx_type find(MathData const & cell) const;
208 /// find index of CursorSlice with inset() == inset (or -1 of not found)
209 idx_type find(InsetMath const * inset) const;
210 /// cut off CursorSlices with index > above and store cut off slices in cut
211 void cutOff(idx_type above, std::vector<CursorSlice> & cut);
212 /// cut off CursorSlices with index > above
213 void cutOff(idx_type above);
214 /// push CursorSlices on top
215 void append(std::vector<CursorSlice> const & x);
216 /// push one CursorSlice on top and set its index and position
217 void append(idx_type idx, pos_type pos);
221 * When the cursor position is i, is the cursor after the i-th char
222 * or before the i+1-th char ? Normally, these two interpretations are
223 * equivalent, except when the fonts of the i-th and i+1-th char
225 * We use boundary_ to distinguish between the two options:
226 * If boundary_=true, then the cursor is after the i-th char
227 * and if boundary_=false, then the cursor is before the i+1-th char.
229 * We currently use the boundary only when the language direction of
230 * the i-th char is different than the one of the i+1-th char.
231 * In this case it is important to distinguish between the two
232 * cursor interpretations, in order to give a reasonable behavior to
237 std::vector<CursorSlice> const & internalData() const {
241 std::vector<CursorSlice> slices_;
247 DocIterator doc_iterator_begin(Inset & inset);
248 DocIterator doc_iterator_end(Inset & inset);
252 bool operator==(DocIterator const & di1, DocIterator const & di2)
254 return di1.slices_ == di2.slices_;
259 bool operator!=(DocIterator const & di1, DocIterator const & di2)
261 return !(di1 == di2);
265 // The difference to a ('non stable') DocIterator is the removed
266 // (overwritten by 0...) part of the CursorSlice data items. So this thing
267 // is suitable for external storage, but not for iteration as such.
269 class StableDocIterator {
272 StableDocIterator() {}
273 /// non-explicit intended
274 StableDocIterator(const DocIterator & it);
276 DocIterator asDocIterator(Inset * start) const;
278 size_t size() const { return data_.size(); }
279 /// return the position within the paragraph
280 pos_type pos() const { return data_.back().pos(); }
281 /// return the position within the paragraph
282 pos_type & pos() { return data_.back().pos(); }
284 friend std::ostream &
285 operator<<(std::ostream & os, StableDocIterator const & cur);
287 friend std::istream &
288 operator>>(std::istream & is, StableDocIterator & cur);
291 operator==(StableDocIterator const &, StableDocIterator const &);
293 std::vector<CursorSlice> data_;