]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
refactoring visual cursor movement functions
[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 LCURSOR_H
13 #define LCURSOR_H
14
15 #include "DispatchResult.h"
16 #include "DocIterator.h"
17 #include "Font.h"
18 #include "Undo.h"
19
20 #include <vector>
21
22
23 namespace lyx {
24
25 class Buffer;
26 class BufferView;
27 class FuncStatus;
28 class FuncRequest;
29 class Row;
30
31 // these should go
32 class InsetMathUnknown;
33 class Encoding;
34
35
36 /// The cursor class describes the position of a cursor within a document.
37
38 // The public inheritance should go in favour of a suitable data member
39 // (or maybe private inheritance) at some point of time.
40 class Cursor : public DocIterator {
41 public:
42         /// create the cursor of a BufferView
43         explicit Cursor(BufferView & bv);
44
45         /// dispatch from innermost inset upwards
46         void dispatch(FuncRequest const & cmd);
47         /// get the resut of the last dispatch
48         DispatchResult result() const;
49         /// add a new cursor slice
50         void push(Inset & inset);
51         /// add a new cursor slice, place cursor at front (move backwards)
52         void pushBackward(Inset & inset);
53         /// pop one level off the cursor
54         void pop();
55         /// pop one slice off the cursor stack and go backwards
56         bool popBackward();
57         /// pop one slice off the cursor stack and go forward
58         bool popForward();
59         /// make sure we are outside of given inset
60         void leaveInset(Inset const & inset);
61         /// sets cursor part
62         void setCursor(DocIterator const & it);
63
64         ///
65         void setCurrentFont();
66
67         //
68         // selection
69         //
70         /// selection active?
71         bool selection() const { return selection_; }
72         /// selection active?
73         bool & selection() { return selection_; }
74         /// did we place the anchor?
75         bool mark() const { return mark_; }
76         /// did we place the anchor?
77         bool & mark() { return mark_; }
78         ///
79         void setSelection();
80         /// set selection at given position
81         void setSelection(DocIterator const & where, int n);
82         ///
83         void clearSelection();
84         /// access start of selection
85         CursorSlice selBegin() const;
86         /// access end of selection
87         CursorSlice selEnd() const;
88         /// access start of selection
89         DocIterator selectionBegin() const;
90         /// access start of selection
91         DocIterator selectionEnd() const;
92         /// FIXME: document this
93         bool selHandle(bool selecting);
94         ///
95         docstring selectionAsString(bool label) const;
96         ///
97         docstring currentState();
98
99         /// auto-correct mode
100         bool autocorrect() const { return autocorrect_; }
101         /// auto-correct mode
102         bool & autocorrect() { return autocorrect_; }
103         /// are we entering a macro name?
104         bool macromode() const { return macromode_; }
105         /// are we entering a macro name?
106         bool & macromode() { return macromode_; }
107         /// returns x,y position
108         void getPos(int & x, int & y) const;
109         /// return logical positions between which the cursor is situated
110         /**
111          * If the cursor is at the edge of a row, the position which is "over the 
112          * edge" will be returned as -1.
113          */
114         void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
115         /// the row in the paragraph we're in
116         Row const & textRow() const;
117
118         //
119         // common part
120         //
121         /// move one step backwards
122         bool posBackward();
123         /// move one step forward
124         bool posForward();
125         /// move visually one step to the right
126         /**
127          * @note: This method may move into an inset unless skip_inset == true.
128          * @note: This method may move into a new paragraph.
129          * @note: This method may move out of the current slice.
130          * @return: true if moved, false if not moved
131          */
132         bool posVisRight(bool skip_inset = false);
133         /// move visually one step to the left
134         /**
135          * @note: This method may move into an inset unless skip_inset == true.
136          * @note: This method may move into a new paragraph.
137          * @note: This method may move out of the current slice.
138          * @return: true if moved, false if not moved
139          */
140         bool posVisLeft(bool skip_inset = false);
141         /// move visually to next/previous row
142         /**
143          * Assuming we were to keep moving left (right) from the current cursor
144          * position, place the cursor at the rightmost (leftmost) edge of the 
145          * new row to which we would move according to visual-mode cursor movement.
146          * This could be either the next or the previous row, depending on the
147          * direction in which we're moving, and whether we're in an LTR or RTL 
148          * paragraph. 
149          * @note: The new position may even be in a new paragraph.
150          * @note: This method will not move out of the current slice.
151          * @return: false if not moved (no more rows to move to in given direction)
152          * @return: true if moved
153          */
154         bool posVisToNewRow(bool movingLeft);
155         /// move to right or left extremity of the current row
156         void posVisToRowExtremity(bool left);
157
158         /// insert an inset
159         void insert(Inset *);
160         /// insert a single char
161         void insert(char_type c);
162         /// insert a string
163         void insert(docstring const & str);
164
165         /// FIXME: rename to something sensible showing difference to x_target()
166         /// in pixels from left of screen, set to current position if unset
167         int targetX() const;
168         /// set the targetX to x
169         void setTargetX(int x);
170         /// return targetX or -1 if unset
171         int x_target() const;
172         /// set targetX to current position
173         void setTargetX();
174         /// clear targetX, i.e. set it to -1
175         void clearTargetX();
176         /// set offset to actual position - targetX
177         void updateTextTargetOffset();
178         /// distance between actual and targeted position during last up/down in text
179         int textTargetOffset() const;
180
181         /// access to normalized selection anchor
182         CursorSlice anchor() const;
183         /// sets anchor to cursor position
184         void resetAnchor();
185         /// access to owning BufferView
186         BufferView & bv() const;
187         /// access to owning Buffer
188         Buffer & buffer() const;
189         /// get some interesting description of top position
190         void info(odocstream & os) const;
191         /// are we in math mode (2), text mode (1) or unsure (0)?
192         int currentMode();
193         /// reset cursor bottom to the beginning of the given inset
194         // (sort of 'chroot' environment...)
195         void reset(Inset &);
196         /// for spellchecking
197         void replaceWord(std::string const & replacestring);
198         /**
199          * the event was not (yet) dispatched.
200          *
201          * Should only be called by an inset's doDispatch() method. It means:
202          * I, the doDispatch() method of InsetFoo, hereby declare that I am
203          * not able to handle that request and trust my parent will do the
204          * Right Thing (even if my getStatus partner said that I can do it).
205          * It is sort of a kludge that should be used only rarely...
206          */
207         void undispatched();
208         /// the event was already dispatched
209         void dispatched();
210         /// Set which update should be done
211         void updateFlags(Update::flags f);
212         /**
213          * don't call update() when done
214          *
215          * Should only be called by an inset's doDispatch() method. It means:
216          * I handled that request and I can reassure you that the screen does
217          * not need to be re-drawn and all entries in the coord cache stay
218          * valid (and there are no other things to put in the coord cache).
219          * This is a fairly rare event as well and only some optimization.
220          * Not using noUpdate() should never be wrong.
221          */
222         void noUpdate();
223         /// fix cursor in circumstances that should never happen.
224         /// \retval true if a fix occured.
225         bool fixIfBroken();
226
227         /// output
228         friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
229         friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
230
231         ///
232         bool textUndo();
233         ///
234         bool textRedo();
235
236         /// makes sure the next operation will be stored
237         void finishUndo();
238
239         /// The general case: prepare undo for an arbitrary range.
240         void recordUndo(UndoKind kind, pit_type from, pit_type to);
241
242         /// Convenience: prepare undo for the range between 'from' and cursor.
243         void recordUndo(UndoKind kind, pit_type from);
244
245         /// Convenience: prepare undo for the single paragraph or cell
246         /// containing the cursor
247         void recordUndo(UndoKind kind = ATOMIC_UNDO);
248
249         /// Convenience: prepare undo for the inset containing the cursor
250         void recordUndoInset(UndoKind kind = ATOMIC_UNDO);
251
252         /// Convenience: prepare undo for the whole buffer
253         void recordUndoFullDocument();
254
255         /// Convenience: prepare undo for the selected paragraphs or cells
256         void recordUndoSelection();
257
258         ///
259         void checkBufferStructure();
260
261 public:
262         ///
263         BufferView * bv_;
264 //private:
265         /// the anchor position
266         DocIterator anchor_;
267         
268         ///
269         DispatchResult disp_;
270         ///
271         DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
272         
273 private:
274         /**
275          * The target x position of the cursor. This is used for when
276          * we have text like :
277          *
278          * blah blah blah blah| blah blah blah
279          * blah blah blah
280          * blah blah blah blah blah blah
281          *
282          * When we move onto row 3, we would like to be vertically aligned
283          * with where we were in row 1, despite the fact that row 2 is
284          * shorter than x()
285          */
286         int x_target_;
287         /// if a x_target cannot be hit exactly in a text, put the difference here
288         int textTargetOffset_;
289         /// do we have a selection?
290         bool selection_;
291         /// are we on the way to get one?
292         bool mark_;
293         /// If true, we are behind the previous char, otherwise we are in front
294         // of the next char. This only make a difference when we are in front
295         // of a big inset spanning a whole row and computing coordinates for
296         // displaying the cursor.
297         bool logicalpos_;
298         /// position before dispatch started
299         DocIterator beforeDispatchCursor_;
300
301 // FIXME: make them private.
302 public:
303         /// the current font settings
304         Font current_font;
305         /// the current font
306         Font real_current_font;
307
308 private:
309
310         //
311         // math specific stuff that could be promoted to "global" later
312         //
313         /// do we allow autocorrection
314         bool autocorrect_;
315         /// are we entering a macro name?
316         bool macromode_;
317
318
319 ///////////////////////////////////////////////////////////////////
320 //
321 // The part below is the non-integrated rest of the original math
322 // cursor. This should be either generalized for texted or moved
323 // back to the math insets.
324 //
325 ///////////////////////////////////////////////////////////////////
326
327 public:
328         ///
329         void insert(MathAtom const &);
330         ///
331         void insert(MathData const &);
332         /// return false for empty math insets
333         bool erase();
334         /// return false for empty math insets
335         bool backspace();
336         /// move the cursor up by sending an internal LFUN_UP
337         /// return true if fullscreen update is needed
338         bool up();
339         /// move the cursor up by sending an internal LFUN_DOWN,
340         /// return true if fullscreen update is needed
341         bool down();
342         /// move up/down in a text inset, called for LFUN_UP/DOWN,
343         /// return true if successful, updateNeeded set to true if fullscreen
344         /// update is needed, otherwise it's not touched
345         bool upDownInText(bool up, bool & updateNeeded);
346         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
347         /// return true if successful
348         bool upDownInMath(bool up);
349         ///
350         void plainErase();
351         ///
352         void plainInsert(MathAtom const & at);
353         ///
354         void niceInsert(MathAtom const & at);
355         ///
356         void niceInsert(docstring const & str);
357
358         /// in pixels from top of screen
359         void setScreenPos(int x, int y);
360         /// current offset in the top cell
361
362         /// interpret name a name of a macro. Returns true if
363         /// something got inserted.
364         bool macroModeClose();
365         /// are we currently typing the name of a macro?
366         bool inMacroMode() const;
367         /// get access to the macro we are currently typing
368         InsetMathUnknown * activeMacro();
369         /// get access to the macro we are currently typing
370         InsetMathUnknown const * activeMacro() const;
371
372         /// replace selected stuff with at, placing the former
373         // selection in given cell of atom
374         void handleNest(MathAtom const & at, int cell = 0);
375         ///
376         bool isInside(Inset const *) const;
377
378         /// make sure cursor position is valid
379         /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
380         void normalize();
381         /// mark current cursor trace for redraw
382         void touch();
383
384         /// hack for reveal codes
385         void markInsert();
386         void markErase();
387         /// injects content of a cell into parent
388         void pullArg();
389         /// split font inset etc
390         void handleFont(std::string const & font);
391
392         /// display a message
393         void message(docstring const & msg) const;
394         /// display an error message
395         void errorMessage(docstring const & msg) const;
396         ///
397         docstring getPossibleLabel();
398
399         /// the name of the macro we are currently inputting
400         docstring macroName();
401         /// where in the curent cell does the macro name start?
402         int macroNamePos();
403         /// can we enter the inset?
404         bool openable(MathAtom const &) const;
405         ///
406         Encoding const * getEncoding() const;
407         /// font at cursor position
408         Font getFont() const;
409 };
410
411
412 /**
413  * Notifies all insets which appear in old, but not in cur. Make
414  * Sure that the cursor old is valid, i.e. all inset pointers
415  * point to valid insets! Use Cursor::fixIfBroken if necessary.
416  */
417 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
418
419
420 } // namespace lyx
421
422 #endif // LYXLCURSOR_H