]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.h
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / CursorSlice.h
1 // -*- C++ -*-
2 /**
3  * \file CursorSlice.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #ifndef CURSORSLICE_H
18 #define CURSORSLICE_H
19
20 #include "support/types.h"
21 #include "insets/Inset.h"
22
23 #include <cstddef>
24 #include <iosfwd>
25
26
27 namespace lyx {
28
29 class Inset;
30 class MathData;
31 class Text;
32 class Paragraph;
33
34 /// This encapsulates a single slice of a document iterator as used e.g.
35 /// for cursors.
36
37 // After IU, the distinction of MathInset and InsetOld as well as
38 // that of MathData and Text should vanish. They are conceptually the
39 // same (now...)
40
41 class CursorSlice {
42 public:
43         /// Those needs inset_ access.
44         ///@{
45         friend class DocIterator;
46         friend class StableDocIterator;
47         ///@}
48
49         /// type for cell number in inset
50         typedef size_t idx_type;
51         /// type for row indices
52         typedef size_t row_type;
53         /// type for col indices
54         typedef size_t col_type;
55
56         ///
57         CursorSlice();
58         ///
59         explicit CursorSlice(Inset &);
60
61         /// the current inset
62         Inset & inset() const { return *inset_; }
63         /// return the cell this cursor is in
64         idx_type idx() const { return idx_; }
65         /// return the cell this cursor is in
66         idx_type & idx() { return idx_; }
67         /// return the last cell in this inset
68         idx_type lastidx() const { return nargs() - 1; }
69         /// return the offset of the paragraph this cursor is in
70         pit_type pit() const { return pit_; }
71         /// set the offset of the paragraph this cursor is in
72         pit_type & pit() { return pit_; }
73         /// return the last paragraph offset this cursor is in
74         pit_type lastpit() const;
75         /// increments the paragraph this cursor is in
76         void incrementPar();
77         /// decrements the paragraph this cursor is in
78         void decrementPar();
79         /// return the position within the paragraph
80         pos_type pos() const { return pos_; }
81         /// return the position within the paragraph
82         pos_type & pos() { return pos_; }
83         /// return the last position within the paragraph
84         pos_type lastpos() const;
85         /// return the number of embedded cells
86         size_t nargs() const { return inset_->nargs(); }
87         /// return the number of columns (1 in non-grid-like insets)
88         size_t ncols() const { return inset_->ncols(); }
89         /// return the number of rows (1 in non-grid-like insets)
90         size_t nrows() const { return inset_->nrows(); }
91         /*!
92          * \return the grid row of the current cell.
93          * This does only make sense in grid like insets.
94          */
95         row_type row() const;
96         /*!
97          * \return the grid column of the current cell.
98          * This does only make sense in grid like insets.
99          */
100         col_type col() const;
101
102         ///
103         /// texted specific stuff
104         ///
105         /// returns text corresponding to this position
106         Text * text() { return inset_->getText(idx_); }
107         /// returns text corresponding to this position
108         Text const * text() const { return inset_->getText(idx_); }
109         /// paragraph in this cell
110         Paragraph & paragraph();
111         /// paragraph in this cell
112         Paragraph const & paragraph() const;
113
114         ///
115         /// mathed specific stuff
116         ///
117         /// returns the owning inset if it is a InsetMath, else 0
118         InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
119         /// returns cell corresponding to this position
120         MathData & cell() const;
121
122         /// write some debug information to \p os
123         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
124 private:
125
126         /// pointer to 'owning' inset. This is some kind of cache.
127         Inset * inset_;
128
129         /*!
130          * Cell index of a position in this inset.
131          * This is the primary cell information also for grid like insets,
132          * although we have the convenience functions row() and col() for
133          * those * and column changes every time the number of columns ornumber
134          * of rows changes. Normally the cursor should stay in the same cell,
135          * so these changes should typically be performed like the following:
136          * \code
137          * row_type const r = cur.row();
138          * col_type const c = cur.col();
139          * // change nrows() and/or ncols()
140          * cur.idx = index(r, c);
141          * \endcode
142          */
143         idx_type idx_;
144         /// paragraph in this cell (used by texted)
145         pit_type pit_;
146         /// true if 'pit' was properly initialized
147         bool pit_valid_;
148         /// position in this cell
149         pos_type pos_;
150 };
151
152 /// test for equality
153 bool operator==(CursorSlice const &, CursorSlice const &);
154 /// test for inequality
155 bool operator!=(CursorSlice const &, CursorSlice const &);
156 /// test for order
157 bool operator<(CursorSlice const &, CursorSlice const &);
158 /// test for order
159 bool operator>(CursorSlice const &, CursorSlice const &);
160 /// test for order
161 bool operator<=(CursorSlice const &, CursorSlice const &);
162
163
164 } // namespace lyx
165
166 #endif