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