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 // only needed for gcc 2.95, remove when support terminated
29 template <typename A, typename B>
30 bool ptr_cmp(A const * a, B const * b)
36 // The public inheritance should go in favour of a suitable data member
37 // (or maybe private inheritance) at some point of time.
38 class DocIterator // : public std::vector<CursorSlice>
41 /// type for cell number in inset
42 typedef CursorSlice::idx_type idx_type;
43 /// type for row indices
44 typedef CursorSlice::row_type row_type;
45 /// type for col indices
46 typedef CursorSlice::col_type col_type;
52 explicit DocIterator(Inset & inset);
54 /// access slice at position \p i
55 CursorSlice const & operator[](size_t i) const { return slices_[i]; }
56 /// access slice at position \p i
57 CursorSlice & operator[](size_t i) { return slices_[i]; }
58 /// chop a few slices from the iterator
59 void resize(size_t i) { slices_.resize(i); }
61 /// is the iterator valid?
62 operator const void*() const { return empty() ? 0 : this; }
63 /// is this iterator invalid?
64 bool operator!() const { return empty(); }
66 /// does this iterator have any content?
67 bool empty() const { return slices_.empty(); }
70 // access to slice at tip
73 CursorSlice & top() { return slices_.back(); }
75 CursorSlice const & top() const { return slices_.back(); }
76 /// access to outermost slice
77 CursorSlice & bottom() { return slices_.front(); }
78 /// access to outermost slice
79 CursorSlice const & bottom() const { return slices_.front(); }
80 /// how many nested insets do we have?
81 size_t depth() const { return slices_.size(); }
82 /// the containing inset
83 Inset & inset() const { return top().inset(); }
84 /// return the cell of the inset this cursor is in
85 idx_type idx() const { return top().idx(); }
86 /// return the cell of the inset this cursor is in
87 idx_type & idx() { return top().idx(); }
88 /// return the last possible cell in this inset
89 idx_type lastidx() const;
90 /// return the paragraph this cursor is in
91 pit_type pit() const { return top().pit(); }
92 /// return the paragraph this cursor is in
93 pit_type & pit() { return top().pit(); }
94 /// return the last possible paragraph in this inset
95 pit_type lastpit() const;
96 /// return the position within the paragraph
97 pos_type pos() const { return top().pos(); }
98 /// return the position within the paragraph
99 pos_type & pos() { return top().pos(); }
100 /// return the last position within the paragraph
101 pos_type lastpos() const;
103 /// return the number of embedded cells
104 size_t nargs() const;
105 /// return the number of embedded cells
106 size_t ncols() const;
107 /// return the number of embedded cells
108 size_t nrows() const;
109 /// return the grid row of the top cell
110 row_type row() const;
111 /// return the last row of the top grid
112 row_type lastrow() const { return nrows() - 1; }
113 /// return the grid column of the top cell
114 col_type col() const;
115 /// return the last column of the top grid
116 col_type lastcol() const { return ncols() - 1; }
117 /// the inset just behind the cursor
119 /// the inset just in front of the cursor
121 /// the inset just in front of the cursor
122 Inset const * prevInset() const;
124 bool boundary() const { return boundary_; }
126 void boundary(bool b) { boundary_ = b; }
128 // the two methods below have been inlined out because of
129 // profiling results under linux when opening a document.
130 /// are we in mathed?.
131 bool inMathed() const
132 { return !empty() && inset().inMathed(); }
133 /// are we in texted?.
134 bool inTexted() const
135 { return !empty() && !inset().inMathed(); }
138 // math-specific part
140 /// return the mathed cell this cursor is in
141 MathData const & cell() const;
142 /// return the mathed cell this cursor is in
144 /// the mathatom left of the cursor
145 MathAtom const & prevAtom() const;
146 /// the mathatom left of the cursor
147 MathAtom & prevAtom();
148 /// the mathatom right of the cursor
149 MathAtom const & nextAtom() const;
150 /// the mathatom right of the cursor
151 MathAtom & nextAtom();
154 // text-specific part
156 /// the paragraph we're in
157 Paragraph & paragraph();
158 /// the paragraph we're in in text mode.
159 /// \warning only works within text!
160 Paragraph const & paragraph() const;
161 /// the paragraph we're in in any case.
162 /// This method will give the containing paragraph if
163 /// in not in text mode (ex: in mathed).
164 Paragraph const & innerParagraph() const;
168 Text const * text() const;
169 /// the containing inset or the cell, respectively
170 Inset * realInset() const;
172 Inset * innerInsetOfType(int code) const;
176 Text const * innerText() const;
181 /// move on one logical position, do not descend into nested insets
182 void forwardPosNoDescend();
184 * move on one logical position, descend into nested insets
185 * skip collapsed insets if \p ignorecollapsed is true
187 void forwardPos(bool ignorecollapsed = false);
188 /// move on one physical character or inset
190 /// move on one paragraph
194 /// move on one inset
196 /// move backward one logical position
198 /// move backward one physical character or inset
200 /// move backward one paragraph
202 /// move backward one cell
204 /// move backward one inset
205 /// FIXME: This is not implemented!
206 //void backwardInset();
208 /// are we some 'extension' (i.e. deeper nested) of the given iterator
209 bool hasPart(DocIterator const & it) const;
212 friend std::ostream &
213 operator<<(std::ostream & os, DocIterator const & cur);
215 friend bool operator==(DocIterator const &, DocIterator const &);
217 friend class StableDocIterator;
220 void clear() { slices_.clear(); }
222 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
224 void pop_back() { slices_.pop_back(); }
225 /// recompute the inset parts of the cursor from the document data
226 void updateInsets(Inset * inset);
230 * When the cursor position is i, is the cursor after the i-th char
231 * or before the i+1-th char ? Normally, these two interpretations are
232 * equivalent, except when the fonts of the i-th and i+1-th char
234 * We use boundary_ to distinguish between the two options:
235 * If boundary_=true, then the cursor is after the i-th char
236 * and if boundary_=false, then the cursor is before the i+1-th char.
238 * We currently use the boundary only when the language direction of
239 * the i-th char is different than the one of the i+1-th char.
240 * In this case it is important to distinguish between the two
241 * cursor interpretations, in order to give a reasonable behavior to
246 std::vector<CursorSlice> const & internalData() const {
250 std::vector<CursorSlice> slices_;
256 DocIterator doc_iterator_begin(Inset & inset);
257 DocIterator doc_iterator_end(Inset & inset);
261 bool operator==(DocIterator const & di1, DocIterator const & di2)
263 return di1.slices_ == di2.slices_;
268 bool operator!=(DocIterator const & di1, DocIterator const & di2)
270 return !(di1 == di2);
274 // The difference to a ('non stable') DocIterator is the removed
275 // (overwritten by 0...) part of the CursorSlice data items. So this thing
276 // is suitable for external storage, but not for iteration as such.
278 class StableDocIterator {
281 StableDocIterator() {}
282 /// non-explicit intended
283 StableDocIterator(const DocIterator & it);
285 DocIterator asDocIterator(Inset * start) const;
287 size_t size() const { return data_.size(); }
288 /// return the position within the paragraph
289 pos_type pos() const { return data_.back().pos(); }
290 /// return the position within the paragraph
291 pos_type & pos() { return data_.back().pos(); }
293 friend std::ostream &
294 operator<<(std::ostream & os, StableDocIterator const & cur);
296 friend std::istream &
297 operator>>(std::istream & is, StableDocIterator & cur);
300 operator==(StableDocIterator const &, StableDocIterator const &);
302 std::vector<CursorSlice> data_;