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"
20 #include "mathed/MathParser_flags.h"
34 class InsetMathUnknown;
38 /// The cursor class describes the position of a cursor within a document.
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
45 /// create the cursor of a BufferView
46 explicit Cursor(BufferView & bv);
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 const & 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
60 /// pop one slice off the cursor stack and go backwards
62 /// pop one slice off the cursor stack and go forward
64 /// make sure we are outside of given inset
65 void leaveInset(Inset const & inset);
67 void setCursor(DocIterator const & it);
68 /// sets the cursor to the normalized selection anchor
69 void setCursorToAnchor();
72 void setCurrentFont();
78 bool selection() const { return 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(); }
88 void setWordSelection(bool set) { word_selection_ = set; }
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; }
97 /// set selection at given position
98 void setSelection(DocIterator const & where, int n);
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;
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
115 bool selHandle(bool selecting);
117 docstring selectionAsString(bool label) const;
119 docstring currentState() const;
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
133 * If the cursor is at the edge of a row, the position which is "over the
134 * edge" will be returned as -1.
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;
143 /// move one step backwards
145 /// move one step forward
147 /// move visually one step to the right
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
154 bool posVisRight(bool skip_inset = false);
155 /// move visually one step to the left
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
162 bool posVisLeft(bool skip_inset = false);
163 /// move visually to next/previous row
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
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
176 bool posVisToNewRow(bool movingLeft);
177 /// move to right or left extremity of the current row
178 void posVisToRowExtremity(bool left);
181 void insert(Inset *);
182 /// insert a single char
183 void insert(char_type c);
185 void insert(docstring const & str);
187 /// FIXME: rename to something sensible showing difference to x_target()
188 /// in pixels from left of screen, set to current position if unset
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
196 /// clear targetX, i.e. set it to -1
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;
203 /// access to normalized selection anchor
204 CursorSlice normalAnchor() const;
205 // FIXME: this can't be a const & and a const function because
206 // LFUN_TAB_* wants to move the real anchor.
207 /// access to real selection anchor
208 DocIterator & realAnchor();
209 /// sets anchor to cursor position
211 /// access to owning BufferView
212 BufferView & bv() const;
213 /// get some interesting description of top position
214 void info(odocstream & os) const;
215 /// are we in math mode (2), text mode (1) or unsure (0)?
217 /// reset cursor bottom to the beginning of the top inset
218 // (sort of 'chroot' environment...)
220 /// for spellchecking
221 void replaceWord(std::string const & replacestring);
223 * the event was not (yet) dispatched.
225 * Should only be called by an inset's doDispatch() method. It means:
226 * I, the doDispatch() method of InsetFoo, hereby declare that I am
227 * not able to handle that request and trust my parent will do the
228 * Right Thing (even if my getStatus partner said that I can do it).
229 * It is sort of a kludge that should be used only rarely...
232 /// the event was already dispatched
234 /// Set which update should be done
235 void updateFlags(Update::flags f);
237 * don't call update() when done
239 * Should only be called by an inset's doDispatch() method. It means:
240 * I handled that request and I can reassure you that the screen does
241 * not need to be re-drawn and all entries in the coord cache stay
242 * valid (and there are no other things to put in the coord cache).
243 * This is a fairly rare event as well and only some optimization.
244 * Not using noUpdate() should never be wrong.
247 /// fix cursor in circumstances that should never happen.
248 /// \retval true if a fix occured.
252 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
253 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
260 /// makes sure the next operation will be stored
261 void finishUndo() const;
263 /// open a new group of undo operations. Groups can be nested.
264 void beginUndoGroup() const;
266 /// end the current undo group
267 void endUndoGroup() const;
269 /// The general case: prepare undo for an arbitrary range.
270 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
272 /// Convenience: prepare undo for the range between 'from' and cursor.
273 void recordUndo(UndoKind kind, pit_type from) const;
275 /// Convenience: prepare undo for the single paragraph or cell
276 /// containing the cursor
277 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
279 /// Convenience: prepare undo for the inset containing the cursor
280 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
282 /// Convenience: prepare undo for the whole buffer
283 void recordUndoFullDocument() const;
285 /// Convenience: prepare undo for the selected paragraphs or cells
286 void recordUndoSelection() const;
289 void checkBufferStructure();
297 DocIterator const & beforeDispatchCursor() const { return beforeDispatchCursor_; }
299 void saveBeforeDispatchPosXY();
302 /// the anchor position
305 mutable DispatchResult disp_;
307 * The target x position of the cursor. This is used for when
308 * we have text like :
310 * blah blah blah blah| blah blah blah
312 * blah blah blah blah blah blah
314 * When we move onto row 3, we would like to be vertically aligned
315 * with where we were in row 1, despite the fact that row 2 is
319 /// if a x_target cannot be hit exactly in a text, put the difference here
320 int textTargetOffset_;
321 /// do we have a selection?
323 /// are we on the way to get one?
325 /// are we in word-selection mode? This is set when double clicking.
326 bool word_selection_;
327 /// If true, we are behind the previous char, otherwise we are in front
328 // of the next char. This only make a difference when we are in front
329 // of a big inset spanning a whole row and computing coordinates for
330 // displaying the cursor.
332 /// position before dispatch started
333 DocIterator beforeDispatchCursor_;
334 /// cursor screen coordinates before dispatch started
335 int beforeDispatchPosX_;
336 int beforeDispatchPosY_;
339 // FIXME: make them private.
341 /// the current font settings
344 Font real_current_font;
349 // math specific stuff that could be promoted to "global" later
351 /// do we allow autocorrection
353 /// are we entering a macro name?
357 ///////////////////////////////////////////////////////////////////
359 // The part below is the non-integrated rest of the original math
360 // cursor. This should be either generalized for texted or moved
361 // back to the math insets.
363 ///////////////////////////////////////////////////////////////////
367 void insert(MathAtom const &);
369 void insert(MathData const &);
370 /// return false for empty math insets
372 /// return false for empty math insets
374 /// move the cursor up by sending an internal LFUN_UP
375 /// return true if fullscreen update is needed
377 /// move the cursor up by sending an internal LFUN_DOWN,
378 /// return true if fullscreen update is needed
380 /// whether the cursor is either at the first or last row
381 bool atFirstOrLastRow(bool up);
382 /// move up/down in a text inset, called for LFUN_UP/DOWN,
383 /// return true if successful, updateNeeded set to true if fullscreen
384 /// update is needed, otherwise it's not touched
385 bool upDownInText(bool up, bool & updateNeeded);
386 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
387 /// return true if successful
388 bool upDownInMath(bool up);
392 void plainInsert(MathAtom const & at);
394 void niceInsert(MathAtom const & at);
396 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL,
399 /// in pixels from top of screen
400 void setScreenPos(int x, int y);
401 /// current offset in the top cell
403 /// interpret name a name of a macro. Returns true if
404 /// something got inserted.
405 bool macroModeClose();
406 /// are we currently typing the name of a macro?
407 bool inMacroMode() const;
408 /// get access to the macro we are currently typing
409 InsetMathUnknown * activeMacro();
410 /// get access to the macro we are currently typing
411 InsetMathUnknown const * activeMacro() const;
413 /// replace selected stuff with at, placing the former
414 // selection in given cell of atom
415 void handleNest(MathAtom const & at, int cell = 0);
417 bool isInside(Inset const *) const;
419 /// make sure cursor position is valid
420 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
422 /// mark current cursor trace for redraw
425 /// hack for reveal codes
428 /// injects content of a cell into parent
430 /// split font inset etc
431 void handleFont(std::string const & font);
433 /// display a message
434 void message(docstring const & msg) const;
435 /// display an error message
436 void errorMessage(docstring const & msg) const;
438 docstring getPossibleLabel() const;
440 /// the name of the macro we are currently inputting
441 docstring macroName();
442 /// where in the curent cell does the macro name start?
444 /// can we enter the inset?
445 bool openable(MathAtom const &) const;
447 Encoding const * getEncoding() const;
448 /// font at cursor position
449 Font getFont() const;
454 * Notifies all insets which appear in old, but not in cur. And then
455 * notify all insets which appear in cur, but not in old.
456 * Make sure that the cursor old is valid, i.e. all inset pointers
457 * point to valid insets! Use Cursor::fixIfBroken if necessary.
459 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
464 #endif // LYXLCURSOR_H