]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.h
Disable CheckTeX while buffer is processed
[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 "support/types.h"
21 #include "support/strfwd.h"
22 #include "insets/Inset.h"
23
24
25 namespace lyx {
26
27 class Inset;
28 class MathData;
29 class Text;
30 class Paragraph;
31
32 /// This encapsulates a single slice of a document iterator as used e.g.
33 /// for cursors.
34
35 // After inset unification, the distinction of InsetMath and Inset as well as
36 // that of MathData and Text should vanish. They are conceptually the
37 // same (now...)
38
39 class CursorSlice {
40 public:
41         /// \name Those needs inset_ access.
42         //@{
43         friend class DocIterator;
44         friend class StableDocIterator;
45         //@}
46
47         /// type for cell number in inset
48         typedef size_t idx_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(Inset &);
58
59         /// \name Comparison operators.
60         //@{
61         friend bool operator==(CursorSlice const &, CursorSlice const &);
62         friend bool operator!=(CursorSlice const &, CursorSlice const &);
63         friend bool operator<(CursorSlice const &, CursorSlice const &);
64         friend bool operator>(CursorSlice const &, CursorSlice const &);
65         friend bool operator<=(CursorSlice const &, CursorSlice const &);
66         //@}
67
68         /// return true if the slice has not been initialized
69         bool empty() const { return !inset_; }
70         /// the current inset
71         Inset & inset() const { return *inset_; }
72         /// return the cell this cursor is in
73         idx_type idx() const { return idx_; }
74         /// return the cell this cursor is in
75         idx_type & idx() { return idx_; }
76         /// return the last cell in this inset
77         idx_type lastidx() const { return nargs() - 1; }
78         /// return the offset of the paragraph this cursor is in
79         pit_type pit() const { return pit_; }
80         /// set the offset of the paragraph this cursor is in
81         pit_type & pit() { return pit_; }
82         /// return the last paragraph offset within the ParagraphList
83         pit_type lastpit() const;
84         /// increments the paragraph this cursor is in
85         void incrementPar();
86         /// decrements the paragraph this cursor is in
87         void decrementPar();
88         /// return the position within the paragraph
89         pos_type pos() const { return pos_; }
90         /// return the position within the paragraph
91         pos_type & pos() { return pos_; }
92         /*!
93          * \return the last position within the paragraph.
94          * Note that this is the position behind the last character or inset,
95          * i.e. you cannot dereference it.
96          */
97         pos_type lastpos() const;
98         /// return the number of embedded cells
99         size_t nargs() const { return inset_->nargs(); }
100         /// return the number of columns (1 in non-grid-like insets)
101         size_t ncols() const { return inset_->ncols(); }
102         /// return the number of rows (1 in non-grid-like insets)
103         size_t nrows() const { return inset_->nrows(); }
104         /*!
105          * \return the grid row of the current cell.
106          * This does only make sense in grid like insets.
107          */
108         row_type row() const;
109         /*!
110          * \return the grid column of the current cell.
111          * This does only make sense in grid like insets.
112          */
113         col_type col() const;
114
115         ///
116         /// texted specific stuff
117         ///
118         /// returns text corresponding to this position
119         Text * text() const { return inset_->getText(idx_); }
120         /// paragraph in this cell
121         Paragraph & paragraph() const;
122         ///
123         void setPitPos(pit_type pit, pos_type pos);
124
125         ///
126         /// mathed specific stuff
127         ///
128         /// returns the owning inset if it is a InsetMath, else 0
129         InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
130         /// returns cell corresponding to this position
131         MathData & cell() const;
132
133         /// write some debug information to \p os
134         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
135         /// move to next position
136         void forwardPos();
137         /// move to previous position
138         void backwardPos();
139         /// move to next cell
140         void forwardIdx();
141         /// move to previous cell
142         void backwardIdx();
143         /// are we at the end of the cell
144         bool at_cell_end() const;
145         /// are we at the start of the cell
146         bool at_cell_begin() const;
147         /// are we at the end of this slice
148         bool at_end() const;
149         /// are we at the start of this slice
150         bool at_begin() const;
151
152 private:
153
154         /// pointer to 'owning' inset. This is some kind of cache.
155         Inset * inset_;
156
157         /*!
158          * Cell index of a position in this inset.
159          * This is the primary cell information also for grid like insets,
160          * although we have the convenience functions row() and col() for
161          * those.
162          * This means that the corresponding idx_ of a cell in a given row
163          * and column changes every time the number of columns or number
164          * of rows changes. Normally the cursor should stay in the same cell,
165          * so these changes should typically be performed like the following:
166          * \code
167          * row_type const r = cur.row();
168          * col_type const c = cur.col();
169          * // change nrows() and/or ncols()
170          * cur.idx = index(r, c);
171          * \endcode
172          */
173         idx_type idx_;
174         /// paragraph in this cell (used by texted)
175         pit_type pit_;
176         /// position in this cell
177         pos_type pos_;
178 };
179
180
181 } // namespace lyx
182
183 #endif