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