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(); }
94 /// return the last possible cell in this inset
95 idx_type lastidx() const;
96 /// return the paragraph this cursor is in
97 pit_type pit() const { return top().pit(); }
98 /// return the paragraph this cursor is in
99 pit_type & pit() { return top().pit(); }
100 /// return the last possible paragraph in this inset
101 pit_type lastpit() const;
102 /// return the position within the paragraph
103 pos_type pos() const { return top().pos(); }
104 /// return the position within the paragraph
105 pos_type & pos() { return top().pos(); }
106 /// return the last position within the paragraph
107 pos_type lastpos() const;
109 /// return the number of embedded cells
110 size_t nargs() const;
111 /// return the number of embedded cells
112 size_t ncols() const;
113 /// return the number of embedded cells
114 size_t nrows() const;
115 /// return the grid row of the top cell
116 row_type row() const;
117 /// return the last row of the top grid
118 row_type lastrow() const { return nrows() - 1; }
119 /// return the grid column of the top cell
120 col_type col() const;
121 /// return the last column of the top grid
122 col_type lastcol() const { return ncols() - 1; }
123 /// the inset just behind the cursor
124 InsetBase * nextInset();
125 /// the inset just in front of the cursor
126 InsetBase * prevInset();
127 /// the inset just in front of the cursor
128 InsetBase const * prevInset() const;
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 /// see comment for boundary_ below
155 bool boundary() const { return top().boundary(); }
156 /// see comment for boundary_ below
157 bool & boundary() { return top().boundary(); }
158 /// the paragraph we're in
159 Paragraph & paragraph();
160 /// the paragraph we're in
161 Paragraph const & paragraph() const;
162 /// the row in the paragraph we're in
164 /// the row in the paragraph we're in
165 Row const & textRow() const;
169 LyXText const * text() const;
171 InsetBase * innerInsetOfType(int code) const;
173 LyXText * innerText();
175 LyXText const * innerText() const;
180 /// move on one logical position
182 /// move on one physical character or inset
184 /// move on one paragraph
188 /// move on one inset
190 /// move backward one logical position
192 /// move backward one physical character or inset
194 /// move backward one paragraph
196 /// move backward one cell
198 /// move backward one inset
199 void backwardInset();
201 /// are we some 'extension' (i.e. deeper nested) of the given iterator
202 bool hasPart(DocIterator const & it) const;
205 friend std::ostream &
206 operator<<(std::ostream & os, DocIterator const & cur);
207 friend bool operator==(DocIterator const &, DocIterator const &);
208 friend class StableDocIterator;
210 void clear() { slices_.clear(); }
211 void push_back(CursorSlice const & sl) {
212 slices_.push_back(sl);
218 std::vector<CursorSlice> const & internalData() const {
221 std::vector<CursorSlice> slices_;
226 DocIterator doc_iterator_begin(InsetBase & inset);
227 DocIterator doc_iterator_end(InsetBase & inset);
231 bool operator==(DocIterator const & di1, DocIterator const & di2)
233 return di1.slices_ == di2.slices_;
238 bool operator!=(DocIterator const & di1, DocIterator const & di2)
240 return !(di1 == di2);
244 // The difference to a ('non stable') DocIterator is the removed
245 // (overwritte by 0...) part of the CursorSlice data items. So this thing
246 // is suitable for external storage, but not for iteration as such.
248 class StableDocIterator {
251 StableDocIterator() {}
252 /// non-explicit intended
253 StableDocIterator(const DocIterator & it);
255 DocIterator asDocIterator(InsetBase * start) const;
257 size_t size() const { return data_.size(); }
259 friend std::ostream &
260 operator<<(std::ostream & os, StableDocIterator const & cur);
262 friend std::istream &
263 operator>>(std::istream & is, StableDocIterator & cur);
265 std::vector<CursorSlice> data_;
268 bool operator==(StableDocIterator const &, StableDocIterator const &);