]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.h
298e0d836c3c779088b08b5e701cae13d7e1f717
[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 "ParagraphList_fwd.h"
21
22 #include "support/types.h"
23
24 #include <cstddef>
25 #include <iosfwd>
26
27 class BufferView;
28 class InsetBase;
29 class MathInset;
30 class MathArray;
31 class LyXText;
32 class Paragraph;
33 class UpdatableInset;
34
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 UpdatableInset 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 paragraph numbers positions within a cell
48         typedef lyx::paroffset_type par_type;
49         /// type for cursor positions within a cell
50         typedef lyx::pos_type pos_type;
51         /// type for row indices
52         typedef size_t row_type;
53         /// type for col indices
54         typedef size_t col_type;
55
56         ///
57         CursorSlice();
58         ///
59         explicit CursorSlice(InsetBase &);
60
61         /// the current inset
62         InsetBase & inset() const { return *inset_; }
63         /// return the cell this cursor is in
64         idx_type idx() const;
65         /// return the cell this cursor is in
66         idx_type & idx();
67         /// return the last cell in this inset
68         idx_type lastidx() const { return nargs() - 1; }
69         /// return the paragraph this cursor is in
70         par_type par() const;
71         /// set the paragraph this cursor is in
72         par_type & par();
73         /// increments the paragraph this cursor is in
74         void incrementPar();
75         /// increments the paragraph this cursor is in
76         void decrementPar();
77         /// return the position within the paragraph
78         pos_type pos() const;
79         /// return the position within the paragraph
80         pos_type & pos();
81         /// return the last position within the paragraph
82         pos_type lastpos() const;
83         /// return the number of embedded cells
84         size_t nargs() const;
85         /// return the number of embedded cells
86         size_t ncols() const;
87         /// return the number of embedded cells
88         size_t nrows() const;
89         /// return the grid row of the current cell
90         row_type row() const;
91         /// return the grid row of the current cell
92         col_type col() const;
93
94         ///
95         /// texted specific stuff
96         ///
97         /// see comment for the member
98         bool boundary() const;
99         /// see comment for the member
100         bool & boundary();
101         ///
102         LyXText * text() const;
103         ///
104         UpdatableInset * asUpdatableInset() const;
105         ///
106         Paragraph & paragraph();
107         ///
108         Paragraph const & paragraph() const;
109
110         ///
111         /// mathed specific stuff
112         ///
113         /// returns cell corresponding to this position
114         MathArray & cell() const;
115         ///
116         MathInset * asMathInset() const;
117
118         ///
119         friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
120 public:
121         /// pointer to 'owning' inset. This is some kind of cache.
122         InsetBase * inset_;
123 private:
124         /// cell index of a position in this inset
125         idx_type idx_;
126         /// paragraph in this cell (used by texted)
127         par_type par_;
128         /// true of 'pit' was properly initialized
129         bool pit_valid_;
130         /// position in this cell
131         pos_type pos_;
132         /**
133          * When the cursor position is i, is the cursor is after the i-th char
134          * or before the i+1-th char ? Normally, these two interpretations are
135          * equivalent, except when the fonts of the i-th and i+1-th char
136          * differ.
137          * We use boundary_ to distinguish between the two options:
138          * If boundary_=true, then the cursor is after the i-th char
139          * and if boundary_=false, then the cursor is before the i+1-th char.
140          *
141          * We currently use the boundary only when the language direction of
142          * the i-th char is different than the one of the i+1-th char.
143          * In this case it is important to distinguish between the two
144          * cursor interpretations, in order to give a reasonable behavior to
145          * the user.
146          */
147         bool boundary_;
148 };
149
150 /// test for equality
151 bool operator==(CursorSlice const &, CursorSlice const &);
152 /// test for inequality
153 bool operator!=(CursorSlice const &, CursorSlice const &);
154 /// test for order
155 bool operator<(CursorSlice const &, CursorSlice const &);
156 /// test for order
157 bool operator>(CursorSlice const &, CursorSlice const &);
158
159 #endif