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"
26 // only needed for gcc 2.95, remove when support terminated
27 template <typename A, typename B>
28 bool ptr_cmp(A const * a, B const * b)
34 // The public inheritance should go in favour of a suitable data member
35 // (or maybe private inheritance) at some point of time.
36 class DocIterator // : public std::vector<CursorSlice>
39 /// type for cell number in inset
40 typedef CursorSlice::idx_type idx_type;
41 /// type for paragraph numbers positions within a cell
42 typedef CursorSlice::pit_type pit_type;
43 /// type for cursor positions within a cell
44 typedef CursorSlice::pos_type pos_type;
45 /// type for row indices
46 typedef CursorSlice::row_type row_type;
47 /// type for col indices
48 typedef CursorSlice::col_type col_type;
54 explicit DocIterator(InsetBase & inset);
56 /// access slice at position \p i
57 CursorSlice const & operator[](size_t i) const { return slices_[i]; }
58 /// access slice at position \p i
59 CursorSlice & operator[](size_t i) { return slices_[i]; }
60 /// chop a few slices from the iterator
61 void resize(size_t i) { slices_.resize(i); }
63 /// is the iterator valid?
64 operator const void*() const { return empty() ? 0 : this; }
65 /// is this iterator invalid?
66 bool operator!() const { return empty(); }
68 /// does this iterator have any content?
69 bool empty() const { return slices_.empty(); }
72 // access to slice at tip
75 CursorSlice & top() { return slices_.back(); }
77 CursorSlice const & top() const { return slices_.back(); }
78 /// access to outermost slice
79 CursorSlice & bottom() { return slices_.front(); }
80 /// access to outermost slice
81 CursorSlice const & bottom() const { return slices_.front(); }
82 /// how many nested insets do we have?
83 size_t depth() const { return slices_.size(); }
84 /// the containing inset
85 InsetBase & inset() const { return top().inset(); }
86 /// return the cell of the inset this cursor is in
87 idx_type idx() const { return top().idx(); }
88 /// return the cell of the inset this cursor is in
89 idx_type & idx() { return top().idx(); }
90 /// return the last possible cell in this inset
91 idx_type lastidx() const;
92 /// return the paragraph this cursor is in
93 pit_type pit() const { return top().pit(); }
94 /// return the paragraph this cursor is in
95 pit_type & pit() { return top().pit(); }
96 /// return the last possible paragraph in this inset
97 pit_type lastpit() const;
98 /// return the position within the paragraph
99 pos_type pos() const { return top().pos(); }
100 /// return the position within the paragraph
101 pos_type & pos() { return top().pos(); }
102 /// return the last position within the paragraph
103 pos_type lastpos() const;
105 /// return the number of embedded cells
106 size_t nargs() const;
107 /// return the number of embedded cells
108 size_t ncols() const;
109 /// return the number of embedded cells
110 size_t nrows() const;
111 /// return the grid row of the top cell
112 row_type row() const;
113 /// return the last row of the top grid
114 row_type lastrow() const { return nrows() - 1; }
115 /// return the grid column of the top cell
116 col_type col() const;
117 /// return the last column of the top grid
118 col_type lastcol() const { return ncols() - 1; }
119 /// the inset just behind the cursor
120 InsetBase * nextInset();
121 /// the inset just in front of the cursor
122 InsetBase * prevInset();
123 /// the inset just in front of the cursor
124 InsetBase const * prevInset() const;
126 bool boundary() const { return boundary_; }
128 void boundary(bool b) { boundary_ = b; }
130 /// are we in mathed?
131 bool inMathed() const;
132 /// are we in texted?
133 bool inTexted() const;
136 // math-specific part
138 /// return the mathed cell this cursor is in
139 MathArray const & cell() const;
140 /// return the mathed cell this cursor is in
142 /// the mathatom left of the cursor
143 MathAtom const & prevAtom() const;
144 /// the mathatom left of the cursor
145 MathAtom & prevAtom();
146 /// the mathatom right of the cursor
147 MathAtom const & nextAtom() const;
148 /// the mathatom right of the cursor
149 MathAtom & nextAtom();
152 // text-specific part
154 /// the paragraph we're in
155 Paragraph & paragraph();
156 /// the paragraph we're in
157 Paragraph const & paragraph() const;
158 /// the row in the paragraph we're in
160 /// the row in the paragraph we're in
161 Row const & textRow() const;
165 LyXText const * text() const;
166 /// the containing inset or the cell, respectively
167 InsetBase * realInset() const;
169 InsetBase * innerInsetOfType(int code) const;
171 LyXText * innerText();
173 LyXText const * innerText() const;
178 /// move on one logical position, do not descend into nested insets
179 void forwardPosNoDescend();
181 * move on one logical position, descend into nested insets
182 * skip collapsed insets if \p ignorecollapsed is true
184 void forwardPos(bool ignorecollapsed = false);
185 /// move on one physical character or inset
187 /// move on one paragraph
191 /// move on one inset
193 /// move backward one logical position
195 /// move backward one physical character or inset
197 /// move backward one paragraph
199 /// move backward one cell
201 /// move backward one inset
202 void backwardInset();
204 /// are we some 'extension' (i.e. deeper nested) of the given iterator
205 bool hasPart(DocIterator const & it) const;
208 friend std::ostream &
209 operator<<(std::ostream & os, DocIterator const & cur);
211 friend bool operator==(DocIterator const &, DocIterator const &);
213 friend class StableDocIterator;
216 void clear() { slices_.clear(); }
218 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
220 void pop_back() { slices_.pop_back(); }
221 /// recompute the inset parts of the cursor from the document data
222 void updateInsets(InsetBase * inset);
226 * When the cursor position is i, is the cursor after the i-th char
227 * or before the i+1-th char ? Normally, these two interpretations are
228 * equivalent, except when the fonts of the i-th and i+1-th char
230 * We use boundary_ to distinguish between the two options:
231 * If boundary_=true, then the cursor is after the i-th char
232 * and if boundary_=false, then the cursor is before the i+1-th char.
234 * We currently use the boundary only when the language direction of
235 * the i-th char is different than the one of the i+1-th char.
236 * In this case it is important to distinguish between the two
237 * cursor interpretations, in order to give a reasonable behavior to
242 std::vector<CursorSlice> const & internalData() const {
246 std::vector<CursorSlice> slices_;
252 DocIterator doc_iterator_begin(InsetBase & inset);
253 DocIterator doc_iterator_end(InsetBase & inset);
257 bool operator==(DocIterator const & di1, DocIterator const & di2)
259 return di1.slices_ == di2.slices_;
264 bool operator!=(DocIterator const & di1, DocIterator const & di2)
266 return !(di1 == di2);
270 // The difference to a ('non stable') DocIterator is the removed
271 // (overwritte by 0...) part of the CursorSlice data items. So this thing
272 // is suitable for external storage, but not for iteration as such.
274 class StableDocIterator {
277 StableDocIterator() {}
278 /// non-explicit intended
279 StableDocIterator(const DocIterator & it);
281 DocIterator asDocIterator(InsetBase * start) const;
283 size_t size() const { return data_.size(); }
284 /// type for cursor positions within a cell
285 typedef CursorSlice::pos_type pos_type;
286 /// return the position within the paragraph
287 pos_type pos() const { return data_.back().pos(); }
288 /// return the position within the paragraph
289 pos_type & pos() { return data_.back().pos(); }
291 friend std::ostream &
292 operator<<(std::ostream & os, StableDocIterator const & cur);
294 friend std::istream &
295 operator>>(std::istream & is, StableDocIterator & cur);
298 operator==(StableDocIterator const &, StableDocIterator const &);
300 std::vector<CursorSlice> data_;