]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
96d0348085baebb5a80f0bfa5afc51de198e5ad9
[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_inset.h"
25 #include "math_pos.h"
26 #include "LString.h"
27
28 class InsetFormulaBase;
29 class MathPainterInfo;
30 class MathUnknownInset;
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         ~MathCursor();
64         ///
65         void insert(MathAtom const &);
66         ///
67         void insert(MathArray const &);
68         ///
69         void paste(MathArray const &);
70         ///
71         void paste(MathGridInset const & data);
72         ///
73         void erase();
74         ///
75         void backspace();
76         /// called for LFUN_HOME etc
77         void home(bool sel = false);
78         /// called for LFUN_END etc
79         void end(bool sel = false);
80         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
81         bool right(bool sel = false);
82         /// called for LFUN_LEFT etc
83         bool left(bool sel = false);
84         /// called for LFUN_UP etc
85         bool up(bool sel = false);
86         /// called for LFUN_DOWN etc
87         bool down(bool sel = false);
88         /// Put the cursor in the first position
89         void first();
90         /// Put the cursor in the last position
91         void last();
92         /// move to next cell in current inset
93         void idxNext();
94         /// move to previous cell in current inset
95         void idxPrev();
96         ///
97         void plainErase();
98         ///
99         void plainInsert(MathAtom const &);
100         ///
101         void niceInsert(MathAtom const &);
102
103         ///
104         void delLine();
105         /// in pixels from top of screen
106         void setPos(int x, int y);
107         /// in pixels from top of screen
108         void getPos(int & x, int & y);
109         /// current inset
110         MathInset * par() const;
111         /// return the next enclosing grid inset and the cursor's index in it
112         MathGridInset * enclosingGrid(idx_type & idx) const;
113         /// return the next enclosing hull inset and the cursor's index in it
114         MathHullInset * enclosingHull(idx_type & idx) const;
115         /// go up to enclosing grid
116         void popToEnclosingGrid();
117         /// go up to the hull inset
118         void popToEnclosingHull();
119         ///
120         InsetFormulaBase * formula() const;
121         /// current offset in the current cell
122         pos_type pos() const;
123         /// current cell
124         idx_type idx() const;
125         /// size of current cell
126         size_type size() const;
127         ///
128         bool script(bool);
129         ///
130         bool interpret(string const &);
131         /// 
132         bool interpret(char);
133         ///
134         bool toggleLimits();
135         /// interpret name a name of a macro
136         void macroModeClose();
137         /// are we currently typing the name of a macro?
138         MathUnknownInset * inMacroMode() const;
139         /// are we currently typing '#1' or '#2' or...?
140         bool inMacroArgMode() const;
141         /// are we in an mbox?
142         bool inMathMode() const;
143
144         // Local selection methods
145         ///
146         bool selection() const;
147         ///
148         void selCopy();
149         ///
150         void selCut();
151         ///
152         void selDel();
153         ///
154         void selPaste();
155         ///
156         void selHandle(bool);
157         ///
158         void selStart();
159         ///
160         void selClear();
161         /// clears or deletes selection depending on lyxrc setting
162         void selClearOrDel();
163         ///
164         void selGet(MathArray & ar);
165         ///
166         void drawSelection(MathPainterInfo & pain) const;
167         ///
168         void handleDelim(string const & l, string const & r);
169         ///
170         void handleNest(MathInset * p);
171         /// splits cells and shifts right part to the next cell
172         void splitCell();
173         /// splits line and insert new row of cell
174         void breakLine();
175         /// read contents of line into an array
176         void readLine(MathArray & ar) const;
177         /// remove this as soon as LyXFunc::getStatus is "localized"
178         string getLastCode() const { return "mathnormal"; }
179         ///
180         void pullArg(bool goright);
181         ///
182         bool isInside(MathInset const *) const;
183         ///
184         char valign() const;
185         ///
186         char halign() const;
187         ///
188         col_type hullCol() const;
189         ///
190         row_type hullRow() const;
191         ///
192         col_type gridCol() const;
193         ///
194         row_type gridRow() const;
195
196         /// make sure cursor position is valid
197         void normalize();
198         /// mark current cursor trace for redraw
199         void touch();
200         ///
201         UpdatableInset * asHyperActiveInset() 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         /// reference to the last item of the path, i.e. "The Cursor"
237         MathCursorPos & cursor();
238         /// reference to the last item of the path, i.e. "The Cursor"
239         MathCursorPos const & cursor() const;
240         /// how deep are we nested?
241         unsigned depth() const;
242
243         /// local dispatcher
244         int dispatch(string const & cmd);
245         /// describe the situation
246         string info() const;
247         /// dump selection information for debugging
248         void seldump(char const * str) const;
249         /// dump selection information for debugging
250         void dump(char const * str) const;
251         /// moves on
252         void setSelection(cursor_type const & where, size_type n);
253         ///
254         void insert(char c);
255         ///
256         void insert(string const & str);
257         /// lock/unlock inset
258         void insetToggle();
259
260         /// hack for reveal codes
261         void markInsert();
262         void markErase();
263         void handleExtern(string const & arg);
264
265         ///
266         friend class Selection;
267
268
269 private:
270         /// moves cursor index one cell to the left
271         bool idxLeft();
272         /// moves cursor index one cell to the right
273         bool idxRight();
274         /// moves cursor to beginning first cell of current line
275         bool idxLineFirst();
276         /// moves cursor to end of last cell of current line
277         bool idxLineLast();
278         /// moves cursor position one cell to the left
279         bool posLeft();
280         /// moves cursor position one cell to the right
281         bool posRight();
282         /// moves position somehow up or down
283         bool goUpDown(bool up);
284         /// moves position into box
285         bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
286
287
288         /// grab grid marked by anchor and current cursor 
289         MathGridInset grabSelection() const;
290         /// erase the selected part and re-sets the cursor
291         void eraseSelection();
292         /// guess what
293         MathGridInset grabAndEraseSelection();
294
295         ///
296         string macroName() const;
297         ///
298         MathInset::difference_type macroNamePos() const;
299         /// can we enter the inset?
300         bool openable(MathAtom const &, bool selection) const;
301         /// write access to cursor cell position
302         pos_type & pos();
303         /// write access to cursor cell index
304         idx_type & idx();
305
306         /// path of positions the cursor had to go if it were leaving each inset
307         cursor_type Cursor_;
308         /// path of positions the anchor had to go if it were leaving each inset
309         mutable cursor_type Anchor_;
310         /// pointer to enclsing LyX inset
311         InsetFormulaBase * formula_;
312         // Selection stuff
313         /// text code of last char entered
314         //MathTextCodes lastcode_;
315         /// do we allow autocorrection
316         bool autocorrect_;
317         /// do we currently select
318         bool selection_;
319         /// are we entering a macro name?
320         bool macromode_;
321         /// are we targeting a certain x coordinate, if so, which one?
322         int targetx_;
323 };
324
325 extern MathCursor * mathcursor;
326
327 #endif