]> git.lyx.org Git - lyx.git/blob - src/cursor.h
35f858f09c5ae67592240b594c87d648cd18bfc3
[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 LyXText;
26 class InsetTabular;
27
28
29 /**
30  * The cursor class describes the position of a cursor within a document.
31  */
32
33 class LCursor {
34 public:
35         /// type for cell number in inset
36         typedef CursorSlice::idx_type idx_type;
37         /// type for paragraph numbers positions within a cell
38         typedef CursorSlice::par_type par_type;
39         /// type for cursor positions within a cell
40         typedef CursorSlice::pos_type pos_type;
41         /// type for row indices
42         typedef CursorSlice::row_type row_type;
43         /// type for col indices
44         typedef CursorSlice::col_type col_type;
45
46         /// create 'empty' cursor. REMOVE ME
47         LCursor();
48         /// create the cursor of a BufferView
49         explicit LCursor(BufferView & bv);
50         /// dispatch from innermost inset upwards
51         DispatchResult dispatch(FuncRequest const & cmd);
52         ///
53         void push(InsetBase * inset);
54         /// restrict cursor nesting to given size
55         void pop(int depth);
56         /// pop one level off the cursor
57         void pop();
58         /// access to cursor 'tip'
59         CursorSlice & top() { return cursor_.back(); }
60         /// access to cursor 'tip'
61         CursorSlice const & top() const { return cursor_.back(); }
62         /// how many nested insets do we have?
63         size_t depth() const { return cursor_.size(); }
64
65         /// access to the topmost slice
66         /// the current inset
67         InsetBase * inset() const { return top().inset(); }
68         /// return the text-ed cell this cursor is in
69         idx_type idx() const { return top().idx(); }
70         /// return the text-ed cell this cursor is in
71         idx_type & idx() { return top().idx(); }
72         /// return the paragraph this cursor is in
73         par_type par() const { return top().par(); }
74         /// return the paragraph this cursor is in
75         par_type & par() { return top().par(); }
76         /// return the position within the paragraph
77         pos_type pos() const { return top().pos(); }
78         /// return the position within the paragraph
79         pos_type & pos() { return top().pos(); }
80         /// return the last position within the paragraph
81         pos_type lastpos() const { return top().lastpos(); }
82         /// return the number of embedded cells
83         size_t nargs() const { return top().nargs(); }
84         /// return the number of embedded cells
85         size_t ncols() const { return top().ncols(); }
86         /// return the number of embedded cells
87         size_t nrows() const { return top().nrows(); }
88         /// return the grid row of the current cell
89         row_type row() const { return top().row(); }
90         /// return the grid row of the current cell
91         col_type col() const { return top().col(); }
92
93         //
94         // math-specific part
95         //
96         /// return the mathed cell this cursor is in
97         MathArray const & cell() const { return top().cell(); }
98         /// return the mathed cell this cursor is in
99         MathArray & cell() { return top().cell(); }
100         /// the mathatom left of the cursor
101         MathAtom const & prevAtom() const;
102         /// the mathatom left of the cursor
103         MathAtom & prevAtom();
104         /// the mathatom right of the cursor
105         MathAtom const & nextAtom() const;
106         /// the mathatom right of the cursor
107         MathAtom & nextAtom();
108
109         //
110         // text-specific part
111         ///
112         UpdatableInset * innerInset() const;
113         ///
114         UpdatableInset * innerInsetOfType(int code) const;
115         ///
116         InsetTabular * innerInsetTabular() const;
117         ///
118         LyXText * innerText() const;
119         /// returns x,y position
120         void getPos(int & x, int & y) const;
121         /// returns cursor dimension
122         void getDim(int & asc, int & desc) const;
123
124         //
125         // common part
126         //
127         /// move one step to the left
128         bool posLeft();
129         /// move one step to the right
130         bool posRight();
131
132         /// cache the absolute coordinate from the top inset
133         void updatePos();
134         /// sets anchor to cursor position
135         void resetAnchor(); 
136         /// access to owning BufferView
137         BufferView & bv() const; 
138         ///
139         friend std::ostream & operator<<(std::ostream &, LCursor const &);
140 public:
141         /// mainly used as stack, but wee need random access
142         std::vector<CursorSlice> cursor_;
143         /// The
144         std::vector<CursorSlice> anchor_;
145         ///
146         BufferView * bv_;
147 private:
148         ///
149         int cached_y_;
150 };
151
152 #endif // LYXCURSOR_H