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