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
43 /// create the cursor of a BufferView
44 explicit Cursor(BufferView & bv);
46 /// dispatch from innermost inset upwards
47 void dispatch(FuncRequest const & cmd);
48 /// get the resut of the last dispatch
49 DispatchResult result() const;
50 /// add a new cursor slice
51 void push(Inset & inset);
52 /// add a new cursor slice, place cursor at front (move backwards)
53 void pushBackward(Inset & inset);
54 /// pop one level off the cursor
56 /// pop one slice off the cursor stack and go backwards
58 /// pop one slice off the cursor stack and go forward
60 /// make sure we are outside of given inset
61 void leaveInset(Inset const & inset);
63 void setCursor(DocIterator const & it);
66 void setCurrentFont();
72 bool selection() const { return selection_; }
74 void setSelection(bool sel) { selection_ = sel; }
75 /// do we have a multicell selection?
76 bool selIsMultiCell() const
77 { return selection_ && selBegin().idx() != selEnd().idx(); }
78 /// do we have a multiline selection?
79 bool selIsMultiLine() const
80 { return selection_ && selBegin().pit() != selEnd().pit(); }
81 /// did we place the anchor?
82 bool mark() const { return mark_; }
83 /// did we place the anchor?
84 void setMark(bool mark) { mark_ = mark; }
87 /// set selection at given position
88 void setSelection(DocIterator const & where, int n);
90 void clearSelection();
91 /// access start of selection
92 CursorSlice selBegin() const;
93 /// access end of selection
94 CursorSlice selEnd() const;
95 /// access start of selection
96 DocIterator selectionBegin() const;
97 /// access start of selection
98 DocIterator selectionEnd() const;
100 * Update the selection status and save permanent
101 * selection if needed.
102 * @param selecting the new selection status
103 * @return whether the selection status has changed
105 bool selHandle(bool selecting);
107 docstring selectionAsString(bool label) const;
109 docstring currentState() const;
111 /// auto-correct mode
112 bool autocorrect() const { return autocorrect_; }
113 /// auto-correct mode
114 bool & autocorrect() { return autocorrect_; }
115 /// are we entering a macro name?
116 bool macromode() const { return macromode_; }
117 /// are we entering a macro name?
118 bool & macromode() { return macromode_; }
119 /// returns x,y position
120 void getPos(int & x, int & y) const;
121 /// return logical positions between which the cursor is situated
123 * If the cursor is at the edge of a row, the position which is "over the
124 * edge" will be returned as -1.
126 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
127 /// the row in the paragraph we're in
128 Row const & textRow() const;
133 /// move one step backwards
135 /// move one step forward
137 /// move visually one step to the right
139 * @note: This method may move into an inset unless skip_inset == true.
140 * @note: This method may move into a new paragraph.
141 * @note: This method may move out of the current slice.
142 * @return: true if moved, false if not moved
144 bool posVisRight(bool skip_inset = false);
145 /// move visually one step to the left
147 * @note: This method may move into an inset unless skip_inset == true.
148 * @note: This method may move into a new paragraph.
149 * @note: This method may move out of the current slice.
150 * @return: true if moved, false if not moved
152 bool posVisLeft(bool skip_inset = false);
153 /// move visually to next/previous row
155 * Assuming we were to keep moving left (right) from the current cursor
156 * position, place the cursor at the rightmost (leftmost) edge of the
157 * new row to which we would move according to visual-mode cursor movement.
158 * This could be either the next or the previous row, depending on the
159 * direction in which we're moving, and whether we're in an LTR or RTL
161 * @note: The new position may even be in a new paragraph.
162 * @note: This method will not move out of the current slice.
163 * @return: false if not moved (no more rows to move to in given direction)
164 * @return: true if moved
166 bool posVisToNewRow(bool movingLeft);
167 /// move to right or left extremity of the current row
168 void posVisToRowExtremity(bool left);
171 void insert(Inset *);
172 /// insert a single char
173 void insert(char_type c);
175 void insert(docstring const & str);
177 /// FIXME: rename to something sensible showing difference to x_target()
178 /// in pixels from left of screen, set to current position if unset
180 /// set the targetX to x
181 void setTargetX(int x);
182 /// return targetX or -1 if unset
183 int x_target() const;
184 /// set targetX to current position
186 /// clear targetX, i.e. set it to -1
188 /// set offset to actual position - targetX
189 void updateTextTargetOffset();
190 /// distance between actual and targeted position during last up/down in text
191 int textTargetOffset() const;
193 /// access to normalized selection anchor
194 CursorSlice anchor() const;
195 /// sets anchor to cursor position
197 /// access to owning BufferView
198 BufferView & bv() const;
199 /// access to owning Buffer
200 Buffer & buffer() const;
201 /// get some interesting description of top position
202 void info(odocstream & os) const;
203 /// are we in math mode (2), text mode (1) or unsure (0)?
205 /// reset cursor bottom to the beginning of the given inset
206 // (sort of 'chroot' environment...)
208 /// for spellchecking
209 void replaceWord(std::string const & replacestring);
211 * the event was not (yet) dispatched.
213 * Should only be called by an inset's doDispatch() method. It means:
214 * I, the doDispatch() method of InsetFoo, hereby declare that I am
215 * not able to handle that request and trust my parent will do the
216 * Right Thing (even if my getStatus partner said that I can do it).
217 * It is sort of a kludge that should be used only rarely...
220 /// the event was already dispatched
222 /// Set which update should be done
223 void updateFlags(Update::flags f);
225 * don't call update() when done
227 * Should only be called by an inset's doDispatch() method. It means:
228 * I handled that request and I can reassure you that the screen does
229 * not need to be re-drawn and all entries in the coord cache stay
230 * valid (and there are no other things to put in the coord cache).
231 * This is a fairly rare event as well and only some optimization.
232 * Not using noUpdate() should never be wrong.
235 /// fix cursor in circumstances that should never happen.
236 /// \retval true if a fix occured.
240 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
241 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
248 /// makes sure the next operation will be stored
249 void finishUndo() const;
251 /// open a new group of undo operations. Groups can be nested.
252 void beginUndoGroup() const;
254 /// end the current undo group
255 void endUndoGroup() const;
257 /// The general case: prepare undo for an arbitrary range.
258 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
260 /// Convenience: prepare undo for the range between 'from' and cursor.
261 void recordUndo(UndoKind kind, pit_type from) const;
263 /// Convenience: prepare undo for the single paragraph or cell
264 /// containing the cursor
265 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
267 /// Convenience: prepare undo for the inset containing the cursor
268 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
270 /// Convenience: prepare undo for the whole buffer
271 void recordUndoFullDocument() const;
273 /// Convenience: prepare undo for the selected paragraphs or cells
274 void recordUndoSelection() const;
277 void checkBufferStructure();
283 /// the anchor position
287 DispatchResult disp_;
289 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
293 * The target x position of the cursor. This is used for when
294 * we have text like :
296 * blah blah blah blah| blah blah blah
298 * blah blah blah blah blah blah
300 * When we move onto row 3, we would like to be vertically aligned
301 * with where we were in row 1, despite the fact that row 2 is
305 /// if a x_target cannot be hit exactly in a text, put the difference here
306 int textTargetOffset_;
307 /// do we have a selection?
309 /// are we on the way to get one?
311 /// If true, we are behind the previous char, otherwise we are in front
312 // of the next char. This only make a difference when we are in front
313 // of a big inset spanning a whole row and computing coordinates for
314 // displaying the cursor.
316 /// position before dispatch started
317 DocIterator beforeDispatchCursor_;
319 // FIXME: make them private.
321 /// the current font settings
324 Font real_current_font;
329 // math specific stuff that could be promoted to "global" later
331 /// do we allow autocorrection
333 /// are we entering a macro name?
337 ///////////////////////////////////////////////////////////////////
339 // The part below is the non-integrated rest of the original math
340 // cursor. This should be either generalized for texted or moved
341 // back to the math insets.
343 ///////////////////////////////////////////////////////////////////
347 void insert(MathAtom const &);
349 void insert(MathData const &);
350 /// return false for empty math insets
352 /// return false for empty math insets
354 /// move the cursor up by sending an internal LFUN_UP
355 /// return true if fullscreen update is needed
357 /// move the cursor up by sending an internal LFUN_DOWN,
358 /// return true if fullscreen update is needed
360 /// move up/down in a text inset, called for LFUN_UP/DOWN,
361 /// return true if successful, updateNeeded set to true if fullscreen
362 /// update is needed, otherwise it's not touched
363 bool upDownInText(bool up, bool & updateNeeded);
364 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
365 /// return true if successful
366 bool upDownInMath(bool up);
370 void plainInsert(MathAtom const & at);
372 void niceInsert(MathAtom const & at);
374 void niceInsert(docstring const & str);
376 /// in pixels from top of screen
377 void setScreenPos(int x, int y);
378 /// current offset in the top cell
380 /// interpret name a name of a macro. Returns true if
381 /// something got inserted.
382 bool macroModeClose();
383 /// are we currently typing the name of a macro?
384 bool inMacroMode() const;
385 /// get access to the macro we are currently typing
386 InsetMathUnknown * activeMacro();
387 /// get access to the macro we are currently typing
388 InsetMathUnknown const * activeMacro() const;
390 /// replace selected stuff with at, placing the former
391 // selection in given cell of atom
392 void handleNest(MathAtom const & at, int cell = 0);
394 bool isInside(Inset const *) const;
396 /// make sure cursor position is valid
397 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
399 /// mark current cursor trace for redraw
402 /// hack for reveal codes
405 /// injects content of a cell into parent
407 /// split font inset etc
408 void handleFont(std::string const & font);
410 /// display a message
411 void message(docstring const & msg) const;
412 /// display an error message
413 void errorMessage(docstring const & msg) const;
415 docstring getPossibleLabel() const;
417 /// the name of the macro we are currently inputting
418 docstring macroName();
419 /// where in the curent cell does the macro name start?
421 /// can we enter the inset?
422 bool openable(MathAtom const &) const;
424 Encoding const * getEncoding() const;
425 /// font at cursor position
426 Font getFont() const;
431 * Notifies all insets which appear in old, but not in cur. Make
432 * Sure that the cursor old is valid, i.e. all inset pointers
433 * point to valid insets! Use Cursor::fixIfBroken if necessary.
435 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
440 #endif // LYXLCURSOR_H