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