]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
Standardise the header blurb in mathed.
[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 "LString.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         ///
163         void handleNest(MathAtom const & at);
164         /// remove this as soon as LyXFunc::getStatus is "localized"
165         string getLastCode() const { return "mathnormal"; }
166         ///
167         bool isInside(MathInset const *) const;
168         ///
169         char valign() const;
170         ///
171         char halign() const;
172
173         /// make sure cursor position is valid
174         void normalize();
175         /// mark current cursor trace for redraw
176         void touch();
177         ///
178         UpdatableInset * asHyperActiveInset() const;
179
180         /// enter a MathInset
181         void push(MathAtom & par);
182         /// enter a MathInset from the front
183         void pushLeft(MathAtom & par);
184         /// enter a MathInset from the back
185         void pushRight(MathAtom & par);
186         /// leave current MathInset to the left
187         bool popLeft();
188         /// leave current MathInset to the left
189         bool popRight();
190
191         ///
192         MathArray & array() const;
193         ///
194         bool hasPrevAtom() const;
195         ///
196         bool hasNextAtom() const;
197         ///
198         MathAtom const & prevAtom() const;
199         ///
200         MathAtom & prevAtom();
201         ///
202         MathAtom const & nextAtom() const;
203         ///
204         MathAtom & nextAtom();
205
206         /// returns the selection
207         void getSelection(CursorPos &, CursorPos &) const;
208         /// returns the normalized anchor of the selection
209         CursorPos normalAnchor() const;
210
211         /// reference to the last item of the path, i.e. "The Cursor"
212         CursorPos & cursor();
213         /// reference to the last item of the path, i.e. "The Cursor"
214         CursorPos const & cursor() const;
215         /// how deep are we nested?
216         unsigned depth() const;
217
218         /// local dispatcher
219         dispatch_result dispatch(FuncRequest const & cmd);
220         /// describe the situation
221         string info() const;
222         /// dump selection information for debugging
223         void seldump(char const * str) const;
224         /// dump selection information for debugging
225         void dump(char const * str) const;
226         /// moves on
227         void setSelection(MathIterator const & where, size_type n);
228         /// grab selection marked by anchor and current cursor
229         string grabSelection() const;
230         /// guess what
231         string grabAndEraseSelection();
232         ///
233         void insert(char c);
234         ///
235         void insert(string const & str);
236         /// lock/unlock inset
237         void insetToggle();
238
239         /// hack for reveal codes
240         void markInsert();
241         void markErase();
242         /// injects content of a cell into parent
243         void pullArg();
244         /// split font inset etc
245         void handleFont(string const & font);
246
247 private:
248         /// moves cursor index one cell to the left
249         bool idxLeft();
250         /// moves cursor index one cell to the right
251         bool idxRight();
252         /// moves cursor to end of last cell of current line
253         bool idxLineLast();
254         /// moves cursor position one cell to the left
255         bool posLeft();
256         /// moves cursor position one cell to the right
257         bool posRight();
258         /// moves position somehow up or down
259         bool goUpDown(bool up);
260         /// moves position closest to (x, y) in given box
261         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
262         /// moves position closest to (x, y) in current cell
263         void bruteFind2(int x, int y);
264         /// are we in a nucleus of a script inset?
265         bool inNucleus() const;
266
267         /// erase the selected part and re-sets the cursor
268         void eraseSelection();
269
270         /// the name of the macro we are currently inputting
271         string macroName() const;
272         /// where in the curent cell does the macro name start?
273         difference_type macroNamePos() const;
274         /// can we enter the inset?
275         bool openable(MathAtom const &, bool selection) const;
276         /// write access to cursor cell position
277         pos_type & pos();
278         /// write access to cursor cell index
279         idx_type & idx();
280
281         /// path of positions the cursor had to go if it were leaving each inset
282         MathIterator Cursor_;
283         /// path of positions the anchor had to go if it were leaving each inset
284         mutable MathIterator Anchor_;
285         /// pointer to enclsing LyX inset
286         InsetFormulaBase * formula_;
287         // Selection stuff
288         /// text code of last char entered
289         //MathTextCodes lastcode_;
290         /// do we allow autocorrection
291         bool autocorrect_;
292         /// do we currently select
293         bool selection_;
294         /// are we entering a macro name?
295         bool macromode_;
296         /// are we targeting a certain x coordinate, if so, which one?
297         int targetx_;
298 };
299
300 extern MathCursor * mathcursor;
301 void releaseMathCursor(BufferView * bv);
302
303 #endif