]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.h
send UpdatableInset down the way of the Dodo
[lyx.git] / src / cursor_slice.h
1 // -*- C++ -*-
2 /**
3  * \file cursor_slice.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
22 #include <cstddef>
23 #include <iosfwd>
24
25 class BufferView;
26 class InsetBase;
27 class MathInset;
28 class MathArray;
29 class LyXText;
30 class Paragraph;
31
32
33 /// This encapsulates a single slice of a document iterator as used e.g.
34 /// for cursors.
35
36 // After IU, the distinction of MathInset and InsetOld as well as
37 // that of MathArray and LyXText should vanish. They are conceptually the
38 // same (now...)
39
40 class CursorSlice {
41 public:
42         /// type for cell number in inset
43         typedef size_t idx_type;
44         /// type for paragraph numbers positions within a cell
45         typedef lyx::pit_type pit_type;
46         /// type for cursor positions within a cell
47         typedef lyx::pos_type pos_type;
48         /// type for row indices
49         typedef size_t row_type;
50         /// type for col indices
51         typedef size_t col_type;
52
53         ///
54         CursorSlice();
55         ///
56         explicit CursorSlice(InsetBase &);
57
58         /// the current inset
59         InsetBase & inset() const { return *inset_; }
60         /// return the cell this cursor is in
61         idx_type idx() const { return idx_; }
62         /// return the cell this cursor is in
63         idx_type & idx() { return idx_; }
64         /// return the last cell in this inset
65         idx_type lastidx() const { return nargs() - 1; }
66         /// return the offset of the paragraph this cursor is in
67         pit_type pit() const { return pit_; }
68         /// set the offset of the paragraph this cursor is in
69         pit_type & pit() { return pit_; }
70         /// increments the paragraph this cursor is in
71         void incrementPar();
72         /// decrements the paragraph this cursor is in
73         void decrementPar();
74         /// return the position within the paragraph
75         pos_type pos() const { return pos_; }
76         /// return the position within the paragraph
77         pos_type & pos() { return pos_; }
78         /// return the last position within the paragraph
79         pos_type lastpos() const;
80         /// return the number of embedded cells
81         size_t nargs() const;
82         /*!
83          * \return the number of columns.
84          * This does only make sense in grid like insets.
85          */
86         size_t ncols() const;
87         /*!
88          * \return the number of rows.
89          * This does only make sense in grid like insets.
90          */
91         size_t nrows() const;
92         /*!
93          * \return the grid row of the current cell.
94          * This does only make sense in grid like insets.
95          */
96         row_type row() const;
97         /*!
98          * \return the grid column of the current cell.
99          * This does only make sense in grid like insets.
100          */
101         col_type col() const;
102
103         ///
104         /// texted specific stuff
105         ///
106         /// returns text corresponding to this position
107         LyXText * text();
108         /// returns text corresponding to this position
109         LyXText const * text() const;
110         /// paragraph in this cell
111         Paragraph & paragraph();
112         /// paragraph in this cell
113         Paragraph const & paragraph() const;
114
115         ///
116         /// mathed specific stuff
117         ///
118         /// returns cell corresponding to this position
119         MathArray & cell() const;
120         /// returns the owning inset if it is a MathInset, else 0
121         MathInset * asMathInset() const;
122
123         /// write some debug information to \p os
124         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
125 public:
126         /// pointer to 'owning' inset. This is some kind of cache.
127         InsetBase * inset_;
128 private:
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.
134          * This means that the corresponding idx_ of a cell in a given row
135          * and column changes every time the number of columns or number of
136          * rows changes. Normally the cursor should stay in the same cell,
137          * so these changes should typically be performed like the following:
138          * \code
139          * row_type const r = cur.row();
140          * col_type const c = cur.col();
141          * // change nrows() and/or ncols()
142          * cur.idx = index(r, c);
143          * \endcode
144          */
145         idx_type idx_;
146         /// paragraph in this cell (used by texted)
147         pit_type pit_;
148         /// true if 'pit' was properly initialized
149         bool pit_valid_;
150         /// position in this cell
151         pos_type pos_;
152 };
153
154 /// test for equality
155 bool operator==(CursorSlice const &, CursorSlice const &);
156 /// test for inequality
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 /// test for order
163 bool operator<=(CursorSlice const &, CursorSlice const &);
164
165 #endif