* qt_helpers.cpp:
[lyx.git] / src / Cursor.h
blob81b2d9b97e472df9ba9c64c6b3f72127947ef2e6
1 // -*- C++ -*-
2 /**
3 * \file Cursor.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author André Pönitz
9 * Full author contact details are available in file CREDITS.
12 #ifndef LCURSOR_H
13 #define LCURSOR_H
15 #include "DispatchResult.h"
16 #include "DocIterator.h"
17 #include "Font.h"
18 #include "Undo.h"
20 #include "mathed/MathParser_flags.h"
22 #include <vector>
25 namespace lyx {
27 class Buffer;
28 class BufferView;
29 class FuncStatus;
30 class FuncRequest;
31 class Row;
33 // these should go
34 class InsetMathUnknown;
35 class Encoding;
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
44 public:
45 /// create the cursor of a BufferView
46 explicit Cursor(BufferView & bv);
48 /// dispatch from innermost inset upwards
49 void dispatch(FuncRequest const & cmd);
50 /// get the resut of the last dispatch
51 DispatchResult result() const;
52 /// add a new cursor slice
53 void push(Inset & inset);
54 /// add a new cursor slice, place cursor at front (move backwards)
55 void pushBackward(Inset & inset);
56 /// pop one level off the cursor
57 void pop();
58 /// pop one slice off the cursor stack and go backwards
59 bool popBackward();
60 /// pop one slice off the cursor stack and go forward
61 bool popForward();
62 /// make sure we are outside of given inset
63 void leaveInset(Inset const & inset);
64 /// sets cursor part
65 void setCursor(DocIterator const & it);
66 /// sets the cursor to the anchor
67 void setCursorToAnchor();
69 ///
70 void setCurrentFont();
73 // selection
75 /// selection active?
76 bool selection() const { return selection_; }
77 /// set selection;
78 void setSelection(bool sel) { selection_ = sel; }
79 /// do we have a multicell selection?
80 bool selIsMultiCell() const
81 { return selection_ && selBegin().idx() != selEnd().idx(); }
82 /// do we have a multiline selection?
83 bool selIsMultiLine() const
84 { return selection_ && selBegin().pit() != selEnd().pit(); }
85 /// did we place the anchor?
86 bool mark() const { return mark_; }
87 /// did we place the anchor?
88 void setMark(bool mark) { mark_ = mark; }
89 ///
90 void setSelection();
91 /// set selection at given position
92 void setSelection(DocIterator const & where, int n);
93 ///
94 void clearSelection();
95 /// access start of selection
96 CursorSlice selBegin() const;
97 /// access end of selection
98 CursorSlice selEnd() const;
99 /// access start of selection
100 DocIterator selectionBegin() const;
101 /// access start of selection
102 DocIterator selectionEnd() const;
104 * Update the selection status and save permanent
105 * selection if needed.
106 * @param selecting the new selection status
107 * @return whether the selection status has changed
109 bool selHandle(bool selecting);
111 docstring selectionAsString(bool label) const;
113 docstring currentState() const;
115 /// auto-correct mode
116 bool autocorrect() const { return autocorrect_; }
117 /// auto-correct mode
118 bool & autocorrect() { return autocorrect_; }
119 /// are we entering a macro name?
120 bool macromode() const { return macromode_; }
121 /// are we entering a macro name?
122 bool & macromode() { return macromode_; }
123 /// returns x,y position
124 void getPos(int & x, int & y) const;
125 /// return logical positions between which the cursor is situated
127 * If the cursor is at the edge of a row, the position which is "over the
128 * edge" will be returned as -1.
130 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
131 /// the row in the paragraph we're in
132 Row const & textRow() const;
135 // common part
137 /// move one step backwards
138 bool posBackward();
139 /// move one step forward
140 bool posForward();
141 /// move visually one step to the right
143 * @note: This method may move into an inset unless skip_inset == true.
144 * @note: This method may move into a new paragraph.
145 * @note: This method may move out of the current slice.
146 * @return: true if moved, false if not moved
148 bool posVisRight(bool skip_inset = false);
149 /// move visually one step to the left
151 * @note: This method may move into an inset unless skip_inset == true.
152 * @note: This method may move into a new paragraph.
153 * @note: This method may move out of the current slice.
154 * @return: true if moved, false if not moved
156 bool posVisLeft(bool skip_inset = false);
157 /// move visually to next/previous row
159 * Assuming we were to keep moving left (right) from the current cursor
160 * position, place the cursor at the rightmost (leftmost) edge of the
161 * new row to which we would move according to visual-mode cursor movement.
162 * This could be either the next or the previous row, depending on the
163 * direction in which we're moving, and whether we're in an LTR or RTL
164 * paragraph.
165 * @note: The new position may even be in a new paragraph.
166 * @note: This method will not move out of the current slice.
167 * @return: false if not moved (no more rows to move to in given direction)
168 * @return: true if moved
170 bool posVisToNewRow(bool movingLeft);
171 /// move to right or left extremity of the current row
172 void posVisToRowExtremity(bool left);
174 /// insert an inset
175 void insert(Inset *);
176 /// insert a single char
177 void insert(char_type c);
178 /// insert a string
179 void insert(docstring const & str);
181 /// FIXME: rename to something sensible showing difference to x_target()
182 /// in pixels from left of screen, set to current position if unset
183 int targetX() const;
184 /// set the targetX to x
185 void setTargetX(int x);
186 /// return targetX or -1 if unset
187 int x_target() const;
188 /// set targetX to current position
189 void setTargetX();
190 /// clear targetX, i.e. set it to -1
191 void clearTargetX();
192 /// set offset to actual position - targetX
193 void updateTextTargetOffset();
194 /// distance between actual and targeted position during last up/down in text
195 int textTargetOffset() const;
197 /// access to normalized selection anchor
198 CursorSlice anchor() const;
199 /// sets anchor to cursor position
200 void resetAnchor();
201 /// access to owning BufferView
202 BufferView & bv() const;
203 /// get some interesting description of top position
204 void info(odocstream & os) const;
205 /// are we in math mode (2), text mode (1) or unsure (0)?
206 int currentMode();
207 /// reset cursor bottom to the beginning of the given inset
208 // (sort of 'chroot' environment...)
209 void reset(Inset &);
210 /// for spellchecking
211 void replaceWord(std::string const & replacestring);
213 * the event was not (yet) dispatched.
215 * Should only be called by an inset's doDispatch() method. It means:
216 * I, the doDispatch() method of InsetFoo, hereby declare that I am
217 * not able to handle that request and trust my parent will do the
218 * Right Thing (even if my getStatus partner said that I can do it).
219 * It is sort of a kludge that should be used only rarely...
221 void undispatched();
222 /// the event was already dispatched
223 void dispatched();
224 /// Set which update should be done
225 void updateFlags(Update::flags f);
227 * don't call update() when done
229 * Should only be called by an inset's doDispatch() method. It means:
230 * I handled that request and I can reassure you that the screen does
231 * not need to be re-drawn and all entries in the coord cache stay
232 * valid (and there are no other things to put in the coord cache).
233 * This is a fairly rare event as well and only some optimization.
234 * Not using noUpdate() should never be wrong.
236 void noUpdate();
237 /// fix cursor in circumstances that should never happen.
238 /// \retval true if a fix occured.
239 bool fixIfBroken();
241 /// output
242 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
243 friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
246 bool textUndo();
248 bool textRedo();
250 /// makes sure the next operation will be stored
251 void finishUndo() const;
253 /// open a new group of undo operations. Groups can be nested.
254 void beginUndoGroup() const;
256 /// end the current undo group
257 void endUndoGroup() const;
259 /// The general case: prepare undo for an arbitrary range.
260 void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
262 /// Convenience: prepare undo for the range between 'from' and cursor.
263 void recordUndo(UndoKind kind, pit_type from) const;
265 /// Convenience: prepare undo for the single paragraph or cell
266 /// containing the cursor
267 void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
269 /// Convenience: prepare undo for the inset containing the cursor
270 void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
272 /// Convenience: prepare undo for the whole buffer
273 void recordUndoFullDocument() const;
275 /// Convenience: prepare undo for the selected paragraphs or cells
276 void recordUndoSelection() const;
279 void checkBufferStructure();
281 public:
283 BufferView * bv_;
284 //private:
285 /// the anchor position
286 DocIterator anchor_;
289 DispatchResult disp_;
291 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
293 private:
295 * The target x position of the cursor. This is used for when
296 * we have text like :
298 * blah blah blah blah| blah blah blah
299 * blah blah blah
300 * blah blah blah blah blah blah
302 * When we move onto row 3, we would like to be vertically aligned
303 * with where we were in row 1, despite the fact that row 2 is
304 * shorter than x()
306 int x_target_;
307 /// if a x_target cannot be hit exactly in a text, put the difference here
308 int textTargetOffset_;
309 /// do we have a selection?
310 bool selection_;
311 /// are we on the way to get one?
312 bool mark_;
313 /// If true, we are behind the previous char, otherwise we are in front
314 // of the next char. This only make a difference when we are in front
315 // of a big inset spanning a whole row and computing coordinates for
316 // displaying the cursor.
317 bool logicalpos_;
318 /// position before dispatch started
319 DocIterator beforeDispatchCursor_;
321 // FIXME: make them private.
322 public:
323 /// the current font settings
324 Font current_font;
325 /// the current font
326 Font real_current_font;
328 private:
331 // math specific stuff that could be promoted to "global" later
333 /// do we allow autocorrection
334 bool autocorrect_;
335 /// are we entering a macro name?
336 bool macromode_;
339 ///////////////////////////////////////////////////////////////////
341 // The part below is the non-integrated rest of the original math
342 // cursor. This should be either generalized for texted or moved
343 // back to the math insets.
345 ///////////////////////////////////////////////////////////////////
347 public:
349 void insert(MathAtom const &);
351 void insert(MathData const &);
352 /// return false for empty math insets
353 bool erase();
354 /// return false for empty math insets
355 bool backspace();
356 /// move the cursor up by sending an internal LFUN_UP
357 /// return true if fullscreen update is needed
358 bool up();
359 /// move the cursor up by sending an internal LFUN_DOWN,
360 /// return true if fullscreen update is needed
361 bool down();
362 /// whether the cursor is either at the first or last row
363 bool atFirstOrLastRow(bool up);
364 /// move up/down in a text inset, called for LFUN_UP/DOWN,
365 /// return true if successful, updateNeeded set to true if fullscreen
366 /// update is needed, otherwise it's not touched
367 bool upDownInText(bool up, bool & updateNeeded);
368 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
369 /// return true if successful
370 bool upDownInMath(bool up);
372 void plainErase();
374 void plainInsert(MathAtom const & at);
376 void niceInsert(MathAtom const & at);
378 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL);
380 /// in pixels from top of screen
381 void setScreenPos(int x, int y);
382 /// current offset in the top cell
384 /// interpret name a name of a macro. Returns true if
385 /// something got inserted.
386 bool macroModeClose();
387 /// are we currently typing the name of a macro?
388 bool inMacroMode() const;
389 /// get access to the macro we are currently typing
390 InsetMathUnknown * activeMacro();
391 /// get access to the macro we are currently typing
392 InsetMathUnknown const * activeMacro() const;
394 /// replace selected stuff with at, placing the former
395 // selection in given cell of atom
396 void handleNest(MathAtom const & at, int cell = 0);
398 bool isInside(Inset const *) const;
400 /// make sure cursor position is valid
401 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
402 void normalize();
403 /// mark current cursor trace for redraw
404 void touch();
406 /// hack for reveal codes
407 void markInsert();
408 void markErase();
409 /// injects content of a cell into parent
410 void pullArg();
411 /// split font inset etc
412 void handleFont(std::string const & font);
414 /// display a message
415 void message(docstring const & msg) const;
416 /// display an error message
417 void errorMessage(docstring const & msg) const;
419 docstring getPossibleLabel() const;
421 /// the name of the macro we are currently inputting
422 docstring macroName();
423 /// where in the curent cell does the macro name start?
424 int macroNamePos();
425 /// can we enter the inset?
426 bool openable(MathAtom const &) const;
428 Encoding const * getEncoding() const;
429 /// font at cursor position
430 Font getFont() const;
435 * Notifies all insets which appear in old, but not in cur. And then
436 * notify all insets which appear in cur, but not in old.
437 * Make sure that the cursor old is valid, i.e. all inset pointers
438 * point to valid insets! Use Cursor::fixIfBroken if necessary.
440 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
443 } // namespace lyx
445 #endif // LYXLCURSOR_H