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