]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[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         /// the row in the paragraph we're in
110         Row const & textRow() const;
111
112         //
113         // common part
114         //
115         /// move one step backwards
116         bool posBackward();
117         /// move one step forward
118         bool posForward();
119
120         /// insert an inset
121         void insert(Inset *);
122         /// insert a single char
123         void insert(char_type c);
124         /// insert a string
125         void insert(docstring const & str);
126
127         /// FIXME: rename to something sensible showing difference to x_target()
128         /// in pixels from left of screen, set to current position if unset
129         int targetX() const;
130         /// set the targetX to x
131         void setTargetX(int x);
132         /// return targetX or -1 if unset
133         int x_target() const;
134         /// set targetX to current position
135         void setTargetX();
136         /// clear targetX, i.e. set it to -1
137         void clearTargetX();
138         /// set offset to actual position - targetX
139         void updateTextTargetOffset();
140         /// distance between actual and targeted position during last up/down in text
141         int textTargetOffset() const;
142
143         /// access to normalized selection anchor
144         CursorSlice anchor() const;
145         /// sets anchor to cursor position
146         void resetAnchor();
147         /// access to owning BufferView
148         BufferView & bv() const;
149         /// access to owning Buffer
150         Buffer & buffer() const;
151         /// get some interesting description of top position
152         void info(odocstream & os) const;
153         /// are we in math mode (2), text mode (1) or unsure (0)?
154         int currentMode();
155         /// reset cursor bottom to the beginning of the given inset
156         // (sort of 'chroot' environment...)
157         void reset(Inset &);
158         /// for spellchecking
159         void replaceWord(std::string const & replacestring);
160         /**
161          * the event was not (yet) dispatched.
162          *
163          * Should only be called by an inset's doDispatch() method. It means:
164          * I, the doDispatch() method of InsetFoo, hereby declare that I am
165          * not able to handle that request and trust my parent will do the
166          * Right Thing (even if my getStatus partner said that I can do it).
167          * It is sort of a kludge that should be used only rarely...
168          */
169         void undispatched();
170         /// the event was already dispatched
171         void dispatched();
172         /// Set which update should be done
173         void updateFlags(Update::flags f);
174         /**
175          * don't call update() when done
176          *
177          * Should only be called by an inset's doDispatch() method. It means:
178          * I handled that request and I can reassure you that the screen does
179          * not need to be re-drawn and all entries in the coord cache stay
180          * valid (and there are no other things to put in the coord cache).
181          * This is a fairly rare event as well and only some optimization.
182          * Not using noUpdate() should never be wrong.
183          */
184         void noUpdate();
185         /// fix cursor in circumstances that should never happen.
186         /// \retval true if a fix occured.
187         bool fixIfBroken();
188
189         /// output
190         friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
191         friend LyXErr & operator<<(LyXErr & 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         /// position before dispatch started
258         DocIterator beforeDispatchCursor_;
259
260 // FIXME: make them private.
261 public:
262         /// the current font settings
263         Font current_font;
264         /// the current font
265         Font real_current_font;
266
267 private:
268
269         //
270         // math specific stuff that could be promoted to "global" later
271         //
272         /// do we allow autocorrection
273         bool autocorrect_;
274         /// are we entering a macro name?
275         bool macromode_;
276
277
278 ///////////////////////////////////////////////////////////////////
279 //
280 // The part below is the non-integrated rest of the original math
281 // cursor. This should be either generalized for texted or moved
282 // back to the math insets.
283 //
284 ///////////////////////////////////////////////////////////////////
285
286 public:
287         ///
288         void insert(MathAtom const &);
289         ///
290         void insert(MathData const &);
291         /// return false for empty math insets
292         bool erase();
293         /// return false for empty math insets
294         bool backspace();
295         /// move the cursor up by sending an internal LFUN_UP
296         /// return true if fullscreen update is needed
297         bool up();
298         /// move the cursor up by sending an internal LFUN_DOWN,
299         /// return true if fullscreen update is needed
300         bool down();
301         /// move up/down in a text inset, called for LFUN_UP/DOWN,
302         /// return true if successful, updateNeeded set to true if fullscreen
303         /// update is needed, otherwise it's not touched
304         bool upDownInText(bool up, bool & updateNeeded);
305         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
306         /// return true if successful
307         bool upDownInMath(bool up);
308         ///
309         void plainErase();
310         ///
311         void plainInsert(MathAtom const & at);
312         ///
313         void niceInsert(MathAtom const & at);
314         ///
315         void niceInsert(docstring const & str);
316
317         /// in pixels from top of screen
318         void setScreenPos(int x, int y);
319         /// current offset in the top cell
320
321         /// interpret name a name of a macro. Returns true if
322         /// something got inserted.
323         bool macroModeClose();
324         /// are we currently typing the name of a macro?
325         bool inMacroMode() const;
326         /// get access to the macro we are currently typing
327         InsetMathUnknown * activeMacro();
328
329         /// replace selected stuff with at, placing the former
330         // selection in given cell of atom
331         void handleNest(MathAtom const & at, int cell = 0);
332         ///
333         bool isInside(Inset const *) const;
334
335         /// make sure cursor position is valid
336         /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
337         void normalize();
338         /// mark current cursor trace for redraw
339         void touch();
340
341         /// hack for reveal codes
342         void markInsert();
343         void markErase();
344         /// injects content of a cell into parent
345         void pullArg();
346         /// split font inset etc
347         void handleFont(std::string const & font);
348
349         /// display a message
350         void message(docstring const & msg) const;
351         /// display an error message
352         void errorMessage(docstring const & msg) const;
353         ///
354         docstring getPossibleLabel();
355
356         /// the name of the macro we are currently inputting
357         docstring macroName();
358         /// where in the curent cell does the macro name start?
359         int macroNamePos();
360         /// can we enter the inset?
361         bool openable(MathAtom const &) const;
362         ///
363         Encoding const * getEncoding() const;
364         /// font at cursor position
365         Font getFont() const;
366 };
367
368
369 /**
370  * Notifies all insets which appear in old, but not in cur. Make
371  * Sure that the cursor old is valid, i.e. als inset pointer
372  * point to valid insets! Use Cursor::fixIfBroken if necessary.
373  */
374 bool notifyCursorLeaves(DocIterator const & old, Cursor & cur);
375
376
377 } // namespace lyx
378
379 #endif // LYXLCURSOR_H