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