]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
3fde675960bca184cfba884d9092ed34a562fe5a
[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 BufferView;
30 class MathPainterInfo;
31 class MathUnknownInset;
32 class Selection;
33
34 /**
35
36 [Have a look at math_inset.h first]
37
38 The MathCursor is different from the kind of cursor used in the Outer
39 World. It contains a stack of MathCursorPositions, each of which is made
40 up of a inset pointer, an index and a position offset, marking a path from
41 this formula's mathHullInset to the current position.
42
43 */
44
45
46 class MathCursor {
47 public:
48         /// short of anything else reasonable
49         typedef MathInset::size_type       size_type;
50         /// type for cursor positions within a cell
51         typedef MathInset::pos_type        pos_type;
52         /// type for cell indices
53         typedef MathInset::idx_type        idx_type;
54         /// type for row numbers
55         typedef MathInset::row_type        row_type;
56         /// type for column numbers
57         typedef MathInset::col_type        col_type;
58         /// how to store a cursor
59         typedef std::vector<MathCursorPos> cursor_type;
60
61         ///
62         explicit MathCursor(InsetFormulaBase *, bool left);
63         ///
64         ~MathCursor();
65         ///
66         void insert(MathAtom const &);
67         ///
68         void insert(MathArray const &);
69         ///
70         void paste(MathArray const &);
71         ///
72         void paste(MathGridInset const & data);
73         ///
74         void erase();
75         ///
76         void backspace();
77         /// called for LFUN_HOME etc
78         bool home(bool sel = false);
79         /// called for LFUN_END etc
80         bool end(bool sel = false);
81         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
82         bool right(bool sel = false);
83         /// called for LFUN_LEFT etc
84         bool left(bool sel = false);
85         /// called for LFUN_UP etc
86         bool up(bool sel = false);
87         /// called for LFUN_DOWN etc
88         bool down(bool sel = false);
89         /// Put the cursor in the first position
90         void first();
91         /// Put the cursor in the last position
92         void last();
93         /// move to next cell in current inset
94         void idxNext();
95         /// move to previous cell in current inset
96         void idxPrev();
97         ///
98         void plainErase();
99         ///
100         void plainInsert(MathAtom const &);
101         ///
102         void niceInsert(MathAtom const &);
103
104         ///
105         void delLine();
106         /// in pixels from top of screen
107         void setPos(int x, int y);
108         /// in pixels from top of screen
109         void getPos(int & x, int & y);
110         /// current inset
111         MathInset * par() const;
112         /// return the next enclosing grid inset and the cursor's index in it
113         MathGridInset * enclosingGrid(idx_type & idx) const;
114         /// return the next enclosing hull inset and the cursor's index in it
115         MathHullInset * enclosingHull(idx_type & idx) const;
116         /// go up to enclosing grid
117         void popToEnclosingGrid();
118         /// go up to the hull inset
119         void popToEnclosingHull();
120         ///
121         InsetFormulaBase * formula() const;
122         /// current offset in the current cell
123         pos_type pos() const;
124         /// current cell
125         idx_type idx() const;
126         /// size of current cell
127         size_type size() const;
128         ///
129         bool script(bool);
130         ///
131         bool interpret(string const &);
132         /// 
133         bool interpret(char);
134         ///
135         bool toggleLimits();
136         /// interpret name a name of a macro
137         void macroModeClose();
138         /// are we currently typing the name of a macro?
139         MathUnknownInset * inMacroMode() const;
140         /// are we currently typing '#1' or '#2' or...?
141         bool inMacroArgMode() const;
142         /// are we in math mode (1), text mode (-1) or unsure?
143         MathInset::mode_type currentMode() const;
144
145         // Local selection methods
146         ///
147         bool selection() const;
148         ///
149         void selCopy();
150         ///
151         void selCut();
152         ///
153         void selDel();
154         ///
155         void selPaste();
156         ///
157         void selHandle(bool);
158         ///
159         void selStart();
160         ///
161         void selClear();
162         /// clears or deletes selection depending on lyxrc setting
163         void selClearOrDel();
164         ///
165         void selGet(MathArray & ar);
166         ///
167         void drawSelection(MathPainterInfo & pain) const;
168         ///
169         void handleNest(MathAtom const & at);
170         /// splits cells and shifts right part to the next cell
171         void splitCell();
172         /// splits line and insert new row of cell
173         void breakLine();
174         /// read contents of line into an array
175         void readLine(MathArray & ar) const;
176         /// remove this as soon as LyXFunc::getStatus is "localized"
177         string getLastCode() const { return "mathnormal"; }
178         ///
179         void pullArg(bool goright);
180         ///
181         bool isInside(MathInset const *) const;
182         ///
183         char valign() const;
184         ///
185         char halign() const;
186         ///
187         col_type hullCol() const;
188         ///
189         row_type hullRow() const;
190         ///
191         col_type gridCol() const;
192         ///
193         row_type gridRow() const;
194
195         /// make sure cursor position is valid
196         void normalize();
197         /// mark current cursor trace for redraw
198         void touch();
199         ///
200         UpdatableInset * asHyperActiveInset() const;
201
202         /// enter a MathInset
203         void push(MathAtom & par);
204         /// enter a MathInset from the front
205         void pushLeft(MathAtom & par);
206         /// enter a MathInset from the back
207         void pushRight(MathAtom & par);
208         /// leave current MathInset to the left
209         bool popLeft();
210         /// leave current MathInset to the left
211         bool popRight();
212
213         ///
214         MathArray & array() const;
215         ///
216         MathXArray & xarray() const;
217         ///
218         bool hasPrevAtom() const;
219         ///
220         bool hasNextAtom() const;
221         ///
222         MathAtom const & prevAtom() const;
223         ///
224         MathAtom & prevAtom();
225         ///
226         MathAtom const & nextAtom() const;
227         ///
228         MathAtom & nextAtom();
229
230         /// returns the selection
231         void getSelection(MathCursorPos &, MathCursorPos &) const;
232         /// returns the normalized anchor of the selection
233         MathCursorPos normalAnchor() const;
234
235         /// reference to the last item of the path, i.e. "The Cursor"
236         MathCursorPos & cursor();
237         /// reference to the last item of the path, i.e. "The Cursor"
238         MathCursorPos const & cursor() const;
239         /// how deep are we nested?
240         unsigned depth() const;
241
242         /// local dispatcher
243         int dispatch(string const & cmd);
244         /// describe the situation
245         string info() const;
246         /// dump selection information for debugging
247         void seldump(char const * str) const;
248         /// dump selection information for debugging
249         void dump(char const * str) const;
250         /// moves on
251         void setSelection(cursor_type const & where, size_type n);
252         ///
253         void insert(char c);
254         ///
255         void insert(string const & str);
256         /// lock/unlock inset
257         void insetToggle();
258
259         /// hack for reveal codes
260         void markInsert();
261         void markErase();
262         void handleExtern(string const & arg);
263
264         ///
265         friend class Selection;
266
267
268 private:
269         /// moves cursor index one cell to the left
270         bool idxLeft();
271         /// moves cursor index one cell to the right
272         bool idxRight();
273         /// moves cursor to beginning first cell of current line
274         bool idxLineFirst();
275         /// moves cursor to end of last cell of current line
276         bool idxLineLast();
277         /// moves cursor position one cell to the left
278         bool posLeft();
279         /// moves cursor position one cell to the right
280         bool posRight();
281         /// moves position somehow up or down
282         bool goUpDown(bool up);
283         /// moves position into box
284         bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
285
286
287         /// grab grid marked by anchor and current cursor 
288         MathGridInset grabSelection() const;
289         /// erase the selected part and re-sets the cursor
290         void eraseSelection();
291         /// guess what
292         MathGridInset grabAndEraseSelection();
293
294         ///
295         string macroName() const;
296         ///
297         MathInset::difference_type macroNamePos() const;
298         /// can we enter the inset?
299         bool openable(MathAtom const &, bool selection) const;
300         /// write access to cursor cell position
301         pos_type & pos();
302         /// write access to cursor cell index
303         idx_type & idx();
304
305         /// path of positions the cursor had to go if it were leaving each inset
306         cursor_type Cursor_;
307         /// path of positions the anchor had to go if it were leaving each inset
308         mutable cursor_type Anchor_;
309         /// pointer to enclsing LyX inset
310         InsetFormulaBase * formula_;
311         // Selection stuff
312         /// text code of last char entered
313         //MathTextCodes lastcode_;
314         /// do we allow autocorrection
315         bool autocorrect_;
316         /// do we currently select
317         bool selection_;
318         /// are we entering a macro name?
319         bool macromode_;
320         /// are we targeting a certain x coordinate, if so, which one?
321         int targetx_;
322 };
323
324 extern MathCursor * mathcursor;
325 void releaseMathCursor(BufferView * bv);
326
327 #endif