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