]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
b59c376894384eeb0ae019c34f60706c6e4ea2ff
[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 setSize(MathStyles);
145         ///
146         bool toggleLimits();
147         ///
148         // Macro mode methods
149         void macroModeClose();
150         ///
151         bool inMacroMode() const;
152         
153         // Local selection methods
154         ///
155         bool selection() const;
156         ///
157         void selCopy();
158         ///
159         void selCut();
160         ///
161         void selDel();
162         ///
163         void selPaste();
164         ///
165         void selHandle(bool);
166         ///
167         void selStart();
168         ///
169         void selClear();
170         ///
171         void drawSelection(Painter & pain) const;
172         ///
173         void handleFont(MathTextCodes t);
174         ///
175         void handleDelim(string const & l, string const & r);
176         ///
177         void handleNest(MathInset * p);
178         /// Splits cells and shifts right part to the next cell
179         void splitCell();
180         /// Splits line and insert new row of cell 
181         void breakLine();
182         ///
183         MathTextCodes getLastCode() const;
184         ///
185         void pullArg(bool goright);
186         ///
187         bool isInside(MathInset const *) const;
188         ///
189         MathTextCodes nextCode() const;
190         ///
191         char valign() const;
192         ///
193         char halign() const;
194         ///
195         col_type col() const;
196         ///
197         row_type row() const;
198
199         ///
200         MathStyles style() const;
201         /// Make sure cursor position is valid
202         void normalize() const;
203         
204         /// enter a MathInset from the front
205         void pushLeft(MathInset * par);
206         /// enter a MathInset from the back
207         void pushRight(MathInset * 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         MathAtom const * prevAtom() const;
219         ///
220         MathAtom * prevAtom();
221         ///
222         MathAtom const * nextAtom() const;
223         ///
224         MathAtom * nextAtom();
225
226         /// returns the selection
227         void getSelection(MathCursorPos &, MathCursorPos &) const;
228         /// returns the normalized anchor of the selection
229         MathCursorPos normalAnchor() const;
230
231         /// path of positions the cursor had to go if it were leving each inset
232         std::vector<MathCursorPos> Cursor_;
233         /// path of positions the anchor had to go if it were leving each inset
234         std::vector<MathCursorPos> Anchor_;
235
236         /// reference to the last item of the path
237         MathCursorPos & cursor();
238         ///
239         MathCursorPos const & cursor() const;
240
241
242         ///  
243         pos_type last() const;
244         ///
245         MathMatrixInset * outerPar() const;
246         ///
247         void seldump(char const * str) const;
248         ///
249         void dump(char const * str) const;
250
251         ///
252         void merge(MathArray const & arr);
253         ///
254         MathInset * nextInset() const;
255         ///
256         MathInset * prevInset() const;
257         ///
258         MathScriptInset * prevScriptInset() const;
259         ///
260         MathSpaceInset * prevSpaceInset() const;
261
262 private:
263         /// moves cursor position one cell to the left
264         bool posLeft();
265         /// moves cursor position one cell to the right
266         bool posRight();
267         /// moves cursor index one cell to the left
268         bool idxLeft();
269         /// moves cursor index one cell to the right
270         bool idxRight();
271         /// moves position somehow up
272         bool goUp();
273         /// moves position somehow down
274         bool goDown();
275         /// glue adjacent atoms if possible
276         void glueAdjacentAtoms();
277
278         ///
279         string macroName() const;
280         ///
281         void insert(char, MathTextCodes t);
282         /// can we enter the inset? 
283         bool openable(MathInset *, bool selection) const;
284         /// can the setPos routine enter that inset?
285         MathInset * positionable(MathAtom *, int x, int y) const;
286         /// write access to "inner" flag
287         bool & inner();
288         /// write access to cursor cell position
289         pos_type & pos();
290         /// write access to cursor cell index
291         idx_type & idx();
292         /// x-offset of current cell relative to par xo
293         int cellXOffset() const;
294         /// y-offset of current cell relative to par yo
295         int cellYOffset() const;
296         /// current x position relative to par xo
297         int xpos() const;
298         /// current y position relative to par yo
299         int ypos() const;
300         /// adjust position in current cell according to x. idx is not changed.
301         void gotoX(int x);
302
303         ///
304         InsetFormulaBase * const formula_;
305         ///
306         MathTextCodes lastcode_;
307         // Selection stuff
308         /// do we currently select
309         bool selection_;
310 };
311
312 extern MathCursor * mathcursor;
313
314 #endif