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 /// is the iterator valid?
58 operator const void*() const { return empty() ? 0 : this; }
59 /// is this iterator invalid?
60 bool operator!() const { return empty(); }
63 // access to slice at tip
66 CursorSlice & top() { return back(); }
68 CursorSlice const & top() const { return back(); }
69 /// access to outermost slice
70 CursorSlice & bottom() { return front(); }
71 /// access to outermost slicetip
72 CursorSlice const & bottom() const { return front(); }
73 /// how many nested insets do we have?
74 size_t depth() const { return size(); }
75 /// the containing inset
76 InsetBase & inset() const { return back().inset(); }
77 /// return the cell of the inset this cursor is in
78 idx_type idx() const { return back().idx(); }
79 /// return the cell of the inset this cursor is in
80 idx_type & idx() { return back().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 back().pit(); }
85 /// return the paragraph this cursor is in
86 pit_type & pit() { return back().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 back().pos(); }
91 /// return the position within the paragraph
92 pos_type & pos() { return back().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 InsetBase * nextInset();
112 /// the inset just in front of the cursor
113 InsetBase * prevInset();
114 /// the inset just in front of the cursor
115 InsetBase const * prevInset() const;
117 /// are we in mathed?
118 bool inMathed() const;
119 /// are we in texted?
120 bool inTexted() const;
123 // math-specific part
125 /// return the mathed cell this cursor is in
126 MathArray const & cell() const;
127 /// return the mathed cell this cursor is in
129 /// the mathatom left of the cursor
130 MathAtom const & prevAtom() const;
131 /// the mathatom left of the cursor
132 MathAtom & prevAtom();
133 /// the mathatom right of the cursor
134 MathAtom const & nextAtom() const;
135 /// the mathatom right of the cursor
136 MathAtom & nextAtom();
139 // text-specific part
141 /// see comment for boundary_ below
142 bool boundary() const { return top().boundary(); }
143 /// see comment for boundary_ below
144 bool & boundary() { return top().boundary(); }
145 /// the paragraph we're in
146 Paragraph & paragraph();
147 /// the paragraph we're in
148 Paragraph const & paragraph() const;
149 /// the row in the paragraph we're in
151 /// the row in the paragraph we're in
152 Row const & textRow() const;
154 LyXText * text() const;
156 InsetBase * innerInsetOfType(int code) const;
158 LyXText * innerText() const;
163 /// move on one logical position
165 /// move on one physical character or inset
167 /// move on one paragraph
171 /// move on one inset
173 /// move backward one logical position
175 /// move backward one physical character or inset
177 /// move backward one paragraph
179 /// move backward one cell
181 /// move backward one inset
182 void backwardInset();
184 /// are we some 'extension' (i.e. deeper nested) of the given iterator
185 bool hasPart(DocIterator const & it) const;
188 friend std::ostream &
189 operator<<(std::ostream & os, DocIterator const & cur);
195 DocIterator doc_iterator_begin(InsetBase & inset);
196 DocIterator doc_iterator_end(InsetBase & inset);
199 // The difference to a ('non stable') DocIterator is the removed
200 // (overwritte by 0...) part of the CursorSlice data items. So this thing
201 // is suitable for external storage, but not for iteration as such.
203 class StableDocIterator {
206 StableDocIterator() {}
207 /// non-explicit intended
208 StableDocIterator(const DocIterator & it);
210 DocIterator asDocIterator(InsetBase * start) const;
212 size_t size() const { return data_.size(); }
214 friend std::ostream &
215 operator<<(std::ostream & os, StableDocIterator const & cur);
217 friend std::istream &
218 operator>>(std::istream & is, StableDocIterator & cur);
220 std::vector<CursorSlice> data_;
223 bool operator==(StableDocIterator const &, StableDocIterator const &);