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