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 to next/previous row
127 * Assuming we were to keep moving left (right) from the current cursor
128 * position, place the cursor at the rightmost (leftmost) edge of the
129 * new row to which we would move according to visual-mode cursor movement.
130 * This could be either the next or the previous row, depending on the
131 * direction in which we're moving, and whether we're in an LTR or RTL
133 * @note: The new position may even be in a new paragraph.
134 * @note: This method will not move out of the current slice.
135 * @return: false if not moved (no more rows to move to in given direction)
136 * @return: true if moved
138 bool posVisToNewRow(bool movingLeft);
139 /// move to right or left extremity of the current row
140 void posVisToRowExtremity(bool left);
143 void insert(Inset *);
144 /// insert a single char
145 void insert(char_type c);
147 void insert(docstring const & str);
149 /// FIXME: rename to something sensible showing difference to x_target()
150 /// in pixels from left of screen, set to current position if unset
152 /// set the targetX to x
153 void setTargetX(int x);
154 /// return targetX or -1 if unset
155 int x_target() const;
156 /// set targetX to current position
158 /// clear targetX, i.e. set it to -1
160 /// set offset to actual position - targetX
161 void updateTextTargetOffset();
162 /// distance between actual and targeted position during last up/down in text
163 int textTargetOffset() const;
165 /// access to normalized selection anchor
166 CursorSlice anchor() const;
167 /// sets anchor to cursor position
169 /// access to owning BufferView
170 BufferView & bv() const;
171 /// access to owning Buffer
172 Buffer & buffer() const;
173 /// get some interesting description of top position
174 void info(odocstream & os) const;
175 /// are we in math mode (2), text mode (1) or unsure (0)?
177 /// reset cursor bottom to the beginning of the given inset
178 // (sort of 'chroot' environment...)
180 /// for spellchecking
181 void replaceWord(std::string const & replacestring);
183 * the event was not (yet) dispatched.
185 * Should only be called by an inset's doDispatch() method. It means:
186 * I, the doDispatch() method of InsetFoo, hereby declare that I am
187 * not able to handle that request and trust my parent will do the
188 * Right Thing (even if my getStatus partner said that I can do it).
189 * It is sort of a kludge that should be used only rarely...
192 /// the event was already dispatched
194 /// Set which update should be done
195 void updateFlags(Update::flags f);
197 * don't call update() when done
199 * Should only be called by an inset's doDispatch() method. It means:
200 * I handled that request and I can reassure you that the screen does
201 * not need to be re-drawn and all entries in the coord cache stay
202 * valid (and there are no other things to put in the coord cache).
203 * This is a fairly rare event as well and only some optimization.
204 * Not using noUpdate() should never be wrong.
207 /// fix cursor in circumstances that should never happen.
208 /// \retval true if a fix occured.
212 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
213 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
220 /// makes sure the next operation will be stored
223 /// The general case: prepare undo for an arbitrary range.
224 void recordUndo(UndoKind kind, pit_type from, pit_type to);
226 /// Convenience: prepare undo for the range between 'from' and cursor.
227 void recordUndo(UndoKind kind, pit_type from);
229 /// Convenience: prepare undo for the single paragraph or cell
230 /// containing the cursor
231 void recordUndo(UndoKind kind = ATOMIC_UNDO);
233 /// Convenience: prepare undo for the inset containing the cursor
234 void recordUndoInset(UndoKind kind = ATOMIC_UNDO);
236 /// Convenience: prepare undo for the whole buffer
237 void recordUndoFullDocument();
239 /// Convenience: prepare undo for the selected paragraphs
240 void recordUndoSelection();
243 void checkBufferStructure();
249 /// the anchor position
253 DispatchResult disp_;
255 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
259 * The target x position of the cursor. This is used for when
260 * we have text like :
262 * blah blah blah blah| blah blah blah
264 * blah blah blah blah blah blah
266 * When we move onto row 3, we would like to be vertically aligned
267 * with where we were in row 1, despite the fact that row 2 is
271 /// if a x_target cannot be hit exactly in a text, put the difference here
272 int textTargetOffset_;
273 /// do we have a selection?
275 /// are we on the way to get one?
277 /// If true, we are behind the previous char, otherwise we are in front
278 // of the next char. This only make a difference when we are in front
279 // of a big inset spanning a whole row and computing coordinates for
280 // displaying the cursor.
282 /// position before dispatch started
283 DocIterator beforeDispatchCursor_;
285 // FIXME: make them private.
287 /// the current font settings
290 Font real_current_font;
295 // math specific stuff that could be promoted to "global" later
297 /// do we allow autocorrection
299 /// are we entering a macro name?
303 ///////////////////////////////////////////////////////////////////
305 // The part below is the non-integrated rest of the original math
306 // cursor. This should be either generalized for texted or moved
307 // back to the math insets.
309 ///////////////////////////////////////////////////////////////////
313 void insert(MathAtom const &);
315 void insert(MathData const &);
316 /// return false for empty math insets
318 /// return false for empty math insets
320 /// move the cursor up by sending an internal LFUN_UP
321 /// return true if fullscreen update is needed
323 /// move the cursor up by sending an internal LFUN_DOWN,
324 /// return true if fullscreen update is needed
326 /// move up/down in a text inset, called for LFUN_UP/DOWN,
327 /// return true if successful, updateNeeded set to true if fullscreen
328 /// update is needed, otherwise it's not touched
329 bool upDownInText(bool up, bool & updateNeeded);
330 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
331 /// return true if successful
332 bool upDownInMath(bool up);
336 void plainInsert(MathAtom const & at);
338 void niceInsert(MathAtom const & at);
340 void niceInsert(docstring const & str);
342 /// in pixels from top of screen
343 void setScreenPos(int x, int y);
344 /// current offset in the top cell
346 /// interpret name a name of a macro. Returns true if
347 /// something got inserted.
348 bool macroModeClose();
349 /// are we currently typing the name of a macro?
350 bool inMacroMode() const;
351 /// get access to the macro we are currently typing
352 InsetMathUnknown * activeMacro();
353 /// get access to the macro we are currently typing
354 InsetMathUnknown const * activeMacro() const;
356 /// replace selected stuff with at, placing the former
357 // selection in given cell of atom
358 void handleNest(MathAtom const & at, int cell = 0);
360 bool isInside(Inset const *) const;
362 /// make sure cursor position is valid
363 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
365 /// mark current cursor trace for redraw
368 /// hack for reveal codes
371 /// injects content of a cell into parent
373 /// split font inset etc
374 void handleFont(std::string const & font);
376 /// display a message
377 void message(docstring const & msg) const;
378 /// display an error message
379 void errorMessage(docstring const & msg) const;
381 docstring getPossibleLabel();
383 /// the name of the macro we are currently inputting
384 docstring macroName();
385 /// where in the curent cell does the macro name start?
387 /// can we enter the inset?
388 bool openable(MathAtom const &) const;
390 Encoding const * getEncoding() const;
391 /// font at cursor position
392 Font getFont() const;
397 * Notifies all insets which appear in old, but not in cur. Make
398 * Sure that the cursor old is valid, i.e. all inset pointers
399 * point to valid insets! Use Cursor::fixIfBroken if necessary.
401 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
406 #endif // LYXLCURSOR_H