]> git.lyx.org Git - lyx.git/blob - src/cursor.h
d59e4312bb9e9f3d30f96dc36f5c96194e8c49e0
[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         /// clears or deletes selection depending on lyxrc setting
100         void selClearOrDel();
101         //
102         std::string selectionAsString(bool label) const;
103         ///
104         void paste(std::string const & data);
105         ///
106         std::string currentState();
107
108         /// auto-correct mode
109         bool autocorrect() const { return autocorrect_; }
110         /// auto-correct mode
111         bool & autocorrect() { return autocorrect_; }
112         /// are we entering a macro name?
113         bool macromode() const { return macromode_; }
114         /// are we entering a macro name?
115         bool & macromode() { return macromode_; }
116         /// returns x,y position
117         void getPos(int & x, int & y) const;
118         /// returns cursor dimension
119         void getDim(int & asc, int & desc) const;
120
121         //
122         // common part
123         //
124         /// move one step to the left
125         bool posLeft();
126         /// move one step to the right
127         bool posRight();
128
129         /// insert an inset
130         void insert(InsetBase *);
131         /// insert a single char
132         void insert(char c);
133         /// insert a string
134         void insert(std::string const & str);
135
136         /// write acess to target x position of cursor
137         int & x_target();
138         /// return target x position of cursor
139         int x_target() const;
140         /// clear target x position of cursor
141         void clearTargetX();
142
143         /// access to selection anchor
144         CursorSlice & anchor();
145         /// access to selection anchor
146         CursorSlice const & anchor() const;
147         /// cache the absolute coordinate from the top inset
148         void updatePos();
149         /// sets anchor to cursor position
150         void resetAnchor();
151         /// access to owning BufferView
152         BufferView & bv() const;
153         /// get some interesting description of top position
154         void info(std::ostream & os) const;
155         /// are we in math mode (2), text mode (1) or unsure (0)?
156         int currentMode();
157         /// reset cursor bottom to the beginning of the given inset
158         // (sort of 'chroot' environment...)
159         void reset(InsetBase &);
160         /// for spellchecking
161         void replaceWord(std::string const & replacestring);
162         /// update our view
163         void update();
164         /// the event was not (yet) dispatched
165         void undispatched();
166         /// the event was already dispatched
167         void dispatched();
168         /// don't call update() when done
169         void noUpdate();
170
171         /// output
172         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
173
174 public:
175         ///
176         BufferView * bv_;
177 //private:
178         /// the anchor position
179         DocIterator anchor_;
180
181         ///
182         DispatchResult disp_;
183
184 private:
185         ///
186         int cached_y_;
187         /**
188          * The target x position of the cursor. This is used for when
189          * we have text like :
190          *
191          * blah blah blah blah| blah blah blah
192          * blah blah blah
193          * blah blah blah blah blah blah
194          *
195          * When we move onto row 3, we would like to be vertically aligned
196          * with where we were in row 1, despite the fact that row 2 is
197          * shorter than x()
198          */
199         int x_target_;
200         /// do we have a selection?
201         bool selection_;
202         /// are we on the way to get one?
203         bool mark_;
204
205         //
206         // math specific stuff that could be promoted to "global" later
207         //
208         /// do we allow autocorrection
209         bool autocorrect_;
210         /// are we entering a macro name?
211         bool macromode_;
212
213
214 ///////////////////////////////////////////////////////////////////
215 //
216 // The part below is the non-integrated rest of the original math
217 // cursor. This should be either generalized for texted or moved
218 // back to the math insets.
219 //
220 ///////////////////////////////////////////////////////////////////
221
222 public:
223         ///
224         void insert(MathAtom const &);
225         ///
226         void insert(MathArray const &);
227         /// return false for empty math insets
228         bool erase();
229         /// return false for empty math insets
230         bool backspace();
231         /// called for LFUN_UP etc
232         bool up();
233         /// called for LFUN_DOWN etc
234         bool down();
235         ///
236         void plainErase();
237         ///
238         void plainInsert(MathAtom const & at);
239         ///
240         void niceInsert(MathAtom const & at);
241         ///
242         void niceInsert(std::string const & str);
243
244         /// in pixels from top of screen
245         void setScreenPos(int x, int y);
246         /// in pixels from left of screen
247         int targetX() const;
248         /// adjust anchor position after deletions/insertions
249         void adjust(pos_type from, int diff);
250         /// current offset in the top cell
251         /// interpret name a name of a macro
252         void macroModeClose();
253         /// are we currently typing the name of a macro?
254         bool inMacroMode() const;
255         /// get access to the macro we are currently typing
256         MathUnknownInset * activeMacro();
257
258         /// replace selected stuff with at, placing the former
259         // selection in given cell of atom
260         void handleNest(MathAtom const & at, int cell = 0);
261         ///
262         bool isInside(InsetBase const *);
263
264         /// make sure cursor position is valid
265         void normalize();
266         /// mark current cursor trace for redraw
267         void touch();
268
269         /// returns the normalized anchor of the selection
270         CursorSlice normalAnchor();
271
272         /// hack for reveal codes
273         void markInsert();
274         void markErase();
275         /// injects content of a cell into parent
276         void pullArg();
277         /// split font inset etc
278         void handleFont(std::string const & font);
279
280         /// display a message
281         void message(std::string const & msg) const;
282         /// display an error message
283         void errorMessage(std::string const & msg) const;
284         ///
285         std::string getPossibleLabel();
286
287         /// moves position somehow up or down
288         bool goUpDown(bool up);
289
290         /// the name of the macro we are currently inputting
291         std::string macroName();
292         /// where in the curent cell does the macro name start?
293         int macroNamePos();
294         /// can we enter the inset?
295         bool openable(MathAtom const &) const;
296         ///
297         Encoding const * getEncoding() const;
298
299 private:
300         /// moves position closest to (x, y) in current cell
301         void bruteFind2(int x, int y);
302         /// moves position closest to (x, y) in given box
303         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
304 };
305
306 #endif // LYXCURSOR_H