]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
small stuff, fix for one of the bugs John mentioned
[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 cursor positions within a cell
50         typedef MathInset::pos_type        pos_type;
51         /// type for cell indices
52         typedef MathInset::idx_type        idx_type;
53         /// type for row numbers
54         typedef MathInset::row_type        row_type;
55         /// type for column numbers
56         typedef MathInset::col_type        col_type;
57
58         ///
59         explicit MathCursor(InsetFormulaBase *, bool left);
60         ///
61         ~MathCursor();
62         ///
63         void insert(MathAtom const &);
64         ///
65         void insert(MathArray const &);
66         ///
67         void paste(string const & data);
68         ///
69         void erase();
70         ///
71         void backspace();
72         /// called for LFUN_HOME etc
73         bool home(bool sel = false);
74         /// called for LFUN_END etc
75         bool end(bool sel = false);
76         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
77         bool right(bool sel = false);
78         /// called for LFUN_LEFT etc
79         bool left(bool sel = false);
80         /// called for LFUN_UP etc
81         bool up(bool sel = false);
82         /// called for LFUN_DOWN etc
83         bool down(bool sel = false);
84         /// Put the cursor in the first position
85         void first();
86         /// Put the cursor in the last position
87         void last();
88         /// move to next cell in current inset
89         void idxNext();
90         /// move to previous cell in current inset
91         void idxPrev();
92         ///
93         void plainErase();
94         ///
95         void plainInsert(MathAtom const &);
96         ///
97         void niceInsert(MathAtom const &);
98         ///
99         void niceInsert(string const &);
100
101         /// in pixels from top of screen
102         void setPos(int x, int y);
103         /// in pixels from top of screen
104         void getPos(int & x, int & y) const;
105         /// in pixels from left of screen
106         int targetX() const;
107         /// current inset
108         MathInset * par() const;
109         /// return the next enclosing grid inset and the cursor's index in it
110         MathGridInset * enclosingGrid(idx_type & idx) const;
111         /// go up to enclosing grid
112         void popToEnclosingGrid();
113         /// go up to the hull inset
114         void popToEnclosingHull();
115         /// go up to the hull inset
116         void popToHere(MathInset const * p);
117         ///
118         InsetFormulaBase * formula() const;
119         /// current offset in the current cell
120         pos_type pos() const;
121         /// current cell
122         idx_type idx() const;
123         /// size of current cell
124         size_type size() const;
125         ///
126         bool script(bool);
127         /// 
128         bool interpret(char);
129         /// interpret name a name of a macro
130         void macroModeClose();
131         /// are we currently typing the name of a macro?
132         bool inMacroMode() const;
133         /// get access to the macro we are currently typing
134         MathUnknownInset * activeMacro();
135         /// get access to the macro we are currently typing
136         MathUnknownInset const * activeMacro() const;
137         /// are we currently typing '#1' or '#2' or...?
138         bool inMacroArgMode() const;
139         /// are we in math mode (1), text mode (-1) or unsure?
140         MathInset::mode_type currentMode() const;
141
142         // Local selection methods
143         ///
144         bool selection() const;
145         ///
146         void selCopy();
147         ///
148         void selCut();
149         ///
150         void selDel();
151         ///
152         void selPaste();
153         ///
154         void selHandle(bool);
155         ///
156         void selStart();
157         ///
158         void selClear();
159         /// clears or deletes selection depending on lyxrc setting
160         void selClearOrDel();
161         /// draws light-blue selection background
162         void drawSelection(MathPainterInfo & pi) const;
163         ///
164         void handleNest(MathAtom const & at);
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(MathCursorPos &, MathCursorPos &) const;
209         /// returns the normalized anchor of the selection
210         MathCursorPos normalAnchor() const;
211
212         /// reference to the last item of the path, i.e. "The Cursor"
213         MathCursorPos & cursor();
214         /// reference to the last item of the path, i.e. "The Cursor"
215         MathCursorPos const & cursor() const;
216         /// how deep are we nested?
217         unsigned depth() const;
218
219         /// local dispatcher
220         MathInset::result_type 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         ///
232         void insert(char c);
233         ///
234         void insert(string const & str);
235         /// lock/unlock inset
236         void insetToggle();
237
238         /// hack for reveal codes
239         void markInsert();
240         void markErase();
241         //void handleExtern(string const & arg);
242
243 private:
244         /// injects content of a cell into parent
245         void pullArg();
246         /// moves cursor index one cell to the left
247         bool idxLeft();
248         /// moves cursor index one cell to the right
249         bool idxRight();
250         /// moves cursor to end of last cell of current line
251         bool idxLineLast();
252         /// moves cursor position one cell to the left
253         bool posLeft();
254         /// moves cursor position one cell to the right
255         bool posRight();
256         /// moves position somehow up or down
257         bool goUpDown(bool up);
258         /// moves position closest to (x, y) in given box
259         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
260         /// moves position closest to (x, y) in current cell
261         void bruteFind2(int x, int y);
262         /// are we in a nucleus of a script inset?
263         bool inNucleus() const;
264
265         /// erase the selected part and re-sets the cursor
266         void eraseSelection();
267         /// guess what
268         string grabAndEraseSelection();
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         MathInset::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