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