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.
13 First some explanation about what a Cursor really is, from local to
16 * a CursorSlice indicates the position of the cursor at local level.
17 It contains in particular:
18 * idx(): the cell that contains the cursor (for Tabular or math
19 arrays). Always 0 for 'plain' insets
20 * pit(): the index of the current paragraph (only for text)
21 * pos(): the position in the current paragraph (or in the math
23 * inset(): the inset in which the cursor is. For a InsetTabular,
24 this is the tabular itself, not the cell inset (which is an
27 * a DocIterator indicated the position of the cursor in the document.
28 It knows about the current buffer (buffer() method) and contains a
29 vector of CursorSlices that describes the nesting of insets up to the
30 point of interest. Note that operator<< has been implemented, so that
31 one can send a DocIterator to a stream to see its value. Try it, it is
32 very helpful to understand the cursor layout.
33 * when using idx/pit/pos on a DocIterator, one gets the information
34 from the inner slice (this slice can be accessed as top())
35 * inMathed() returns true when the cursor is in a math formula
36 * inTexted() returns true when the cursor is in text
37 * innerTextSlice() returns the deepest slice that is text (useful
38 when one is in a math equation and looks for the enclosing text)
40 * A CursorData is a descendant of Dociterator that contains
41 * a second DocIterator object, the anchor, that is useful when
43 * some other data that describes current selection, cursor font, etc.
45 This class is mostly used only for undo and contains the Cursor
46 elements that are not GUI-related. In LyX 2.0, Cursor was directly
47 deriving from DocIterator
49 * A Cursor is a descendant of CursorData that contains interesting
50 display-related information, in particular targetX(), the horizontal
51 position of the cursor in pixels.
57 #include "DispatchResult.h"
58 #include "DocIterator.h"
62 #include "mathed/MathParser_flags.h"
74 class InsetMathUnknown;
78 * This class describes the position of a cursor within a document,
79 * but does not contain any detail about the view. It is currently
80 * only used to save cursor position in Undo, but culd be extended to
81 * handle the methods that only need this data.
83 class CursorData : public DocIterator
89 explicit CursorData(Buffer * buffer);
91 explicit CursorData(DocIterator const & dit);
93 friend std::ostream & operator<<(std::ostream & os, CursorData const & cur);
94 friend LyXErr & operator<<(LyXErr & os, CursorData const & cur);
97 /// the anchor position
99 /// do we have a selection?
101 /// are we on the way to get one?
103 /// are we in word-selection mode? This is set when double clicking.
104 bool word_selection_;
106 // FIXME: make them protected.
108 /// the current font settings
111 Font real_current_font;
116 // math specific stuff that could be promoted to "global" later
118 /// do we allow autocorrection
120 /// are we entering a macro name?
125 /// The cursor class describes the position of a cursor within a document.
126 class Cursor : public CursorData
129 /// create the cursor of a BufferView
130 explicit Cursor(BufferView & bv);
132 /// returns true if we made a decision
133 bool getStatus(FuncRequest const & cmd, FuncStatus & flag) const;
134 /// dispatch from innermost inset upwards
135 void dispatch(FuncRequest const & cmd);
136 /// get the resut of the last dispatch
137 DispatchResult const & result() const;
138 /// add a new cursor slice
139 void push(Inset & inset);
140 /// add a new cursor slice, place cursor at front (move backwards)
141 void pushBackward(Inset & inset);
142 /// pop one level off the cursor
144 /// pop one slice off the cursor stack and go backwards
146 /// pop one slice off the cursor stack and go forward
148 /// make sure we are outside of given inset
149 void leaveInset(Inset const & inset);
150 /// set the cursor data
151 void setCursorData(CursorData const & data);
153 /// this (intentionally) does neither touch anchor nor selection status
154 void setCursor(DocIterator const & it);
155 /// set the cursor to dit normalised against the anchor, and set selection.
156 void setCursorSelectionTo(DocIterator dit);
157 /// sets the cursor to the normalized selection anchor
158 void setCursorToAnchor();
161 void setCurrentFont();
166 /// selection active?
167 bool selection() const { return selection_; }
168 /// set selection; this is lower level than (set|clear)Selection
169 void selection(bool sel) { selection_ = sel; }
170 /// do we have a multicell selection?
171 bool selIsMultiCell() const
172 { return selection_ && selBegin().idx() != selEnd().idx(); }
173 /// do we have a multiline selection?
174 bool selIsMultiLine() const
175 { return selection_ && selBegin().pit() != selEnd().pit(); }
177 void setWordSelection(bool set) { word_selection_ = set; }
179 bool wordSelection() { return word_selection_; }
180 /// did we place the anchor?
181 bool mark() const { return mark_; }
182 /// did we place the anchor?
183 void setMark(bool mark) { mark_ = mark; }
186 /// set selection at given position
187 void setSelection(DocIterator const & where, int n);
189 void clearSelection();
190 /// access start of selection
191 CursorSlice selBegin() const;
192 /// access end of selection
193 CursorSlice selEnd() const;
194 /// access start of selection
195 DocIterator selectionBegin() const;
196 /// access end of selection
197 DocIterator selectionEnd() const;
199 * Update the selection status and save permanent
200 * selection if needed.
201 * @param selecting the new selection status
202 * @return whether the selection status has changed
204 bool selHandle(bool selecting);
206 docstring selectionAsString(bool with_label) const;
208 docstring currentState() const;
210 /// auto-correct mode
211 bool autocorrect() const { return autocorrect_; }
212 /// auto-correct mode
213 bool & autocorrect() { return autocorrect_; }
214 /// are we entering a macro name?
215 bool macromode() const { return macromode_; }
216 /// are we entering a macro name?
217 bool & macromode() { return macromode_; }
219 /// returns true when all insets in cursor stack are in cache
220 bool inCoordCache() const;
221 /// returns x,y position
222 void getPos(int & x, int & y) const;
223 /// return logical positions between which the cursor is situated
225 * If the cursor is at the edge of a row, the position which is "over the
226 * edge" will be returned as -1.
228 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const;
229 /// the row in the paragraph we're in
230 Row const & textRow() const;
235 /// move visually one step to the right
237 * @note: This method may move into an inset unless skip_inset == true.
238 * @note: This method may move into a new paragraph.
239 * @note: This method may move out of the current slice.
240 * @return: true if moved, false if not moved
242 bool posVisRight(bool skip_inset = false);
243 /// move visually one step to the left
245 * @note: This method may move into an inset unless skip_inset == true.
246 * @note: This method may move into a new paragraph.
247 * @note: This method may move out of the current slice.
248 * @return: true if moved, false if not moved
250 bool posVisLeft(bool skip_inset = false);
251 /// move visually to next/previous row
253 * Assuming we were to keep moving left (right) from the current cursor
254 * position, place the cursor at the rightmost (leftmost) edge of the
255 * new row to which we would move according to visual-mode cursor movement.
256 * This could be either the next or the previous row, depending on the
257 * direction in which we're moving, and whether we're in an LTR or RTL
259 * @note: The new position may even be in a new paragraph.
260 * @note: This method will not move out of the current slice.
261 * @return: false if not moved (no more rows to move to in given direction)
262 * @return: true if moved
264 bool posVisToNewRow(bool movingLeft);
265 /// move to right or left extremity of the current row
266 void posVisToRowExtremity(bool left);
267 /// Should interpretation of the arrow keys be reversed?
268 bool reverseDirectionNeeded() const;
271 void insert(Inset *);
272 /// insert a single char
273 void insert(char_type c);
275 void insert(docstring const & str);
277 /// FIXME: rename to something sensible showing difference to x_target()
278 /// in pixels from left of screen, set to current position if unset
280 /// set the targetX to x
281 void setTargetX(int x);
282 /// return targetX or -1 if unset
283 int x_target() const;
284 /// set targetX to current position
286 /// clear targetX, i.e. set it to -1
288 /// set offset to actual position - targetX
289 void updateTextTargetOffset();
290 /// distance between actual and targeted position during last up/down in text
291 int textTargetOffset() const;
293 /// access to normalized selection anchor
294 CursorSlice normalAnchor() const;
295 /// access to real selection anchor
296 DocIterator const & realAnchor() const { return anchor_; }
297 DocIterator & realAnchor() { return anchor_; }
298 /// sets anchor to cursor position
300 /// access to owning BufferView
301 BufferView & bv() const;
302 /// get some interesting description of top position
303 void info(odocstream & os) const;
304 /// are we in math mode (2), text mode (1) or unsure (0)?
306 /// reset cursor bottom to the beginning of the top inset
307 // (sort of 'chroot' environment...)
309 /// for spellchecking
310 void replaceWord(std::string const & replacestring);
312 * the event was not (yet) dispatched.
314 * Should only be called by an inset's doDispatch() method. It means:
315 * I, the doDispatch() method of InsetFoo, hereby declare that I am
316 * not able to handle that request and trust my parent will do the
317 * Right Thing (even if my getStatus partner said that I can do it).
318 * It is sort of a kludge that should be used only rarely...
320 void undispatched() const;
321 /// the event was already dispatched
322 void dispatched() const;
323 /// Set which screen update should be done
324 void screenUpdateFlags(Update::flags f) const;
325 /// Forces an updateBuffer() call
326 void forceBufferUpdate() const;
327 /// Removes any pending updateBuffer() call
328 void clearBufferUpdate() const;
329 /// Do we need to call updateBuffer()?
330 bool needBufferUpdate() const;
332 * don't call update() when done
334 * Should only be called by an inset's doDispatch() method. It means:
335 * I handled that request and I can reassure you that the screen does
336 * not need to be re-drawn and all entries in the coord cache stay
337 * valid (and there are no other things to put in the coord cache).
338 * This is a fairly rare event as well and only some optimization.
339 * Not using noScreenUpdate() should never be wrong.
341 void noScreenUpdate() const;
342 /// fix cursor in circumstances that should never happen.
343 /// \retval true if a fix occurred.
345 /// Repopulate the slices insets from bottom to top. Useful
346 /// for stable iterators or Undo data.
354 /// makes sure the next operation will be stored
355 void finishUndo() const;
357 /// open a new group of undo operations. Groups can be nested.
358 void beginUndoGroup() const;
360 /// end the current undo group
361 void endUndoGroup() const;
363 /// The general case: prepare undo for an arbitrary range.
364 void recordUndo(pit_type from, pit_type to) const;
366 /// Convenience: prepare undo for the range between 'from' and cursor.
367 void recordUndo(pit_type from) const;
369 /// Convenience: prepare undo for the single paragraph or cell
370 /// containing the cursor
371 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
373 /// Convenience: prepare undo for the inset containing the cursor
374 void recordUndoInset(Inset const * inset = 0) const;
376 /// Convenience: prepare undo for the whole buffer
377 void recordUndoFullBuffer() const;
379 /// Convenience: prepare undo for buffer parameters
380 void recordUndoBufferParams() const;
382 /// Convenience: prepare undo for the selected paragraphs or cells
383 void recordUndoSelection() const;
386 void checkBufferStructure();
388 /// hook for text input to maintain the "new born word"
389 void markNewWordPosition();
391 /// The position of the new born word
392 /// As the user is entering a word without leaving it
393 /// the result is not empty. When not in text mode
394 /// and after leaving the word the result is empty.
395 DocIterator newWord() const { return new_word_; }
397 /// Return true if the next or previous inset has confirmDeletion depending
398 /// on the boolean before. If there is a selection, return true if at least
399 /// one inset in the selection has confirmDeletion.
400 bool confirmDeletion(bool before = false) const;
402 /// Determine if x falls to the left or to the side of the middle of the
403 /// inset, and advance the cursor to match this position. If edit is true,
404 /// keep the cursor in front of the inset if it matter for dialogs.
405 /// Note: it does not handle RTL text yet, and is only used in math for now.
406 void moveToClosestEdge(int x, bool edit = false);
412 DocIterator const & beforeDispatchCursor() const { return beforeDispatchCursor_; }
414 void saveBeforeDispatchPosXY();
417 /// validate the "new born word" position
418 void checkNewWordPosition();
419 /// clear the "new born word" position
420 void clearNewWordPosition();
426 mutable DispatchResult disp_;
428 * The target x position of the cursor. This is used for when
429 * we have text like :
431 * blah blah blah blah| blah blah blah
433 * blah blah blah blah blah blah
435 * When we move onto row 3, we would like to be vertically aligned
436 * with where we were in row 1, despite the fact that row 2 is
440 /// if a x_target cannot be hit exactly in a text, put the difference here
441 int textTargetOffset_;
442 /// the start of the new born word
443 DocIterator new_word_;
444 /// position before dispatch started
445 DocIterator beforeDispatchCursor_;
446 /// cursor screen coordinates before dispatch started
447 int beforeDispatchPosX_;
448 int beforeDispatchPosY_;
450 ///////////////////////////////////////////////////////////////////
452 // The part below is the non-integrated rest of the original math
453 // cursor. This should be either generalized for texted or moved
454 // back to the math insets.
456 ///////////////////////////////////////////////////////////////////
460 void insert(MathAtom const &);
462 void insert(MathData const &);
463 /// return false for empty math insets
464 /// Use force to skip the confirmDeletion check.
465 bool erase(bool force = false);
466 bool backspace(bool force = false);
468 /// move the cursor up by sending an internal LFUN_UP
469 /// return true if fullscreen update is needed
471 /// move the cursor up by sending an internal LFUN_DOWN,
472 /// return true if fullscreen update is needed
474 /// whether the cursor is either at the first or last row
475 bool atFirstOrLastRow(bool up);
476 /// move up/down in a text inset, called for LFUN_UP/DOWN,
477 /// return true if successful, updateNeeded set to true if fullscreen
478 /// update is needed, otherwise it's not touched
479 bool upDownInText(bool up, bool & updateNeeded);
480 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
481 /// return true if successful
482 bool upDownInMath(bool up);
484 InsetMath & nextMath();
486 InsetMath & prevMath();
487 /// move forward in math. word: whether to skip a whole "word" (insets with
488 /// the same mathclass)
489 bool mathForward(bool word);
491 bool mathBackward(bool word);
495 void plainInsert(MathAtom const & at);
497 void niceInsert(MathAtom const & at);
498 /// return the number of inserted array items
499 int niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL,
502 /// in pixels from top of screen
503 void setScreenPos(int x, int y);
504 /// current offset in the top cell
506 /// interpret name a name of a macro. Returns true if
507 /// something got inserted.
508 bool macroModeClose();
509 /// are we currently typing the name of a macro?
510 bool inMacroMode() const;
511 /// get access to the macro we are currently typing
512 InsetMathUnknown * activeMacro();
513 /// get access to the macro we are currently typing
514 InsetMathUnknown const * activeMacro() const;
516 /// replace selected stuff with at, placing the former
517 // selection in given cell of atom
518 void handleNest(MathAtom const & at, int cell = 0);
520 bool isInside(Inset const *) const;
522 /// make sure cursor position is valid
523 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
525 /// mark current cursor trace for redraw
528 /// hack for reveal codes
531 /// injects content of a cell into parent
533 /// split font inset etc
534 void handleFont(std::string const & font);
536 /// display a message
537 void message(docstring const & msg) const;
538 /// display an error message
539 void errorMessage(docstring const & msg) const;
541 docstring getPossibleLabel() const;
543 /// the name of the macro we are currently inputting
544 docstring macroName();
545 /// where in the curent cell does the macro name start?
547 /// can we enter the inset?
548 bool openable(MathAtom const &) const;
550 Encoding const * getEncoding() const;
551 /// font at cursor position
552 Font getFont() const;
557 * Notifies all insets which appear in \c old, but not in \c cur. And then
558 * notify all insets which appear in \c cur, but not in \c old.
559 * \returns true if cursor is now invalid, e.g. if some insets in
560 * higher cursor slices of \c old do not exist anymore. In this case
561 * it may be necessary to use Use Cursor::fixIfBroken.
563 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
568 #endif // LYXLCURSOR_H