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