]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
fix #1411
[lyx.git] / src / mathed / math_cursor.h
1 // -*- C++ -*-
2 /**
3  * \file math_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 Alejandro Aguilar Sierra
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_CURSOR
14 #define MATH_CURSOR
15
16
17 #include "math_inset.h"
18 #include "math_iterator.h"
19 #include "support/std_string.h"
20
21 class InsetFormulaBase;
22 class BufferView;
23 class PainterInfo;
24 class MathUnknownInset;
25
26 /**
27
28 [Have a look at math_inset.h first]
29
30 The MathCursor is different from the kind of cursor used in the Outer
31 World. It contains a stack of CursorPos, each of which is made
32 up of a inset pointer, an index and a position offset, marking a path from
33 this formula's MathHullInset to the current position.
34
35 */
36
37
38 class MathCursor {
39 public:
40         /// short of anything else reasonable
41         typedef MathInset::size_type       size_type;
42         /// type for column numbers
43         typedef MathArray::difference_type difference_type;
44         /// type for cursor positions within a cell
45         typedef MathInset::pos_type        pos_type;
46         /// type for cell indices
47         typedef MathInset::idx_type        idx_type;
48         /// type for row numbers
49         typedef MathInset::row_type        row_type;
50         /// type for column numbers
51         typedef MathInset::col_type        col_type;
52
53         ///
54         explicit MathCursor(InsetFormulaBase *, bool left);
55         ///
56         ~MathCursor();
57         ///
58         void insert(MathAtom const &);
59         ///
60         void insert(MathArray const &);
61         ///
62         void insert2(string const &);
63         ///
64         void paste(string const & data);
65         /// return false for empty math insets
66         bool erase();
67         /// return false for empty math insets
68         bool backspace();
69         /// called for LFUN_HOME etc
70         bool home(bool sel = false);
71         /// called for LFUN_END etc
72         bool end(bool sel = false);
73         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
74         bool right(bool sel = false);
75         /// called for LFUN_LEFT etc
76         bool left(bool sel = false);
77         /// called for LFUN_UP etc
78         bool up(bool sel = false);
79         /// called for LFUN_DOWN etc
80         bool down(bool sel = false);
81         /// Put the cursor in the first position
82         void first();
83         /// Put the cursor in the last position
84         void last();
85         /// move to next cell in current inset
86         void idxNext();
87         /// move to previous cell in current inset
88         void idxPrev();
89         ///
90         void plainErase();
91         ///
92         void plainInsert(MathAtom const &);
93         ///
94         void niceInsert(MathAtom const &);
95         ///
96         void niceInsert(string const &);
97
98         /// in pixels from top of screen
99         void setPos(int x, int y);
100         /// in pixels from top of screen
101         void getPos(int & x, int & y) const;
102         /// in pixels from left of screen
103         int targetX() const;
104         /// current inset
105         MathInset * inset() const;
106         /// return the next enclosing grid inset and the cursor's index in it
107         MathGridInset * enclosingGrid(idx_type & idx) const;
108         /// go up to enclosing grid
109         void popToEnclosingGrid();
110         /// go up to the hull inset
111         void popToEnclosingHull();
112         /// go up to the hull inset
113         void popToHere(MathInset const * p);
114         /// adjust anchor position after deletions/insertions
115         void adjust(pos_type from, difference_type diff);
116         ///
117         InsetFormulaBase * formula() const;
118         /// current offset in the current cell
119         pos_type pos() const;
120         /// current cell
121         idx_type idx() const;
122         /// size of current cell
123         size_type size() const;
124         ///
125         bool script(bool);
126         ///
127         bool interpret(char);
128         /// interpret name a name of a macro
129         void macroModeClose();
130         /// are we currently typing the name of a macro?
131         bool inMacroMode() const;
132         /// get access to the macro we are currently typing
133         MathUnknownInset * activeMacro();
134         /// get access to the macro we are currently typing
135         MathUnknownInset const * activeMacro() const;
136         /// are we currently typing '#1' or '#2' or...?
137         bool inMacroArgMode() const;
138         /// are we in math mode (1), text mode (-1) or unsure?
139         MathInset::mode_type currentMode() const;
140
141         // Local selection methods
142         ///
143         bool selection() const;
144         ///
145         void selCopy();
146         ///
147         void selCut();
148         ///
149         void selDel();
150         /// pastes n-th element of cut buffer
151         void selPaste(size_t n);
152         ///
153         void selHandle(bool);
154         ///
155         void selStart();
156         ///
157         void selClear();
158         /// clears or deletes selection depending on lyxrc setting
159         void selClearOrDel();
160         /// draws light-blue selection background
161         void drawSelection(PainterInfo & pi) const;
162         /// replace selected stuff with at, placing the former 
163         // selection in given cell of atom
164         void handleNest(MathAtom const & at, int cell = 0);
165         /// remove this as soon as LyXFunc::getStatus is "localized"
166         string getLastCode() const { return "mathnormal"; }
167         ///
168         bool isInside(MathInset const *) const;
169         ///
170         char valign() const;
171         ///
172         char halign() const;
173
174         /// make sure cursor position is valid
175         void normalize();
176         /// mark current cursor trace for redraw
177         void touch();
178         ///
179         UpdatableInset * asHyperActiveInset() const;
180
181         /// enter a MathInset
182         void push(MathAtom & par);
183         /// enter a MathInset from the front
184         void pushLeft(MathAtom & par);
185         /// enter a MathInset from the back
186         void pushRight(MathAtom & par);
187         /// leave current MathInset to the left
188         bool popLeft();
189         /// leave current MathInset to the left
190         bool popRight();
191
192         ///
193         MathArray & array() const;
194         ///
195         bool hasPrevAtom() const;
196         ///
197         bool hasNextAtom() const;
198         ///
199         MathAtom const & prevAtom() const;
200         ///
201         MathAtom & prevAtom();
202         ///
203         MathAtom const & nextAtom() const;
204         ///
205         MathAtom & nextAtom();
206
207         /// returns the selection
208         void getSelection(CursorPos &, CursorPos &) const;
209         /// returns the normalized anchor of the selection
210         CursorPos normalAnchor() const;
211
212         /// reference to the last item of the path, i.e. "The Cursor"
213         CursorPos & cursor();
214         /// reference to the last item of the path, i.e. "The Cursor"
215         CursorPos const & cursor() const;
216         /// how deep are we nested?
217         unsigned depth() const;
218
219         /// local dispatcher
220         dispatch_result dispatch(FuncRequest const & cmd);
221         /// describe the situation
222         string info() const;
223         /// dump selection information for debugging
224         void seldump(char const * str) const;
225         /// dump selection information for debugging
226         void dump(char const * str) const;
227         /// moves on
228         void setSelection(MathIterator const & where, size_type n);
229         /// grab selection marked by anchor and current cursor
230         string grabSelection() const;
231         /// guess what
232         string grabAndEraseSelection();
233         ///
234         void insert(char c);
235         ///
236         void insert(string const & str);
237         /// lock/unlock inset
238         void insetToggle();
239
240         /// hack for reveal codes
241         void markInsert();
242         void markErase();
243         /// injects content of a cell into parent
244         void pullArg();
245         /// split font inset etc
246         void handleFont(string const & font);
247
248 private:
249         /// moves cursor index one cell to the left
250         bool idxLeft();
251         /// moves cursor index one cell to the right
252         bool idxRight();
253         /// moves cursor to end of last cell of current line
254         bool idxLineLast();
255         /// moves cursor position one cell to the left
256         bool posLeft();
257         /// moves cursor position one cell to the right
258         bool posRight();
259         /// moves position somehow up or down
260         bool goUpDown(bool up);
261         /// moves position closest to (x, y) in given box
262         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
263         /// moves position closest to (x, y) in current cell
264         void bruteFind2(int x, int y);
265         /// are we in a nucleus of a script inset?
266         bool inNucleus() const;
267
268         /// erase the selected part and re-sets the cursor
269         void eraseSelection();
270
271         /// the name of the macro we are currently inputting
272         string macroName() const;
273         /// where in the curent cell does the macro name start?
274         difference_type macroNamePos() const;
275         /// can we enter the inset?
276         bool openable(MathAtom const &, bool selection) const;
277         /// write access to cursor cell position
278         pos_type & pos();
279         /// write access to cursor cell index
280         idx_type & idx();
281
282         /// path of positions the cursor had to go if it were leaving each inset
283         MathIterator Cursor_;
284         /// path of positions the anchor had to go if it were leaving each inset
285         mutable MathIterator Anchor_;
286         /// pointer to enclsing LyX inset
287         InsetFormulaBase * formula_;
288         // Selection stuff
289         /// text code of last char entered
290         //MathTextCodes lastcode_;
291         /// do we allow autocorrection
292         bool autocorrect_;
293         /// do we currently select
294         bool selection_;
295         /// are we entering a macro name?
296         bool macromode_;
297         /// are we targeting a certain x coordinate, if so, which one?
298         int targetx_;
299 };
300
301 extern MathCursor * mathcursor;
302 void releaseMathCursor(BufferView * bv);
303
304 #endif