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