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::par_type par_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 par_type par() const { return back().par(); }
85 /// return the paragraph this cursor is in
86 par_type & par() { return back().par(); }
87 /// return the last possible paragraph in this inset
88 par_type lastpar() 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;
95 /// return the display row of the cursor with in the top par
96 row_type crow() const;
97 /// return the display row of the cursor with in the top par
98 row_type lastcrow() const;
100 /// return the number of embedded cells
101 size_t nargs() const;
102 /// return the number of embedded cells
103 size_t ncols() const;
104 /// return the number of embedded cells
105 size_t nrows() const;
106 /// return the grid row of the top cell
107 row_type row() const;
108 /// return the last row of the top grid
109 row_type lastrow() const { return nrows() - 1; }
110 /// return the grid column of the top cell
111 col_type col() const;
112 /// return the last column of the top grid
113 col_type lastcol() const { return ncols() - 1; }
114 /// the inset just behind the cursor
115 InsetBase * nextInset();
116 /// the inset just in front of the cursor
117 InsetBase * prevInset();
118 /// the inset just in front of the cursor
119 InsetBase const * prevInset() const;
121 /// are we in mathed?
122 bool inMathed() const;
123 /// are we in texted?
124 bool inTexted() const;
127 // math-specific part
129 /// return the mathed cell this cursor is in
130 MathArray const & cell() const;
131 /// return the mathed cell this cursor is in
133 /// the mathatom left of the cursor
134 MathAtom const & prevAtom() const;
135 /// the mathatom left of the cursor
136 MathAtom & prevAtom();
137 /// the mathatom right of the cursor
138 MathAtom const & nextAtom() const;
139 /// the mathatom right of the cursor
140 MathAtom & nextAtom();
143 // text-specific part
145 /// see comment for boundary_ below
146 bool boundary() const { return top().boundary(); }
147 /// see comment for boundary_ below
148 bool & boundary() { return top().boundary(); }
149 /// the paragraph we're in
150 Paragraph & paragraph();
151 /// the paragraph we're in
152 Paragraph const & paragraph() const;
153 /// the row in the paragraph we're in
155 /// the row in the paragraph we're in
156 Row const & textRow() const;
158 LyXText * text() const;
160 InsetBase * innerInsetOfType(int code) const;
162 LyXText * innerText() const;
167 /// move on one logical position
169 /// move on one physical character or inset
171 /// 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 cell
185 /// move backward one inset
186 void backwardInset();
189 friend std::ostream &
190 operator<<(std::ostream & os, DocIterator const & cur);
196 DocIterator doc_iterator_begin(InsetBase & inset);
197 DocIterator doc_iterator_end(InsetBase & inset);
200 // The difference to a ('non stable') DocIterator is the removed
201 // (overwritte by 0...) part of the CursorSlice data items. So this thing
202 // is suitable for external storage, but not for iteration as such.
204 class StableDocIterator {
207 StableDocIterator() {}
208 /// non-explicit intended
209 StableDocIterator(const DocIterator & it);
211 DocIterator asDocIterator(InsetBase * start) const;
213 size_t size() const { return data_.size(); }
215 friend std::ostream &
216 operator<<(std::ostream & os, StableDocIterator const & cur);
218 friend std::istream &
219 operator>>(std::istream & is, StableDocIterator & cur);
221 std::vector<CursorSlice> data_;
224 bool operator==(StableDocIterator const &, StableDocIterator const &);