]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
350de76ad90177b7645a1e3b4236c61d75893d39
[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_defs.h"
25 #include "math_inset.h"
26 #include "math_pos.h"
27 #include "LString.h"
28
29 class InsetFormulaBase;
30 class Painter;
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         void insert(MathAtom const &);
64         ///
65         void insert(MathArray const &);
66         ///
67         void paste(MathArray const &);
68         ///
69         void erase();
70         ///
71         void backspace();
72         /// called for LFUN_HOME etc
73         void home(bool sel = false);
74         /// called for LFUN_END etc
75         void 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         ///
100         void delLine();
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);
105         /// current inset
106         MathInset * par() const;
107         /// return the next enclosing grid inset and the cursor's index in it
108         MathGridInset * enclosingGrid(idx_type &) const;
109         /// return the next enclosing grid inset and the cursor's index in it
110         void popToEnclosingGrid();
111         ///
112         InsetFormulaBase * formula();
113         /// current offset in the current cell
114         pos_type pos() const;
115         /// current cell
116         idx_type idx() const;
117         /// size of current cell
118         size_type size() const;
119         ///
120         bool script(bool);
121         ///
122         bool interpret(string const &);
123         ///
124         bool interpret(char);
125         ///
126         bool toggleLimits();
127         /// interpret name a name of a macro
128         void macroModeClose();
129         /// are we currently typing the name of a macro?
130         bool inMacroMode() const;
131         /// are we currently typing '#1' or '#2' or...?
132         bool inMacroArgMode() const;
133
134         // Local selection methods
135         ///
136         bool selection() const;
137         ///
138         void selCopy();
139         ///
140         void selCut();
141         ///
142         void selDel();
143         ///
144         void selPaste();
145         ///
146         void selHandle(bool);
147         ///
148         void selStart();
149         ///
150         void selClear();
151         /// clears or deletes selection depending on lyxrc setting
152         void selClearOrDel();
153         ///
154         void selGet(MathArray & ar);
155         ///
156         void drawSelection(Painter & pain) const;
157         ///
158         void handleFont(MathTextCodes t);
159         ///
160         void handleDelim(string const & l, string const & r);
161         ///
162         void handleNest(MathInset * p);
163         /// splits cells and shifts right part to the next cell
164         void splitCell();
165         /// splits line and insert new row of cell
166         void breakLine();
167         /// read contents of line into an array
168         void readLine(MathArray & ar) const;
169         /// remove this as soon as LyXFunc::getStatus is "localized"
170         MathTextCodes getLastCode() const { return lastcode_; }
171         ///
172         void pullArg(bool goright);
173         ///
174         bool isInside(MathInset const *) const;
175         ///
176         MathTextCodes nextCode() const;
177         ///
178         char valign() const;
179         ///
180         char halign() const;
181         ///
182         col_type hullCol() const;
183         ///
184         row_type hullRow() const;
185         ///
186         col_type gridCol() const;
187         ///
188         row_type gridRow() const;
189
190         /// make sure cursor position is valid
191         void normalize();
192         /// mark current cursor trace for redraw
193         void touch();
194         ///
195         UpdatableInset * asHyperActiveInset() const;
196
197         /// enter a MathInset
198         void push(MathAtom & par);
199         /// enter a MathInset from the front
200         void pushLeft(MathAtom & par);
201         /// enter a MathInset from the back
202         void pushRight(MathAtom & par);
203         /// leave current MathInset to the left
204         bool popLeft();
205         /// leave current MathInset to the left
206         bool popRight();
207
208         ///
209         MathArray & array() const;
210         ///
211         MathXArray & xarray() const;
212         ///
213         bool hasPrevAtom() const;
214         ///
215         bool hasNextAtom() const;
216         ///
217         MathAtom const & prevAtom() const;
218         ///
219         MathAtom & prevAtom();
220         ///
221         MathAtom const & nextAtom() const;
222         ///
223         MathAtom & nextAtom();
224
225         /// returns the selection
226         void getSelection(MathCursorPos &, MathCursorPos &) const;
227         /// returns the normalized anchor of the selection
228         MathCursorPos normalAnchor() const;
229
230         /// reference to the last item of the path, i.e. "The Cursor"
231         MathCursorPos & cursor();
232         /// reference to the last item of the path, i.e. "The Cursor"
233         MathCursorPos const & cursor() const;
234
235         /// describe the situation
236         string info() const;
237         /// dump selection information for debugging
238         void seldump(char const * str) const;
239         /// dump selection information for debugging
240         void dump(char const * str) const;
241         ///
242         void stripFromLastEqualSign();
243         /// moves on
244         void setSelection(cursor_type const & where, size_type n);
245         ///
246         void insert(char);
247
248         /// hack for reveal codes
249         void markInsert();
250         void markErase();
251
252         ///
253         friend class Selection;
254
255 private:
256         /// moves cursor position one cell to the left
257         bool posLeft();
258         /// moves cursor position one cell to the right
259         bool posRight();
260         /// moves cursor index one cell to the left
261         bool idxLeft();
262         /// moves cursor index one cell to the right
263         bool idxRight();
264         /// moves position somehow up or down
265         bool goUpDown(bool up);
266         /// moves position into box
267         bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
268
269         ///
270         string macroName() const;
271         ///
272         MathInset::difference_type macroNamePos() const;
273         ///
274         void insert(char, MathTextCodes t);
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 leving each inset
283         cursor_type Cursor_;
284         /// path of positions the anchor had to go if it were leving each inset
285         mutable cursor_type Anchor_;
286         /// pointer to enclsing LyX inset
287         InsetFormulaBase * formula_;
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 };
295
296 extern MathCursor * mathcursor;
297
298 #endif