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