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