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 CursorSlice const & operator[](size_t i) const {
61 CursorSlice & operator[](size_t i) {
65 // What is the point of this function?
66 void resize(size_t i) { slices_.resize(i); }
68 /// is the iterator valid?
69 operator const void*() const { return empty() ? 0 : this; }
70 /// is this iterator invalid?
71 bool operator!() const { return empty(); }
73 bool empty() const { return slices_.empty(); }
76 // access to slice at tip
79 CursorSlice & top() { return slices_.back(); }
81 CursorSlice const & top() const { return slices_.back(); }
82 /// access to outermost slice
83 CursorSlice & bottom() { return slices_.front(); }
84 /// access to outermost slicetip
85 CursorSlice const & bottom() const { return slices_.front(); }
86 /// how many nested insets do we have?
87 size_t depth() const { return slices_.size(); }
88 /// the containing inset
89 InsetBase & inset() const { return top().inset(); }
90 /// return the cell of the inset this cursor is in
91 idx_type idx() const { return top().idx(); }
92 /// return the cell of the inset this cursor is in
93 idx_type & idx() { return top().idx(); }
95 void idxSave() { top().idxSave(); }
97 void idxLoad() { top().idxLoad(); }
98 /// return the last possible cell in this inset
99 idx_type lastidx() const;
100 /// return the paragraph this cursor is in
101 pit_type pit() const { return top().pit(); }
102 /// return the paragraph this cursor is in
103 pit_type & pit() { return top().pit(); }
104 /// return the last possible paragraph in this inset
105 pit_type lastpit() const;
106 /// return the position within the paragraph
107 pos_type pos() const { return top().pos(); }
108 /// return the position within the paragraph
109 pos_type & pos() { return top().pos(); }
110 /// return the last position within the paragraph
111 pos_type lastpos() const;
113 /// return the number of embedded cells
114 size_t nargs() const;
115 /// return the number of embedded cells
116 size_t ncols() const;
117 /// return the number of embedded cells
118 size_t nrows() const;
119 /// return the grid row of the top cell
120 row_type row() const;
121 /// return the last row of the top grid
122 row_type lastrow() const { return nrows() - 1; }
123 /// return the grid column of the top cell
124 col_type col() const;
125 /// return the last column of the top grid
126 col_type lastcol() const { return ncols() - 1; }
127 /// the inset just behind the cursor
128 InsetBase * nextInset();
129 /// the inset just in front of the cursor
130 InsetBase * prevInset();
131 /// the inset just in front of the cursor
132 InsetBase const * prevInset() const;
134 /// are we in mathed?
135 bool inMathed() const;
136 /// are we in texted?
137 bool inTexted() const;
140 // math-specific part
142 /// return the mathed cell this cursor is in
143 MathArray const & cell() const;
144 /// return the mathed cell this cursor is in
146 /// the mathatom left of the cursor
147 MathAtom const & prevAtom() const;
148 /// the mathatom left of the cursor
149 MathAtom & prevAtom();
150 /// the mathatom right of the cursor
151 MathAtom const & nextAtom() const;
152 /// the mathatom right of the cursor
153 MathAtom & nextAtom();
156 // text-specific part
158 /// see comment for boundary_ below
159 bool boundary() const { return top().boundary(); }
160 /// see comment for boundary_ below
161 bool & boundary() { return top().boundary(); }
162 /// the paragraph we're in
163 Paragraph & paragraph();
164 /// the paragraph we're in
165 Paragraph const & paragraph() const;
166 /// the row in the paragraph we're in
168 /// the row in the paragraph we're in
169 Row const & textRow() const;
173 LyXText const * text() const;
175 InsetBase * innerInsetOfType(int code) const;
177 LyXText * innerText();
179 LyXText const * innerText() const;
184 /// move on one logical position, do not descend into nested insets
185 void forwardPosNoDescend();
186 /// move on one logical position, descend into nested insets
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 void backwardInset();
207 /// are we some 'extension' (i.e. deeper nested) of the given iterator
208 bool hasPart(DocIterator const & it) const;
211 friend std::ostream &
212 operator<<(std::ostream & os, DocIterator const & cur);
213 friend bool operator==(DocIterator const &, DocIterator const &);
214 friend class StableDocIterator;
216 void clear() { slices_.clear(); }
217 void push_back(CursorSlice const & sl) {
218 slices_.push_back(sl);
224 std::vector<CursorSlice> const & internalData() const {
227 std::vector<CursorSlice> slices_;
232 DocIterator doc_iterator_begin(InsetBase & inset);
233 DocIterator doc_iterator_end(InsetBase & inset);
237 bool operator==(DocIterator const & di1, DocIterator const & di2)
239 return di1.slices_ == di2.slices_;
244 bool operator!=(DocIterator const & di1, DocIterator const & di2)
246 return !(di1 == di2);
250 // The difference to a ('non stable') DocIterator is the removed
251 // (overwritte by 0...) part of the CursorSlice data items. So this thing
252 // is suitable for external storage, but not for iteration as such.
254 class StableDocIterator {
257 StableDocIterator() {}
258 /// non-explicit intended
259 StableDocIterator(const DocIterator & it);
261 DocIterator asDocIterator(InsetBase * start) const;
263 size_t size() const { return data_.size(); }
265 friend std::ostream &
266 operator<<(std::ostream & os, StableDocIterator const & cur);
268 friend std::istream &
269 operator>>(std::istream & is, StableDocIterator & cur);
272 operator==(StableDocIterator const &, StableDocIterator const &);
274 std::vector<CursorSlice> data_;