]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.h
89fd6f5219163504284d038c80ed2818acf466b0
[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         virtual ~CursorSlice();
67         ///
68         CursorSlice & operator=(CursorSlice const &);
69         ///
70         bool isValid() const;
71
72         /// the current inset
73         Inset & inset() const { return *inset_; }
74         /// return the cell this cursor is in
75         idx_type idx() const { return idx_; }
76         /// return the cell this cursor is in
77         idx_type & idx() { return idx_; }
78         /// return the last cell in this inset
79         idx_type lastidx() const { return nargs() - 1; }
80         /// return the offset of the paragraph this cursor is in
81         pit_type pit() const { return pit_; }
82         /// set the offset of the paragraph this cursor is in
83         pit_type & pit() { return pit_; }
84         /// increments the paragraph this cursor is in
85         void incrementPar();
86         /// decrements the paragraph this cursor is in
87         void decrementPar();
88         /// return the position within the paragraph
89         pos_type pos() const { return pos_; }
90         /// return the position within the paragraph
91         pos_type & pos() { return pos_; }
92         /// return the last position within the paragraph
93         pos_type lastpos() const;
94         /// return the number of embedded cells
95         size_t nargs() const { return inset_->nargs(); }
96         /// return the number of columns (1 in non-grid-like insets)
97         size_t ncols() const { return inset_->ncols(); }
98         /// return the number of rows (1 in non-grid-like insets)
99         size_t nrows() const { return inset_->nrows(); }
100         /*!
101          * \return the grid row of the current cell.
102          * This does only make sense in grid like insets.
103          */
104         row_type row() const;
105         /*!
106          * \return the grid column of the current cell.
107          * This does only make sense in grid like insets.
108          */
109         col_type col() const;
110
111         ///
112         /// texted specific stuff
113         ///
114         /// returns text corresponding to this position
115         Text * text() { return inset_->getText(idx_); }
116         /// returns text corresponding to this position
117         Text const * text() const { return inset_->getText(idx_); }
118         /// paragraph in this cell
119         Paragraph & paragraph();
120         /// paragraph in this cell
121         Paragraph const & paragraph() const;
122
123         ///
124         /// mathed specific stuff
125         ///
126         /// returns the owning inset if it is a InsetMath, else 0
127         InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
128         /// returns cell corresponding to this position
129         MathData & cell() const;
130
131         /// write some debug information to \p os
132         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
133 private:
134         ///
135         void invalidate();
136
137         /// pointer to 'owning' inset. This is some kind of cache.
138         Inset * inset_;
139
140         /*!
141          * Cell index of a position in this inset.
142          * This is the primary cell information also for grid like insets,
143          * although we have the convenience functions row() and col() for
144          * those * and column changes every time the number of columns ornumber
145          * of rows changes. Normally the cursor should stay in the same cell,
146          * so these changes should typically be performed like the following:
147          * \code
148          * row_type const r = cur.row();
149          * col_type const c = cur.col();
150          * // change nrows() and/or ncols()
151          * cur.idx = index(r, c);
152          * \endcode
153          */
154         idx_type idx_;
155         /// paragraph in this cell (used by texted)
156         pit_type pit_;
157         /// true if 'pit' was properly initialized
158         bool pit_valid_;
159         /// position in this cell
160         pos_type pos_;
161         /// connection to referred \c inset_ destruction signal.
162         boost::signals::connection inset_connection_;
163 };
164
165 /// test for equality
166 bool operator==(CursorSlice const &, CursorSlice const &);
167 /// test for inequality
168 bool operator!=(CursorSlice const &, CursorSlice const &);
169 /// test for order
170 bool operator<(CursorSlice const &, CursorSlice const &);
171 /// test for order
172 bool operator>(CursorSlice const &, CursorSlice const &);
173 /// test for order
174 bool operator<=(CursorSlice const &, CursorSlice const &);
175
176
177 } // namespace lyx
178
179 #endif