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