]> git.lyx.org Git - lyx.git/blob - src/cursor.h
e99a8a06bd4e16ba32c5e91988ea2e5cdae4e1d5
[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
26 // these should go
27 class MathUnknownInset;
28 class Encoding;
29
30
31 /// The cursor class describes the position of a cursor within a document.
32
33 // The public inheritance should go in favour of a suitable data member
34 // (or maybe private inheritance) at some point of time.
35 class LCursor : public DocIterator {
36 public:
37         /// create the cursor of a BufferView
38         explicit LCursor(BufferView & bv);
39
40         /// dispatch from innermost inset upwards
41         DispatchResult dispatch(FuncRequest const & cmd);
42         /// are we willing to handle this event?
43         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
44
45         /// add a new cursor slice
46         void push(InsetBase & inset);
47         /// add a new cursor slice, place cursor on left end
48         void pushLeft(InsetBase & inset);
49         /// pop one level off the cursor
50         void pop();
51         /// pop one slice off the cursor stack and go left
52         bool popLeft();
53         /// pop one slice off the cursor stack and go right
54         bool popRight();
55         /// sets cursor part
56         void setCursor(DocIterator const & it, bool sel);
57
58         //
59         // selection
60         //
61         /// selection active?
62         bool selection() const { return selection_; }
63         /// selection active?
64         bool & selection() { return selection_; }
65         /// did we place the anchor?
66         bool mark() const { return mark_; }
67         /// did we place the anchor?
68         bool & mark() { return mark_; }
69         ///
70         void setSelection();
71         /// set selection at given position
72         void setSelection(DocIterator const & where, size_t n);
73         ///
74         void clearSelection();
75         /// access start of selection
76         CursorSlice selBegin() const;
77         /// access end of selection
78         CursorSlice selEnd() const;
79         /// access start of selection
80         DocIterator selectionBegin() const;
81         /// access start of selection
82         DocIterator selectionEnd() const;
83         ///
84         void selHandle(bool selecting);
85         //
86         std::string selectionAsString(bool label) const;
87         ///
88         void paste(std::string const & data);
89         ///
90         std::string currentState();
91
92         /// auto-correct mode
93         bool autocorrect() const { return autocorrect_; }
94         /// auto-correct mode
95         bool & autocorrect() { return autocorrect_; }
96         /// are we entering a macro name?
97         bool macromode() const { return macromode_; }
98         /// are we entering a macro name?
99         bool & macromode() { return macromode_; }
100         /// returns x,y position
101         void getPos(int & x, int & y) const;
102         /// returns cursor dimension
103         void getDim(int & asc, int & desc) const;
104
105         //
106         // common part
107         //
108         /// move one step to the left
109         bool posLeft();
110         /// move one step to the right
111         bool posRight();
112
113         /// insert an inset
114         void insert(InsetBase *);
115         /// insert a single char
116         void insert(char c);
117         /// insert a string
118         void insert(std::string const & str);
119
120         /// write acess to target x position of cursor
121         int & x_target();
122         /// return target x position of cursor
123         int x_target() const;
124         /// clear target x position of cursor
125         void clearTargetX();
126
127         /// access to normalized selection anchor
128         CursorSlice anchor() const;
129         /// sets anchor to cursor position
130         void resetAnchor();
131         /// access to owning BufferView
132         BufferView & bv() const;
133         /// access to owning Buffer
134         Buffer & buffer() const;
135         /// get some interesting description of top position
136         void info(std::ostream & os) const;
137         /// are we in math mode (2), text mode (1) or unsure (0)?
138         int currentMode();
139         /// reset cursor bottom to the beginning of the given inset
140         // (sort of 'chroot' environment...)
141         void reset(InsetBase &);
142         /// for spellchecking
143         void replaceWord(std::string const & replacestring);
144         /// the event was not (yet) dispatched
145         void undispatched();
146         /// the event was already dispatched
147         void dispatched();
148         /// don't call update() when done
149         void noUpdate();
150
151         /// output
152         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
153
154 public:
155         ///
156         BufferView * bv_;
157 //private:
158         /// the anchor position
159         DocIterator anchor_;
160
161         ///
162         DispatchResult disp_;
163
164 private:
165         /**
166          * The target x position of the cursor. This is used for when
167          * we have text like :
168          *
169          * blah blah blah blah| blah blah blah
170          * blah blah blah
171          * blah blah blah blah blah blah
172          *
173          * When we move onto row 3, we would like to be vertically aligned
174          * with where we were in row 1, despite the fact that row 2 is
175          * shorter than x()
176          */
177         int x_target_;
178         /// do we have a selection?
179         bool selection_;
180         /// are we on the way to get one?
181         bool mark_;
182
183         //
184         // math specific stuff that could be promoted to "global" later
185         //
186         /// do we allow autocorrection
187         bool autocorrect_;
188         /// are we entering a macro name?
189         bool macromode_;
190
191
192 ///////////////////////////////////////////////////////////////////
193 //
194 // The part below is the non-integrated rest of the original math
195 // cursor. This should be either generalized for texted or moved
196 // back to the math insets.
197 //
198 ///////////////////////////////////////////////////////////////////
199
200 public:
201         ///
202         void insert(MathAtom const &);
203         ///
204         void insert(MathArray const &);
205         /// return false for empty math insets
206         bool erase();
207         /// return false for empty math insets
208         bool backspace();
209         /// called for LFUN_UP etc
210         bool up();
211         /// called for LFUN_DOWN etc
212         bool down();
213         ///
214         void plainErase();
215         ///
216         void plainInsert(MathAtom const & at);
217         ///
218         void niceInsert(MathAtom const & at);
219         ///
220         void niceInsert(std::string const & str);
221
222         /// in pixels from top of screen
223         void setScreenPos(int x, int y);
224         /// in pixels from left of screen
225         int targetX() const;
226         /// current offset in the top cell
227         /// interpret name a name of a macro
228         void macroModeClose();
229         /// are we currently typing the name of a macro?
230         bool inMacroMode() const;
231         /// get access to the macro we are currently typing
232         MathUnknownInset * activeMacro();
233
234         /// replace selected stuff with at, placing the former
235         // selection in given cell of atom
236         void handleNest(MathAtom const & at, int cell = 0);
237         ///
238         bool isInside(InsetBase const *);
239
240         /// make sure cursor position is valid
241         void normalize();
242         /// mark current cursor trace for redraw
243         void touch();
244
245         /// hack for reveal codes
246         void markInsert();
247         void markErase();
248         /// injects content of a cell into parent
249         void pullArg();
250         /// split font inset etc
251         void handleFont(std::string const & font);
252
253         /// display a message
254         void message(std::string const & msg) const;
255         /// display an error message
256         void errorMessage(std::string const & msg) const;
257         ///
258         std::string getPossibleLabel();
259
260         /// moves position somehow up or down
261         bool goUpDown(bool up);
262
263         /// the name of the macro we are currently inputting
264         std::string macroName();
265         /// where in the curent cell does the macro name start?
266         int macroNamePos();
267         /// can we enter the inset?
268         bool openable(MathAtom const &) const;
269         ///
270         Encoding const * getEncoding() const;
271
272 private:
273         /// moves position closest to (x, y) in current cell
274         void bruteFind2(int x, int y);
275         /// moves position closest to (x, y) in given box
276         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
277 };
278
279 #endif // LYXCURSOR_H