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