]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
enable insertion of spaces in all \textxxx modes.
[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_pos.h"
26 #include "LString.h"
27
28 class InsetFormulaBase;
29 class MathPainterInfo;
30 class MathUnknownInset;
31 class Selection;
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 MathCursorPositions, 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         /// how to store a cursor
58         typedef std::vector<MathCursorPos> cursor_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(MathArray const &);
70         ///
71         void paste(MathGridInset const & data);
72         ///
73         void erase();
74         ///
75         void backspace();
76         /// called for LFUN_HOME etc
77         bool home(bool sel = false);
78         /// called for LFUN_END etc
79         bool end(bool sel = false);
80         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
81         bool right(bool sel = false);
82         /// called for LFUN_LEFT etc
83         bool left(bool sel = false);
84         /// called for LFUN_UP etc
85         bool up(bool sel = false);
86         /// called for LFUN_DOWN etc
87         bool down(bool sel = false);
88         /// Put the cursor in the first position
89         void first();
90         /// Put the cursor in the last position
91         void last();
92         /// move to next cell in current inset
93         void idxNext();
94         /// move to previous cell in current inset
95         void idxPrev();
96         ///
97         void plainErase();
98         ///
99         void plainInsert(MathAtom const &);
100         ///
101         void niceInsert(MathAtom const &);
102
103         ///
104         void delLine();
105         /// in pixels from top of screen
106         void setPos(int x, int y);
107         /// in pixels from top of screen
108         void getPos(int & x, int & y);
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         /// return the next enclosing hull inset and the cursor's index in it
114         MathHullInset * enclosingHull(idx_type & idx) const;
115         /// go up to enclosing grid
116         void popToEnclosingGrid();
117         /// go up to the hull inset
118         void popToEnclosingHull();
119         ///
120         InsetFormulaBase * formula() const;
121         /// current offset in the current cell
122         pos_type pos() const;
123         /// current cell
124         idx_type idx() const;
125         /// size of current cell
126         size_type size() const;
127         ///
128         bool script(bool);
129         ///
130         bool interpret(string const &);
131         /// 
132         bool interpret(char);
133         ///
134         bool toggleLimits();
135         /// interpret name a name of a macro
136         void macroModeClose();
137         /// are we currently typing the name of a macro?
138         MathUnknownInset * inMacroMode() const;
139         /// are we currently typing '#1' or '#2' or...?
140         bool inMacroArgMode() const;
141         /// are we in math mode (1), text mode (-1) or unsure?
142         MathInset::mode_type currentMode() const;
143
144         // Local selection methods
145         ///
146         bool selection() const;
147         ///
148         void selCopy();
149         ///
150         void selCut();
151         ///
152         void selDel();
153         ///
154         void selPaste();
155         ///
156         void selHandle(bool);
157         ///
158         void selStart();
159         ///
160         void selClear();
161         /// clears or deletes selection depending on lyxrc setting
162         void selClearOrDel();
163         ///
164         void selGet(MathArray & ar);
165         ///
166         void drawSelection(MathPainterInfo & pain) const;
167         ///
168         void handleNest(MathAtom const & at);
169         /// splits cells and shifts right part to the next cell
170         void splitCell();
171         /// splits line and insert new row of cell
172         void breakLine();
173         /// read contents of line into an array
174         void readLine(MathArray & ar) const;
175         /// remove this as soon as LyXFunc::getStatus is "localized"
176         string getLastCode() const { return "mathnormal"; }
177         ///
178         void pullArg(bool goright);
179         ///
180         bool isInside(MathInset const *) const;
181         ///
182         char valign() const;
183         ///
184         char halign() const;
185         ///
186         col_type hullCol() const;
187         ///
188         row_type hullRow() const;
189         ///
190         col_type gridCol() const;
191         ///
192         row_type gridRow() const;
193
194         /// make sure cursor position is valid
195         void normalize();
196         /// mark current cursor trace for redraw
197         void touch();
198         ///
199         UpdatableInset * asHyperActiveInset() const;
200
201         /// enter a MathInset
202         void push(MathAtom & par);
203         /// enter a MathInset from the front
204         void pushLeft(MathAtom & par);
205         /// enter a MathInset from the back
206         void pushRight(MathAtom & par);
207         /// leave current MathInset to the left
208         bool popLeft();
209         /// leave current MathInset to the left
210         bool popRight();
211
212         ///
213         MathArray & array() const;
214         ///
215         MathXArray & xarray() const;
216         ///
217         bool hasPrevAtom() const;
218         ///
219         bool hasNextAtom() const;
220         ///
221         MathAtom const & prevAtom() const;
222         ///
223         MathAtom & prevAtom();
224         ///
225         MathAtom const & nextAtom() const;
226         ///
227         MathAtom & nextAtom();
228
229         /// returns the selection
230         void getSelection(MathCursorPos &, MathCursorPos &) const;
231         /// returns the normalized anchor of the selection
232         MathCursorPos normalAnchor() const;
233
234         /// reference to the last item of the path, i.e. "The Cursor"
235         MathCursorPos & cursor();
236         /// reference to the last item of the path, i.e. "The Cursor"
237         MathCursorPos const & cursor() const;
238         /// how deep are we nested?
239         unsigned depth() const;
240
241         /// local dispatcher
242         int dispatch(string const & cmd);
243         /// describe the situation
244         string info() const;
245         /// dump selection information for debugging
246         void seldump(char const * str) const;
247         /// dump selection information for debugging
248         void dump(char const * str) const;
249         /// moves on
250         void setSelection(cursor_type const & where, size_type n);
251         ///
252         void insert(char c);
253         ///
254         void insert(string const & str);
255         /// lock/unlock inset
256         void insetToggle();
257
258         /// hack for reveal codes
259         void markInsert();
260         void markErase();
261         void handleExtern(string const & arg);
262
263         ///
264         friend class Selection;
265
266
267 private:
268         /// moves cursor index one cell to the left
269         bool idxLeft();
270         /// moves cursor index one cell to the right
271         bool idxRight();
272         /// moves cursor to beginning first cell of current line
273         bool idxLineFirst();
274         /// moves cursor to end of last cell of current line
275         bool idxLineLast();
276         /// moves cursor position one cell to the left
277         bool posLeft();
278         /// moves cursor position one cell to the right
279         bool posRight();
280         /// moves position somehow up or down
281         bool goUpDown(bool up);
282         /// moves position into box
283         bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
284
285
286         /// grab grid marked by anchor and current cursor 
287         MathGridInset grabSelection() const;
288         /// erase the selected part and re-sets the cursor
289         void eraseSelection();
290         /// guess what
291         MathGridInset grabAndEraseSelection();
292
293         ///
294         string macroName() const;
295         ///
296         MathInset::difference_type macroNamePos() const;
297         /// can we enter the inset?
298         bool openable(MathAtom const &, bool selection) const;
299         /// write access to cursor cell position
300         pos_type & pos();
301         /// write access to cursor cell index
302         idx_type & idx();
303
304         /// path of positions the cursor had to go if it were leaving each inset
305         cursor_type Cursor_;
306         /// path of positions the anchor had to go if it were leaving each inset
307         mutable cursor_type Anchor_;
308         /// pointer to enclsing LyX inset
309         InsetFormulaBase * formula_;
310         // Selection stuff
311         /// text code of last char entered
312         //MathTextCodes lastcode_;
313         /// do we allow autocorrection
314         bool autocorrect_;
315         /// do we currently select
316         bool selection_;
317         /// are we entering a macro name?
318         bool macromode_;
319         /// are we targeting a certain x coordinate, if so, which one?
320         int targetx_;
321 };
322
323 extern MathCursor * mathcursor;
324
325 #endif