]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
small mouse click stuff. still not ok...
[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         // don't move further in than the Anchor's inset if respect_anchor == true
114         void setPos(int x, int y, bool respect_anchor = false);
115         /// This is in pixels from (maybe?) the top of inset, don't move further
116         ///
117         void getPos(int & x, int & y);
118         ///
119         MathAtom & par() const;
120         /// return the next enclosing grid inset and the cursor's index in it
121         MathGridInset * enclosingGrid(idx_type &) const;
122         ///
123         InsetFormulaBase const * formula();
124         ///
125         pos_type pos() const;
126         ///
127         idx_type idx() const;
128         ///
129         size_type size() const;
130         ///
131         bool interpret(string const &);
132         ///
133         bool interpret(char);
134         ///
135         bool toggleLimits();
136         ///
137         // Macro mode methods
138         void macroModeClose();
139         ///
140         bool inMacroMode() const;
141         
142         // Local selection methods
143         ///
144         bool selection() const;
145         ///
146         void selCopy();
147         ///
148         void selCut();
149         ///
150         void selDel();
151         ///
152         void selPaste();
153         ///
154         void selHandle(bool);
155         ///
156         void selStart();
157         ///
158         void selClear();
159         ///
160         void selGet(MathArray & ar);
161         ///
162         void drawSelection(Painter & pain) const;
163         ///
164         void handleFont(MathTextCodes t);
165         ///
166         void handleDelim(string const & l, string const & r);
167         ///
168         void handleNest(MathInset * p);
169         /// Splits cells and shifts right part to the next cell
170         void splitCell();
171         /// Splits line and insert new row of cell 
172         void breakLine();
173         /// read contents of line into an array
174         void readLine(MathArray & ar) const;
175         ///
176         MathTextCodes getLastCode() const;
177         ///
178         void pullArg(bool goright);
179         ///
180         bool isInside(MathInset const *) const;
181         ///
182         MathTextCodes nextCode() const;
183         ///
184         char valign() const;
185         ///
186         char halign() const;
187         ///
188         col_type ncols() const;
189         ///
190         col_type col() const;
191         ///
192         row_type row() const;
193
194         /// Make sure cursor position is valid
195         void normalize() const;
196         ///
197         UpdatableInset * asHyperActiveInset() const;
198
199         /// enter a MathInset 
200         void push(MathAtom & par);
201         /// enter a MathInset from the front
202         void pushLeft(MathAtom & par);
203         /// enter a MathInset from the back
204         void pushRight(MathAtom & par);
205         /// leave current MathInset to the left
206         bool popLeft();
207         /// leave current MathInset to the left
208         bool popRight();
209
210         ///
211         MathArray & array() const;
212         ///
213         MathXArray & xarray() const;
214         ///
215         bool hasPrevAtom() const;
216         ///
217         bool hasNextAtom() const;
218         ///
219         MathAtom const & prevAtom() const;
220         ///
221         MathAtom & prevAtom();
222         ///
223         MathAtom const & nextAtom() const;
224         ///
225         MathAtom & nextAtom();
226
227         /// returns the selection
228         void getSelection(MathCursorPos &, MathCursorPos &) const;
229         /// returns the normalized anchor of the selection
230         MathCursorPos normalAnchor() const;
231
232         /// path of positions the cursor had to go if it were leving each inset
233         std::vector<MathCursorPos> Cursor_;
234         /// path of positions the anchor had to go if it were leving each inset
235         std::vector<MathCursorPos> Anchor_;
236
237         /// reference to the last item of the path
238         MathCursorPos & cursor();
239         ///
240         MathCursorPos const & cursor() const;
241
242         ///
243         void seldump(char const * str) const;
244         ///
245         void dump(char const * str) const;
246         ///
247         void stripFromLastEqualSign();
248
249         ///
250         friend class Selection;
251
252 private:
253         /// moves cursor position one cell to the left
254         bool posLeft();
255         /// moves cursor position one cell to the right
256         bool posRight();
257         /// moves cursor index one cell to the left
258         bool idxLeft();
259         /// moves cursor index one cell to the right
260         bool idxRight();
261         /// moves position somehow up
262         bool goUp();
263         /// moves position somehow down
264         bool goDown();
265
266         ///
267         string macroName() const;
268         ///
269         int macroNamePos() const;
270         ///
271         void insert(char, MathTextCodes t);
272         /// can we enter the inset? 
273         bool openable(MathAtom const &, bool selection) const;
274         /// can the setPos routine enter that inset?
275         bool positionable(MathAtom const &, int x, int y) const;
276         /// write access to cursor cell position
277         pos_type & pos();
278         /// write access to cursor cell index
279         idx_type & idx();
280
281         ///
282         InsetFormulaBase * const formula_;
283         ///
284         MathTextCodes lastcode_;
285         // Selection stuff
286         /// do we currently select
287         bool selection_;
288 };
289
290 extern MathCursor * mathcursor;
291
292 #endif