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