]> git.lyx.org Git - lyx.git/blob - src/cursor.h
d02952f0af2c8cec78a886c812fc9a106c58b641
[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 CURSOR_H
13 #define CURSOR_H
14
15 #include "dispatchresult.h"
16 #include "dociterator.h"
17
18 #include <iosfwd>
19 #include <vector>
20
21 class Buffer;
22 class BufferView;
23 class FuncStatus;
24 class FuncRequest;
25 class Point;
26 class LyXFont;
27
28 // these should go
29 class MathUnknownInset;
30 class Encoding;
31
32
33 /// The cursor class describes the position of a cursor within a document.
34
35 // The public inheritance should go in favour of a suitable data member
36 // (or maybe private inheritance) at some point of time.
37 class LCursor : public DocIterator {
38 public:
39         /// create the cursor of a BufferView
40         explicit LCursor(BufferView & bv);
41
42         /// dispatch from innermost inset upwards
43         void dispatch(FuncRequest const & cmd);
44         /// get the resut of the last dispatch
45         DispatchResult result() const;
46         /// add a new cursor slice
47         void push(InsetBase & inset);
48         /// add a new cursor slice, place cursor on left end
49         void pushLeft(InsetBase & inset);
50         /// pop one level off the cursor
51         void pop();
52         /// pop one slice off the cursor stack and go left
53         bool popLeft();
54         /// pop one slice off the cursor stack and go right
55         bool popRight();
56         /// make sure we are outside of given inset
57         void leaveInset(InsetBase const & inset);
58         /// sets cursor part
59         void setCursor(DocIterator const & it);
60
61         //
62         // selection
63         //
64         /// selection active?
65         bool selection() const { return selection_; }
66         /// selection active?
67         bool & selection() { return selection_; }
68         /// did we place the anchor?
69         bool mark() const { return mark_; }
70         /// did we place the anchor?
71         bool & mark() { return mark_; }
72         ///
73         void setSelection();
74         /// set selection at given position
75         void setSelection(DocIterator const & where, size_t n);
76         ///
77         void clearSelection();
78         /// access start of selection
79         CursorSlice selBegin() const;
80         /// access end of selection
81         CursorSlice selEnd() const;
82         /// access start of selection
83         DocIterator selectionBegin() const;
84         /// access start of selection
85         DocIterator selectionEnd() const;
86         ///
87         void selHandle(bool selecting);
88         //
89         std::string selectionAsString(bool label) const;
90         ///
91         std::string currentState();
92
93         /// auto-correct mode
94         bool autocorrect() const { return autocorrect_; }
95         /// auto-correct mode
96         bool & autocorrect() { return autocorrect_; }
97         /// are we entering a macro name?
98         bool macromode() const { return macromode_; }
99         /// are we entering a macro name?
100         bool & macromode() { return macromode_; }
101         /// returns x,y position
102         void getPos(int & x, int & y) const;
103
104         //
105         // common part
106         //
107         /// move one step to the left
108         bool posLeft();
109         /// move one step to the right
110         bool posRight();
111
112         /// insert an inset
113         void insert(InsetBase *);
114         /// insert a single char
115         void insert(char c);
116         /// insert a string
117         void insert(std::string const & str);
118
119         /// in pixels from left of screen
120         int targetX() const;
121         /// write acess to target x position of cursor
122         int & x_target();
123         /// return target x position of cursor
124         int x_target() const;
125         /// set targetX in current position
126         void setTargetX();
127         /// clear target x position of cursor
128         void clearTargetX();
129
130         /// access to normalized selection anchor
131         CursorSlice anchor() const;
132         /// sets anchor to cursor position
133         void resetAnchor();
134         /// access to owning BufferView
135         BufferView & bv() const;
136         /// access to owning Buffer
137         Buffer & buffer() const;
138         /// get some interesting description of top position
139         void info(std::ostream & os) const;
140         /// are we in math mode (2), text mode (1) or unsure (0)?
141         int currentMode();
142         /// reset cursor bottom to the beginning of the given inset
143         // (sort of 'chroot' environment...)
144         void reset(InsetBase &);
145         /// for spellchecking
146         void replaceWord(std::string const & replacestring);
147         /**
148          * the event was not (yet) dispatched.
149          *
150          * Should only be called by an inset's doDispatch() method. It means:
151          * I, the doDispatch() method of InsetFoo, hereby declare that I am
152          * not able to handle that request and trust my parent will do the
153          * Right Thing (even if my getStatus partner said that I can do it).
154          * It is sort of a kludge that should be used only rarely...
155          */
156         void undispatched();
157         /// the event was already dispatched
158         void dispatched();
159         /// call update() when done
160         void needsUpdate();
161         /**
162          * don't call update() when done
163          *
164          * Should only be called by an inset's doDispatch() method. It means:
165          * I handled that request and I can reassure you that the screen does
166          * not need to be re-drawn and all entries in the coord cache stay
167          * valid (and there are no other things to put in the coord cache).
168          * This is a fairly rare event as well and only some optimization.
169          * Not using noUpdate() should never be wrong.
170          */
171         void noUpdate();
172         /// fix cursor in circumstances that should never happen
173         void fixIfBroken();
174
175         /// output
176         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
177
178 public:
179         ///
180         BufferView * bv_;
181 //private:
182         /// the anchor position
183         DocIterator anchor_;
184
185         ///
186         DispatchResult disp_;
187
188
189 private:
190         /**
191          * The target x position of the cursor. This is used for when
192          * we have text like :
193          *
194          * blah blah blah blah| blah blah blah
195          * blah blah blah
196          * blah blah blah blah blah blah
197          *
198          * When we move onto row 3, we would like to be vertically aligned
199          * with where we were in row 1, despite the fact that row 2 is
200          * shorter than x()
201          */
202         int x_target_;
203         /// do we have a selection?
204         bool selection_;
205         /// are we on the way to get one?
206         bool mark_;
207         /// If true, we are behind the previous char, otherwise we are in front
208         // of the next char. This only make a difference when we are in front
209         // of a big inset spanning a whole row and computing coordinates for
210         // displaying the cursor.
211         bool logicalpos_;
212
213 private:
214
215         //
216         // math specific stuff that could be promoted to "global" later
217         //
218         /// do we allow autocorrection
219         bool autocorrect_;
220         /// are we entering a macro name?
221         bool macromode_;
222
223
224 ///////////////////////////////////////////////////////////////////
225 //
226 // The part below is the non-integrated rest of the original math
227 // cursor. This should be either generalized for texted or moved
228 // back to the math insets.
229 //
230 ///////////////////////////////////////////////////////////////////
231
232 public:
233         ///
234         void insert(MathAtom const &);
235         ///
236         void insert(MathArray const &);
237         /// return false for empty math insets
238         bool erase();
239         /// return false for empty math insets
240         bool backspace();
241         /// called for LFUN_UP etc
242         bool up();
243         /// called for LFUN_DOWN etc
244         bool down();
245         ///
246         void plainErase();
247         ///
248         void plainInsert(MathAtom const & at);
249         ///
250         void niceInsert(MathAtom const & at);
251         ///
252         void niceInsert(std::string const & str);
253
254         /// in pixels from top of screen
255         void setScreenPos(int x, int y);
256         /// current offset in the top cell
257
258         /// interpret name a name of a macro. Returns true if
259         /// something got inserted.
260         bool macroModeClose();
261         /// are we currently typing the name of a macro?
262         bool inMacroMode() const;
263         /// get access to the macro we are currently typing
264         MathUnknownInset * activeMacro();
265
266         /// replace selected stuff with at, placing the former
267         // selection in given cell of atom
268         void handleNest(MathAtom const & at, int cell = 0);
269         ///
270         bool isInside(InsetBase const *);
271
272         /// make sure cursor position is valid
273         void normalize();
274         /// mark current cursor trace for redraw
275         void touch();
276
277         /// hack for reveal codes
278         void markInsert();
279         void markErase();
280         /// injects content of a cell into parent
281         void pullArg();
282         /// split font inset etc
283         void handleFont(std::string const & font);
284
285         /// display a message
286         void message(std::string const & msg) const;
287         /// display an error message
288         void errorMessage(std::string const & msg) const;
289         ///
290         std::string getPossibleLabel();
291
292         /// moves position somehow up or down
293         bool goUpDown(bool up);
294
295         /// the name of the macro we are currently inputting
296         std::string macroName();
297         /// where in the curent cell does the macro name start?
298         int macroNamePos();
299         /// can we enter the inset?
300         bool openable(MathAtom const &) const;
301         ///
302         Encoding const * getEncoding() const;
303         /// font at cursor position
304         LyXFont getFont() const;
305 };
306
307
308 #endif // LYXCURSOR_H