]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
52220585b1db08c51c429018a713f4bcf0aa57d8
[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 to next/previous row
126         /**
127          * Assuming we were to keep moving left (right) from the current cursor
128          * position, place the cursor at the rightmost (leftmost) edge of the 
129          * new row to which we would move according to visual-mode cursor movement.
130          * This could be either the next or the previous row, depending on the
131          * direction in which we're moving, and whether we're in an LTR or RTL 
132          * paragraph. 
133          * @note: The new position may even be in a new paragraph.
134          * @note: This method will not move out of the current slice.
135          * @return: false if not moved (no more rows to move to in given direction)
136          * @return: true if moved
137          */
138         bool posVisToNewRow(bool movingLeft);
139         /// move to right or left extremity of the current row
140         void posVisToRowExtremity(bool left);
141
142         /// insert an inset
143         void insert(Inset *);
144         /// insert a single char
145         void insert(char_type c);
146         /// insert a string
147         void insert(docstring const & str);
148
149         /// FIXME: rename to something sensible showing difference to x_target()
150         /// in pixels from left of screen, set to current position if unset
151         int targetX() const;
152         /// set the targetX to x
153         void setTargetX(int x);
154         /// return targetX or -1 if unset
155         int x_target() const;
156         /// set targetX to current position
157         void setTargetX();
158         /// clear targetX, i.e. set it to -1
159         void clearTargetX();
160         /// set offset to actual position - targetX
161         void updateTextTargetOffset();
162         /// distance between actual and targeted position during last up/down in text
163         int textTargetOffset() const;
164
165         /// access to normalized selection anchor
166         CursorSlice anchor() const;
167         /// sets anchor to cursor position
168         void resetAnchor();
169         /// access to owning BufferView
170         BufferView & bv() const;
171         /// access to owning Buffer
172         Buffer & buffer() const;
173         /// get some interesting description of top position
174         void info(odocstream & os) const;
175         /// are we in math mode (2), text mode (1) or unsure (0)?
176         int currentMode();
177         /// reset cursor bottom to the beginning of the given inset
178         // (sort of 'chroot' environment...)
179         void reset(Inset &);
180         /// for spellchecking
181         void replaceWord(std::string const & replacestring);
182         /**
183          * the event was not (yet) dispatched.
184          *
185          * Should only be called by an inset's doDispatch() method. It means:
186          * I, the doDispatch() method of InsetFoo, hereby declare that I am
187          * not able to handle that request and trust my parent will do the
188          * Right Thing (even if my getStatus partner said that I can do it).
189          * It is sort of a kludge that should be used only rarely...
190          */
191         void undispatched();
192         /// the event was already dispatched
193         void dispatched();
194         /// Set which update should be done
195         void updateFlags(Update::flags f);
196         /**
197          * don't call update() when done
198          *
199          * Should only be called by an inset's doDispatch() method. It means:
200          * I handled that request and I can reassure you that the screen does
201          * not need to be re-drawn and all entries in the coord cache stay
202          * valid (and there are no other things to put in the coord cache).
203          * This is a fairly rare event as well and only some optimization.
204          * Not using noUpdate() should never be wrong.
205          */
206         void noUpdate();
207         /// fix cursor in circumstances that should never happen.
208         /// \retval true if a fix occured.
209         bool fixIfBroken();
210
211         /// output
212         friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
213         friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
214
215         ///
216         bool textUndo();
217         ///
218         bool textRedo();
219
220         /// makes sure the next operation will be stored
221         void finishUndo();
222
223         /// The general case: prepare undo for an arbitrary range.
224         void recordUndo(UndoKind kind, pit_type from, pit_type to);
225
226         /// Convenience: prepare undo for the range between 'from' and cursor.
227         void recordUndo(UndoKind kind, pit_type from);
228
229         /// Convenience: prepare undo for the single paragraph or cell
230         /// containing the cursor
231         void recordUndo(UndoKind kind = ATOMIC_UNDO);
232
233         /// Convenience: prepare undo for the inset containing the cursor
234         void recordUndoInset(UndoKind kind = ATOMIC_UNDO);
235
236         /// Convenience: prepare undo for the whole buffer
237         void recordUndoFullDocument();
238
239         /// Convenience: prepare undo for the selected paragraphs
240         void recordUndoSelection();
241
242         ///
243         void checkBufferStructure();
244
245 public:
246         ///
247         BufferView * bv_;
248 //private:
249         /// the anchor position
250         DocIterator anchor_;
251         
252         ///
253         DispatchResult disp_;
254         ///
255         DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
256         
257 private:
258         /**
259          * The target x position of the cursor. This is used for when
260          * we have text like :
261          *
262          * blah blah blah blah| blah blah blah
263          * blah blah blah
264          * blah blah blah blah blah blah
265          *
266          * When we move onto row 3, we would like to be vertically aligned
267          * with where we were in row 1, despite the fact that row 2 is
268          * shorter than x()
269          */
270         int x_target_;
271         /// if a x_target cannot be hit exactly in a text, put the difference here
272         int textTargetOffset_;
273         /// do we have a selection?
274         bool selection_;
275         /// are we on the way to get one?
276         bool mark_;
277         /// If true, we are behind the previous char, otherwise we are in front
278         // of the next char. This only make a difference when we are in front
279         // of a big inset spanning a whole row and computing coordinates for
280         // displaying the cursor.
281         bool logicalpos_;
282         /// position before dispatch started
283         DocIterator beforeDispatchCursor_;
284
285 // FIXME: make them private.
286 public:
287         /// the current font settings
288         Font current_font;
289         /// the current font
290         Font real_current_font;
291
292 private:
293
294         //
295         // math specific stuff that could be promoted to "global" later
296         //
297         /// do we allow autocorrection
298         bool autocorrect_;
299         /// are we entering a macro name?
300         bool macromode_;
301
302
303 ///////////////////////////////////////////////////////////////////
304 //
305 // The part below is the non-integrated rest of the original math
306 // cursor. This should be either generalized for texted or moved
307 // back to the math insets.
308 //
309 ///////////////////////////////////////////////////////////////////
310
311 public:
312         ///
313         void insert(MathAtom const &);
314         ///
315         void insert(MathData const &);
316         /// return false for empty math insets
317         bool erase();
318         /// return false for empty math insets
319         bool backspace();
320         /// move the cursor up by sending an internal LFUN_UP
321         /// return true if fullscreen update is needed
322         bool up();
323         /// move the cursor up by sending an internal LFUN_DOWN,
324         /// return true if fullscreen update is needed
325         bool down();
326         /// move up/down in a text inset, called for LFUN_UP/DOWN,
327         /// return true if successful, updateNeeded set to true if fullscreen
328         /// update is needed, otherwise it's not touched
329         bool upDownInText(bool up, bool & updateNeeded);
330         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
331         /// return true if successful
332         bool upDownInMath(bool up);
333         ///
334         void plainErase();
335         ///
336         void plainInsert(MathAtom const & at);
337         ///
338         void niceInsert(MathAtom const & at);
339         ///
340         void niceInsert(docstring const & str);
341
342         /// in pixels from top of screen
343         void setScreenPos(int x, int y);
344         /// current offset in the top cell
345
346         /// interpret name a name of a macro. Returns true if
347         /// something got inserted.
348         bool macroModeClose();
349         /// are we currently typing the name of a macro?
350         bool inMacroMode() const;
351         /// get access to the macro we are currently typing
352         InsetMathUnknown * activeMacro();
353         /// get access to the macro we are currently typing
354         InsetMathUnknown const * activeMacro() const;
355
356         /// replace selected stuff with at, placing the former
357         // selection in given cell of atom
358         void handleNest(MathAtom const & at, int cell = 0);
359         ///
360         bool isInside(Inset const *) const;
361
362         /// make sure cursor position is valid
363         /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
364         void normalize();
365         /// mark current cursor trace for redraw
366         void touch();
367
368         /// hack for reveal codes
369         void markInsert();
370         void markErase();
371         /// injects content of a cell into parent
372         void pullArg();
373         /// split font inset etc
374         void handleFont(std::string const & font);
375
376         /// display a message
377         void message(docstring const & msg) const;
378         /// display an error message
379         void errorMessage(docstring const & msg) const;
380         ///
381         docstring getPossibleLabel();
382
383         /// the name of the macro we are currently inputting
384         docstring macroName();
385         /// where in the curent cell does the macro name start?
386         int macroNamePos();
387         /// can we enter the inset?
388         bool openable(MathAtom const &) const;
389         ///
390         Encoding const * getEncoding() const;
391         /// font at cursor position
392         Font getFont() const;
393 };
394
395
396 /**
397  * Notifies all insets which appear in old, but not in cur. Make
398  * Sure that the cursor old is valid, i.e. all inset pointers
399  * point to valid insets! Use Cursor::fixIfBroken if necessary.
400  */
401 bool notifyCursorLeaves(DocIterator const & old, Cursor & cur);
402
403
404 } // namespace lyx
405
406 #endif // LYXLCURSOR_H