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