]> git.lyx.org Git - lyx.git/blob - src/DocIterator.h
Transfer current_font and real_current_font from Text to Cursor.
[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 "CursorSlice.h"
16
17 #include <vector>
18 #include <iosfwd>
19
20
21 namespace lyx {
22
23 class Text;
24 class MathAtom;
25 class Paragraph;
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 DocIterator // : public std::vector<CursorSlice>
39 {
40 public:
41         /// type for cell number in inset
42         typedef CursorSlice::idx_type idx_type;
43         /// type for row indices
44         typedef CursorSlice::row_type row_type;
45         /// type for col indices
46         typedef CursorSlice::col_type col_type;
47
48 public:
49         ///
50         DocIterator();
51         ///
52         explicit DocIterator(Inset & inset);
53
54         /// access slice at position \p i
55         CursorSlice const & operator[](size_t i) const { return slices_[i]; }
56         /// access slice at position \p i
57         CursorSlice & operator[](size_t i) { return slices_[i]; }
58         /// chop a few slices from the iterator
59         void resize(size_t i) { slices_.resize(i); }
60
61         /// is the iterator valid?
62         operator const void*() const { return empty() ? 0 : this; }
63         /// is this iterator invalid?
64         bool operator!() const { return empty(); }
65
66         /// does this iterator have any content?
67         bool empty() const { return slices_.empty(); }
68
69         //
70         // access to slice at tip
71         //
72         /// access to tip
73         CursorSlice & top() { return slices_.back(); }
74         /// access to tip
75         CursorSlice const & top() const { return slices_.back(); }
76         /// access to outermost slice
77         CursorSlice & bottom() { return slices_.front(); }
78         /// access to outermost slice
79         CursorSlice const & bottom() const { return slices_.front(); }
80         /// how many nested insets do we have?
81         size_t depth() const { return slices_.size(); }
82         /// the containing inset
83         Inset & inset() const { return top().inset(); }
84         /// return the cell of the inset this cursor is in
85         idx_type idx() const { return top().idx(); }
86         /// return the cell of the inset this cursor is in
87         idx_type & idx() { return top().idx(); }
88         /// return the last possible cell in this inset
89         idx_type lastidx() const;
90         /// return the paragraph this cursor is in
91         pit_type pit() const { return top().pit(); }
92         /// return the paragraph this cursor is in
93         pit_type & pit() { return top().pit(); }
94         /// return the last possible paragraph in this inset
95         pit_type lastpit() const;
96         /// return the position within the paragraph
97         pos_type pos() const { return top().pos(); }
98         /// return the position within the paragraph
99         pos_type & pos() { return top().pos(); }
100         /// return the last position within the paragraph
101         pos_type lastpos() const;
102
103         /// return the number of embedded cells
104         size_t nargs() const;
105         /// return the number of embedded cells
106         size_t ncols() const;
107         /// return the number of embedded cells
108         size_t nrows() const;
109         /// return the grid row of the top cell
110         row_type row() const;
111         /// return the last row of the top grid
112         row_type lastrow() const { return nrows() - 1; }
113         /// return the grid column of the top cell
114         col_type col() const;
115         /// return the last column of the top grid
116         col_type lastcol() const { return ncols() - 1; }
117         /// the inset just behind the cursor
118         Inset * nextInset() const;
119         /// the inset just in front of the cursor
120         Inset * prevInset() const;
121         ///
122         bool boundary() const { return boundary_; }
123         ///
124         void boundary(bool b) { boundary_ = b; }
125
126         // the two methods below have been inlined out because of
127         // profiling results under linux when opening a document.
128         /// are we in mathed?.
129         bool inMathed() const
130         { return !empty() && inset().inMathed(); }
131         /// are we in texted?.
132         bool inTexted() const
133         { return !empty() && !inset().inMathed(); }
134
135         //
136         // math-specific part
137         //
138         /// return the mathed cell this cursor is in
139         MathData & cell() const;
140         /// the mathatom left of the cursor
141         MathAtom & prevAtom() const;
142         /// the mathatom right of the cursor
143         MathAtom & nextAtom() const;
144
145         // text-specific part
146         //
147         /// the paragraph we're in in text mode.
148         /// \warning only works within text!
149         Paragraph & paragraph() const;
150         /// the paragraph we're in in any case.
151         /// This method will give the containing paragraph if
152         /// in not in text mode (ex: in mathed).
153         Paragraph & innerParagraph() const;
154         ///
155         Text * text() const;
156         /// the containing inset or the cell, respectively
157         Inset * realInset() const;
158         ///
159         Inset * innerInsetOfType(int code) const;
160         ///
161         Text * innerText() const;
162
163         //
164         // elementary moving
165         //
166         /**
167          * move on one logical position, descend into nested insets
168          * skip collapsed insets if \p ignorecollapsed is true
169          */
170         void forwardPos(bool ignorecollapsed = false);
171         /// move on one physical character or inset
172         void forwardChar();
173         /// move on one paragraph
174         void forwardPar();
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 inset
184         /// FIXME: This is not implemented!
185         //void backwardInset();
186
187         /// are we some 'extension' (i.e. deeper nested) of the given iterator
188         bool hasPart(DocIterator const & it) const;
189
190         /// output
191         friend std::ostream &
192         operator<<(std::ostream & os, DocIterator const & cur);
193         ///
194         friend bool operator==(DocIterator const &, DocIterator const &);
195         friend bool operator<(DocIterator const &, DocIterator const &);
196         friend bool operator>(DocIterator const &, DocIterator const &);
197         friend bool operator<=(DocIterator const &, DocIterator const &);
198         ///
199         friend class StableDocIterator;
200 //protected:
201         ///
202         void clear() { slices_.clear(); }
203         ///
204         void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
205         ///
206         void pop_back() { slices_.pop_back(); }
207         /// recompute the inset parts of the cursor from the document data
208         void updateInsets(Inset * inset);
209         /// fix DocIterator in circumstances that should never happen.
210         /// \return true if the DocIterator was fixed.
211         bool fixIfBroken();
212
213 private:
214         /**
215          * When the cursor position is i, is the cursor after the i-th char
216          * or before the i+1-th char ? Normally, these two interpretations are
217          * equivalent, except when the fonts of the i-th and i+1-th char
218          * differ.
219          * We use boundary_ to distinguish between the two options:
220          * If boundary_=true, then the cursor is after the i-th char
221          * and if boundary_=false, then the cursor is before the i+1-th char.
222          *
223          * We currently use the boundary only when the language direction of
224          * the i-th char is different than the one of the i+1-th char.
225          * In this case it is important to distinguish between the two
226          * cursor interpretations, in order to give a reasonable behavior to
227          * the user.
228          */
229         bool boundary_;
230         ///
231         std::vector<CursorSlice> const & internalData() const {
232                 return slices_;
233         }
234         ///
235         std::vector<CursorSlice> slices_;
236         ///
237         Inset * inset_;
238 };
239
240
241 DocIterator doc_iterator_begin(Inset & inset);
242 DocIterator doc_iterator_end(Inset & inset);
243
244
245 inline
246 bool operator==(DocIterator const & di1, DocIterator const & di2)
247 {
248         return di1.slices_ == di2.slices_;
249 }
250
251
252 inline
253 bool operator!=(DocIterator const & di1, DocIterator const & di2)
254 {
255         return !(di1 == di2);
256 }
257
258
259 // The difference to a ('non stable') DocIterator is the removed
260 // (overwritten by 0...) part of the CursorSlice data items. So this thing
261 // is suitable for external storage, but not for iteration as such.
262
263 class StableDocIterator {
264 public:
265         ///
266         StableDocIterator() {}
267         /// non-explicit intended
268         StableDocIterator(const DocIterator & it);
269         ///
270         DocIterator asDocIterator(Inset * start) const;
271         ///
272         size_t size() const { return data_.size(); }
273         ///  return the position within the paragraph
274         pos_type pos() const { return data_.back().pos(); }
275         ///  return the position within the paragraph
276         pos_type & pos() { return data_.back().pos(); }
277         ///
278         friend std::ostream &
279         operator<<(std::ostream & os, StableDocIterator const & cur);
280         ///
281         friend std::istream &
282         operator>>(std::istream & is, StableDocIterator & cur);
283         ///
284         friend bool
285         operator==(StableDocIterator const &, StableDocIterator const &);
286 private:
287         std::vector<CursorSlice> data_;
288 };
289
290
291 } // namespace lyx
292
293 #endif