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(); }
127 /// are we in regexp-mode ?
128 bool inRegexped() const;
131 // math-specific part
133 /// return the mathed cell this cursor is in
134 MathData & cell() const;
135 /// the mathatom left of the cursor
136 MathAtom & prevAtom() const;
137 /// the mathatom right of the cursor
138 MathAtom & nextAtom() const;
140 // text-specific part
142 /// the paragraph we're in in text mode.
143 /// \warning only works within text!
144 Paragraph & paragraph() const;
145 /// the paragraph we're in in any case.
146 /// This method will give the containing paragraph even
147 /// if not in text mode (ex: in mathed).
148 Paragraph & innerParagraph() const;
149 /// return the inner text slice.
150 CursorSlice const & innerTextSlice() const;
153 /// the containing inset or the cell, respectively
154 Inset * realInset() const;
156 Inset * innerInsetOfType(int code) const;
158 Text * innerText() const;
164 * move on one logical position, descend into nested insets
165 * including collapsed insets
169 * move on one logical position, descend into nested insets
170 * skip collapsed insets
172 void forwardPosIgnoreCollapsed();
173 /// move on one physical character or inset
175 /// move on one paragraph
177 /// move on one inset
179 /// move backward one logical position
181 /// move backward one physical character or inset
183 /// move backward one paragraph
185 /// move backward one inset
186 /// FIXME: This is not implemented!
187 //void backwardInset();
189 /// are we some 'extension' (i.e. deeper nested) of the given iterator
190 bool hasPart(DocIterator const & it) const;
193 friend std::ostream &
194 operator<<(std::ostream & os, DocIterator const & cur);
195 friend LyXErr & operator<<(LyXErr & os, DocIterator const & it);
197 friend bool operator==(DocIterator const &, DocIterator const &);
198 friend bool operator<(DocIterator const &, DocIterator const &);
199 friend bool operator>(DocIterator const &, DocIterator const &);
200 friend bool operator<=(DocIterator const &, DocIterator const &);
202 friend class StableDocIterator;
205 void clear() { slices_.clear(); }
207 void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
209 void pop_back() { slices_.pop_back(); }
210 /// recompute the inset parts of the cursor from the document data
211 void updateInsets(Inset * inset);
212 /// fix DocIterator in circumstances that should never happen.
213 /// \return true if the DocIterator was fixed.
216 /// find index of CursorSlice with &cell() == &cell (or -1 if not found)
217 int find(MathData const & cell) const;
218 /// find index of CursorSlice with inset() == inset (or -1 of not found)
219 int find(Inset const * inset) const;
220 /// cut off CursorSlices with index > above and store cut off slices in cut.
221 void cutOff(int above, std::vector<CursorSlice> & cut);
222 /// cut off CursorSlices with index > above
223 void cutOff(int above);
224 /// push CursorSlices on top
225 void append(std::vector<CursorSlice> const & x);
226 /// push one CursorSlice on top and set its index and position
227 void append(idx_type idx, pos_type pos);
230 friend class InsetIterator;
231 friend DocIterator doc_iterator_begin(Inset & inset);
232 friend DocIterator doc_iterator_end(Inset & inset);
234 explicit DocIterator(Inset & inset);
236 * Normally, when the cursor is at position i, it is painted *before*
237 * the character at position i. However, what if we want the cursor
238 * painted *after* position i? That's what boundary_ is for: if
239 * boundary_==true, the cursor is painted *after* position i-1, instead
240 * of before position i.
242 * Note 1: Usually, after i-1 or before i are actually the same place!
243 * However, this is not the case when i-1 and i are not painted
244 * contiguously, and in these cases we sometimes do want to have control
245 * over whether to paint before i or after i-1.
246 * Some concrete examples of where this happens:
247 * a. i-1 at the end of one row, i at the beginning of next row
248 * b. in bidi text, at transitions between RTL and LTR or vice versa
250 * Note 2: Why i and i-1? Why, if boundary_==false means: *before* i,
251 * couldn't boundary_==true mean: *after* i?
252 * Well, the reason is this: cursor position is not used only for
253 * painting the cursor, but it also affects other things, for example:
254 * where the next insertion will be placed (it is inserted at the current
255 * position, pushing anything at the current position and beyond forward).
256 * Now, when the current position is i and boundary_==true, insertion would
257 * happen *before* i. If the cursor, however, were painted *after* i, that
258 * would be very unnatural...
262 std::vector<CursorSlice> const & internalData() const {
266 std::vector<CursorSlice> slices_;
272 DocIterator doc_iterator_begin(Inset & inset);
273 DocIterator doc_iterator_end(Inset & inset);
276 inline bool operator==(DocIterator const & di1, DocIterator const & di2)
278 return di1.slices_ == di2.slices_;
282 inline bool operator!=(DocIterator const & di1, DocIterator const & di2)
284 return !(di1 == di2);
289 bool operator<(DocIterator const & p, DocIterator const & q)
291 size_t depth = std::min(p.depth(), q.depth());
292 for (size_t i = 0 ; i < depth ; ++i) {
296 return p.depth() < q.depth();
301 bool operator>(DocIterator const & p, DocIterator const & q)
308 bool operator<=(DocIterator const & p, DocIterator const & q)
315 bool operator>=(DocIterator const & p, DocIterator const & q)
321 // The difference to a ('non stable') DocIterator is the removed
322 // (overwritten by 0...) part of the CursorSlice data items. So this thing
323 // is suitable for external storage, but not for iteration as such.
325 class StableDocIterator {
328 StableDocIterator() {}
329 /// non-explicit intended
330 StableDocIterator(const DocIterator & it);
332 DocIterator asDocIterator(Inset * start) const;
334 size_t size() const { return data_.size(); }
335 /// return the position within the paragraph
336 pos_type pos() const { return data_.back().pos(); }
337 /// return the position within the paragraph
338 pos_type & pos() { return data_.back().pos(); }
340 friend std::ostream &
341 operator<<(std::ostream & os, StableDocIterator const & cur);
343 friend std::istream &
344 operator>>(std::istream & is, StableDocIterator & cur);
347 operator==(StableDocIterator const &, StableDocIterator const &);
349 std::vector<CursorSlice> data_;