]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
Reduce Michael's buglist.
[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
71         ///
72         explicit MathCursor(InsetFormulaBase *, bool left);
73         ///
74         void insert(MathAtom const &);
75         ///
76         void insert(MathArray const &);
77         ///
78         void paste(MathArray const &);
79         ///
80         void erase();
81         ///
82         void backspace();
83         ///
84         void home(bool sel = false);
85         ///
86         void end(bool sel = false);
87         ///
88         bool right(bool sel = false);
89         ///
90         bool left(bool sel = false);
91         ///
92         bool up(bool sel = false);
93         ///
94         bool down(bool sel = false);
95         /// Put the cursor in the first position
96         void first();
97         /// Put the cursor in the last position
98         void last();
99         ///
100         void idxNext();
101         ///
102         void idxPrev();
103         ///
104         void plainErase();
105         ///
106         void plainInsert(MathAtom const &);
107         ///
108         void niceInsert(MathAtom const &);
109
110         ///
111         void delLine();
112         /// This is in pixels from (maybe?) the top of inset
113         void setPos(int x, int y);
114         /// This is in pixels from (maybe?) the top of inset, don't move further
115         ///
116         void getPos(int & x, int & y);
117         ///
118         MathAtom & par() const;
119         /// return the next enclosing grid inset and the cursor's index in it
120         MathGridInset * enclosingGrid(idx_type &) const;
121         ///
122         InsetFormulaBase const * formula();
123         ///
124         pos_type pos() const;
125         ///
126         idx_type idx() const;
127         ///
128         size_type size() const;
129         ///
130         bool interpret(string const &);
131         ///
132         bool interpret(char);
133         ///
134         bool toggleLimits();
135         ///
136         // Macro mode methods
137         void macroModeClose();
138         ///
139         bool inMacroMode() const;
140         
141         // Local selection methods
142         ///
143         bool selection() const;
144         ///
145         void selCopy();
146         ///
147         void selCut();
148         ///
149         void selDel();
150         ///
151         void selPaste();
152         ///
153         void selHandle(bool);
154         ///
155         void selStart();
156         ///
157         void selClear();
158         ///
159         void selGet(MathArray & ar);
160         ///
161         void drawSelection(Painter & pain) const;
162         ///
163         void handleFont(MathTextCodes t);
164         ///
165         void handleDelim(string const & l, string const & r);
166         ///
167         void handleNest(MathInset * p);
168         /// Splits cells and shifts right part to the next cell
169         void splitCell();
170         /// Splits line and insert new row of cell 
171         void breakLine();
172         /// read contents of line into an array
173         void readLine(MathArray & ar) const;
174         ///
175         MathTextCodes getLastCode() const;
176         ///
177         void pullArg(bool goright);
178         ///
179         bool isInside(MathInset const *) const;
180         ///
181         MathTextCodes nextCode() const;
182         ///
183         char valign() const;
184         ///
185         char halign() const;
186         ///
187         col_type ncols() const;
188         ///
189         col_type col() const;
190         ///
191         row_type row() const;
192
193         /// Make sure cursor position is valid
194         void normalize() const;
195         ///
196         UpdatableInset * asHyperActiveInset() const;
197
198         /// enter a MathInset 
199         void push(MathAtom & par);
200         /// enter a MathInset from the front
201         void pushLeft(MathAtom & par);
202         /// enter a MathInset from the back
203         void pushRight(MathAtom & par);
204         /// leave current MathInset to the left
205         bool popLeft();
206         /// leave current MathInset to the left
207         bool popRight();
208
209         ///
210         MathArray & array() const;
211         ///
212         MathXArray & xarray() const;
213         ///
214         bool hasPrevAtom() const;
215         ///
216         bool hasNextAtom() 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         void seldump(char const * str) const;
243         ///
244         void dump(char const * str) const;
245         ///
246         void stripFromLastEqualSign();
247
248         ///
249         friend class Selection;
250
251 private:
252         /// moves cursor position one cell to the left
253         bool posLeft();
254         /// moves cursor position one cell to the right
255         bool posRight();
256         /// moves cursor index one cell to the left
257         bool idxLeft();
258         /// moves cursor index one cell to the right
259         bool idxRight();
260         /// moves position somehow up
261         bool goUp();
262         /// moves position somehow down
263         bool goDown();
264
265         ///
266         string macroName() const;
267         ///
268         int macroNamePos() const;
269         ///
270         void insert(char, MathTextCodes t);
271         /// can we enter the inset? 
272         bool openable(MathAtom const &, bool selection) const;
273         /// can the setPos routine enter that inset?
274         bool positionable(MathAtom const &, int x, int y) const;
275         /// write access to cursor cell position
276         pos_type & pos();
277         /// write access to cursor cell index
278         idx_type & idx();
279
280         ///
281         InsetFormulaBase * const formula_;
282         ///
283         MathTextCodes lastcode_;
284         // Selection stuff
285         /// do we currently select
286         bool selection_;
287 };
288
289 extern MathCursor * mathcursor;
290
291 #endif