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