]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.h
make boundary property an iterator property instead of a CursorSlice property
[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 class UpdatableInset;
32
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 UpdatableInset as well as
38 // that of MathArray and LyXText should vanish. They are conceptually the
39 // same (now...)
40
41 class CursorSlice {
42 public:
43         /// type for cell number in inset
44         typedef size_t idx_type;
45         /// type for paragraph numbers positions within a cell
46         typedef lyx::pit_type pit_type;
47         /// type for cursor positions within a cell
48         typedef lyx::pos_type pos_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(InsetBase &);
58
59         /// the current inset
60         InsetBase & 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         /// increments the paragraph this cursor is in
72         void incrementPar();
73         /// decrements the paragraph this cursor is in
74         void decrementPar();
75         /// return the position within the paragraph
76         pos_type pos() const { return pos_; }
77         /// return the position within the paragraph
78         pos_type & pos() { return pos_; }
79         /// return the last position within the paragraph
80         pos_type lastpos() const;
81         /// return the number of embedded cells
82         size_t nargs() const;
83         /*!
84          * \return the number of columns.
85          * This does only make sense in grid like insets.
86          */
87         size_t ncols() const;
88         /*!
89          * \return the number of rows.
90          * This does only make sense in grid like insets.
91          */
92         size_t nrows() const;
93         /*!
94          * \return the grid row of the current cell.
95          * This does only make sense in grid like insets.
96          */
97         row_type row() const;
98         /*!
99          * \return the grid column of the current cell.
100          * This does only make sense in grid like insets.
101          */
102         col_type col() const;
103
104         ///
105         /// texted specific stuff
106         ///
107         /// returns text corresponding to this position
108         LyXText * text();
109         /// returns text corresponding to this position
110         LyXText const * text() const;
111         /// returns the owning inset if it is an UpdatableInset, else 0
112         UpdatableInset * asUpdatableInset() const;
113         /// paragraph in this cell
114         Paragraph & paragraph();
115         /// paragraph in this cell
116         Paragraph const & paragraph() const;
117
118         ///
119         /// mathed specific stuff
120         ///
121         /// returns cell corresponding to this position
122         MathArray & cell() const;
123         /// returns the owning inset if it is a MathInset, else 0
124         MathInset * asMathInset() const;
125
126         /// write some debug information to \p os
127         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
128 public:
129         /// pointer to 'owning' inset. This is some kind of cache.
130         InsetBase * inset_;
131 private:
132         /*!
133          * Cell index of a position in this inset.
134          * This is the primary cell information also for grid like insets,
135          * although we have the convenience functions row() and col() for
136          * those.
137          * This means that the corresponding idx_ of a cell in a given row
138          * and column changes every time the number of columns or number of
139          * rows changes. Normally the cursor should stay in the same cell,
140          * so these changes should typically be performed like the following:
141          * \code
142          * row_type const r = cur.row();
143          * col_type const c = cur.col();
144          * // change nrows() and/or ncols()
145          * cur.idx = index(r, c);
146          * \endcode
147          */
148         idx_type idx_;
149         /// paragraph in this cell (used by texted)
150         pit_type pit_;
151         /// true if 'pit' was properly initialized
152         bool pit_valid_;
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 #endif