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