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