]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
the DocIterator stuff
[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
19 class BufferView;
20 class MathAtom;
21 class LyXText;
22 class Paragraph;
23 class Row;
24
25
26 // only needed for gcc 2.95, remove when support terminated
27 template <typename A, typename B>
28 bool ptr_cmp(A const * a, B const * b)
29 {
30         return a == b;
31 }
32
33
34 // The public inheritance should go in favour of a suitable data member
35 // (or maybe private inheritance) at some point of time.
36 class DocumentIterator : public std::vector<CursorSlice>
37 {
38 public:
39         /// type for cell number in inset
40         typedef CursorSlice::idx_type idx_type;
41         /// type for paragraph numbers positions within a cell
42         typedef CursorSlice::par_type par_type;
43         /// type for cursor positions within a cell
44         typedef CursorSlice::pos_type pos_type;
45         /// type for row indices
46         typedef CursorSlice::row_type row_type;
47         /// type for col indices
48         typedef CursorSlice::col_type col_type;
49
50 public:
51         ///
52         DocumentIterator();
53         ///
54         explicit DocumentIterator(BufferView & bv);
55         ///
56         BufferView & bv() const { return *bv_; } 
57
58         //
59         // access to slice at tip
60         //
61         /// access to tip
62         CursorSlice & top() { return back(); }
63         /// access to tip
64         CursorSlice const & top() const { return back(); }
65         /// how many nested insets do we have?
66         size_t depth() const { return size(); }
67         /// the containing inset
68         InsetBase * inset() const { return back().inset(); }
69         /// return the cell of the inset this cursor is in
70         idx_type idx() const { return back().idx(); }
71         /// return the cell of the inset this cursor is in
72         idx_type & idx() { return back().idx(); }
73         /// return the last possible cell in this inset
74         idx_type lastidx() const;
75         /// return the paragraph this cursor is in
76         par_type par() const { return back().par(); }
77         /// return the paragraph this cursor is in
78         par_type & par() { return back().par(); }
79         /// return the last possible paragraph in this inset
80         par_type lastpar() const;
81         /// return the position within the paragraph
82         pos_type pos() const { return back().pos(); }
83         /// return the position within the paragraph
84         pos_type & pos() { return back().pos(); }
85         /// return the last position within the paragraph
86         pos_type lastpos() const;
87         /// return the display row of the cursor with in the top par
88         row_type crow() const;
89         /// return the display row of the cursor with in the top par
90         row_type lastcrow() const;
91
92         /// return the number of embedded cells
93         size_t nargs() const;
94         /// return the number of embedded cells
95         size_t ncols() const;
96         /// return the number of embedded cells
97         size_t nrows() const;
98         /// return the grid row of the top cell
99         row_type row() const;
100         /// return the last row of the top grid
101         row_type lastrow() const { return nrows() - 1; }
102         /// return the grid column of the top cell
103         col_type col() const;
104         /// return the last column of the top grid
105         col_type lastcol() const { return ncols() - 1; }
106         /// the inset just behind the cursor
107         InsetBase * nextInset();
108         /// the inset just in front of the cursor
109         InsetBase * prevInset();
110         /// the inset just in front of the cursor
111         InsetBase const * prevInset() const;
112
113         /// are we in mathed?
114         bool inMathed() const;
115         /// are we in texted?
116         bool inTexted() const;
117
118         //
119         // math-specific part
120         //
121         /// return the mathed cell this cursor is in
122         MathArray const & cell() const;
123         /// return the mathed cell this cursor is in
124         MathArray & cell();
125         /// the mathatom left of the cursor
126         MathAtom const & prevAtom() const;
127         /// the mathatom left of the cursor
128         MathAtom & prevAtom();
129         /// the mathatom right of the cursor
130         MathAtom const & nextAtom() const;
131         /// the mathatom right of the cursor
132         MathAtom & nextAtom();
133
134         //
135         // text-specific part
136         //
137         /// see comment for boundary_ below
138         bool boundary() const { return top().boundary(); }
139         /// see comment for boundary_ below
140         bool & boundary() { return top().boundary(); }
141         /// the paragraph we're in
142         Paragraph & paragraph();
143         /// the paragraph we're in
144         Paragraph const & paragraph() const;
145         /// the row in the paragraph we're in
146         Row & textRow();
147         /// the row in the paragraph we're in
148         Row const & textRow() const;
149         ///
150         LyXText * text() const;
151         ///
152         CursorSlice const & innerTextSlice() const;
153         ///
154         InsetBase * innerInsetOfType(int code) const;
155         ///
156         LyXText * innerText() const;
157
158         //
159         // elementary moving
160         //
161         /// move on one position
162         void forwardPos();
163         /// move on one paragraph
164         void forwardPar();
165         /// move on one cell
166         void forwardIdx();
167         /// move on one inset
168         void forwardInset();
169
170 private:
171         ///
172         BufferView * bv_;       
173 };
174
175
176 ///
177 DocumentIterator bufferBegin(BufferView & bv);
178 ///
179 DocumentIterator bufferEnd();
180 ///
181 DocumentIterator insetBegin(BufferView & bv, InsetBase * inset);
182 ///
183 DocumentIterator insetEnd();
184
185 #endif