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"
28 // only needed for gcc 2.95, remove when support terminated
29 template <typename A, typename B>
30 bool ptr_cmp(A const * a, B const * b)
36 // The public inheritance should go in favour of a suitable data member
37 // (or maybe private inheritance) at some point of time.
38 class DocumentIterator : public std::vector<CursorSlice>
41 /// type for cell number in inset
42 typedef CursorSlice::idx_type idx_type;
43 /// type for paragraph numbers positions within a cell
44 typedef CursorSlice::par_type par_type;
45 /// type for cursor positions within a cell
46 typedef CursorSlice::pos_type pos_type;
47 /// type for row indices
48 typedef CursorSlice::row_type row_type;
49 /// type for col indices
50 typedef CursorSlice::col_type col_type;
56 explicit DocumentIterator(BufferView & bv);
58 BufferView & bv() const { return *bv_; }
61 // access to slice at tip
64 CursorSlice & top() { return back(); }
66 CursorSlice const & top() const { return back(); }
67 /// how many nested insets do we have?
68 size_t depth() const { return size(); }
69 /// the containing inset
70 InsetBase * inset() const { return back().inset(); }
71 /// return the cell of the inset this cursor is in
72 idx_type idx() const { return back().idx(); }
73 /// return the cell of the inset this cursor is in
74 idx_type & idx() { return back().idx(); }
75 /// return the last possible cell in this inset
76 idx_type lastidx() const;
77 /// return the paragraph this cursor is in
78 par_type par() const { return back().par(); }
79 /// return the paragraph this cursor is in
80 par_type & par() { return back().par(); }
81 /// return the last possible paragraph in this inset
82 par_type lastpar() const;
83 /// return the position within the paragraph
84 pos_type pos() const { return back().pos(); }
85 /// return the position within the paragraph
86 pos_type & pos() { return back().pos(); }
87 /// return the last position within the paragraph
88 pos_type lastpos() const;
89 /// return the display row of the cursor with in the top par
90 row_type crow() const;
91 /// return the display row of the cursor with in the top par
92 row_type lastcrow() const;
94 /// return the number of embedded cells
96 /// return the number of embedded cells
98 /// return the number of embedded cells
100 /// return the grid row of the top cell
101 row_type row() const;
102 /// return the last row of the top grid
103 row_type lastrow() const { return nrows() - 1; }
104 /// return the grid column of the top cell
105 col_type col() const;
106 /// return the last column of the top grid
107 col_type lastcol() const { return ncols() - 1; }
108 /// the inset just behind the cursor
109 InsetBase * nextInset();
110 /// the inset just in front of the cursor
111 InsetBase * prevInset();
112 /// the inset just in front of the cursor
113 InsetBase const * prevInset() const;
115 /// are we in mathed?
116 bool inMathed() const;
117 /// are we in texted?
118 bool inTexted() const;
121 // math-specific part
123 /// return the mathed cell this cursor is in
124 MathArray const & cell() const;
125 /// return the mathed cell this cursor is in
127 /// the mathatom left of the cursor
128 MathAtom const & prevAtom() const;
129 /// the mathatom left of the cursor
130 MathAtom & prevAtom();
131 /// the mathatom right of the cursor
132 MathAtom const & nextAtom() const;
133 /// the mathatom right of the cursor
134 MathAtom & nextAtom();
137 // text-specific part
139 /// see comment for boundary_ below
140 bool boundary() const { return top().boundary(); }
141 /// see comment for boundary_ below
142 bool & boundary() { return top().boundary(); }
143 /// the paragraph we're in
144 Paragraph & paragraph();
145 /// the paragraph we're in
146 Paragraph const & paragraph() const;
147 /// the row in the paragraph we're in
149 /// the row in the paragraph we're in
150 Row const & textRow() const;
152 LyXText * text() const;
154 CursorSlice const & innerTextSlice() const;
156 InsetBase * innerInsetOfType(int code) const;
158 LyXText * innerText() const;
163 /// move on one position
165 /// move on one paragraph
169 /// move on one inset
172 friend std::ostream &
173 operator<<(std::ostream & os, DocumentIterator const & cur);
182 DocumentIterator bufferBegin(BufferView & bv);
184 DocumentIterator bufferEnd();
186 DocumentIterator insetBegin(BufferView & bv, InsetBase * inset);
188 DocumentIterator insetEnd();
191 // The difference to a ('non stable') DocumentIterator is the removed
192 // (overwritte by 0...) part of the CursorSlice data items. So this thing
193 // is suitable for external storage, but not for iteration as such.
195 class StableDocumentIterator {
198 StableDocumentIterator() {}
199 /// non-explicit intended
200 StableDocumentIterator(const DocumentIterator & it);
202 DocumentIterator asDocumentIterator(BufferView & bv) const;
204 size_t size() const { return data_.size(); }
206 friend std::ostream &
207 operator<<(std::ostream & os, StableDocumentIterator const & cur);
209 friend std::istream &
210 operator>>(std::istream & is, StableDocumentIterator & cur);
212 std::vector<CursorSlice> data_;
215 bool operator==(StableDocumentIterator const &, StableDocumentIterator const &);