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