]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
revert Buffer LyxText->InsetText commit
[lyx.git] / src / dociterator.h
1 // -*- C++ -*-
2 /**
3  * \file dociterator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DOCITERATOR_H
13 #define DOCITERATOR_H
14
15 #include "cursor_slice.h"
16
17 #include <vector>
18 #include <iosfwd>
19
20 class BufferView;
21 class MathAtom;
22 class LyXText;
23 class Paragraph;
24 class Row;
25
26
27
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)
31 {
32         return a == b;
33 }
34
35
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>
39 {
40 public:
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;
51
52 public:
53         ///
54         DocumentIterator();
55         ///
56         explicit DocumentIterator(BufferView & bv);
57         ///
58         BufferView & bv() const { return *bv_; } 
59
60         //
61         // access to slice at tip
62         //
63         /// access to tip
64         CursorSlice & top() { return back(); }
65         /// access to tip
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;
93
94         /// return the number of embedded cells
95         size_t nargs() const;
96         /// return the number of embedded cells
97         size_t ncols() const;
98         /// return the number of embedded cells
99         size_t nrows() const;
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;
114
115         /// are we in mathed?
116         bool inMathed() const;
117         /// are we in texted?
118         bool inTexted() const;
119
120         //
121         // math-specific part
122         //
123         /// return the mathed cell this cursor is in
124         MathArray const & cell() const;
125         /// return the mathed cell this cursor is in
126         MathArray & cell();
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();
135
136         //
137         // text-specific part
138         //
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
148         Row & textRow();
149         /// the row in the paragraph we're in
150         Row const & textRow() const;
151         ///
152         LyXText * text() const;
153         ///
154         CursorSlice const & innerTextSlice() const;
155         ///
156         InsetBase * innerInsetOfType(int code) const;
157         ///
158         LyXText * innerText() const;
159
160         //
161         // elementary moving
162         //
163         /// move on one position
164         void forwardPos();
165         /// move on one paragraph
166         void forwardPar();
167         /// move on one cell
168         void forwardIdx();
169         /// move on one inset
170         void forwardInset();
171         /// output
172         friend std::ostream &
173         operator<<(std::ostream & os, DocumentIterator const & cur);
174
175 private:
176         ///
177         BufferView * bv_;       
178 };
179
180
181 ///
182 DocumentIterator bufferBegin(BufferView & bv);
183 ///
184 DocumentIterator bufferEnd();
185 ///
186 DocumentIterator insetBegin(BufferView & bv, InsetBase * inset);
187 ///
188 DocumentIterator insetEnd();
189
190
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.
194
195 class StableDocumentIterator {
196 public:
197         ///
198         StableDocumentIterator() {}
199         /// non-explicit intended
200         StableDocumentIterator(const DocumentIterator & it);
201         ///
202         DocumentIterator asDocumentIterator(BufferView & bv) const;
203         ///
204         size_t size() const { return data_.size(); }
205         ///
206         friend std::ostream &
207         operator<<(std::ostream & os, StableDocumentIterator const & cur);
208         ///
209         friend std::istream &
210         operator>>(std::istream & is, StableDocumentIterator & cur);
211 private:
212         std::vector<CursorSlice> data_;
213 };
214
215 bool operator==(StableDocumentIterator const &, StableDocumentIterator const &);
216
217 #endif