LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / Cursor.h
blob9b1b1b681d0d443e5e6100bbce8cee0464747b91
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);
67 ///
68 void setCurrentFont();
71 // selection
73 /// selection active?
74 bool selection() const { return selection_; }
75 /// set selection;
76 void setSelection(bool sel) { selection_ = sel; }
77 /// do we have a multicell selection?
78 bool selIsMultiCell() const
79 { return selection_ && selBegin().idx() != selEnd().idx(); }
80 /// do we have a multiline selection?
81 bool selIsMultiLine() const
82 { return selection_ && selBegin().pit() != selEnd().pit(); }
83 /// did we place the anchor?
84 bool mark() const { return mark_; }
85 /// did we place the anchor?
86 void setMark(bool mark) { mark_ = mark; }
87 ///
88 void setSelection();
89 /// set selection at given position
90 void setSelection(DocIterator const & where, int n);
91 ///
92 void clearSelection();
93 /// access start of selection
94 CursorSlice selBegin() const;
95 /// access end of selection
96 CursorSlice selEnd() const;
97 /// access start of selection
98 DocIterator selectionBegin() const;
99 /// access start of selection
100 DocIterator selectionEnd() const;
102 * Update the selection status and save permanent
103 * selection if needed.
104 * @param selecting the new selection status
105 * @return whether the selection status has changed
107 bool selHandle(bool selecting);
109 docstring selectionAsString(bool label) const;
111 docstring currentState() const;
113 /// auto-correct mode
114 bool autocorrect() const { return autocorrect_; }
115 /// auto-correct mode
116 bool & autocorrect() { return autocorrect_; }
117 /// are we entering a macro name?
118 bool macromode() const { return macromode_; }
119 /// are we entering a macro name?
120 bool & macromode() { return macromode_; }
121 /// returns x,y position
122 void getPos(int & x, int & y) const;
123 /// return logical positions between which the cursor is situated
125 * If the cursor is at the edge of a row, the position which is "over the
126 * edge" will be returned as -1.
128 void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
129 /// the row in the paragraph we're in
130 Row const & textRow() const;
133 // common part
135 /// move one step backwards
136 bool posBackward();
137 /// move one step forward
138 bool posForward();
139 /// move visually one step to the right
141 * @note: This method may move into an inset unless skip_inset == true.
142 * @note: This method may move into a new paragraph.
143 * @note: This method may move out of the current slice.
144 * @return: true if moved, false if not moved
146 bool posVisRight(bool skip_inset = false);
147 /// move visually one step to the left
149 * @note: This method may move into an inset unless skip_inset == true.
150 * @note: This method may move into a new paragraph.
151 * @note: This method may move out of the current slice.
152 * @return: true if moved, false if not moved
154 bool posVisLeft(bool skip_inset = false);
155 /// move visually to next/previous row
157 * Assuming we were to keep moving left (right) from the current cursor
158 * position, place the cursor at the rightmost (leftmost) edge of the
159 * new row to which we would move according to visual-mode cursor movement.
160 * This could be either the next or the previous row, depending on the
161 * direction in which we're moving, and whether we're in an LTR or RTL
162 * paragraph.
163 * @note: The new position may even be in a new paragraph.
164 * @note: This method will not move out of the current slice.
165 * @return: false if not moved (no more rows to move to in given direction)
166 * @return: true if moved
168 bool posVisToNewRow(bool movingLeft);
169 /// move to right or left extremity of the current row
170 void posVisToRowExtremity(bool left);
172 /// insert an inset
173 void insert(Inset *);
174 /// insert a single char
175 void insert(char_type c);
176 /// insert a string
177 void insert(docstring const & str);
179 /// FIXME: rename to something sensible showing difference to x_target()
180 /// in pixels from left of screen, set to current position if unset
181 int targetX() const;
182 /// set the targetX to x
183 void setTargetX(int x);
184 /// return targetX or -1 if unset
185 int x_target() const;
186 /// set targetX to current position
187 void setTargetX();
188 /// clear targetX, i.e. set it to -1
189 void clearTargetX();
190 /// set offset to actual position - targetX
191 void updateTextTargetOffset();
192 /// distance between actual and targeted position during last up/down in text
193 int textTargetOffset() const;
195 /// access to normalized selection anchor
196 CursorSlice anchor() const;
197 /// sets anchor to cursor position
198 void resetAnchor();
199 /// access to owning BufferView
200 BufferView & bv() const;
201 /// access to owning Buffer
202 Buffer & buffer() 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 /// move up/down in a text inset, called for LFUN_UP/DOWN,
363 /// return true if successful, updateNeeded set to true if fullscreen
364 /// update is needed, otherwise it's not touched
365 bool upDownInText(bool up, bool & updateNeeded);
366 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
367 /// return true if successful
368 bool upDownInMath(bool up);
370 void plainErase();
372 void plainInsert(MathAtom const & at);
374 void niceInsert(MathAtom const & at);
376 void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL);
378 /// in pixels from top of screen
379 void setScreenPos(int x, int y);
380 /// current offset in the top cell
382 /// interpret name a name of a macro. Returns true if
383 /// something got inserted.
384 bool macroModeClose();
385 /// are we currently typing the name of a macro?
386 bool inMacroMode() const;
387 /// get access to the macro we are currently typing
388 InsetMathUnknown * activeMacro();
389 /// get access to the macro we are currently typing
390 InsetMathUnknown const * activeMacro() const;
392 /// replace selected stuff with at, placing the former
393 // selection in given cell of atom
394 void handleNest(MathAtom const & at, int cell = 0);
396 bool isInside(Inset const *) const;
398 /// make sure cursor position is valid
399 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
400 void normalize();
401 /// mark current cursor trace for redraw
402 void touch();
404 /// hack for reveal codes
405 void markInsert();
406 void markErase();
407 /// injects content of a cell into parent
408 void pullArg();
409 /// split font inset etc
410 void handleFont(std::string const & font);
412 /// display a message
413 void message(docstring const & msg) const;
414 /// display an error message
415 void errorMessage(docstring const & msg) const;
417 docstring getPossibleLabel() const;
419 /// the name of the macro we are currently inputting
420 docstring macroName();
421 /// where in the curent cell does the macro name start?
422 int macroNamePos();
423 /// can we enter the inset?
424 bool openable(MathAtom const &) const;
426 Encoding const * getEncoding() const;
427 /// font at cursor position
428 Font getFont() const;
433 * Notifies all insets which appear in old, but not in cur. Make
434 * Sure that the cursor old is valid, i.e. all inset pointers
435 * point to valid insets! Use Cursor::fixIfBroken if necessary.
437 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
440 } // namespace lyx
442 #endif // LYXLCURSOR_H