]> git.lyx.org Git - lyx.git/blob - src/cursor.h
try to fix broken cursor (from Andr�)
[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         /// sets cursor part
57         void setCursor(DocIterator const & it);
58
59         //
60         // selection
61         //
62         /// selection active?
63         bool selection() const { return selection_; }
64         /// selection active?
65         bool & selection() { return selection_; }
66         /// did we place the anchor?
67         bool mark() const { return mark_; }
68         /// did we place the anchor?
69         bool & mark() { return mark_; }
70         ///
71         void setSelection();
72         /// set selection at given position
73         void setSelection(DocIterator const & where, size_t n);
74         ///
75         void clearSelection();
76         /// access start of selection
77         CursorSlice selBegin() const;
78         /// access end of selection
79         CursorSlice selEnd() const;
80         /// access start of selection
81         DocIterator selectionBegin() const;
82         /// access start of selection
83         DocIterator selectionEnd() const;
84         ///
85         void selHandle(bool selecting);
86         //
87         std::string selectionAsString(bool label) const;
88         ///
89         void paste(std::string const & data);
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         /// the event was not (yet) dispatched
148         void undispatched();
149         /// the event was already dispatched
150         void dispatched();
151         /// call update() when done
152         void needsUpdate();
153         /// don't call update() when done
154         void noUpdate();
155         /// fix cursor in circumstances that should never happen
156         void fixIfBroken();
157
158         /// output
159         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
160
161 public:
162         ///
163         BufferView * bv_;
164 //private:
165         /// the anchor position
166         DocIterator anchor_;
167
168         ///
169         DispatchResult disp_;
170
171
172 private:
173         /**
174          * The target x position of the cursor. This is used for when
175          * we have text like :
176          *
177          * blah blah blah blah| blah blah blah
178          * blah blah blah
179          * blah blah blah blah blah blah
180          *
181          * When we move onto row 3, we would like to be vertically aligned
182          * with where we were in row 1, despite the fact that row 2 is
183          * shorter than x()
184          */
185         int x_target_;
186         /// do we have a selection?
187         bool selection_;
188         /// are we on the way to get one?
189         bool mark_;
190
191 private:
192
193         //
194         // math specific stuff that could be promoted to "global" later
195         //
196         /// do we allow autocorrection
197         bool autocorrect_;
198         /// are we entering a macro name?
199         bool macromode_;
200
201
202 ///////////////////////////////////////////////////////////////////
203 //
204 // The part below is the non-integrated rest of the original math
205 // cursor. This should be either generalized for texted or moved
206 // back to the math insets.
207 //
208 ///////////////////////////////////////////////////////////////////
209
210 public:
211         ///
212         void insert(MathAtom const &);
213         ///
214         void insert(MathArray const &);
215         /// return false for empty math insets
216         bool erase();
217         /// return false for empty math insets
218         bool backspace();
219         /// called for LFUN_UP etc
220         bool up();
221         /// called for LFUN_DOWN etc
222         bool down();
223         ///
224         void plainErase();
225         ///
226         void plainInsert(MathAtom const & at);
227         ///
228         void niceInsert(MathAtom const & at);
229         ///
230         void niceInsert(std::string const & str);
231
232         /// in pixels from top of screen
233         void setScreenPos(int x, int y);
234         /// current offset in the top cell
235         /// interpret name a name of a macro
236         void macroModeClose();
237         /// are we currently typing the name of a macro?
238         bool inMacroMode() const;
239         /// get access to the macro we are currently typing
240         MathUnknownInset * activeMacro();
241
242         /// replace selected stuff with at, placing the former
243         // selection in given cell of atom
244         void handleNest(MathAtom const & at, int cell = 0);
245         ///
246         bool isInside(InsetBase const *);
247
248         /// make sure cursor position is valid
249         void normalize();
250         /// mark current cursor trace for redraw
251         void touch();
252
253         /// hack for reveal codes
254         void markInsert();
255         void markErase();
256         /// injects content of a cell into parent
257         void pullArg();
258         /// split font inset etc
259         void handleFont(std::string const & font);
260
261         /// display a message
262         void message(std::string const & msg) const;
263         /// display an error message
264         void errorMessage(std::string const & msg) const;
265         ///
266         std::string getPossibleLabel();
267
268         /// moves position somehow up or down
269         bool goUpDown(bool up);
270
271         /// the name of the macro we are currently inputting
272         std::string macroName();
273         /// where in the curent cell does the macro name start?
274         int macroNamePos();
275         /// can we enter the inset?
276         bool openable(MathAtom const &) const;
277         ///
278         Encoding const * getEncoding() const;
279         /// font at cursor position
280         LyXFont getFont() const;
281 };
282
283
284 #endif // LYXCURSOR_H