]> git.lyx.org Git - lyx.git/blob - src/cursor.h
d6537ffd5214b09da720ca6a638fb5d01ce10fd2
[lyx.git] / src / cursor.h
1 // -*- C++ -*-
2 /**
3  * \file cursor.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef CURSOR_H
13 #define CURSOR_H
14
15 #include "cursor_slice.h"
16
17 #include <iosfwd>
18 #include <vector>
19
20 class BufferView;
21 class UpdatableInset;
22 class MathAtom;
23 class DispatchResult;
24 class FuncRequest;
25 class InsetTabular;
26 class LyXText;
27 class Paragraph;
28
29
30
31 // this is used for traversing math insets
32 typedef std::vector<CursorSlice> CursorBase;
33 /// move on one step
34 void increment(CursorBase &);
35 ///
36 CursorBase ibegin(InsetBase * p);
37 ///
38 CursorBase iend(InsetBase * p);
39
40
41 /**
42  * The cursor class describes the position of a cursor within a document.
43  */
44
45 class LCursor {
46 public:
47         /// type for cell number in inset
48         typedef CursorSlice::idx_type idx_type;
49         /// type for paragraph numbers positions within a cell
50         typedef CursorSlice::par_type par_type;
51         /// type for cursor positions within a cell
52         typedef CursorSlice::pos_type pos_type;
53         /// type for row indices
54         typedef CursorSlice::row_type row_type;
55         /// type for col indices
56         typedef CursorSlice::col_type col_type;
57
58         /// create the cursor of a BufferView
59         explicit LCursor(BufferView & bv);
60         /// dispatch from innermost inset upwards
61         DispatchResult dispatch(FuncRequest const & cmd);
62         ///
63         void push(InsetBase * inset);
64         /// restrict cursor nesting to given size
65         void pop(int depth);
66         /// pop one level off the cursor
67         void pop();
68         /// access to current cursor slice
69         CursorSlice & current();
70         /// access to current cursor slice
71         CursorSlice const & current() const;
72         /// how many nested insets do we have?
73         size_t depth() const { return cursor_.size(); }
74
75         //
76         // selection
77         //
78         /// selection active?
79         bool selection() const { return selection_; }
80         /// selection active?
81         bool & selection() { return selection_; }
82         /// did we place the anchor?
83         bool mark() const { return mark_; }
84         /// did we place the anchor?
85         bool & mark() { return mark_; }
86         ///
87         void setSelection();
88         ///
89         void clearSelection();
90         ///
91         CursorSlice & selStart();
92         ///
93         CursorSlice const & selStart() const;
94         ///
95         CursorSlice & selEnd();
96         ///
97         CursorSlice const & selEnd() const;
98
99         //
100         // access to the 'current' cursor slice
101         //
102         /// the current inset
103         InsetBase * inset() const { return current().inset(); }
104         /// return the text-ed cell this cursor is in
105         idx_type idx() const { return current().idx(); }
106         /// return the text-ed cell this cursor is in
107         idx_type & idx() { return current().idx(); }
108         /// return the paragraph this cursor is in
109         par_type par() const { return current().par(); }
110         /// return the paragraph this cursor is in
111         par_type & par() { return current().par(); }
112         /// return the position within the paragraph
113         pos_type pos() const { return current().pos(); }
114         /// return the position within the paragraph
115         pos_type & pos() { return current().pos(); }
116         /// return the last position within the paragraph
117         pos_type lastpos() const;
118         /// return the number of embedded cells
119         size_t nargs() const;
120         /// return the number of embedded cells
121         size_t ncols() const;
122         /// return the number of embedded cells
123         size_t nrows() const;
124         /// return the grid row of the current cell
125         row_type row() const;
126         /// return the grid row of the current cell
127         col_type col() const;
128
129         //
130         // math-specific part
131         //
132         /// return the mathed cell this cursor is in
133         MathArray const & cell() const;
134         /// return the mathed cell this cursor is in
135         MathArray & cell();
136         /// the mathatom left of the cursor
137         MathAtom const & prevAtom() const;
138         /// the mathatom left of the cursor
139         MathAtom & prevAtom();
140         /// the mathatom right of the cursor
141         MathAtom const & nextAtom() const;
142         /// the mathatom right of the cursor
143         MathAtom & nextAtom();
144         /// auto-correct mode
145         bool autocorrect() const { return autocorrect_; }
146         /// auto-correct mode
147         bool & autocorrect() { return autocorrect_; }
148         /// are we entering a macro name?
149         bool macromode() const { return macromode_; }
150         /// are we entering a macro name?
151         bool & macromode() { return macromode_; }
152
153         //
154         // text-specific part
155         ///
156         bool boundary() const { return current().boundary(); }
157         ///
158         Paragraph & paragraph();
159         ///
160         Paragraph const & paragraph() const;
161         ///
162         InsetBase * innerInsetOfType(int code) const;
163         ///
164         InsetTabular * innerInsetTabular() const;
165         ///
166         LyXText * innerText() const;
167         /// returns x,y position
168         void getPos(int & x, int & y) const;
169         /// returns cursor dimension
170         void getDim(int & asc, int & desc) const;
171
172         //
173         // common part
174         //
175         /// move one step to the left
176         bool posLeft();
177         /// move one step to the right
178         bool posRight();
179
180         /// set target x position of cursor
181         void x_target(int x);
182         /// return target x position of cursor
183         int x_target() const;
184
185         /// access to selection anchor
186         CursorSlice & anchor();
187         /// access to selection anchor
188         CursorSlice const & anchor() const;
189         /// cache the absolute coordinate from the top inset
190         void updatePos();
191         /// sets anchor to cursor position
192         void resetAnchor(); 
193         /// access to owning BufferView
194         BufferView & bv() const; 
195         /// get some interesting description of current position
196         void info(std::ostream & os);
197
198         /// output
199         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
200 public:
201 //private:
202         /// mainly used as stack, but wee need random access
203         std::vector<CursorSlice> cursor_;
204         /// the anchor position
205         std::vector<CursorSlice> anchor_;
206
207 private:
208         /// don't implement this
209         void operator=(LCursor const &);
210         /// don't implement this
211         LCursor(LCursor const &);
212
213         ///
214         BufferView * const bv_;
215         /// current slice
216         int current_;
217         ///
218         int cached_y_;
219         /**
220          * The target x position of the cursor. This is used for when
221          * we have text like :
222          *
223          * blah blah blah blah| blah blah blah
224          * blah blah blah
225          * blah blah blah blah blah blah
226          *
227          * When we move onto row 3, we would like to be vertically aligned
228          * with where we were in row 1, despite the fact that row 2 is
229          * shorter than x()
230          */
231         int x_target_;
232         // do we have a selection?
233         bool selection_;
234         // are we on the way to get one?
235         bool mark_;
236
237         //
238         // math specific stuff that could be promoted to "global" later
239         //
240         /// do we allow autocorrection
241         bool autocorrect_;
242         /// are we entering a macro name?
243         bool macromode_;
244 };
245
246 #endif // LYXCURSOR_H