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 /// sets anchor to cursor position
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)?
213 /// reset cursor bottom to the beginning of the top inset
214 // (sort of 'chroot' environment...)
216 /// for spellchecking
217 void replaceWord(std::string const & replacestring);
219 * the event was not (yet) dispatched.
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...
228 /// the event was already dispatched
230 /// Set which update should be done
231 void updateFlags(Update::flags f);
233 * don't call update() when done
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.
243 /// fix cursor in circumstances that should never happen.
244 /// \retval true if a fix occured.
248 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
249 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
256 /// makes sure the next operation will be stored
257 void finishUndo() const;
259 /// open a new group of undo operations. Groups can be nested.
260 void beginUndoGroup() const;
262 /// end the current undo group
263 void endUndoGroup() const;
265 /// The general case: prepare undo for an arbitrary range.
266 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
268 /// Convenience: prepare undo for the range between 'from' and cursor.
269 void recordUndo(UndoKind kind, pit_type from) const;
271 /// Convenience: prepare undo for the single paragraph or cell
272 /// containing the cursor
273 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
275 /// Convenience: prepare undo for the inset containing the cursor
276 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
278 /// Convenience: prepare undo for the whole buffer
279 void recordUndoFullDocument() const;
281 /// Convenience: prepare undo for the selected paragraphs or cells
282 void recordUndoSelection() const;
285 void checkBufferStructure();
291 /// the anchor position
295 DocIterator const & beforeDispatchCursor() const { return beforeDispatchCursor_; }
297 void saveBeforeDispatchPosXY();
301 mutable DispatchResult disp_;
303 * The target x position of the cursor. This is used for when
304 * we have text like :
306 * blah blah blah blah| blah blah blah
308 * blah blah blah blah blah blah
310 * When we move onto row 3, we would like to be vertically aligned
311 * with where we were in row 1, despite the fact that row 2 is
315 /// if a x_target cannot be hit exactly in a text, put the difference here
316 int textTargetOffset_;
317 /// do we have a selection?
319 /// are we on the way to get one?
321 /// are we in word-selection mode? This is set when double clicking.
322 bool word_selection_;
323 /// If true, we are behind the previous char, otherwise we are in front
324 // of the next char. This only make a difference when we are in front
325 // of a big inset spanning a whole row and computing coordinates for
326 // displaying the cursor.
328 /// position before dispatch started
329 DocIterator beforeDispatchCursor_;
330 /// cursor screen coordinates before dispatch started
331 int beforeDispatchPosX_;
332 int beforeDispatchPosY_;
335 // FIXME: make them private.
337 /// the current font settings
340 Font real_current_font;
345 // math specific stuff that could be promoted to "global" later
347 /// do we allow autocorrection
349 /// are we entering a macro name?
353 ///////////////////////////////////////////////////////////////////
355 // The part below is the non-integrated rest of the original math
356 // cursor. This should be either generalized for texted or moved
357 // back to the math insets.
359 ///////////////////////////////////////////////////////////////////
363 void insert(MathAtom const &);
365 void insert(MathData const &);
366 /// return false for empty math insets
368 /// return false for empty math insets
370 /// move the cursor up by sending an internal LFUN_UP
371 /// return true if fullscreen update is needed
373 /// move the cursor up by sending an internal LFUN_DOWN,
374 /// return true if fullscreen update is needed
376 /// whether the cursor is either at the first or last row
377 bool atFirstOrLastRow(bool up);
378 /// move up/down in a text inset, called for LFUN_UP/DOWN,
379 /// return true if successful, updateNeeded set to true if fullscreen
380 /// update is needed, otherwise it's not touched
381 bool upDownInText(bool up, bool & updateNeeded);
382 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
383 /// return true if successful
384 bool upDownInMath(bool up);
388 void plainInsert(MathAtom const & at);
390 void niceInsert(MathAtom const & at);
392 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL,
395 /// in pixels from top of screen
396 void setScreenPos(int x, int y);
397 /// current offset in the top cell
399 /// interpret name a name of a macro. Returns true if
400 /// something got inserted.
401 bool macroModeClose();
402 /// are we currently typing the name of a macro?
403 bool inMacroMode() const;
404 /// get access to the macro we are currently typing
405 InsetMathUnknown * activeMacro();
406 /// get access to the macro we are currently typing
407 InsetMathUnknown const * activeMacro() const;
409 /// replace selected stuff with at, placing the former
410 // selection in given cell of atom
411 void handleNest(MathAtom const & at, int cell = 0);
413 bool isInside(Inset const *) const;
415 /// make sure cursor position is valid
416 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
418 /// mark current cursor trace for redraw
421 /// hack for reveal codes
424 /// injects content of a cell into parent
426 /// split font inset etc
427 void handleFont(std::string const & font);
429 /// display a message
430 void message(docstring const & msg) const;
431 /// display an error message
432 void errorMessage(docstring const & msg) const;
434 docstring getPossibleLabel() const;
436 /// the name of the macro we are currently inputting
437 docstring macroName();
438 /// where in the curent cell does the macro name start?
440 /// can we enter the inset?
441 bool openable(MathAtom const &) const;
443 Encoding const * getEncoding() const;
444 /// font at cursor position
445 Font getFont() const;
450 * Notifies all insets which appear in old, but not in cur. And then
451 * notify all insets which appear in cur, but not in old.
452 * Make sure that the cursor old is valid, i.e. all inset pointers
453 * point to valid insets! Use Cursor::fixIfBroken if necessary.
455 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
460 #endif // LYXLCURSOR_H