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