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