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