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 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 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(); }
87 /// did we place the anchor?
88 bool mark() const { return mark_; }
89 /// did we place the anchor?
90 void setMark(bool mark) { mark_ = mark; }
93 /// set selection at given position
94 void setSelection(DocIterator const & where, int n);
96 void clearSelection();
97 /// access start of selection
98 CursorSlice selBegin() const;
99 /// access end of selection
100 CursorSlice selEnd() const;
101 /// access start of selection
102 DocIterator selectionBegin() const;
103 /// access start of selection
104 DocIterator selectionEnd() const;
106 * Update the selection status and save permanent
107 * selection if needed.
108 * @param selecting the new selection status
109 * @return whether the selection status has changed
111 bool selHandle(bool selecting);
113 docstring selectionAsString(bool label) const;
115 docstring currentState() const;
117 /// auto-correct mode
118 bool autocorrect() const { return autocorrect_; }
119 /// auto-correct mode
120 bool & autocorrect() { return autocorrect_; }
121 /// are we entering a macro name?
122 bool macromode() const { return macromode_; }
123 /// are we entering a macro name?
124 bool & macromode() { return macromode_; }
125 /// returns x,y position
126 void getPos(int & x, int & y) const;
127 /// return logical positions between which the cursor is situated
129 * If the cursor is at the edge of a row, the position which is "over the
130 * edge" will be returned as -1.
132 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
133 /// the row in the paragraph we're in
134 Row const & textRow() const;
139 /// move one step backwards
141 /// move one step forward
143 /// move visually one step to the right
145 * @note: This method may move into an inset unless skip_inset == true.
146 * @note: This method may move into a new paragraph.
147 * @note: This method may move out of the current slice.
148 * @return: true if moved, false if not moved
150 bool posVisRight(bool skip_inset = false);
151 /// move visually one step to the left
153 * @note: This method may move into an inset unless skip_inset == true.
154 * @note: This method may move into a new paragraph.
155 * @note: This method may move out of the current slice.
156 * @return: true if moved, false if not moved
158 bool posVisLeft(bool skip_inset = false);
159 /// move visually to next/previous row
161 * Assuming we were to keep moving left (right) from the current cursor
162 * position, place the cursor at the rightmost (leftmost) edge of the
163 * new row to which we would move according to visual-mode cursor movement.
164 * This could be either the next or the previous row, depending on the
165 * direction in which we're moving, and whether we're in an LTR or RTL
167 * @note: The new position may even be in a new paragraph.
168 * @note: This method will not move out of the current slice.
169 * @return: false if not moved (no more rows to move to in given direction)
170 * @return: true if moved
172 bool posVisToNewRow(bool movingLeft);
173 /// move to right or left extremity of the current row
174 void posVisToRowExtremity(bool left);
177 void insert(Inset *);
178 /// insert a single char
179 void insert(char_type c);
181 void insert(docstring const & str);
183 /// FIXME: rename to something sensible showing difference to x_target()
184 /// in pixels from left of screen, set to current position if unset
186 /// set the targetX to x
187 void setTargetX(int x);
188 /// return targetX or -1 if unset
189 int x_target() const;
190 /// set targetX to current position
192 /// clear targetX, i.e. set it to -1
194 /// set offset to actual position - targetX
195 void updateTextTargetOffset();
196 /// distance between actual and targeted position during last up/down in text
197 int textTargetOffset() const;
199 /// access to normalized selection anchor
200 CursorSlice anchor() const;
201 /// sets anchor to cursor position
203 /// access to owning BufferView
204 BufferView & bv() const;
205 /// get some interesting description of top position
206 void info(odocstream & os) const;
207 /// are we in math mode (2), text mode (1) or unsure (0)?
209 /// reset cursor bottom to the beginning of the top inset
210 // (sort of 'chroot' environment...)
212 /// for spellchecking
213 void replaceWord(std::string const & replacestring);
215 * the event was not (yet) dispatched.
217 * Should only be called by an inset's doDispatch() method. It means:
218 * I, the doDispatch() method of InsetFoo, hereby declare that I am
219 * not able to handle that request and trust my parent will do the
220 * Right Thing (even if my getStatus partner said that I can do it).
221 * It is sort of a kludge that should be used only rarely...
224 /// the event was already dispatched
226 /// Set which update should be done
227 void updateFlags(Update::flags f);
229 * don't call update() when done
231 * Should only be called by an inset's doDispatch() method. It means:
232 * I handled that request and I can reassure you that the screen does
233 * not need to be re-drawn and all entries in the coord cache stay
234 * valid (and there are no other things to put in the coord cache).
235 * This is a fairly rare event as well and only some optimization.
236 * Not using noUpdate() should never be wrong.
239 /// fix cursor in circumstances that should never happen.
240 /// \retval true if a fix occured.
244 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
245 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
252 /// makes sure the next operation will be stored
253 void finishUndo() const;
255 /// open a new group of undo operations. Groups can be nested.
256 void beginUndoGroup() const;
258 /// end the current undo group
259 void endUndoGroup() const;
261 /// The general case: prepare undo for an arbitrary range.
262 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
264 /// Convenience: prepare undo for the range between 'from' and cursor.
265 void recordUndo(UndoKind kind, pit_type from) const;
267 /// Convenience: prepare undo for the single paragraph or cell
268 /// containing the cursor
269 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
271 /// Convenience: prepare undo for the inset containing the cursor
272 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
274 /// Convenience: prepare undo for the whole buffer
275 void recordUndoFullDocument() const;
277 /// Convenience: prepare undo for the selected paragraphs or cells
278 void recordUndoSelection() const;
281 void checkBufferStructure();
287 /// the anchor position
291 DispatchResult disp_;
293 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
297 * The target x position of the cursor. This is used for when
298 * we have text like :
300 * blah blah blah blah| blah blah blah
302 * blah blah blah blah blah blah
304 * When we move onto row 3, we would like to be vertically aligned
305 * with where we were in row 1, despite the fact that row 2 is
309 /// if a x_target cannot be hit exactly in a text, put the difference here
310 int textTargetOffset_;
311 /// do we have a selection?
313 /// are we on the way to get one?
315 /// If true, we are behind the previous char, otherwise we are in front
316 // of the next char. This only make a difference when we are in front
317 // of a big inset spanning a whole row and computing coordinates for
318 // displaying the cursor.
320 /// position before dispatch started
321 DocIterator beforeDispatchCursor_;
323 // FIXME: make them private.
325 /// the current font settings
328 Font real_current_font;
333 // math specific stuff that could be promoted to "global" later
335 /// do we allow autocorrection
337 /// are we entering a macro name?
341 ///////////////////////////////////////////////////////////////////
343 // The part below is the non-integrated rest of the original math
344 // cursor. This should be either generalized for texted or moved
345 // back to the math insets.
347 ///////////////////////////////////////////////////////////////////
351 void insert(MathAtom const &);
353 void insert(MathData const &);
354 /// return false for empty math insets
356 /// return false for empty math insets
358 /// move the cursor up by sending an internal LFUN_UP
359 /// return true if fullscreen update is needed
361 /// move the cursor up by sending an internal LFUN_DOWN,
362 /// return true if fullscreen update is needed
364 /// whether the cursor is either at the first or last row
365 bool atFirstOrLastRow(bool up);
366 /// move up/down in a text inset, called for LFUN_UP/DOWN,
367 /// return true if successful, updateNeeded set to true if fullscreen
368 /// update is needed, otherwise it's not touched
369 bool upDownInText(bool up, bool & updateNeeded);
370 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
371 /// return true if successful
372 bool upDownInMath(bool up);
376 void plainInsert(MathAtom const & at);
378 void niceInsert(MathAtom const & at);
380 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL);
382 /// in pixels from top of screen
383 void setScreenPos(int x, int y);
384 /// current offset in the top cell
386 /// interpret name a name of a macro. Returns true if
387 /// something got inserted.
388 bool macroModeClose();
389 /// are we currently typing the name of a macro?
390 bool inMacroMode() const;
391 /// get access to the macro we are currently typing
392 InsetMathUnknown * activeMacro();
393 /// get access to the macro we are currently typing
394 InsetMathUnknown const * activeMacro() const;
396 /// replace selected stuff with at, placing the former
397 // selection in given cell of atom
398 void handleNest(MathAtom const & at, int cell = 0);
400 bool isInside(Inset const *) const;
402 /// make sure cursor position is valid
403 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
405 /// mark current cursor trace for redraw
408 /// hack for reveal codes
411 /// injects content of a cell into parent
413 /// split font inset etc
414 void handleFont(std::string const & font);
416 /// display a message
417 void message(docstring const & msg) const;
418 /// display an error message
419 void errorMessage(docstring const & msg) const;
421 docstring getPossibleLabel() const;
423 /// the name of the macro we are currently inputting
424 docstring macroName();
425 /// where in the curent cell does the macro name start?
427 /// can we enter the inset?
428 bool openable(MathAtom const &) const;
430 Encoding const * getEncoding() const;
431 /// font at cursor position
432 Font getFont() const;
437 * Notifies all insets which appear in old, but not in cur. And then
438 * notify all insets which appear in cur, but not in old.
439 * Make sure that the cursor old is valid, i.e. all inset pointers
440 * point to valid insets! Use Cursor::fixIfBroken if necessary.
442 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
447 #endif // LYXLCURSOR_H