]> git.lyx.org Git - lyx.git/blob - src/cursor.h
07a47b4e843031dfa7c868b35957b9731b433c7c
[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 DocumentIterator {
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(DocumentIterator 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(DocumentIterator const & where, size_t n);
73         ///
74         void clearSelection();
75         /// access start of selection
76         CursorSlice & selBegin();
77         /// access start of selection
78         CursorSlice const & selBegin() const;
79         /// access end of selection
80         CursorSlice & selEnd();
81         /// access end of selection
82         CursorSlice const & selEnd() 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         /// cache the absolute coordinate from the top inset
153         void updatePos();
154         /// sets anchor to cursor position
155         void resetAnchor(); 
156         /// access to owning BufferView
157         BufferView & bv() const; 
158         /// get some interesting description of top position
159         void info(std::ostream & os) const;
160         /// are we in math mode (2), text mode (1) or unsure (0)?
161         int currentMode();
162         /// reset cursor
163         void reset();
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         /// don't call update() when done
171         void noUpdate();
172         /// don't pop cursor to the level where the LFUN was handled
173         void noPop();
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         DocumentIterator 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         /// Reset cursor to the value it had at the beginning of the latest
209         // dispatch() once the event is fully handled.
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         /// current offset in the top cell
260         /// interpret name a name of a macro
261         void macroModeClose();
262         /// are we currently typing the name of a macro?
263         bool inMacroMode() const;
264         /// get access to the macro we are currently typing
265         MathUnknownInset * activeMacro();
266         /// are we currently typing '#1' or '#2' or...?
267         bool inMacroArgMode() const;
268
269         /// replace selected stuff with at, placing the former
270         // selection in given cell of atom
271         void handleNest(MathAtom const & at, int cell = 0);
272         /// remove this as soon as LyXFunc::getStatus is "localized"
273         //inline std::string getLastCode() { return "mathnormal"; }
274         ///
275         bool isInside(InsetBase const *);
276         ///
277         char valign();
278         ///
279         char halign();
280
281         /// make sure cursor position is valid
282         void normalize();
283         /// mark current cursor trace for redraw
284         void touch();
285
286         /// returns the normalized anchor of the selection
287         CursorSlice normalAnchor();
288
289         /// hack for reveal codes
290         void markInsert();
291         void markErase();
292         /// injects content of a cell into parent
293         void pullArg();
294         /// split font inset etc
295         void handleFont(std::string const & font);
296
297         /// display a message
298         void message(std::string const & msg) const;
299         /// display an error message
300         void errorMessage(std::string const & msg) const;
301         ///
302         std::string getPossibleLabel();
303
304         /// moves position somehow up or down
305         bool goUpDown(bool up);
306
307         /// the name of the macro we are currently inputting
308         std::string macroName();
309         /// where in the curent cell does the macro name start?
310         int macroNamePos();
311         /// can we enter the inset?
312         bool openable(MathAtom const &) const;
313         ///
314         Encoding const * getEncoding() const;
315
316 private:
317         /// moves position closest to (x, y) in current cell
318         void bruteFind2(int x, int y);
319         /// moves position closest to (x, y) in given box
320         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
321 };
322
323 #endif // LYXCURSOR_H