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"
29 // The public inheritance should go in favour of a suitable data member
30 // (or maybe private inheritance) at some point of time.
31 class DocIterator
// : public std::vector<CursorSlice>
34 /// type for cell number in inset
35 typedef CursorSlice::idx_type idx_type
;
36 /// type for row indices
37 typedef CursorSlice::row_type row_type
;
38 /// type for col indices
39 typedef CursorSlice::col_type col_type
;
45 /// access slice at position \p i
46 CursorSlice
const & operator[](size_t i
) const { return slices_
[i
]; }
47 /// access slice at position \p i
48 CursorSlice
& operator[](size_t i
) { return slices_
[i
]; }
49 /// chop a few slices from the iterator
50 void resize(size_t i
) { slices_
.resize(i
); }
52 /// is the iterator valid?
53 operator const void*() const { return empty() ? 0 : this; }
54 /// is this iterator invalid?
55 bool operator!() const { return empty(); }
57 /// does this iterator have any content?
58 bool empty() const { return slices_
.empty(); }
59 /// is this the end position?
60 bool atEnd() const { return slices_
.empty(); }
63 // access to slice at tip
66 CursorSlice
& top() { return slices_
.back(); }
68 CursorSlice
const & top() const { return slices_
.back(); }
69 /// access to outermost slice
70 CursorSlice
& bottom() { return slices_
.front(); }
71 /// access to outermost slice
72 CursorSlice
const & bottom() const { return slices_
.front(); }
73 /// how many nested insets do we have?
74 size_t depth() const { return slices_
.size(); }
75 /// the containing inset
76 Inset
& inset() const { return top().inset(); }
77 /// return the cell of the inset this cursor is in
78 idx_type
idx() const { return top().idx(); }
79 /// return the cell of the inset this cursor is in
80 idx_type
& idx() { return top().idx(); }
81 /// return the last possible cell in this inset
82 idx_type
lastidx() const;
83 /// return the paragraph this cursor is in
84 pit_type
pit() const { return top().pit(); }
85 /// return the paragraph this cursor is in
86 pit_type
& pit() { return top().pit(); }
87 /// return the last possible paragraph in this inset
88 pit_type
lastpit() const;
89 /// return the position within the paragraph
90 pos_type
pos() const { return top().pos(); }
91 /// return the position within the paragraph
92 pos_type
& pos() { return top().pos(); }
93 /// return the last position within the paragraph
94 pos_type
lastpos() const;
96 /// return the number of embedded cells
98 /// return the number of embedded cells
100 /// return the number of embedded cells
101 size_t nrows() const;
102 /// return the grid row of the top cell
103 row_type
row() const;
104 /// return the last row of the top grid
105 row_type
lastrow() const { return nrows() - 1; }
106 /// return the grid column of the top cell
107 col_type
col() const;
108 /// return the last column of the top grid
109 col_type
lastcol() const { return ncols() - 1; }
110 /// the inset just behind the cursor
111 Inset
* nextInset() const;
112 /// the inset just in front of the cursor
113 Inset
* prevInset() const;
115 bool boundary() const { return boundary_
; }
117 void boundary(bool b
) { boundary_
= b
; }
119 // the two methods below have been inlined out because of
120 // profiling results under linux when opening a document.
121 /// are we in mathed?.
122 bool inMathed() const
123 { return !empty() && inset().inMathed(); }
124 /// are we in texted?.
125 bool inTexted() const
126 { return !empty() && !inset().inMathed(); }
129 // math-specific part
131 /// return the mathed cell this cursor is in
132 MathData
& cell() const;
133 /// the mathatom left of the cursor
134 MathAtom
& prevAtom() const;
135 /// the mathatom right of the cursor
136 MathAtom
& nextAtom() const;
138 // text-specific part
140 /// the paragraph we're in in text mode.
141 /// \warning only works within text!
142 Paragraph
& paragraph() const;
143 /// the paragraph we're in in any case.
144 /// This method will give the containing paragraph even
145 /// if not in text mode (ex: in mathed).
146 Paragraph
& innerParagraph() const;
147 /// return the inner text slice.
148 CursorSlice
const & innerTextSlice() const;
151 /// the containing inset or the cell, respectively
152 Inset
* realInset() const;
154 Inset
* innerInsetOfType(int code
) const;
156 Text
* innerText() const;
162 * move on one logical position, descend into nested insets
163 * including collapsed insets
167 * move on one logical position, descend into nested insets
168 * skip collapsed insets
170 void forwardPosIgnoreCollapsed();
171 /// move on one physical character or inset
173 /// move on one paragraph
175 /// move on one inset
177 /// move backward one logical position
179 /// move backward one physical character or inset
181 /// move backward one paragraph
183 /// move backward one inset
184 /// FIXME: This is not implemented!
185 //void backwardInset();
187 /// are we some 'extension' (i.e. deeper nested) of the given iterator
188 bool hasPart(DocIterator
const & it
) const;
191 friend std::ostream
&
192 operator<<(std::ostream
& os
, DocIterator
const & cur
);
193 friend LyXErr
& operator<<(LyXErr
& os
, DocIterator
const & it
);
195 friend bool operator==(DocIterator
const &, DocIterator
const &);
196 friend bool operator<(DocIterator
const &, DocIterator
const &);
197 friend bool operator>(DocIterator
const &, DocIterator
const &);
198 friend bool operator<=(DocIterator
const &, DocIterator
const &);
200 friend class StableDocIterator
;
203 void clear() { slices_
.clear(); }
205 void push_back(CursorSlice
const & sl
) { slices_
.push_back(sl
); }
207 void pop_back() { slices_
.pop_back(); }
208 /// recompute the inset parts of the cursor from the document data
209 void updateInsets(Inset
* inset
);
210 /// fix DocIterator in circumstances that should never happen.
211 /// \return true if the DocIterator was fixed.
214 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
215 int find(MathData
const & cell
) const;
216 /// find index of CursorSlice with inset() == inset (or -1 of not found)
217 int find(Inset
const * inset
) const;
218 /// cut off CursorSlices with index > above and store cut off slices in cut.
219 void cutOff(int above
, std::vector
<CursorSlice
> & cut
);
220 /// cut off CursorSlices with index > above
221 void cutOff(int above
);
222 /// push CursorSlices on top
223 void append(std::vector
<CursorSlice
> const & x
);
224 /// push one CursorSlice on top and set its index and position
225 void append(idx_type idx
, pos_type pos
);
228 friend class InsetIterator
;
229 friend DocIterator
doc_iterator_begin(Inset
& inset
);
230 friend DocIterator
doc_iterator_end(Inset
& inset
);
232 explicit DocIterator(Inset
& inset
);
234 * Normally, when the cursor is at position i, it is painted *before*
235 * the character at position i. However, what if we want the cursor
236 * painted *after* position i? That's what boundary_ is for: if
237 * boundary_==true, the cursor is painted *after* position i-1, instead
238 * of before position i.
240 * Note 1: Usually, after i-1 or before i are actually the same place!
241 * However, this is not the case when i-1 and i are not painted
242 * contiguously, and in these cases we sometimes do want to have control
243 * over whether to paint before i or after i-1.
244 * Some concrete examples of where this happens:
245 * a. i-1 at the end of one row, i at the beginning of next row
246 * b. in bidi text, at transitions between RTL and LTR or vice versa
248 * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
249 * couldn't boundary_==true mean: *after* i?
250 * Well, the reason is this: cursor position is not used only for
251 * painting the cursor, but it also affects other things, for example:
252 * where the next insertion will be placed (it is inserted at the current
253 * position, pushing anything at the current position and beyond forward).
254 * Now, when the current position is i and boundary_==true, insertion would
255 * happen *before* i. If the cursor, however, were painted *after* i, that
256 * would be very unnatural...
260 std::vector
<CursorSlice
> const & internalData() const {
264 std::vector
<CursorSlice
> slices_
;
270 DocIterator
doc_iterator_begin(Inset
& inset
);
271 DocIterator
doc_iterator_end(Inset
& inset
);
274 inline bool operator==(DocIterator
const & di1
, DocIterator
const & di2
)
276 return di1
.slices_
== di2
.slices_
;
280 inline bool operator!=(DocIterator
const & di1
, DocIterator
const & di2
)
282 return !(di1
== di2
);
287 bool operator<(DocIterator
const & p
, DocIterator
const & q
)
289 size_t depth
= std::min(p
.depth(), q
.depth());
290 for (size_t i
= 0 ; i
< depth
; ++i
) {
294 return p
.depth() < q
.depth();
299 bool operator>(DocIterator
const & p
, DocIterator
const & q
)
306 bool operator<=(DocIterator
const & p
, DocIterator
const & q
)
313 bool operator>=(DocIterator
const & p
, DocIterator
const & q
)
319 // The difference to a ('non stable') DocIterator is the removed
320 // (overwritten by 0...) part of the CursorSlice data items. So this thing
321 // is suitable for external storage, but not for iteration as such.
323 class StableDocIterator
{
326 StableDocIterator() {}
327 /// non-explicit intended
328 StableDocIterator(const DocIterator
& it
);
330 DocIterator
asDocIterator(Inset
* start
) const;
332 size_t size() const { return data_
.size(); }
333 /// return the position within the paragraph
334 pos_type
pos() const { return data_
.back().pos(); }
335 /// return the position within the paragraph
336 pos_type
& pos() { return data_
.back().pos(); }
338 friend std::ostream
&
339 operator<<(std::ostream
& os
, StableDocIterator
const & cur
);
341 friend std::istream
&
342 operator>>(std::istream
& is
, StableDocIterator
& cur
);
345 operator==(StableDocIterator
const &, StableDocIterator
const &);
347 std::vector
<CursorSlice
> data_
;