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;
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
104 bool selHandle(bool selecting);
106 docstring selectionAsString(bool label) const;
108 docstring currentState() const;
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
122 * If the cursor is at the edge of a row, the position which is "over the
123 * edge" will be returned as -1.
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;
132 /// move one step backwards
134 /// move one step forward
136 /// move visually one step to the right
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
143 bool posVisRight(bool skip_inset = false);
144 /// move visually one step to the left
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
151 bool posVisLeft(bool skip_inset = false);
152 /// move visually to next/previous row
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
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
165 bool posVisToNewRow(bool movingLeft);
166 /// move to right or left extremity of the current row
167 void posVisToRowExtremity(bool left);
170 void insert(Inset *);
171 /// insert a single char
172 void insert(char_type c);
174 void insert(docstring const & str);
176 /// FIXME: rename to something sensible showing difference to x_target()
177 /// in pixels from left of screen, set to current position if unset
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
185 /// clear targetX, i.e. set it to -1
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;
192 /// access to normalized selection anchor
193 CursorSlice anchor() const;
194 /// sets anchor to cursor position
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)?
204 /// reset cursor bottom to the beginning of the given inset
205 // (sort of 'chroot' environment...)
207 /// for spellchecking
208 void replaceWord(std::string const & replacestring);
210 * the event was not (yet) dispatched.
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...
219 /// the event was already dispatched
221 /// Set which update should be done
222 void updateFlags(Update::flags f);
224 * don't call update() when done
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.
234 /// fix cursor in circumstances that should never happen.
235 /// \retval true if a fix occured.
239 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
240 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
247 /// makes sure the next operation will be stored
248 void finishUndo() const;
250 /// open a new group of undo operations. Groups can be nested.
251 void beginUndoGroup() const;
253 /// end the current undo group
254 void endUndoGroup() const;
256 /// The general case: prepare undo for an arbitrary range.
257 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
259 /// Convenience: prepare undo for the range between 'from' and cursor.
260 void recordUndo(UndoKind kind, pit_type from) const;
262 /// Convenience: prepare undo for the single paragraph or cell
263 /// containing the cursor
264 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
266 /// Convenience: prepare undo for the inset containing the cursor
267 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
269 /// Convenience: prepare undo for the whole buffer
270 void recordUndoFullDocument() const;
272 /// Convenience: prepare undo for the selected paragraphs or cells
273 void recordUndoSelection() const;
276 void checkBufferStructure();
282 /// the anchor position
286 DispatchResult disp_;
288 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
292 * The target x position of the cursor. This is used for when
293 * we have text like :
295 * blah blah blah blah| blah blah blah
297 * blah blah blah blah blah blah
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
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?
308 /// are we on the way to get one?
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.
315 /// position before dispatch started
316 DocIterator beforeDispatchCursor_;
318 // FIXME: make them private.
320 /// the current font settings
323 Font real_current_font;
328 // math specific stuff that could be promoted to "global" later
330 /// do we allow autocorrection
332 /// are we entering a macro name?
336 ///////////////////////////////////////////////////////////////////
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.
342 ///////////////////////////////////////////////////////////////////
346 void insert(MathAtom const &);
348 void insert(MathData const &);
349 /// return false for empty math insets
351 /// return false for empty math insets
353 /// move the cursor up by sending an internal LFUN_UP
354 /// return true if fullscreen update is needed
356 /// move the cursor up by sending an internal LFUN_DOWN,
357 /// return true if fullscreen update is needed
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);
369 void plainInsert(MathAtom const & at);
371 void niceInsert(MathAtom const & at);
373 void niceInsert(docstring const & str);
375 /// in pixels from top of screen
376 void setScreenPos(int x, int y);
377 /// current offset in the top cell
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;
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);
393 bool isInside(Inset const *) const;
395 /// make sure cursor position is valid
396 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
398 /// mark current cursor trace for redraw
401 /// hack for reveal codes
404 /// injects content of a cell into parent
406 /// split font inset etc
407 void handleFont(std::string const & font);
409 /// display a message
410 void message(docstring const & msg) const;
411 /// display an error message
412 void errorMessage(docstring const & msg) const;
414 docstring getPossibleLabel() const;
416 /// the name of the macro we are currently inputting
417 docstring macroName();
418 /// where in the curent cell does the macro name start?
420 /// can we enter the inset?
421 bool openable(MathAtom const &) const;
423 Encoding const * getEncoding() const;
424 /// font at cursor position
425 Font getFont() const;
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.
434 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
439 #endif // LYXLCURSOR_H