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