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