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