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