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