]> git.lyx.org Git - lyx.git/blob - src/cursor.h
more cursor dispatch
[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 "cursor_slice.h"
16
17 #include <iosfwd>
18 #include <vector>
19
20 class BufferView;
21 class UpdatableInset;
22 class MathAtom;
23 class DispatchResult;
24 class FuncRequest;
25 class InsetTabular;
26 class LyXText;
27 class Paragraph;
28
29
30 // these should go
31 class MathHullInset;
32 class PainterInfo;
33 class MathUnknownInset;
34 class MathGridInset;
35
36 // this is used for traversing math insets
37 typedef std::vector<CursorSlice> CursorBase;
38 /// move on one step
39 void increment(CursorBase &);
40 ///
41 CursorBase ibegin(InsetBase * p);
42 ///
43 CursorBase iend(InsetBase * p);
44
45
46 /**
47  * The cursor class describes the position of a cursor within a document.
48  */
49
50 class LCursor {
51 public:
52         /// type for cell number in inset
53         typedef CursorSlice::idx_type idx_type;
54         /// type for paragraph numbers positions within a cell
55         typedef CursorSlice::par_type par_type;
56         /// type for cursor positions within a cell
57         typedef CursorSlice::pos_type pos_type;
58         /// type for row indices
59         typedef CursorSlice::row_type row_type;
60         /// type for col indices
61         typedef CursorSlice::col_type col_type;
62
63         /// create the cursor of a BufferView
64         explicit LCursor(BufferView & bv);
65         /// dispatch from innermost inset upwards
66         DispatchResult dispatch(FuncRequest const & cmd);
67         /// add a new cursor slice
68         void push(InsetBase * inset);
69         /// add a new cursor slice, place cursor on left end
70         void pushLeft(InsetBase * inset);
71         /// pop one level off the cursor
72         void pop();
73         /// pop one slice off the cursor stack and go left
74         bool popLeft();
75         /// pop one slice off the cursor stack and go right
76         bool popRight();
77         /// restrict cursor nesting to given size
78         void pop(int depth);
79         /// access to current cursor slice
80         CursorSlice & current();
81         /// access to current cursor slice
82         CursorSlice const & current() const;
83         /// how many nested insets do we have?
84         size_t depth() const { return cursor_.size(); }
85         /// depth of current slice
86         int currentDepth() const { return current_; }
87
88         //
89         // selection
90         //
91         /// selection active?
92         bool selection() const { return selection_; }
93         /// selection active?
94         bool & selection() { return selection_; }
95         /// did we place the anchor?
96         bool mark() const { return mark_; }
97         /// did we place the anchor?
98         bool & mark() { return mark_; }
99         ///
100         void setSelection();
101         /// set selection at given position
102         void setSelection(CursorBase const & where, size_t n);
103         ///
104         void clearSelection();
105         /// access start of selection
106         CursorSlice & selBegin();
107         /// access start of selection
108         CursorSlice const & selBegin() const;
109         /// access end of selection
110         CursorSlice & selEnd();
111         /// access end of selection
112         CursorSlice const & selEnd() const;
113         ///
114         std::string grabSelection();
115         ///
116         void eraseSelection();
117         ///
118         std::string grabAndEraseSelection();
119         // other selection methods
120         ///
121         void selCopy();
122         ///
123         void selCut();
124         ///
125         void selDel();
126         /// pastes n-th element of cut buffer
127         void selPaste(size_t n);
128         ///
129         void selHandle(bool selecting);
130         /// start selection
131         void selStart();
132         /// clear selection
133         void selClear();
134         /// clears or deletes selection depending on lyxrc setting
135         void selClearOrDel();
136         ///
137         void paste(std::string const & data);
138
139         //
140         // access to the 'current' cursor slice
141         //
142         /// the containing inset
143         InsetBase * inset() const { return current().inset(); }
144         /// return the cell of the inset this cursor is in
145         idx_type idx() const { return current().idx(); }
146         /// return the cell of the inset this cursor is in
147         idx_type & idx() { return current().idx(); }
148         /// return the last possible cell in this inset
149         idx_type lastidx() const { return current().lastidx(); }
150         /// return the paragraph this cursor is in
151         par_type par() const { return current().par(); }
152         /// return the paragraph this cursor is in
153         par_type & par() { return current().par(); }
154         /// return the last possible paragraph in this inset
155         par_type lastpar() const;
156         /// return the position within the paragraph
157         pos_type pos() const { return current().pos(); }
158         /// return the position within the paragraph
159         pos_type & pos() { return current().pos(); }
160         /// return the last position within the paragraph
161         pos_type lastpos() const;
162         /// return the display row of the cursor with in the current par
163         row_type crow() const;
164         /// return the display row of the cursor with in the current par
165         row_type lastcrow() const;
166
167         /// return the number of embedded cells
168         size_t nargs() const;
169         /// return the number of embedded cells
170         size_t ncols() const;
171         /// return the number of embedded cells
172         size_t nrows() const;
173         /// return the grid row of the current cell
174         row_type row() const;
175         /// return the grid row of the current cell
176         col_type col() const;
177         /// the inset just behind the cursor
178         InsetBase * nextInset();
179         /// the inset just in front of the cursor
180         InsetBase * prevInset();
181
182         //
183         // math-specific part
184         //
185         /// return the mathed cell this cursor is in
186         MathArray const & cell() const;
187         /// return the mathed cell this cursor is in
188         MathArray & cell();
189         /// the mathatom left of the cursor
190         MathAtom const & prevAtom() const;
191         /// the mathatom left of the cursor
192         MathAtom & prevAtom();
193         /// the mathatom right of the cursor
194         MathAtom const & nextAtom() const;
195         /// the mathatom right of the cursor
196         MathAtom & nextAtom();
197         /// auto-correct mode
198         bool autocorrect() const { return autocorrect_; }
199         /// auto-correct mode
200         bool & autocorrect() { return autocorrect_; }
201         /// are we entering a macro name?
202         bool macromode() const { return macromode_; }
203         /// are we entering a macro name?
204         bool & macromode() { return macromode_; }
205
206         //
207         // text-specific part
208         ///
209         bool boundary() const { return current().boundary(); }
210         ///
211         bool & boundary() { return current().boundary(); }
212         ///
213         Paragraph & paragraph();
214         ///
215         Paragraph const & paragraph() const;
216         ///
217         LyXText * text() const;
218         ///
219         InsetBase * innerInsetOfType(int code) const;
220         ///
221         InsetTabular * innerInsetTabular() const;
222         ///
223         LyXText * innerText() const;
224         ///
225         CursorSlice const & innerTextSlice() const;
226         /// returns x,y position
227         void getPos(int & x, int & y) const;
228         /// returns cursor dimension
229         void getDim(int & asc, int & desc) const;
230
231         //
232         // common part
233         //
234         /// move one step to the left
235         bool posLeft();
236         /// move one step to the right
237         bool posRight();
238
239         /// write acess to target x position of cursor
240         int & x_target();
241         /// return target x position of cursor
242         int x_target() const;
243         /// clear target x position of cursor
244         void clearTargetX();
245
246         /// access to selection anchor
247         CursorSlice & anchor();
248         /// access to selection anchor
249         CursorSlice const & anchor() const;
250         /// cache the absolute coordinate from the top inset
251         void updatePos();
252         /// sets anchor to cursor position
253         void resetAnchor(); 
254         /// access to owning BufferView
255         BufferView & bv() const; 
256         /// get some interesting description of current position
257         void info(std::ostream & os);
258         /// are we in math mode (2), text mode (1) or unsure (0)?
259         int currentMode();
260         /// reset cursor
261         void reset();
262
263         /// output
264         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
265 public:
266 //private:
267         /// mainly used as stack, but wee need random access
268         std::vector<CursorSlice> cursor_;
269         /// the anchor position
270         std::vector<CursorSlice> anchor_;
271
272 private:
273         ///
274         BufferView * bv_;
275         /// current slice
276         int current_;
277         ///
278         int cached_y_;
279         /**
280          * The target x position of the cursor. This is used for when
281          * we have text like :
282          *
283          * blah blah blah blah| blah blah blah
284          * blah blah blah
285          * blah blah blah blah blah blah
286          *
287          * When we move onto row 3, we would like to be vertically aligned
288          * with where we were in row 1, despite the fact that row 2 is
289          * shorter than x()
290          */
291         int x_target_;
292         // do we have a selection?
293         bool selection_;
294         // are we on the way to get one?
295         bool mark_;
296
297         //
298         // math specific stuff that could be promoted to "global" later
299         //
300         /// do we allow autocorrection
301         bool autocorrect_;
302         /// are we entering a macro name?
303         bool macromode_;
304
305
306 ///////////////////////////////////////////////////////////////////
307 //
308 // The part below is the non-integrated rest of the original math
309 // cursor. This should be either generalized for texted or moved
310 // back to the math insets.
311 //
312 ///////////////////////////////////////////////////////////////////
313
314 public:
315         ///
316         void insert(MathAtom const &);
317         ///
318         void insert(MathArray const &);
319         ///
320         void insert2(std::string const &);
321         /// return false for empty math insets
322         bool erase();
323         /// return false for empty math insets
324         bool backspace();
325         /// called for LFUN_HOME etc
326         bool home();
327         /// called for LFUN_END etc
328         bool end();
329         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
330         bool right();
331         /// called for LFUN_LEFT etc
332         bool left();
333         /// called for LFUN_UP etc
334         bool up();
335         /// called for LFUN_DOWN etc
336         bool down();
337         ///
338         void plainErase();
339         ///
340         void plainInsert(MathAtom const & at);
341         ///
342         void niceInsert(MathAtom const & at);
343         ///
344         void niceInsert(std::string const & str);
345
346         /// in pixels from top of screen
347         void setScreenPos(int x, int y);
348         /// in pixels from left of screen
349         int targetX() const;
350         /// return the next enclosing grid inset and the cursor's index in it
351         MathGridInset * enclosingGrid(idx_type & idx) const;
352         /// adjust anchor position after deletions/insertions
353         void adjust(pos_type from, int diff);
354         ///
355         MathHullInset * formula() const;
356         /// current offset in the current cell
357         ///
358         bool script(bool);
359         ///
360         bool interpret(char);
361         /// interpret name a name of a macro
362         void macroModeClose();
363         /// are we currently typing the name of a macro?
364         bool inMacroMode() const;
365         /// get access to the macro we are currently typing
366         MathUnknownInset * activeMacro();
367         /// are we currently typing '#1' or '#2' or...?
368         bool inMacroArgMode() const;
369
370         /// replace selected stuff with at, placing the former
371         // selection in given cell of atom
372         void handleNest(MathAtom const & at, int cell = 0);
373         /// remove this as soon as LyXFunc::getStatus is "localized"
374         //inline std::string getLastCode() { return "mathnormal"; }
375         ///
376         bool isInside(InsetBase const *);
377         ///
378         char valign();
379         ///
380         char halign();
381
382         /// make sure cursor position is valid
383         void normalize();
384         /// mark current cursor trace for redraw
385         void touch();
386
387         /// returns the normalized anchor of the selection
388         CursorSlice normalAnchor();
389
390         ///
391         void insert(char c);
392         ///
393         void insert(std::string const & str);
394         /// lock/unlock inset
395         void lockToggle();
396
397         /// hack for reveal codes
398         void markInsert();
399         void markErase();
400         /// injects content of a cell into parent
401         void pullArg();
402         /// split font inset etc
403         void handleFont(std::string const & font);
404
405         void releaseMathCursor();
406         /// are we in mathed?
407         bool inMathed() const;
408
409         /// display a message
410         void message(std::string const & msg) const;
411         /// display an error message
412         void errorMessage(std::string const & msg) const;
413
414 private:
415         /// moves cursor index one cell to the left
416         bool idxLeft();
417         /// moves cursor index one cell to the right
418         bool idxRight();
419         /// moves cursor to end of last cell of current line
420         bool idxLineLast();
421         /// moves position somehow up or down
422         bool goUpDown(bool up);
423         /// moves position closest to (x, y) in given box
424         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
425         /// moves position closest to (x, y) in current cell
426         void bruteFind2(int x, int y);
427         /// are we in a nucleus of a script inset?
428         bool inNucleus();
429
430         /// the name of the macro we are currently inputting
431         std::string macroName();
432         /// where in the curent cell does the macro name start?
433         int macroNamePos();
434         /// can we enter the inset?
435         bool openable(MathAtom const &);
436 };
437
438 #endif // LYXCURSOR_H