4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "DispatchResult.h"
16 #include "DocIterator.h"
32 class InsetMathUnknown;
36 /// The cursor class describes the position of a cursor within a document.
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 {
42 /// create the cursor of a BufferView
43 explicit Cursor(BufferView & bv);
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
55 /// pop one slice off the cursor stack and go backwards
57 /// pop one slice off the cursor stack and go forward
59 /// make sure we are outside of given inset
60 void leaveInset(Inset const & inset);
62 void setCursor(DocIterator const & it);
65 void setCurrentFont();
71 bool selection() const { return selection_; }
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_; }
86 /// set selection at given position
87 void setSelection(DocIterator const & where, int n);
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);
101 docstring selectionAsString(bool label) const;
103 docstring currentState();
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
117 * If the cursor is at the edge of a row, the position which is "over the
118 * edge" will be returned as -1.
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;
127 /// move one step backwards
129 /// move one step forward
131 /// move visually one step to the right
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
138 bool posVisRight(bool skip_inset = false);
139 /// move visually one step to the left
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
146 bool posVisLeft(bool skip_inset = false);
147 /// move visually to next/previous row
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
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
160 bool posVisToNewRow(bool movingLeft);
161 /// move to right or left extremity of the current row
162 void posVisToRowExtremity(bool left);
165 void insert(Inset *);
166 /// insert a single char
167 void insert(char_type c);
169 void insert(docstring const & str);
171 /// FIXME: rename to something sensible showing difference to x_target()
172 /// in pixels from left of screen, set to current position if unset
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
180 /// clear targetX, i.e. set it to -1
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;
187 /// access to normalized selection anchor
188 CursorSlice anchor() const;
189 /// sets anchor to cursor position
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)?
199 /// reset cursor bottom to the beginning of the given inset
200 // (sort of 'chroot' environment...)
202 /// for spellchecking
203 void replaceWord(std::string const & replacestring);
205 * the event was not (yet) dispatched.
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...
214 /// the event was already dispatched
216 /// Set which update should be done
217 void updateFlags(Update::flags f);
219 * don't call update() when done
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.
229 /// fix cursor in circumstances that should never happen.
230 /// \retval true if a fix occured.
234 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
235 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
242 /// makes sure the next operation will be stored
245 /// The general case: prepare undo for an arbitrary range.
246 void recordUndo(UndoKind kind, pit_type from, pit_type to);
248 /// Convenience: prepare undo for the range between 'from' and cursor.
249 void recordUndo(UndoKind kind, pit_type from);
251 /// Convenience: prepare undo for the single paragraph or cell
252 /// containing the cursor
253 void recordUndo(UndoKind kind = ATOMIC_UNDO);
255 /// Convenience: prepare undo for the inset containing the cursor
256 void recordUndoInset(UndoKind kind = ATOMIC_UNDO);
258 /// Convenience: prepare undo for the whole buffer
259 void recordUndoFullDocument();
261 /// Convenience: prepare undo for the selected paragraphs or cells
262 void recordUndoSelection();
265 void checkBufferStructure();
271 /// the anchor position
275 DispatchResult disp_;
277 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
281 * The target x position of the cursor. This is used for when
282 * we have text like :
284 * blah blah blah blah| blah blah blah
286 * blah blah blah blah blah blah
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
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?
297 /// are we on the way to get one?
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.
304 /// position before dispatch started
305 DocIterator beforeDispatchCursor_;
307 // FIXME: make them private.
309 /// the current font settings
312 Font real_current_font;
317 // math specific stuff that could be promoted to "global" later
319 /// do we allow autocorrection
321 /// are we entering a macro name?
325 ///////////////////////////////////////////////////////////////////
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.
331 ///////////////////////////////////////////////////////////////////
335 void insert(MathAtom const &);
337 void insert(MathData const &);
338 /// return false for empty math insets
340 /// return false for empty math insets
342 /// move the cursor up by sending an internal LFUN_UP
343 /// return true if fullscreen update is needed
345 /// move the cursor up by sending an internal LFUN_DOWN,
346 /// return true if fullscreen update is needed
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);
358 void plainInsert(MathAtom const & at);
360 void niceInsert(MathAtom const & at);
362 void niceInsert(docstring const & str);
364 /// in pixels from top of screen
365 void setScreenPos(int x, int y);
366 /// current offset in the top cell
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;
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);
382 bool isInside(Inset const *) const;
384 /// make sure cursor position is valid
385 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
387 /// mark current cursor trace for redraw
390 /// hack for reveal codes
393 /// injects content of a cell into parent
395 /// split font inset etc
396 void handleFont(std::string const & font);
398 /// display a message
399 void message(docstring const & msg) const;
400 /// display an error message
401 void errorMessage(docstring const & msg) const;
403 docstring getPossibleLabel();
405 /// the name of the macro we are currently inputting
406 docstring macroName();
407 /// where in the curent cell does the macro name start?
409 /// can we enter the inset?
410 bool openable(MathAtom const &) const;
412 Encoding const * getEncoding() const;
413 /// font at cursor position
414 Font getFont() const;
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.
423 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
428 #endif // LYXLCURSOR_H