]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
* src/inset/InsetNomencl.cpp:
[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         /// open a new group of undo operations. Groups can be nested.
246         void beginUndoGroup() const;
247
248         /// end the current undo group
249         void endUndoGroup() const;
250
251         /// The general case: prepare undo for an arbitrary range.
252         void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
253
254         /// Convenience: prepare undo for the range between 'from' and cursor.
255         void recordUndo(UndoKind kind, pit_type from) const;
256
257         /// Convenience: prepare undo for the single paragraph or cell
258         /// containing the cursor
259         void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
260
261         /// Convenience: prepare undo for the inset containing the cursor
262         void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
263
264         /// Convenience: prepare undo for the whole buffer
265         void recordUndoFullDocument() const;
266
267         /// Convenience: prepare undo for the selected paragraphs or cells
268         void recordUndoSelection() const;
269
270         ///
271         void checkBufferStructure();
272
273 public:
274         ///
275         BufferView * bv_;
276 //private:
277         /// the anchor position
278         DocIterator anchor_;
279         
280         ///
281         DispatchResult disp_;
282         ///
283         DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
284         
285 private:
286         /**
287          * The target x position of the cursor. This is used for when
288          * we have text like :
289          *
290          * blah blah blah blah| blah blah blah
291          * blah blah blah
292          * blah blah blah blah blah blah
293          *
294          * When we move onto row 3, we would like to be vertically aligned
295          * with where we were in row 1, despite the fact that row 2 is
296          * shorter than x()
297          */
298         int x_target_;
299         /// if a x_target cannot be hit exactly in a text, put the difference here
300         int textTargetOffset_;
301         /// do we have a selection?
302         bool selection_;
303         /// are we on the way to get one?
304         bool mark_;
305         /// If true, we are behind the previous char, otherwise we are in front
306         // of the next char. This only make a difference when we are in front
307         // of a big inset spanning a whole row and computing coordinates for
308         // displaying the cursor.
309         bool logicalpos_;
310         /// position before dispatch started
311         DocIterator beforeDispatchCursor_;
312
313 // FIXME: make them private.
314 public:
315         /// the current font settings
316         Font current_font;
317         /// the current font
318         Font real_current_font;
319
320 private:
321
322         //
323         // math specific stuff that could be promoted to "global" later
324         //
325         /// do we allow autocorrection
326         bool autocorrect_;
327         /// are we entering a macro name?
328         bool macromode_;
329
330
331 ///////////////////////////////////////////////////////////////////
332 //
333 // The part below is the non-integrated rest of the original math
334 // cursor. This should be either generalized for texted or moved
335 // back to the math insets.
336 //
337 ///////////////////////////////////////////////////////////////////
338
339 public:
340         ///
341         void insert(MathAtom const &);
342         ///
343         void insert(MathData const &);
344         /// return false for empty math insets
345         bool erase();
346         /// return false for empty math insets
347         bool backspace();
348         /// move the cursor up by sending an internal LFUN_UP
349         /// return true if fullscreen update is needed
350         bool up();
351         /// move the cursor up by sending an internal LFUN_DOWN,
352         /// return true if fullscreen update is needed
353         bool down();
354         /// move up/down in a text inset, called for LFUN_UP/DOWN,
355         /// return true if successful, updateNeeded set to true if fullscreen
356         /// update is needed, otherwise it's not touched
357         bool upDownInText(bool up, bool & updateNeeded);
358         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
359         /// return true if successful
360         bool upDownInMath(bool up);
361         ///
362         void plainErase();
363         ///
364         void plainInsert(MathAtom const & at);
365         ///
366         void niceInsert(MathAtom const & at);
367         ///
368         void niceInsert(docstring const & str);
369
370         /// in pixels from top of screen
371         void setScreenPos(int x, int y);
372         /// current offset in the top cell
373
374         /// interpret name a name of a macro. Returns true if
375         /// something got inserted.
376         bool macroModeClose();
377         /// are we currently typing the name of a macro?
378         bool inMacroMode() const;
379         /// get access to the macro we are currently typing
380         InsetMathUnknown * activeMacro();
381         /// get access to the macro we are currently typing
382         InsetMathUnknown const * activeMacro() const;
383
384         /// replace selected stuff with at, placing the former
385         // selection in given cell of atom
386         void handleNest(MathAtom const & at, int cell = 0);
387         ///
388         bool isInside(Inset const *) const;
389
390         /// make sure cursor position is valid
391         /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
392         void normalize();
393         /// mark current cursor trace for redraw
394         void touch();
395
396         /// hack for reveal codes
397         void markInsert();
398         void markErase();
399         /// injects content of a cell into parent
400         void pullArg();
401         /// split font inset etc
402         void handleFont(std::string const & font);
403
404         /// display a message
405         void message(docstring const & msg) const;
406         /// display an error message
407         void errorMessage(docstring const & msg) const;
408         ///
409         docstring getPossibleLabel() const;
410
411         /// the name of the macro we are currently inputting
412         docstring macroName();
413         /// where in the curent cell does the macro name start?
414         int macroNamePos();
415         /// can we enter the inset?
416         bool openable(MathAtom const &) const;
417         ///
418         Encoding const * getEncoding() const;
419         /// font at cursor position
420         Font getFont() const;
421 };
422
423
424 /**
425  * Notifies all insets which appear in old, but not in cur. Make
426  * Sure that the cursor old is valid, i.e. all inset pointers
427  * point to valid insets! Use Cursor::fixIfBroken if necessary.
428  */
429 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
430
431
432 } // namespace lyx
433
434 #endif // LYXLCURSOR_H