]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.h
document my LFUN changes in the past
[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 "support/strfwd.h"
22 #include "insets/Inset.h"
23
24
25 namespace lyx {
26
27 class Inset;
28 class MathData;
29 class Text;
30 class Paragraph;
31
32 /// This encapsulates a single slice of a document iterator as used e.g.
33 /// for cursors.
34
35 // After IU, the distinction of MathInset and InsetOld as well as
36 // that of MathData and Text should vanish. They are conceptually the
37 // same (now...)
38
39 class CursorSlice {
40 public:
41         /// Those needs inset_ access.
42         ///@{
43         friend class DocIterator;
44         friend class StableDocIterator;
45         ///@}
46
47         /// type for cell number in inset
48         typedef size_t idx_type;
49         /// type for row indices
50         typedef size_t row_type;
51         /// type for col indices
52         typedef size_t col_type;
53
54         ///
55         CursorSlice();
56         ///
57         explicit CursorSlice(Inset &);
58
59         /// the current inset
60         Inset & inset() const { return *inset_; }
61         /// return the cell this cursor is in
62         idx_type idx() const { return idx_; }
63         /// return the cell this cursor is in
64         idx_type & idx() { return idx_; }
65         /// return the last cell in this inset
66         idx_type lastidx() const { return nargs() - 1; }
67         /// return the offset of the paragraph this cursor is in
68         pit_type pit() const { return pit_; }
69         /// set the offset of the paragraph this cursor is in
70         pit_type & pit() { return pit_; }
71         /// return the last paragraph offset within the ParagraphList
72         pit_type lastpit() const;
73         /// increments the paragraph this cursor is in
74         void incrementPar();
75         /// decrements the paragraph this cursor is in
76         void decrementPar();
77         /// return the position within the paragraph
78         pos_type pos() const { return pos_; }
79         /// return the position within the paragraph
80         pos_type & pos() { return pos_; }
81         /// return the last position within the paragraph
82         pos_type lastpos() const;
83         /// return the number of embedded cells
84         size_t nargs() const { return inset_->nargs(); }
85         /// return the number of columns (1 in non-grid-like insets)
86         size_t ncols() const { return inset_->ncols(); }
87         /// return the number of rows (1 in non-grid-like insets)
88         size_t nrows() const { return inset_->nrows(); }
89         /*!
90          * \return the grid row of the current cell.
91          * This does only make sense in grid like insets.
92          */
93         row_type row() const;
94         /*!
95          * \return the grid column of the current cell.
96          * This does only make sense in grid like insets.
97          */
98         col_type col() const;
99
100         ///
101         /// texted specific stuff
102         ///
103         /// returns text corresponding to this position
104         Text * text() const { return inset_->getText(idx_); }
105         /// paragraph in this cell
106         Paragraph & paragraph() const;
107
108         ///
109         /// mathed specific stuff
110         ///
111         /// returns the owning inset if it is a InsetMath, else 0
112         InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
113         /// returns cell corresponding to this position
114         MathData & cell() const;
115
116         /// write some debug information to \p os
117         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
118         /// move to next position
119         void forwardPos();
120         /// move to previous position
121         void backwardPos();
122         /// move to next cell
123         void forwardIdx();
124         /// move to previous cell
125         void backwardIdx();
126         /// are we at the end of this slice
127         bool at_end() const;
128         /// are we at the start of this slice
129         bool at_begin() const;
130         
131 private:
132
133         /// pointer to 'owning' inset. This is some kind of cache.
134         Inset * inset_;
135
136         /*!
137          * Cell index of a position in this inset.
138          * This is the primary cell information also for grid like insets,
139          * although we have the convenience functions row() and col() for
140          * those * and column changes every time the number of columns ornumber
141          * of rows changes. Normally the cursor should stay in the same cell,
142          * so these changes should typically be performed like the following:
143          * \code
144          * row_type const r = cur.row();
145          * col_type const c = cur.col();
146          * // change nrows() and/or ncols()
147          * cur.idx = index(r, c);
148          * \endcode
149          */
150         idx_type idx_;
151         /// paragraph in this cell (used by texted)
152         pit_type pit_;
153         /// position in this cell
154         pos_type pos_;
155 };
156
157 /// test for equality
158 bool operator==(CursorSlice const &, CursorSlice const &);
159 /// test for inequality
160 bool operator!=(CursorSlice const &, CursorSlice const &);
161 /// test for order
162 bool operator<(CursorSlice const &, CursorSlice const &);
163 /// test for order
164 bool operator>(CursorSlice const &, CursorSlice const &);
165 /// test for order
166 bool operator<=(CursorSlice const &, CursorSlice const &);
167
168
169 } // namespace lyx
170
171 #endif