]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
the Buffer::text -> inset change
[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 LyXText;
21 class MathAtom;
22 class Paragraph;
23 class Row;
24
25
26
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)
30 {
31         return a == b;
32 }
33
34
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 DocumentIterator : public std::vector<CursorSlice>
38 {
39 public:
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;
50
51 public:
52         //
53         // access to slice at tip
54         //
55         /// access to tip
56         CursorSlice & top() { return back(); }
57         /// access to tip
58         CursorSlice const & top() const { return back(); }
59         /// how many nested insets do we have?
60         size_t depth() const { return size(); }
61         /// the containing inset
62         InsetBase & inset() const { return back().inset(); }
63         /// return the cell of the inset this cursor is in
64         idx_type idx() const { return back().idx(); }
65         /// return the cell of the inset this cursor is in
66         idx_type & idx() { return back().idx(); }
67         /// return the last possible cell in this inset
68         idx_type lastidx() const;
69         /// return the paragraph this cursor is in
70         par_type par() const { return back().par(); }
71         /// return the paragraph this cursor is in
72         par_type & par() { return back().par(); }
73         /// return the last possible paragraph in this inset
74         par_type lastpar() const;
75         /// return the position within the paragraph
76         pos_type pos() const { return back().pos(); }
77         /// return the position within the paragraph
78         pos_type & pos() { return back().pos(); }
79         /// return the last position within the paragraph
80         pos_type lastpos() const;
81         /// return the display row of the cursor with in the top par
82         row_type crow() const;
83         /// return the display row of the cursor with in the top par
84         row_type lastcrow() const;
85
86         /// return the number of embedded cells
87         size_t nargs() const;
88         /// return the number of embedded cells
89         size_t ncols() const;
90         /// return the number of embedded cells
91         size_t nrows() const;
92         /// return the grid row of the top cell
93         row_type row() const;
94         /// return the last row of the top grid
95         row_type lastrow() const { return nrows() - 1; }
96         /// return the grid column of the top cell
97         col_type col() const;
98         /// return the last column of the top grid
99         col_type lastcol() const { return ncols() - 1; }
100         /// the inset just behind the cursor
101         InsetBase * nextInset();
102         /// the inset just in front of the cursor
103         InsetBase * prevInset();
104         /// the inset just in front of the cursor
105         InsetBase const * prevInset() const;
106
107         /// are we in mathed?
108         bool inMathed() const;
109         /// are we in texted?
110         bool inTexted() const;
111
112         //
113         // math-specific part
114         //
115         /// return the mathed cell this cursor is in
116         MathArray const & cell() const;
117         /// return the mathed cell this cursor is in
118         MathArray & cell();
119         /// the mathatom left of the cursor
120         MathAtom const & prevAtom() const;
121         /// the mathatom left of the cursor
122         MathAtom & prevAtom();
123         /// the mathatom right of the cursor
124         MathAtom const & nextAtom() const;
125         /// the mathatom right of the cursor
126         MathAtom & nextAtom();
127
128         //
129         // text-specific part
130         //
131         /// see comment for boundary_ below
132         bool boundary() const { return top().boundary(); }
133         /// see comment for boundary_ below
134         bool & boundary() { return top().boundary(); }
135         /// the paragraph we're in
136         Paragraph & paragraph();
137         /// the paragraph we're in
138         Paragraph const & paragraph() const;
139         /// the row in the paragraph we're in
140         Row & textRow();
141         /// the row in the paragraph we're in
142         Row const & textRow() const;
143         ///
144         LyXText * text() const;
145         ///
146         InsetBase * innerInsetOfType(int code) const;
147         ///
148         LyXText * innerText() const;
149
150         //
151         // elementary moving
152         //
153         /// move on one position
154         void forwardPos();
155         /// move on one paragraph
156         void forwardPar();
157         /// move on one cell
158         void forwardIdx();
159         /// move on one inset
160         void forwardInset();
161         /// output
162         friend std::ostream &
163         operator<<(std::ostream & os, DocumentIterator const & cur);
164 };
165
166
167 ///
168 DocumentIterator insetBegin(InsetBase & inset);
169 ///
170 DocumentIterator insetEnd();
171
172
173 // The difference to a ('non stable') DocumentIterator is the removed
174 // (overwritte by 0...) part of the CursorSlice data items. So this thing
175 // is suitable for external storage, but not for iteration as such.
176
177 class StableDocumentIterator {
178 public:
179         ///
180         StableDocumentIterator() {}
181         /// non-explicit intended
182         StableDocumentIterator(const DocumentIterator & it);
183         ///
184         DocumentIterator asDocumentIterator(InsetBase * start) const;
185         ///
186         size_t size() const { return data_.size(); }
187         ///
188         friend std::ostream &
189         operator<<(std::ostream & os, StableDocumentIterator const & cur);
190         ///
191         friend std::istream &
192         operator>>(std::istream & is, StableDocumentIterator & cur);
193 private:
194         std::vector<CursorSlice> data_;
195 };
196
197 bool operator==(StableDocumentIterator const &, StableDocumentIterator const &);
198
199 #endif