]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.h
fix bug 2089: Touching Navigate menu crashes Lyx when a TOC inset is in a section...
[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 class BufferView;
27 class InsetBase;
28 class MathInset;
29 class MathArray;
30 class LyXText;
31 class Paragraph;
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 { return inset_->nargs(); }
82         /// return the number of columns (1 in non-grid-like insets)
83         size_t ncols() const { return inset_->ncols(); }
84         /// return the number of rows (1 in non-grid-like insets)
85         size_t nrows() const { return inset_->nrows(); }
86         /*!
87          * \return the grid row of the current cell.
88          * This does only make sense in grid like insets.
89          */
90         row_type row() const;
91         /*!
92          * \return the grid column of the current cell.
93          * This does only make sense in grid like insets.
94          */
95         col_type col() const;
96
97         ///
98         /// texted specific stuff
99         ///
100         /// returns text corresponding to this position
101         LyXText * text() { return inset_->getText(idx_); }
102         /// returns text corresponding to this position
103         LyXText const * text() const { return inset_->getText(idx_); }
104         /// paragraph in this cell
105         Paragraph & paragraph();
106         /// paragraph in this cell
107         Paragraph const & paragraph() const;
108
109         ///
110         /// mathed specific stuff
111         ///
112         /// returns the owning inset if it is a MathInset, else 0
113         MathInset * asMathInset() const { return inset_->asMathInset(); }
114         /// returns cell corresponding to this position
115         MathArray & cell() const;
116
117         /// write some debug information to \p os
118         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
119 public:
120         /// pointer to 'owning' inset. This is some kind of cache.
121         InsetBase * inset_;
122 private:
123         /*!
124          * Cell index of a position in this inset.
125          * This is the primary cell information also for grid like insets,
126          * although we have the convenience functions row() and col() for
127          * those * and column changes every time the number of columns ornumber
128          * of rows changes. Normally the cursor should stay in the same cell,
129          * so these changes should typically be performed like the following:
130          * \code
131          * row_type const r = cur.row();
132          * col_type const c = cur.col();
133          * // change nrows() and/or ncols()
134          * cur.idx = index(r, c);
135          * \endcode
136          */
137         idx_type idx_;
138         /// paragraph in this cell (used by texted)
139         pit_type pit_;
140         /// true if 'pit' was properly initialized
141         bool pit_valid_;
142         /// position in this cell
143         pos_type pos_;
144 };
145
146 /// test for equality
147 bool operator==(CursorSlice const &, CursorSlice const &);
148 /// test for inequality
149 bool operator!=(CursorSlice const &, CursorSlice const &);
150 /// test for order
151 bool operator<(CursorSlice const &, CursorSlice const &);
152 /// test for order
153 bool operator>(CursorSlice const &, CursorSlice const &);
154 /// test for order
155 bool operator<=(CursorSlice const &, CursorSlice const &);
156
157 #endif