]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
Use references instead of pointers where possible
[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
27 class MathInset;
28 class MathAtom;
29 class MathArrayInset;
30 class MathFuncInset;
31 class MathMatrixInset;
32 class MathScriptInset;
33 class MathSpaceInset;
34 class InsetFormulaBase;
35 class MathArray;
36 class MathXArray;
37 class Painter;
38 class Selection;
39 class latexkeys;
40
41 /// Description of a position 
42 struct MathCursorPos {
43         /// inset
44         MathAtom * par_;
45         /// cell index
46         MathInset::idx_type idx_;
47         /// cell position
48         MathInset::pos_type pos_;
49
50         /// returns cell corresponding to this position
51         MathArray & cell() const;
52         /// returns cell corresponding to this position
53         MathArray & cell(MathInset::idx_type idx) const;
54         /// returns xcell corresponding to this position
55         MathXArray & xcell() const;
56         /// returns xcell corresponding to this position
57         MathXArray & xcell(MathInset::idx_type idx) const;
58 };
59
60 /// 
61 bool operator==(MathCursorPos const &, MathCursorPos const &);
62 /// 
63 bool operator<(MathCursorPos const &, MathCursorPos const &);
64
65
66 /// This is the external interface of Math's subkernel
67 class MathCursor {
68 public:
69         /// short of anything else reasonable
70         typedef MathInset::size_type    size_type;
71         /// type for cursor positions within a cell
72         typedef MathInset::pos_type     pos_type;
73         /// type for cell indices
74         typedef MathInset::idx_type     idx_type;
75         /// type for row numbers
76         typedef MathInset::row_type     row_type;
77         /// type for column numbers
78         typedef MathInset::col_type     col_type;
79
80         ///
81         explicit MathCursor(InsetFormulaBase *, bool left);
82         ///
83         void insert(MathAtom const &);
84         ///
85         void insert(MathArray const &);
86         ///
87         void paste(MathArray const &);
88         ///
89         void erase();
90         ///
91         void backspace();
92         ///
93         void home(bool sel = false);
94         ///
95         void end(bool sel = false);
96         ///
97         bool right(bool sel = false);
98         ///
99         bool left(bool sel = false);
100         ///
101         bool up(bool sel = false);
102         ///
103         bool down(bool sel = false);
104         /// Put the cursor in the first position
105         void first();
106         /// Put the cursor in the last position
107         void last();
108         ///
109         void idxNext();
110         ///
111         void idxPrev();
112         ///
113         void plainErase();
114         ///
115         void plainInsert(MathAtom const &);
116         ///
117         void niceInsert(MathAtom const &);
118
119         ///
120         void delLine();
121         /// This is in pixels from (maybe?) the top of inset
122         void setPos(int, int);
123         ///
124         void getPos(int & x, int & y);
125         ///
126         MathAtom & par() const;
127         /// return the next enclosing grid inset and the cursor's index in it
128         MathArrayInset * enclosingArray(idx_type &) const;
129         ///
130         InsetFormulaBase const * formula();
131         ///
132         pos_type pos() const;
133         ///
134         idx_type idx() const;
135         ///
136         size_type size() const;
137         ///
138         void interpret(string const &);
139         ///
140         void interpret(char);
141         ///
142         void setSize(MathStyles);
143         ///
144         bool toggleLimits();
145         ///
146         // Macro mode methods
147         void macroModeClose();
148         ///
149         bool inMacroMode() const;
150         
151         // Local selection methods
152         ///
153         bool selection() const;
154         ///
155         void selCopy();
156         ///
157         void selCut();
158         ///
159         void selDel();
160         ///
161         void selPaste();
162         ///
163         void selHandle(bool);
164         ///
165         void selStart();
166         ///
167         void selClear();
168         ///
169         void drawSelection(Painter & pain) const;
170         ///
171         void handleFont(MathTextCodes t);
172         ///
173         void handleDelim(string const & l, string const & r);
174         ///
175         void handleNest(MathInset * p);
176         /// Splits cells and shifts right part to the next cell
177         void splitCell();
178         /// Splits line and insert new row of cell 
179         void breakLine();
180         ///
181         MathTextCodes getLastCode() const;
182         ///
183         void pullArg(bool goright);
184         ///
185         bool isInside(MathInset const *) const;
186         ///
187         MathTextCodes nextCode() const;
188         ///
189         char valign() const;
190         ///
191         char halign() const;
192         ///
193         col_type col() const;
194         ///
195         row_type row() const;
196
197         ///
198         MathStyles style() const;
199         /// Make sure cursor position is valid
200         void normalize() const;
201
202         /// enter a MathInset 
203         void push(MathAtom & par);
204         /// enter a MathInset from the front
205         void pushLeft(MathAtom & par);
206         /// enter a MathInset from the back
207         void pushRight(MathAtom & par);
208         /// leave current MathInset to the left
209         bool popLeft();
210         /// leave current MathInset to the left
211         bool popRight();
212
213         ///
214         MathArray & array() const;
215         ///
216         MathXArray & xarray() const;
217         ///
218         bool hasPrevAtom() const;
219         ///
220         bool hasNextAtom() const;
221         ///
222         MathAtom const & prevAtom() const;
223         ///
224         MathAtom & prevAtom();
225         ///
226         MathAtom const & nextAtom() const;
227         ///
228         MathAtom & nextAtom();
229
230         /// returns the selection
231         void getSelection(MathCursorPos &, MathCursorPos &) const;
232         /// returns the normalized anchor of the selection
233         MathCursorPos normalAnchor() const;
234
235         /// path of positions the cursor had to go if it were leving each inset
236         std::vector<MathCursorPos> Cursor_;
237         /// path of positions the anchor had to go if it were leving each inset
238         std::vector<MathCursorPos> Anchor_;
239
240         /// reference to the last item of the path
241         MathCursorPos & cursor();
242         ///
243         MathCursorPos const & cursor() const;
244
245
246         ///  
247         pos_type last() const;
248         ///
249         void seldump(char const * str) const;
250         ///
251         void dump(char const * str) const;
252
253         ///
254         void merge(MathArray const & arr);
255         ///
256         MathScriptInset * prevScriptInset() const;
257         ///
258         MathSpaceInset * prevSpaceInset() const;
259         /// glue adjacent atoms if possible
260         bool glueAdjacentAtoms();
261
262         ///
263         friend class Selection;
264
265 private:
266         /// moves cursor position one cell to the left
267         bool posLeft();
268         /// moves cursor position one cell to the right
269         bool posRight();
270         /// moves cursor index one cell to the left
271         bool idxLeft();
272         /// moves cursor index one cell to the right
273         bool idxRight();
274         /// moves position somehow up
275         bool goUp();
276         /// moves position somehow down
277         bool goDown();
278
279         ///
280         string macroName() const;
281         ///
282         int macroNamePos() const;
283         ///
284         void insert(char, MathTextCodes t);
285         /// can we enter the inset? 
286         bool openable(MathAtom const &, bool selection) const;
287         /// can the setPos routine enter that inset?
288         bool positionable(MathAtom const &, int x, int y) const;
289         /// write access to cursor cell position
290         pos_type & pos();
291         /// write access to cursor cell index
292         idx_type & idx();
293         /// x-offset of current cell relative to par xo
294         int cellXOffset() const;
295         /// y-offset of current cell relative to par yo
296         int cellYOffset() const;
297         /// current x position relative to par xo
298         int xpos() const;
299         /// current y position relative to par yo
300         int ypos() const;
301         /// adjust position in current cell according to x. idx is not changed.
302         void gotoX(int x);
303
304         ///
305         InsetFormulaBase * const formula_;
306         ///
307         MathTextCodes lastcode_;
308         // Selection stuff
309         /// do we currently select
310         bool selection_;
311 };
312
313 extern MathCursor * mathcursor;
314
315 #endif