]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
* Only enter inset which return true on isActive(). This is the behavior in the curso...
[lyx.git] / src / Cursor.h
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.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LCURSOR_H
13 #define LCURSOR_H
14
15 #include "DispatchResult.h"
16 #include "DocIterator.h"
17
18 #include <iosfwd>
19 #include <vector>
20
21
22 namespace lyx {
23
24 class Buffer;
25 class BufferView;
26 class FuncStatus;
27 class FuncRequest;
28 class Font;
29 class Row;
30
31 // these should go
32 class InsetMathUnknown;
33 class Encoding;
34
35
36 /// The cursor class describes the position of a cursor within a document.
37
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 {
41 public:
42         /// create the cursor of a BufferView
43         explicit Cursor(BufferView & bv);
44
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
54         void pop();
55         /// pop one slice off the cursor stack and go left
56         bool popLeft();
57         /// pop one slice off the cursor stack and go right
58         bool popRight();
59         /// make sure we are outside of given inset
60         void leaveInset(Inset const & inset);
61         /// sets cursor part
62         void setCursor(DocIterator const & it);
63
64         //
65         // selection
66         //
67         /// selection active?
68         bool selection() const { return selection_; }
69         /// selection active?
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_; }
75         ///
76         void setSelection();
77         /// set selection at given position
78         void setSelection(DocIterator const & where, int n);
79         ///
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         ///
90         bool selHandle(bool selecting);
91         //
92         docstring selectionAsString(bool label) const;
93         ///
94         docstring currentState();
95
96         /// auto-correct mode
97         bool autocorrect() const { return autocorrect_; }
98         /// auto-correct mode
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;
108
109         //
110         // common part
111         //
112         /// move one step to the left
113         bool posLeft();
114         /// move one step to the right
115         bool posRight();
116
117         /// insert an inset
118         void insert(Inset *);
119         /// insert a single char
120         void insert(char_type c);
121         /// insert a string
122         void insert(docstring const & str);
123
124         /// FIXME: rename to something sensible showing difference to x_target()
125         /// in pixels from left of screen, set to current position if unset
126         int targetX() const;
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
132         void setTargetX();
133         /// clear targetX, i.e. set it to -1
134         void clearTargetX();
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;
139
140         /// access to normalized selection anchor
141         CursorSlice anchor() const;
142         /// sets anchor to cursor position
143         void resetAnchor();
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)?
151         int currentMode();
152         /// reset cursor bottom to the beginning of the given inset
153         // (sort of 'chroot' environment...)
154         void reset(Inset &);
155         /// for spellchecking
156         void replaceWord(std::string const & replacestring);
157         /**
158          * the event was not (yet) dispatched.
159          *
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...
165          */
166         void undispatched();
167         /// the event was already dispatched
168         void dispatched();
169         /// Set which update should be done
170         void updateFlags(Update::flags f);
171         /**
172          * don't call update() when done
173          *
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.
180          */
181         void noUpdate();
182         /// fix cursor in circumstances that should never happen
183         void fixIfBroken();
184
185         /// output
186         friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
187
188 public:
189         ///
190         BufferView * bv_;
191 //private:
192         /// the anchor position
193         DocIterator anchor_;
194         
195         ///
196         DispatchResult disp_;
197         
198 private:
199         /**
200          * The target x position of the cursor. This is used for when
201          * we have text like :
202          *
203          * blah blah blah blah| blah blah blah
204          * blah blah blah
205          * blah blah blah blah blah blah
206          *
207          * When we move onto row 3, we would like to be vertically aligned
208          * with where we were in row 1, despite the fact that row 2 is
209          * shorter than x()
210          */
211         int x_target_;
212         /// if a x_target cannot be hit exactly in a text, put the difference here
213         int textTargetOffset_;
214         /// do we have a selection?
215         bool selection_;
216         /// are we on the way to get one?
217         bool mark_;
218         /// If true, we are behind the previous char, otherwise we are in front
219         // of the next char. This only make a difference when we are in front
220         // of a big inset spanning a whole row and computing coordinates for
221         // displaying the cursor.
222         bool logicalpos_;
223         /// x position before dispatch started
224         int beforeDispX_;
225         /// y position before dispatch started
226         int beforeDispY_;
227         /// position before dispatch started
228         size_t beforeDispDepth_;
229                 
230 private:
231
232         //
233         // math specific stuff that could be promoted to "global" later
234         //
235         /// do we allow autocorrection
236         bool autocorrect_;
237         /// are we entering a macro name?
238         bool macromode_;
239
240
241 ///////////////////////////////////////////////////////////////////
242 //
243 // The part below is the non-integrated rest of the original math
244 // cursor. This should be either generalized for texted or moved
245 // back to the math insets.
246 //
247 ///////////////////////////////////////////////////////////////////
248
249 public:
250         ///
251         void insert(MathAtom const &);
252         ///
253         void insert(MathData const &);
254         /// return false for empty math insets
255         bool erase();
256         /// return false for empty math insets
257         bool backspace();
258         /// move the cursor up by sending an internal LFUN_UP
259         /// return true if fullscreen update is needed
260         bool up();
261         /// move the cursor up by sending an internal LFUN_DOWN,
262         /// return true if fullscreen update is needed
263         bool down();
264         /// move up/down in a text inset, called for LFUN_UP/DOWN,
265         /// return true if successful, updateNeeded set to true if fullscreen
266         /// update is needed, otherwise it's not touched
267         bool upDownInText(bool up, bool & updateNeeded);
268         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
269         /// return true if successful
270         bool upDownInMath(bool up);
271         ///
272         void plainErase();
273         ///
274         void plainInsert(MathAtom const & at);
275         ///
276         void niceInsert(MathAtom const & at);
277         ///
278         void niceInsert(docstring const & str);
279
280         /// in pixels from top of screen
281         void setScreenPos(int x, int y);
282         /// current offset in the top cell
283
284         /// interpret name a name of a macro. Returns true if
285         /// something got inserted.
286         bool macroModeClose();
287         /// are we currently typing the name of a macro?
288         bool inMacroMode() const;
289         /// get access to the macro we are currently typing
290         InsetMathUnknown * activeMacro();
291
292         /// replace selected stuff with at, placing the former
293         // selection in given cell of atom
294         void handleNest(MathAtom const & at, int cell = 0);
295         ///
296         bool isInside(Inset const *);
297
298         /// make sure cursor position is valid
299         void normalize();
300         /// mark current cursor trace for redraw
301         void touch();
302
303         /// hack for reveal codes
304         void markInsert();
305         void markErase();
306         /// injects content of a cell into parent
307         void pullArg();
308         /// split font inset etc
309         void handleFont(std::string const & font);
310
311         /// display a message
312         void message(docstring const & msg) const;
313         /// display an error message
314         void errorMessage(docstring const & msg) const;
315         ///
316         docstring getPossibleLabel();
317
318         /// the name of the macro we are currently inputting
319         docstring macroName();
320         /// where in the curent cell does the macro name start?
321         int macroNamePos();
322         /// can we enter the inset?
323         bool openable(MathAtom const &) const;
324         ///
325         Encoding const * getEncoding() const;
326         /// font at cursor position
327         Font getFont() const;
328 };
329
330
331
332 } // namespace lyx
333
334 #endif // LYXLCURSOR_H