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 {
62 /// access slice at position \p i
63 CursorSlice
& operator[](size_t i
) {
67 // What is the point of this function?
68 void resize(size_t i
) { slices_
.resize(i
); }
70 /// is the iterator valid?
71 operator const void*() const { return empty() ? 0 : this; }
72 /// is this iterator invalid?
73 bool operator!() const { return empty(); }
75 /// does this iterator have any content?
76 bool empty() const { return slices_
.empty(); }
79 // access to slice at tip
82 CursorSlice
& top() { return slices_
.back(); }
84 CursorSlice
const & top() const { return slices_
.back(); }
85 /// access to outermost slice
86 CursorSlice
& bottom() { return slices_
.front(); }
87 /// access to outermost slice
88 CursorSlice
const & bottom() const { return slices_
.front(); }
89 /// how many nested insets do we have?
90 size_t depth() const { return slices_
.size(); }
91 /// the containing inset
92 InsetBase
& inset() const { return top().inset(); }
93 /// return the cell of the inset this cursor is in
94 idx_type
idx() const { return top().idx(); }
95 /// return the cell of the inset this cursor is in
96 idx_type
& idx() { return top().idx(); }
97 /// return the last possible cell in this inset
98 idx_type
lastidx() const;
99 /// return the paragraph this cursor is in
100 pit_type
pit() const { return top().pit(); }
101 /// return the paragraph this cursor is in
102 pit_type
& pit() { return top().pit(); }
103 /// return the last possible paragraph in this inset
104 pit_type
lastpit() const;
105 /// return the position within the paragraph
106 pos_type
pos() const { return top().pos(); }
107 /// return the position within the paragraph
108 pos_type
& pos() { return top().pos(); }
109 /// return the last position within the paragraph
110 pos_type
lastpos() const;
112 /// return the number of embedded cells
113 size_t nargs() const;
114 /// return the number of embedded cells
115 size_t ncols() const;
116 /// return the number of embedded cells
117 size_t nrows() const;
118 /// return the grid row of the top cell
119 row_type
row() const;
120 /// return the last row of the top grid
121 row_type
lastrow() const { return nrows() - 1; }
122 /// return the grid column of the top cell
123 col_type
col() const;
124 /// return the last column of the top grid
125 col_type
lastcol() const { return ncols() - 1; }
126 /// the inset just behind the cursor
127 InsetBase
* nextInset();
128 /// the inset just in front of the cursor
129 InsetBase
* prevInset();
130 /// the inset just in front of the cursor
131 InsetBase
const * prevInset() const;
133 /// are we in mathed?
134 bool inMathed() const;
135 /// are we in texted?
136 bool inTexted() const;
139 // math-specific part
141 /// return the mathed cell this cursor is in
142 MathArray
const & cell() const;
143 /// return the mathed cell this cursor is in
145 /// the mathatom left of the cursor
146 MathAtom
const & prevAtom() const;
147 /// the mathatom left of the cursor
148 MathAtom
& prevAtom();
149 /// the mathatom right of the cursor
150 MathAtom
const & nextAtom() const;
151 /// the mathatom right of the cursor
152 MathAtom
& nextAtom();
155 // text-specific part
158 bool boundary() const { return top().boundary(); }
160 bool & boundary() { return top().boundary(); }
161 /// the paragraph we're in
162 Paragraph
& paragraph();
163 /// the paragraph we're in
164 Paragraph
const & paragraph() const;
165 /// the row in the paragraph we're in
167 /// the row in the paragraph we're in
168 Row
const & textRow() const;
172 LyXText
const * text() const;
174 InsetBase
* innerInsetOfType(int code
) const;
176 LyXText
* innerText();
178 LyXText
const * innerText() const;
183 /// move on one logical position, do not descend into nested insets
184 void forwardPosNoDescend();
185 /// move on one logical position, descend into nested insets
187 /// move on one physical character or inset
189 /// move on one paragraph
193 /// move on one inset
195 /// move backward one logical position
197 /// move backward one physical character or inset
199 /// move backward one paragraph
201 /// move backward one cell
203 /// move backward one inset
204 void backwardInset();
206 /// are we some 'extension' (i.e. deeper nested) of the given iterator
207 bool hasPart(DocIterator
const & it
) const;
210 friend std::ostream
&
211 operator<<(std::ostream
& os
, DocIterator
const & cur
);
212 friend bool operator==(DocIterator
const &, DocIterator
const &);
213 friend class StableDocIterator
;
215 void clear() { slices_
.clear(); }
216 void push_back(CursorSlice
const & sl
) {
217 slices_
.push_back(sl
);
223 std::vector
<CursorSlice
> const & internalData() const {
226 std::vector
<CursorSlice
> slices_
;
231 DocIterator
doc_iterator_begin(InsetBase
& inset
);
232 DocIterator
doc_iterator_end(InsetBase
& inset
);
236 bool operator==(DocIterator
const & di1
, DocIterator
const & di2
)
238 return di1
.slices_
== di2
.slices_
;
243 bool operator!=(DocIterator
const & di1
, DocIterator
const & di2
)
245 return !(di1
== di2
);
249 // The difference to a ('non stable') DocIterator is the removed
250 // (overwritte by 0...) part of the CursorSlice data items. So this thing
251 // is suitable for external storage, but not for iteration as such.
253 class StableDocIterator
{
256 StableDocIterator() {}
257 /// non-explicit intended
258 StableDocIterator(const DocIterator
& it
);
260 DocIterator
asDocIterator(InsetBase
* start
) const;
262 size_t size() const { return data_
.size(); }
264 friend std::ostream
&
265 operator<<(std::ostream
& os
, StableDocIterator
const & cur
);
267 friend std::istream
&
268 operator>>(std::istream
& is
, StableDocIterator
& cur
);
271 operator==(StableDocIterator
const &, StableDocIterator
const &);
273 std::vector
<CursorSlice
> data_
;