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