]> git.lyx.org Git - lyx.git/blob - src/cursor.h
01c4d82432cbe40861701e719e1d4ad3d3ce9c88
[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 "dispatchresult.h"
16 #include "dociterator.h"
17
18 #include <iosfwd>
19 #include <vector>
20
21 class Buffer;
22 class BufferView;
23 class FuncStatus;
24 class FuncRequest;
25 class Point;
26
27 // these should go
28 class MathUnknownInset;
29 class Encoding;
30
31
32 /// The cursor class describes the position of a cursor within a document.
33
34 // The public inheritance should go in favour of a suitable data member
35 // (or maybe private inheritance) at some point of time.
36 class LCursor : public DocIterator {
37 public:
38         /// create the cursor of a BufferView
39         explicit LCursor(BufferView & bv);
40
41         /// dispatch from innermost inset upwards
42         void dispatch(FuncRequest const & cmd);
43         /// get the resut of the last dispatch
44         DispatchResult result() const;
45         /// are we willing to handle this event?
46         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
47
48         /// add a new cursor slice
49         void push(InsetBase & inset);
50         /// add a new cursor slice, place cursor on left end
51         void pushLeft(InsetBase & inset);
52         /// pop one level off the cursor
53         void pop();
54         /// pop one slice off the cursor stack and go left
55         bool popLeft();
56         /// pop one slice off the cursor stack and go right
57         bool popRight();
58         /// sets cursor part
59         void setCursor(DocIterator const & it);
60
61         //
62         // selection
63         //
64         /// selection active?
65         bool selection() const { return selection_; }
66         /// selection active?
67         bool & selection() { return selection_; }
68         /// did we place the anchor?
69         bool mark() const { return mark_; }
70         /// did we place the anchor?
71         bool & mark() { return mark_; }
72         ///
73         void setSelection();
74         /// set selection at given position
75         void setSelection(DocIterator const & where, size_t n);
76         ///
77         void clearSelection();
78         /// access start of selection
79         CursorSlice selBegin() const;
80         /// access end of selection
81         CursorSlice selEnd() const;
82         /// access start of selection
83         DocIterator selectionBegin() const;
84         /// access start of selection
85         DocIterator selectionEnd() const;
86         ///
87         void selHandle(bool selecting);
88         //
89         std::string selectionAsString(bool label) const;
90         ///
91         void paste(std::string const & data);
92         ///
93         std::string currentState();
94
95         /// auto-correct mode
96         bool autocorrect() const { return autocorrect_; }
97         /// auto-correct mode
98         bool & autocorrect() { return autocorrect_; }
99         /// are we entering a macro name?
100         bool macromode() const { return macromode_; }
101         /// are we entering a macro name?
102         bool & macromode() { return macromode_; }
103         /// returns x,y position
104         void getPos(int & x, int & y) const;
105         /// returns cursor dimension
106         void getDim(int & asc, int & desc) const;
107
108         //
109         // common part
110         //
111         /// move one step to the left
112         bool posLeft();
113         /// move one step to the right
114         bool posRight();
115
116         /// insert an inset
117         void insert(InsetBase *);
118         /// insert a single char
119         void insert(char c);
120         /// insert a string
121         void insert(std::string const & str);
122
123         /// in pixels from left of screen
124         int targetX() const;
125         /// write acess to target x position of cursor
126         int & x_target();
127         /// return target x position of cursor
128         int x_target() const;
129         /// set targetX in current position
130         void setTargetX();
131         /// clear target x position of cursor
132         void clearTargetX();
133
134         /// access to normalized selection anchor
135         CursorSlice anchor() const;
136         /// sets anchor to cursor position
137         void resetAnchor();
138         /// access to owning BufferView
139         BufferView & bv() const;
140         /// access to owning Buffer
141         Buffer & buffer() const;
142         /// get some interesting description of top position
143         void info(std::ostream & os) const;
144         /// are we in math mode (2), text mode (1) or unsure (0)?
145         int currentMode();
146         /// reset cursor bottom to the beginning of the given inset
147         // (sort of 'chroot' environment...)
148         void reset(InsetBase &);
149         /// for spellchecking
150         void replaceWord(std::string const & replacestring);
151         /// the event was not (yet) dispatched
152         void undispatched();
153         /// the event was already dispatched
154         void dispatched();
155         /// call update() when done
156         void needsUpdate();
157         /// don't call update() when done
158         void noUpdate();
159
160         /// output
161         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
162
163 public:
164         ///
165         BufferView * bv_;
166 //private:
167         /// the anchor position
168         DocIterator anchor_;
169
170         ///
171         DispatchResult disp_;
172
173
174 private:
175         /**
176          * The target x position of the cursor. This is used for when
177          * we have text like :
178          *
179          * blah blah blah blah| blah blah blah
180          * blah blah blah
181          * blah blah blah blah blah blah
182          *
183          * When we move onto row 3, we would like to be vertically aligned
184          * with where we were in row 1, despite the fact that row 2 is
185          * shorter than x()
186          */
187         int x_target_;
188         /// do we have a selection?
189         bool selection_;
190         /// are we on the way to get one?
191         bool mark_;
192
193 private:
194
195         //
196         // math specific stuff that could be promoted to "global" later
197         //
198         /// do we allow autocorrection
199         bool autocorrect_;
200         /// are we entering a macro name?
201         bool macromode_;
202
203
204 ///////////////////////////////////////////////////////////////////
205 //
206 // The part below is the non-integrated rest of the original math
207 // cursor. This should be either generalized for texted or moved
208 // back to the math insets.
209 //
210 ///////////////////////////////////////////////////////////////////
211
212 public:
213         ///
214         void insert(MathAtom const &);
215         ///
216         void insert(MathArray const &);
217         /// return false for empty math insets
218         bool erase();
219         /// return false for empty math insets
220         bool backspace();
221         /// called for LFUN_UP etc
222         bool up();
223         /// called for LFUN_DOWN etc
224         bool down();
225         ///
226         void plainErase();
227         ///
228         void plainInsert(MathAtom const & at);
229         ///
230         void niceInsert(MathAtom const & at);
231         ///
232         void niceInsert(std::string const & str);
233
234         /// in pixels from top of screen
235         void setScreenPos(int x, int y);
236         /// current offset in the top cell
237         /// interpret name a name of a macro
238         void macroModeClose();
239         /// are we currently typing the name of a macro?
240         bool inMacroMode() const;
241         /// get access to the macro we are currently typing
242         MathUnknownInset * activeMacro();
243
244         /// replace selected stuff with at, placing the former
245         // selection in given cell of atom
246         void handleNest(MathAtom const & at, int cell = 0);
247         ///
248         bool isInside(InsetBase const *);
249
250         /// make sure cursor position is valid
251         void normalize();
252         /// mark current cursor trace for redraw
253         void touch();
254
255         /// hack for reveal codes
256         void markInsert();
257         void markErase();
258         /// injects content of a cell into parent
259         void pullArg();
260         /// split font inset etc
261         void handleFont(std::string const & font);
262
263         /// display a message
264         void message(std::string const & msg) const;
265         /// display an error message
266         void errorMessage(std::string const & msg) const;
267         ///
268         std::string getPossibleLabel();
269
270         /// moves position somehow up or down
271         bool goUpDown(bool up);
272
273         /// the name of the macro we are currently inputting
274         std::string macroName();
275         /// where in the curent cell does the macro name start?
276         int macroNamePos();
277         /// can we enter the inset?
278         bool openable(MathAtom const &) const;
279         ///
280         Encoding const * getEncoding() const;
281 };
282
283
284
285 #endif // LYXCURSOR_H