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 * skip collapsed insets if \p ignorecollapsed is true
164 void forwardPos(bool ignorecollapsed = false);
165 /// move on one physical character or inset
167 /// move on one paragraph
169 /// move on one inset
171 /// move backward one logical position
173 /// move backward one physical character or inset
175 /// move backward one paragraph
177 /// move backward one inset
178 /// FIXME: This is not implemented!
179 //void backwardInset();
181 /// are we some 'extension' (i.e. deeper nested) of the given iterator
182 bool hasPart(DocIterator const & it) const;
185 friend std::ostream &
186 operator<<(std::ostream & os, DocIterator const & cur);
187 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
189 friend bool operator==(DocIterator const &, DocIterator const &);
190 friend bool operator<(DocIterator const &, DocIterator const &);
191 friend bool operator>(DocIterator const &, DocIterator const &);
192 friend bool operator<=(DocIterator const &, DocIterator const &);
194 friend class StableDocIterator;
197 void clear() { slices_.clear(); }
199 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
201 void pop_back() { slices_.pop_back(); }
202 /// recompute the inset parts of the cursor from the document data
203 void updateInsets(Inset * inset);
204 /// fix DocIterator in circumstances that should never happen.
205 /// \return true if the DocIterator was fixed.
208 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
209 idx_type find(MathData const & cell) const;
210 /// find index of CursorSlice with inset() == inset (or -1 of not found)
211 idx_type find(InsetMath const * inset) const;
212 /// cut off CursorSlices with index > above and store cut off slices in cut
213 void cutOff(idx_type above, std::vector<CursorSlice> & cut);
214 /// cut off CursorSlices with index > above
215 void cutOff(idx_type above);
216 /// push CursorSlices on top
217 void append(std::vector<CursorSlice> const & x);
218 /// push one CursorSlice on top and set its index and position
219 void append(idx_type idx, pos_type pos);
223 * When the cursor position is i, is the cursor after the i-th char
224 * or before the i+1-th char ? Normally, these two interpretations are
225 * equivalent, except when the fonts of the i-th and i+1-th char
227 * We use boundary_ to distinguish between the two options:
228 * If boundary_=true, then the cursor is after the i-th char
229 * and if boundary_=false, then the cursor is before the i+1-th char.
231 * We currently use the boundary only when the language direction of
232 * the i-th char is different than the one of the i+1-th char.
233 * In this case it is important to distinguish between the two
234 * cursor interpretations, in order to give a reasonable behavior to
239 std::vector<CursorSlice> const & internalData() const {
243 std::vector<CursorSlice> slices_;
249 DocIterator doc_iterator_begin(Inset & inset);
250 DocIterator doc_iterator_end(Inset & inset);
253 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
255 return di1.slices_ == di2.slices_;
259 inline 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_;