]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.h
Avoid full metrics computation with Update:FitCursor
[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 InsetMath;
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         ///
48         CursorSlice();
49         ///
50         explicit CursorSlice(Inset &);
51
52         /// \name Comparison operators.
53         //@{
54         friend bool operator==(CursorSlice const &, CursorSlice const &);
55         friend bool operator!=(CursorSlice const &, CursorSlice const &);
56         friend bool operator<(CursorSlice const &, CursorSlice const &);
57         friend bool operator>(CursorSlice const &, CursorSlice const &);
58         friend bool operator<=(CursorSlice const &, CursorSlice const &);
59         //@}
60
61         /// return true if the slice has not been initialized
62         bool empty() const { return !inset_; }
63         /// the current inset
64         Inset & inset() const { return *inset_; }
65         /// return the cell this cursor is in
66         idx_type idx() const { return idx_; }
67         /// return the cell this cursor is in
68         idx_type & idx() { return idx_; }
69         /// return the last cell in this inset
70         idx_type lastidx() const { return nargs() - 1; }
71         /// return the offset of the paragraph this cursor is in
72         pit_type pit() const { return pit_; }
73         /// set the offset of the paragraph this cursor is in
74         pit_type & pit() { return pit_; }
75         /// return the last paragraph offset within the ParagraphList
76         pit_type lastpit() const;
77         /// increments the paragraph this cursor is in
78         void incrementPar();
79         /// decrements the paragraph this cursor is in
80         void decrementPar();
81         /// return the position within the paragraph
82         pos_type pos() const { return pos_; }
83         /// return the position within the paragraph
84         pos_type & pos() { return pos_; }
85         /*!
86          * \return the last position within the paragraph.
87          * Note that this is the position behind the last character or inset,
88          * i.e. you cannot dereference it.
89          */
90         pos_type lastpos() const;
91         /// return the number of embedded cells
92         size_t nargs() const { return inset_->nargs(); }
93         /// return the number of columns (1 in non-grid-like insets)
94         size_t ncols() const { return inset_->ncols(); }
95         /// return the number of rows (1 in non-grid-like insets)
96         size_t nrows() const { return inset_->nrows(); }
97         /*!
98          * \return the grid row of the current cell.
99          * This does only make sense in grid like insets.
100          */
101         row_type row() const;
102         /*!
103          * \return the grid column of the current cell.
104          * This does only make sense in grid like insets.
105          */
106         col_type col() const;
107
108         ///
109         /// texted specific stuff
110         ///
111         /// returns text corresponding to this position
112         Text * text() const { return inset_->getText(idx_); }
113         /// paragraph in this cell
114         Paragraph & paragraph() const;
115         ///
116         void setPitPos(pit_type pit, pos_type pos);
117
118         ///
119         /// mathed specific stuff
120         ///
121         /// returns the owning inset if it is a InsetMath, else 0
122         InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
123         /// returns cell corresponding to this position
124         MathData & cell() const;
125
126         /// write some debug information to \p os
127         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
128         /// move to next position
129         void forwardPos();
130         /// move to previous position
131         void backwardPos();
132         /// move to next cell
133         void forwardIdx();
134         /// move to previous cell
135         void backwardIdx();
136         /// are we at the end of the cell
137         bool at_cell_end() const;
138         /// are we at the start of the cell
139         bool at_cell_begin() const;
140         /// are we at the end of this slice
141         bool at_end() const;
142         /// are we at the start of this slice
143         bool at_begin() const;
144
145 private:
146
147         /// pointer to 'owning' inset. This is some kind of cache.
148         Inset * inset_;
149
150         /*!
151          * Cell index of a position in this inset.
152          * This is the primary cell information also for grid like insets,
153          * although we have the convenience functions row() and col() for
154          * those.
155          * This means that the corresponding idx_ of a cell in a given row
156          * and column changes every time the number of columns or number
157          * of rows changes. Normally the cursor should stay in the same cell,
158          * so these changes should typically be performed like the following:
159          * \code
160          * row_type const r = cur.row();
161          * col_type const c = cur.col();
162          * // change nrows() and/or ncols()
163          * cur.idx = index(r, c);
164          * \endcode
165          */
166         idx_type idx_;
167         /// paragraph in this cell (used by texted)
168         pit_type pit_;
169         /// position in this cell
170         pos_type pos_;
171 };
172
173
174 } // namespace lyx
175
176 #endif