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 "cursor_slice.h"
27 // only needed for gcc 2.95, remove when support terminated
28 template <typename A, typename B>
29 bool ptr_cmp(A const * a, B const * b)
35 // The public inheritance should go in favour of a suitable data member
36 // (or maybe private inheritance) at some point of time.
37 class DocIterator // : public std::vector<CursorSlice>
40 /// type for cell number in inset
41 typedef CursorSlice::idx_type idx_type;
42 /// type for paragraph numbers positions within a cell
43 typedef CursorSlice::pit_type pit_type;
44 /// type for cursor positions within a cell
45 typedef CursorSlice::pos_type pos_type;
46 /// type for row indices
47 typedef CursorSlice::row_type row_type;
48 /// type for col indices
49 typedef CursorSlice::col_type col_type;
55 explicit DocIterator(InsetBase & inset);
57 /// access slice at position \p i
58 CursorSlice const & operator[](size_t i) const { return slices_[i]; }
59 /// access slice at position \p i
60 CursorSlice & operator[](size_t i) { return slices_[i]; }
61 /// chop a few slices from the iterator
62 void resize(size_t i) { slices_.resize(i); }
64 /// is the iterator valid?
65 operator const void*() const { return empty() ? 0 : this; }
66 /// is this iterator invalid?
67 bool operator!() const { return empty(); }
69 /// does this iterator have any content?
70 bool empty() const { return slices_.empty(); }
73 // access to slice at tip
76 CursorSlice & top() { return slices_.back(); }
78 CursorSlice const & top() const { return slices_.back(); }
79 /// access to outermost slice
80 CursorSlice & bottom() { return slices_.front(); }
81 /// access to outermost slice
82 CursorSlice const & bottom() const { return slices_.front(); }
83 /// how many nested insets do we have?
84 size_t depth() const { return slices_.size(); }
85 /// the containing inset
86 InsetBase & inset() const { return top().inset(); }
87 /// return the cell of the inset this cursor is in
88 idx_type idx() const { return top().idx(); }
89 /// return the cell of the inset this cursor is in
90 idx_type & idx() { return top().idx(); }
91 /// return the last possible cell in this inset
92 idx_type lastidx() const;
93 /// return the paragraph this cursor is in
94 pit_type pit() const { return top().pit(); }
95 /// return the paragraph this cursor is in
96 pit_type & pit() { return top().pit(); }
97 /// return the last possible paragraph in this inset
98 pit_type lastpit() const;
99 /// return the position within the paragraph
100 pos_type pos() const { return top().pos(); }
101 /// return the position within the paragraph
102 pos_type & pos() { return top().pos(); }
103 /// return the last position within the paragraph
104 pos_type lastpos() const;
106 /// return the number of embedded cells
107 size_t nargs() const;
108 /// return the number of embedded cells
109 size_t ncols() const;
110 /// return the number of embedded cells
111 size_t nrows() const;
112 /// return the grid row of the top cell
113 row_type row() const;
114 /// return the last row of the top grid
115 row_type lastrow() const { return nrows() - 1; }
116 /// return the grid column of the top cell
117 col_type col() const;
118 /// return the last column of the top grid
119 col_type lastcol() const { return ncols() - 1; }
120 /// the inset just behind the cursor
121 InsetBase * nextInset();
122 /// the inset just in front of the cursor
123 InsetBase * prevInset();
124 /// the inset just in front of the cursor
125 InsetBase const * prevInset() const;
127 bool boundary() const { return boundary_; }
129 void boundary(bool b) { boundary_ = b; }
131 /// are we in mathed?
132 bool inMathed() const;
133 /// are we in texted?
134 bool inTexted() const;
137 // math-specific part
139 /// return the mathed cell this cursor is in
140 MathArray const & cell() const;
141 /// return the mathed cell this cursor is in
143 /// the mathatom left of the cursor
144 MathAtom const & prevAtom() const;
145 /// the mathatom left of the cursor
146 MathAtom & prevAtom();
147 /// the mathatom right of the cursor
148 MathAtom const & nextAtom() const;
149 /// the mathatom right of the cursor
150 MathAtom & nextAtom();
153 // text-specific part
155 /// the paragraph we're in
156 Paragraph & paragraph();
157 /// the paragraph we're in
158 Paragraph const & paragraph() const;
159 /// the row in the paragraph we're in
161 /// the row in the paragraph we're in
162 Row const & textRow() const;
166 LyXText const * text() const;
167 /// the containing inset or the cell, respectively
168 InsetBase * realInset() const;
170 InsetBase * innerInsetOfType(int code) const;
172 LyXText * innerText();
174 LyXText const * innerText() const;
179 /// move on one logical position, do not descend into nested insets
180 void forwardPosNoDescend();
182 * move on one logical position, descend into nested insets
183 * skip collapsed insets if \p ignorecollapsed is true
185 void forwardPos(bool ignorecollapsed = false);
186 /// move on one physical character or inset
188 /// move on one paragraph
192 /// move on one inset
194 /// move backward one logical position
196 /// move backward one physical character or inset
198 /// move backward one paragraph
200 /// move backward one cell
202 /// move backward one inset
203 void backwardInset();
205 /// are we some 'extension' (i.e. deeper nested) of the given iterator
206 bool hasPart(DocIterator const & it) const;
209 friend std::ostream &
210 operator<<(std::ostream & os, DocIterator const & cur);
212 friend bool operator==(DocIterator const &, DocIterator const &);
214 friend class StableDocIterator;
217 void clear() { slices_.clear(); }
219 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
221 void pop_back() { slices_.pop_back(); }
222 /// recompute the inset parts of the cursor from the document data
223 void updateInsets(InsetBase * inset);
227 * When the cursor position is i, is the cursor after the i-th char
228 * or before the i+1-th char ? Normally, these two interpretations are
229 * equivalent, except when the fonts of the i-th and i+1-th char
231 * We use boundary_ to distinguish between the two options:
232 * If boundary_=true, then the cursor is after the i-th char
233 * and if boundary_=false, then the cursor is before the i+1-th char.
235 * We currently use the boundary only when the language direction of
236 * the i-th char is different than the one of the i+1-th char.
237 * In this case it is important to distinguish between the two
238 * cursor interpretations, in order to give a reasonable behavior to
243 std::vector<CursorSlice> const & internalData() const {
247 std::vector<CursorSlice> slices_;
253 DocIterator doc_iterator_begin(InsetBase & inset);
254 DocIterator doc_iterator_end(InsetBase & inset);
258 bool operator==(DocIterator const & di1, DocIterator const & di2)
260 return di1.slices_ == di2.slices_;
265 bool operator!=(DocIterator const & di1, DocIterator const & di2)
267 return !(di1 == di2);
271 // The difference to a ('non stable') DocIterator is the removed
272 // (overwritte by 0...) part of the CursorSlice data items. So this thing
273 // is suitable for external storage, but not for iteration as such.
275 class StableDocIterator {
278 StableDocIterator() {}
279 /// non-explicit intended
280 StableDocIterator(const DocIterator & it);
282 DocIterator asDocIterator(InsetBase * start) const;
284 size_t size() const { return data_.size(); }
285 /// type for cursor positions within a cell
286 typedef CursorSlice::pos_type pos_type;
287 /// return the position within the paragraph
288 pos_type pos() const { return data_.back().pos(); }
289 /// return the position within the paragraph
290 pos_type & pos() { return data_.back().pos(); }
292 friend std::ostream &
293 operator<<(std::ostream & os, StableDocIterator const & cur);
295 friend std::istream &
296 operator>>(std::istream & is, StableDocIterator & cur);
299 operator==(StableDocIterator const &, StableDocIterator const &);
301 std::vector<CursorSlice> data_;