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