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 /// dispatch from innermost inset upwards
49 void dispatch(FuncRequest const & cmd);
50 /// get the resut of the last dispatch
51 DispatchResult result() const;
52 /// add a new cursor slice
53 void push(Inset & inset);
54 /// add a new cursor slice, place cursor at front (move backwards)
55 void pushBackward(Inset & inset);
56 /// pop one level off the cursor
58 /// pop one slice off the cursor stack and go backwards
60 /// pop one slice off the cursor stack and go forward
62 /// make sure we are outside of given inset
63 void leaveInset(Inset const & inset);
65 void setCursor(DocIterator const & it);
66 /// sets the cursor to the anchor
67 void setCursorToAnchor();
70 void setCurrentFont();
76 bool selection() const { return selection_; }
78 void setSelection(bool sel) { selection_ = sel; }
79 /// do we have a multicell selection?
80 bool selIsMultiCell() const
81 { return selection_ && selBegin().idx() != selEnd().idx(); }
82 /// do we have a multiline selection?
83 bool selIsMultiLine() const
84 { return selection_ && selBegin().pit() != selEnd().pit(); }
85 /// did we place the anchor?
86 bool mark() const { return mark_; }
87 /// did we place the anchor?
88 void setMark(bool mark) { mark_ = mark; }
91 /// set selection at given position
92 void setSelection(DocIterator const & where, int n);
94 void clearSelection();
95 /// access start of selection
96 CursorSlice selBegin() const;
97 /// access end of selection
98 CursorSlice selEnd() const;
99 /// access start of selection
100 DocIterator selectionBegin() const;
101 /// access start of selection
102 DocIterator selectionEnd() const;
104 * Update the selection status and save permanent
105 * selection if needed.
106 * @param selecting the new selection status
107 * @return whether the selection status has changed
109 bool selHandle(bool selecting);
111 docstring selectionAsString(bool label) const;
113 docstring currentState() const;
115 /// auto-correct mode
116 bool autocorrect() const { return autocorrect_; }
117 /// auto-correct mode
118 bool & autocorrect() { return autocorrect_; }
119 /// are we entering a macro name?
120 bool macromode() const { return macromode_; }
121 /// are we entering a macro name?
122 bool & macromode() { return macromode_; }
123 /// returns x,y position
124 void getPos(int & x, int & y) const;
125 /// return logical positions between which the cursor is situated
127 * If the cursor is at the edge of a row, the position which is "over the
128 * edge" will be returned as -1.
130 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
131 /// the row in the paragraph we're in
132 Row const & textRow() const;
137 /// move one step backwards
139 /// move one step forward
141 /// move visually one step to the right
143 * @note: This method may move into an inset unless skip_inset == true.
144 * @note: This method may move into a new paragraph.
145 * @note: This method may move out of the current slice.
146 * @return: true if moved, false if not moved
148 bool posVisRight(bool skip_inset = false);
149 /// move visually one step to the left
151 * @note: This method may move into an inset unless skip_inset == true.
152 * @note: This method may move into a new paragraph.
153 * @note: This method may move out of the current slice.
154 * @return: true if moved, false if not moved
156 bool posVisLeft(bool skip_inset = false);
157 /// move visually to next/previous row
159 * Assuming we were to keep moving left (right) from the current cursor
160 * position, place the cursor at the rightmost (leftmost) edge of the
161 * new row to which we would move according to visual-mode cursor movement.
162 * This could be either the next or the previous row, depending on the
163 * direction in which we're moving, and whether we're in an LTR or RTL
165 * @note: The new position may even be in a new paragraph.
166 * @note: This method will not move out of the current slice.
167 * @return: false if not moved (no more rows to move to in given direction)
168 * @return: true if moved
170 bool posVisToNewRow(bool movingLeft);
171 /// move to right or left extremity of the current row
172 void posVisToRowExtremity(bool left);
175 void insert(Inset *);
176 /// insert a single char
177 void insert(char_type c);
179 void insert(docstring const & str);
181 /// FIXME: rename to something sensible showing difference to x_target()
182 /// in pixels from left of screen, set to current position if unset
184 /// set the targetX to x
185 void setTargetX(int x);
186 /// return targetX or -1 if unset
187 int x_target() const;
188 /// set targetX to current position
190 /// clear targetX, i.e. set it to -1
192 /// set offset to actual position - targetX
193 void updateTextTargetOffset();
194 /// distance between actual and targeted position during last up/down in text
195 int textTargetOffset() const;
197 /// access to normalized selection anchor
198 CursorSlice anchor() const;
199 /// sets anchor to cursor position
201 /// access to owning BufferView
202 BufferView & bv() const;
203 /// get some interesting description of top position
204 void info(odocstream & os) const;
205 /// are we in math mode (2), text mode (1) or unsure (0)?
207 /// reset cursor bottom to the beginning of the given inset
208 // (sort of 'chroot' environment...)
210 /// for spellchecking
211 void replaceWord(std::string const & replacestring);
213 * the event was not (yet) dispatched.
215 * Should only be called by an inset's doDispatch() method. It means:
216 * I, the doDispatch() method of InsetFoo, hereby declare that I am
217 * not able to handle that request and trust my parent will do the
218 * Right Thing (even if my getStatus partner said that I can do it).
219 * It is sort of a kludge that should be used only rarely...
222 /// the event was already dispatched
224 /// Set which update should be done
225 void updateFlags(Update::flags f);
227 * don't call update() when done
229 * Should only be called by an inset's doDispatch() method. It means:
230 * I handled that request and I can reassure you that the screen does
231 * not need to be re-drawn and all entries in the coord cache stay
232 * valid (and there are no other things to put in the coord cache).
233 * This is a fairly rare event as well and only some optimization.
234 * Not using noUpdate() should never be wrong.
237 /// fix cursor in circumstances that should never happen.
238 /// \retval true if a fix occured.
242 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
243 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
250 /// makes sure the next operation will be stored
251 void finishUndo() const;
253 /// open a new group of undo operations. Groups can be nested.
254 void beginUndoGroup() const;
256 /// end the current undo group
257 void endUndoGroup() const;
259 /// The general case: prepare undo for an arbitrary range.
260 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
262 /// Convenience: prepare undo for the range between 'from' and cursor.
263 void recordUndo(UndoKind kind, pit_type from) const;
265 /// Convenience: prepare undo for the single paragraph or cell
266 /// containing the cursor
267 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
269 /// Convenience: prepare undo for the inset containing the cursor
270 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
272 /// Convenience: prepare undo for the whole buffer
273 void recordUndoFullDocument() const;
275 /// Convenience: prepare undo for the selected paragraphs or cells
276 void recordUndoSelection() const;
279 void checkBufferStructure();
285 /// the anchor position
289 DispatchResult disp_;
291 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
295 * The target x position of the cursor. This is used for when
296 * we have text like :
298 * blah blah blah blah| blah blah blah
300 * blah blah blah blah blah blah
302 * When we move onto row 3, we would like to be vertically aligned
303 * with where we were in row 1, despite the fact that row 2 is
307 /// if a x_target cannot be hit exactly in a text, put the difference here
308 int textTargetOffset_;
309 /// do we have a selection?
311 /// are we on the way to get one?
313 /// If true, we are behind the previous char, otherwise we are in front
314 // of the next char. This only make a difference when we are in front
315 // of a big inset spanning a whole row and computing coordinates for
316 // displaying the cursor.
318 /// position before dispatch started
319 DocIterator beforeDispatchCursor_;
321 // FIXME: make them private.
323 /// the current font settings
326 Font real_current_font;
331 // math specific stuff that could be promoted to "global" later
333 /// do we allow autocorrection
335 /// are we entering a macro name?
339 ///////////////////////////////////////////////////////////////////
341 // The part below is the non-integrated rest of the original math
342 // cursor. This should be either generalized for texted or moved
343 // back to the math insets.
345 ///////////////////////////////////////////////////////////////////
349 void insert(MathAtom const &);
351 void insert(MathData const &);
352 /// return false for empty math insets
354 /// return false for empty math insets
356 /// move the cursor up by sending an internal LFUN_UP
357 /// return true if fullscreen update is needed
359 /// move the cursor up by sending an internal LFUN_DOWN,
360 /// return true if fullscreen update is needed
362 /// whether the cursor is either at the first or last row
363 bool atFirstOrLastRow(bool up);
364 /// move up/down in a text inset, called for LFUN_UP/DOWN,
365 /// return true if successful, updateNeeded set to true if fullscreen
366 /// update is needed, otherwise it's not touched
367 bool upDownInText(bool up, bool & updateNeeded);
368 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
369 /// return true if successful
370 bool upDownInMath(bool up);
374 void plainInsert(MathAtom const & at);
376 void niceInsert(MathAtom const & at);
378 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL);
380 /// in pixels from top of screen
381 void setScreenPos(int x, int y);
382 /// current offset in the top cell
384 /// interpret name a name of a macro. Returns true if
385 /// something got inserted.
386 bool macroModeClose();
387 /// are we currently typing the name of a macro?
388 bool inMacroMode() const;
389 /// get access to the macro we are currently typing
390 InsetMathUnknown * activeMacro();
391 /// get access to the macro we are currently typing
392 InsetMathUnknown const * activeMacro() const;
394 /// replace selected stuff with at, placing the former
395 // selection in given cell of atom
396 void handleNest(MathAtom const & at, int cell = 0);
398 bool isInside(Inset const *) const;
400 /// make sure cursor position is valid
401 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
403 /// mark current cursor trace for redraw
406 /// hack for reveal codes
409 /// injects content of a cell into parent
411 /// split font inset etc
412 void handleFont(std::string const & font);
414 /// display a message
415 void message(docstring const & msg) const;
416 /// display an error message
417 void errorMessage(docstring const & msg) const;
419 docstring getPossibleLabel() const;
421 /// the name of the macro we are currently inputting
422 docstring macroName();
423 /// where in the curent cell does the macro name start?
425 /// can we enter the inset?
426 bool openable(MathAtom const &) const;
428 Encoding const * getEncoding() const;
429 /// font at cursor position
430 Font getFont() const;
435 * Notifies all insets which appear in old, but not in cur. And then
436 * notify all insets which appear in cur, but not in old.
437 * Make sure that the cursor old is valid, i.e. all inset pointers
438 * point to valid insets! Use Cursor::fixIfBroken if necessary.
440 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
445 #endif // LYXLCURSOR_H