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