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 {
42 /// create the cursor of a BufferView
43 explicit Cursor(BufferView & bv);
45 /// dispatch from innermost inset upwards
46 void dispatch(FuncRequest const & cmd);
47 /// get the resut of the last dispatch
48 DispatchResult result() const;
49 /// add a new cursor slice
50 void push(Inset & inset);
51 /// add a new cursor slice, place cursor at front (move backwards)
52 void pushBackward(Inset & inset);
53 /// pop one level off the cursor
55 /// pop one slice off the cursor stack and go backwards
57 /// pop one slice off the cursor stack and go forward
59 /// make sure we are outside of given inset
60 void leaveInset(Inset const & inset);
62 void setCursor(DocIterator const & it);
65 void setCurrentFont();
71 bool selection() const { return selection_; }
73 bool & selection() { return selection_; }
74 /// did we place the anchor?
75 bool mark() const { return mark_; }
76 /// did we place the anchor?
77 bool & mark() { return mark_; }
80 /// set selection at given position
81 void setSelection(DocIterator const & where, int n);
83 void clearSelection();
84 /// access start of selection
85 CursorSlice selBegin() const;
86 /// access end of selection
87 CursorSlice selEnd() const;
88 /// access start of selection
89 DocIterator selectionBegin() const;
90 /// access start of selection
91 DocIterator selectionEnd() const;
92 /// FIXME: document this
93 bool selHandle(bool selecting);
95 docstring selectionAsString(bool label) const;
97 docstring currentState();
100 bool autocorrect() const { return autocorrect_; }
101 /// auto-correct mode
102 bool & autocorrect() { return autocorrect_; }
103 /// are we entering a macro name?
104 bool macromode() const { return macromode_; }
105 /// are we entering a macro name?
106 bool & macromode() { return macromode_; }
107 /// returns x,y position
108 void getPos(int & x, int & y) const;
109 /// return logical positions between which the cursor is situated
111 * If the cursor is at the edge of a row, the position which is "over the
112 * edge" will be returned as -1.
114 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
115 /// the row in the paragraph we're in
116 Row const & textRow() const;
121 /// move one step backwards
123 /// move one step forward
125 /// move visually one step to the right
127 * @note: This method may move into an inset unless skip_inset == true.
128 * @note: This method may move into a new paragraph.
129 * @note: This method may move out of the current slice.
130 * @return: true if moved, false if not moved
132 bool posVisRight(bool skip_inset = false);
133 /// move visually one step to the left
135 * @note: This method may move into an inset unless skip_inset == true.
136 * @note: This method may move into a new paragraph.
137 * @note: This method may move out of the current slice.
138 * @return: true if moved, false if not moved
140 bool posVisLeft(bool skip_inset = false);
141 /// move visually to next/previous row
143 * Assuming we were to keep moving left (right) from the current cursor
144 * position, place the cursor at the rightmost (leftmost) edge of the
145 * new row to which we would move according to visual-mode cursor movement.
146 * This could be either the next or the previous row, depending on the
147 * direction in which we're moving, and whether we're in an LTR or RTL
149 * @note: The new position may even be in a new paragraph.
150 * @note: This method will not move out of the current slice.
151 * @return: false if not moved (no more rows to move to in given direction)
152 * @return: true if moved
154 bool posVisToNewRow(bool movingLeft);
155 /// move to right or left extremity of the current row
156 void posVisToRowExtremity(bool left);
159 void insert(Inset *);
160 /// insert a single char
161 void insert(char_type c);
163 void insert(docstring const & str);
165 /// FIXME: rename to something sensible showing difference to x_target()
166 /// in pixels from left of screen, set to current position if unset
168 /// set the targetX to x
169 void setTargetX(int x);
170 /// return targetX or -1 if unset
171 int x_target() const;
172 /// set targetX to current position
174 /// clear targetX, i.e. set it to -1
176 /// set offset to actual position - targetX
177 void updateTextTargetOffset();
178 /// distance between actual and targeted position during last up/down in text
179 int textTargetOffset() const;
181 /// access to normalized selection anchor
182 CursorSlice anchor() const;
183 /// sets anchor to cursor position
185 /// access to owning BufferView
186 BufferView & bv() const;
187 /// access to owning Buffer
188 Buffer & buffer() const;
189 /// get some interesting description of top position
190 void info(odocstream & os) const;
191 /// are we in math mode (2), text mode (1) or unsure (0)?
193 /// reset cursor bottom to the beginning of the given inset
194 // (sort of 'chroot' environment...)
196 /// for spellchecking
197 void replaceWord(std::string const & replacestring);
199 * the event was not (yet) dispatched.
201 * Should only be called by an inset's doDispatch() method. It means:
202 * I, the doDispatch() method of InsetFoo, hereby declare that I am
203 * not able to handle that request and trust my parent will do the
204 * Right Thing (even if my getStatus partner said that I can do it).
205 * It is sort of a kludge that should be used only rarely...
208 /// the event was already dispatched
210 /// Set which update should be done
211 void updateFlags(Update::flags f);
213 * don't call update() when done
215 * Should only be called by an inset's doDispatch() method. It means:
216 * I handled that request and I can reassure you that the screen does
217 * not need to be re-drawn and all entries in the coord cache stay
218 * valid (and there are no other things to put in the coord cache).
219 * This is a fairly rare event as well and only some optimization.
220 * Not using noUpdate() should never be wrong.
223 /// fix cursor in circumstances that should never happen.
224 /// \retval true if a fix occured.
228 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
229 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
236 /// makes sure the next operation will be stored
239 /// The general case: prepare undo for an arbitrary range.
240 void recordUndo(UndoKind kind, pit_type from, pit_type to);
242 /// Convenience: prepare undo for the range between 'from' and cursor.
243 void recordUndo(UndoKind kind, pit_type from);
245 /// Convenience: prepare undo for the single paragraph or cell
246 /// containing the cursor
247 void recordUndo(UndoKind kind = ATOMIC_UNDO);
249 /// Convenience: prepare undo for the inset containing the cursor
250 void recordUndoInset(UndoKind kind = ATOMIC_UNDO);
252 /// Convenience: prepare undo for the whole buffer
253 void recordUndoFullDocument();
255 /// Convenience: prepare undo for the selected paragraphs or cells
256 void recordUndoSelection();
259 void checkBufferStructure();
265 /// the anchor position
269 DispatchResult disp_;
271 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
275 * The target x position of the cursor. This is used for when
276 * we have text like :
278 * blah blah blah blah| blah blah blah
280 * blah blah blah blah blah blah
282 * When we move onto row 3, we would like to be vertically aligned
283 * with where we were in row 1, despite the fact that row 2 is
287 /// if a x_target cannot be hit exactly in a text, put the difference here
288 int textTargetOffset_;
289 /// do we have a selection?
291 /// are we on the way to get one?
293 /// If true, we are behind the previous char, otherwise we are in front
294 // of the next char. This only make a difference when we are in front
295 // of a big inset spanning a whole row and computing coordinates for
296 // displaying the cursor.
298 /// position before dispatch started
299 DocIterator beforeDispatchCursor_;
301 // FIXME: make them private.
303 /// the current font settings
306 Font real_current_font;
311 // math specific stuff that could be promoted to "global" later
313 /// do we allow autocorrection
315 /// are we entering a macro name?
319 ///////////////////////////////////////////////////////////////////
321 // The part below is the non-integrated rest of the original math
322 // cursor. This should be either generalized for texted or moved
323 // back to the math insets.
325 ///////////////////////////////////////////////////////////////////
329 void insert(MathAtom const &);
331 void insert(MathData const &);
332 /// return false for empty math insets
334 /// return false for empty math insets
336 /// move the cursor up by sending an internal LFUN_UP
337 /// return true if fullscreen update is needed
339 /// move the cursor up by sending an internal LFUN_DOWN,
340 /// return true if fullscreen update is needed
342 /// move up/down in a text inset, called for LFUN_UP/DOWN,
343 /// return true if successful, updateNeeded set to true if fullscreen
344 /// update is needed, otherwise it's not touched
345 bool upDownInText(bool up, bool & updateNeeded);
346 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
347 /// return true if successful
348 bool upDownInMath(bool up);
352 void plainInsert(MathAtom const & at);
354 void niceInsert(MathAtom const & at);
356 void niceInsert(docstring const & str);
358 /// in pixels from top of screen
359 void setScreenPos(int x, int y);
360 /// current offset in the top cell
362 /// interpret name a name of a macro. Returns true if
363 /// something got inserted.
364 bool macroModeClose();
365 /// are we currently typing the name of a macro?
366 bool inMacroMode() const;
367 /// get access to the macro we are currently typing
368 InsetMathUnknown * activeMacro();
369 /// get access to the macro we are currently typing
370 InsetMathUnknown const * activeMacro() const;
372 /// replace selected stuff with at, placing the former
373 // selection in given cell of atom
374 void handleNest(MathAtom const & at, int cell = 0);
376 bool isInside(Inset const *) const;
378 /// make sure cursor position is valid
379 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
381 /// mark current cursor trace for redraw
384 /// hack for reveal codes
387 /// injects content of a cell into parent
389 /// split font inset etc
390 void handleFont(std::string const & font);
392 /// display a message
393 void message(docstring const & msg) const;
394 /// display an error message
395 void errorMessage(docstring const & msg) const;
397 docstring getPossibleLabel();
399 /// the name of the macro we are currently inputting
400 docstring macroName();
401 /// where in the curent cell does the macro name start?
403 /// can we enter the inset?
404 bool openable(MathAtom const &) const;
406 Encoding const * getEncoding() const;
407 /// font at cursor position
408 Font getFont() const;
413 * Notifies all insets which appear in old, but not in cur. Make
414 * Sure that the cursor old is valid, i.e. all inset pointers
415 * point to valid insets! Use Cursor::fixIfBroken if necessary.
417 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
422 #endif // LYXLCURSOR_H