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 on left end
52 void pushLeft(Inset & inset);
53 /// pop one level off the cursor
55 /// pop one slice off the cursor stack and go left
57 /// pop one slice off the cursor stack and go right
59 /// make sure we are outside of given inset
60 void leaveInset(Inset const & inset);
62 void setCursor(DocIterator const & it);
68 bool selection() const { return selection_; }
70 bool & selection() { return selection_; }
71 /// did we place the anchor?
72 bool mark() const { return mark_; }
73 /// did we place the anchor?
74 bool & mark() { return mark_; }
77 /// set selection at given position
78 void setSelection(DocIterator const & where, int n);
80 void clearSelection();
81 /// access start of selection
82 CursorSlice selBegin() const;
83 /// access end of selection
84 CursorSlice selEnd() const;
85 /// access start of selection
86 DocIterator selectionBegin() const;
87 /// access start of selection
88 DocIterator selectionEnd() const;
89 /// FIXME: document this
90 bool selHandle(bool selecting);
92 docstring selectionAsString(bool label) const;
94 docstring currentState();
97 bool autocorrect() const { return autocorrect_; }
99 bool & autocorrect() { return autocorrect_; }
100 /// are we entering a macro name?
101 bool macromode() const { return macromode_; }
102 /// are we entering a macro name?
103 bool & macromode() { return macromode_; }
104 /// returns x,y position
105 void getPos(int & x, int & y) const;
106 /// the row in the paragraph we're in
107 Row const & textRow() const;
112 /// move one step to the left
114 /// move one step to the right
118 void insert(Inset *);
119 /// insert a single char
120 void insert(char_type c);
122 void insert(docstring const & str);
124 /// FIXME: rename to something sensible showing difference to x_target()
125 /// in pixels from left of screen, set to current position if unset
127 /// set the targetX to x
128 void setTargetX(int x);
129 /// return targetX or -1 if unset
130 int x_target() const;
131 /// set targetX to current position
133 /// clear targetX, i.e. set it to -1
135 /// set offset to actual position - targetX
136 void updateTextTargetOffset();
137 /// distance between actual and targeted position during last up/down in text
138 int textTargetOffset() const;
140 /// access to normalized selection anchor
141 CursorSlice anchor() const;
142 /// sets anchor to cursor position
144 /// access to owning BufferView
145 BufferView & bv() const;
146 /// access to owning Buffer
147 Buffer & buffer() const;
148 /// get some interesting description of top position
149 void info(odocstream & os) const;
150 /// are we in math mode (2), text mode (1) or unsure (0)?
152 /// reset cursor bottom to the beginning of the given inset
153 // (sort of 'chroot' environment...)
155 /// for spellchecking
156 void replaceWord(std::string const & replacestring);
158 * the event was not (yet) dispatched.
160 * Should only be called by an inset's doDispatch() method. It means:
161 * I, the doDispatch() method of InsetFoo, hereby declare that I am
162 * not able to handle that request and trust my parent will do the
163 * Right Thing (even if my getStatus partner said that I can do it).
164 * It is sort of a kludge that should be used only rarely...
167 /// the event was already dispatched
169 /// Set which update should be done
170 void updateFlags(Update::flags f);
172 * don't call update() when done
174 * Should only be called by an inset's doDispatch() method. It means:
175 * I handled that request and I can reassure you that the screen does
176 * not need to be re-drawn and all entries in the coord cache stay
177 * valid (and there are no other things to put in the coord cache).
178 * This is a fairly rare event as well and only some optimization.
179 * Not using noUpdate() should never be wrong.
182 /// fix cursor in circumstances that should never happen
186 friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
192 /// the anchor position
196 DispatchResult disp_;
198 DocIterator const & beforeDispatchCursor() { return beforeDispatchCursor_; }
202 * The target x position of the cursor. This is used for when
203 * we have text like :
205 * blah blah blah blah| blah blah blah
207 * blah blah blah blah blah blah
209 * When we move onto row 3, we would like to be vertically aligned
210 * with where we were in row 1, despite the fact that row 2 is
214 /// if a x_target cannot be hit exactly in a text, put the difference here
215 int textTargetOffset_;
216 /// do we have a selection?
218 /// are we on the way to get one?
220 /// If true, we are behind the previous char, otherwise we are in front
221 // of the next char. This only make a difference when we are in front
222 // of a big inset spanning a whole row and computing coordinates for
223 // displaying the cursor.
225 /// x position before dispatch started
227 /// y position before dispatch started
229 /// position before dispatch started
230 DocIterator beforeDispatchCursor_;
235 // math specific stuff that could be promoted to "global" later
237 /// do we allow autocorrection
239 /// are we entering a macro name?
243 ///////////////////////////////////////////////////////////////////
245 // The part below is the non-integrated rest of the original math
246 // cursor. This should be either generalized for texted or moved
247 // back to the math insets.
249 ///////////////////////////////////////////////////////////////////
253 void insert(MathAtom const &);
255 void insert(MathData const &);
256 /// return false for empty math insets
258 /// return false for empty math insets
260 /// move the cursor up by sending an internal LFUN_UP
261 /// return true if fullscreen update is needed
263 /// move the cursor up by sending an internal LFUN_DOWN,
264 /// return true if fullscreen update is needed
266 /// move up/down in a text inset, called for LFUN_UP/DOWN,
267 /// return true if successful, updateNeeded set to true if fullscreen
268 /// update is needed, otherwise it's not touched
269 bool upDownInText(bool up, bool & updateNeeded);
270 /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
271 /// return true if successful
272 bool upDownInMath(bool up);
276 void plainInsert(MathAtom const & at);
278 void niceInsert(MathAtom const & at);
280 void niceInsert(docstring const & str);
282 /// in pixels from top of screen
283 void setScreenPos(int x, int y);
284 /// current offset in the top cell
286 /// interpret name a name of a macro. Returns true if
287 /// something got inserted.
288 bool macroModeClose();
289 /// are we currently typing the name of a macro?
290 bool inMacroMode() const;
291 /// get access to the macro we are currently typing
292 InsetMathUnknown * activeMacro();
294 /// replace selected stuff with at, placing the former
295 // selection in given cell of atom
296 void handleNest(MathAtom const & at, int cell = 0);
298 bool isInside(Inset const *);
300 /// make sure cursor position is valid
301 /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
303 /// mark current cursor trace for redraw
306 /// hack for reveal codes
309 /// injects content of a cell into parent
311 /// split font inset etc
312 void handleFont(std::string const & font);
314 /// display a message
315 void message(docstring const & msg) const;
316 /// display an error message
317 void errorMessage(docstring const & msg) const;
319 docstring getPossibleLabel();
321 /// the name of the macro we are currently inputting
322 docstring macroName();
323 /// where in the curent cell does the macro name start?
325 /// can we enter the inset?
326 bool openable(MathAtom const &) const;
328 Encoding const * getEncoding() const;
329 /// font at cursor position
330 Font getFont() const;
335 * Notifies all insets which appear in old, but not in cur. Make
336 * Sure that the cursor old is valid, i.e. als inset pointer
337 * point to valid insets! Use Cursor::fixIfBroken if necessary.
339 bool notifyCursorLeaves(DocIterator const & old, Cursor & cur);
344 #endif // LYXLCURSOR_H