]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
Running Angus' rmcopyright.sh and hand-correcting a few cases.
[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(MathGridInset const & data);
69         ///
70         void erase();
71         ///
72         void backspace();
73         /// called for LFUN_HOME etc
74         bool home(bool sel = false);
75         /// called for LFUN_END etc
76         bool end(bool sel = false);
77         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
78         bool right(bool sel = false);
79         /// called for LFUN_LEFT etc
80         bool left(bool sel = false);
81         /// called for LFUN_UP etc
82         bool up(bool sel = false);
83         /// called for LFUN_DOWN etc
84         bool down(bool sel = false);
85         /// Put the cursor in the first position
86         void first();
87         /// Put the cursor in the last position
88         void last();
89         /// move to next cell in current inset
90         void idxNext();
91         /// move to previous cell in current inset
92         void idxPrev();
93         ///
94         void plainErase();
95         ///
96         void plainInsert(MathAtom const &);
97         ///
98         void niceInsert(MathAtom const &);
99
100         /// in pixels from top of screen
101         void setPos(int x, int y);
102         /// in pixels from top of screen
103         void getPos(int & x, int & y) const;
104         /// in pixels from left of screen
105         int targetX() const;
106         /// current inset
107         MathInset * par() const;
108         /// return the next enclosing grid inset and the cursor's index in it
109         MathGridInset * enclosingGrid(idx_type & idx) const;
110         /// go up to enclosing grid
111         void popToEnclosingGrid();
112         /// go up to the hull inset
113         void popToEnclosingHull();
114         /// go up to the hull inset
115         void popToHere(MathInset const * p);
116         ///
117         InsetFormulaBase * formula() const;
118         /// current offset in the current cell
119         pos_type pos() const;
120         /// current cell
121         idx_type idx() const;
122         /// size of current cell
123         size_type size() const;
124         ///
125         bool script(bool);
126         ///
127         bool interpret(string const &);
128         /// 
129         bool interpret(char);
130         /// interpret name a name of a macro
131         void macroModeClose();
132         /// are we currently typing the name of a macro?
133         bool inMacroMode() const;
134         /// get access to the macro we are currently typing
135         MathUnknownInset * activeMacro();
136         /// get access to the macro we are currently typing
137         MathUnknownInset const * activeMacro() 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         /// remove this as soon as LyXFunc::getStatus is "localized"
169         string getLastCode() const { return "mathnormal"; }
170         ///
171         bool isInside(MathInset const *) const;
172         ///
173         char valign() const;
174         ///
175         char halign() const;
176
177         /// make sure cursor position is valid
178         void normalize();
179         /// mark current cursor trace for redraw
180         void touch();
181         ///
182         UpdatableInset * asHyperActiveInset() const;
183
184         /// enter a MathInset
185         void push(MathAtom & par);
186         /// enter a MathInset from the front
187         void pushLeft(MathAtom & par);
188         /// enter a MathInset from the back
189         void pushRight(MathAtom & par);
190         /// leave current MathInset to the left
191         bool popLeft();
192         /// leave current MathInset to the left
193         bool popRight();
194
195         ///
196         MathArray & array() const;
197         ///
198         bool hasPrevAtom() const;
199         ///
200         bool hasNextAtom() const;
201         ///
202         MathAtom const & prevAtom() const;
203         ///
204         MathAtom & prevAtom();
205         ///
206         MathAtom const & nextAtom() const;
207         ///
208         MathAtom & nextAtom();
209
210         /// returns the selection
211         void getSelection(MathCursorPos &, MathCursorPos &) const;
212         /// returns the normalized anchor of the selection
213         MathCursorPos normalAnchor() const;
214
215         /// reference to the last item of the path, i.e. "The Cursor"
216         MathCursorPos & cursor();
217         /// reference to the last item of the path, i.e. "The Cursor"
218         MathCursorPos const & cursor() const;
219         /// how deep are we nested?
220         unsigned depth() const;
221
222         /// local dispatcher
223         MathInset::result_type dispatch(FuncRequest const & cmd);
224         /// describe the situation
225         string info() const;
226         /// dump selection information for debugging
227         void seldump(char const * str) const;
228         /// dump selection information for debugging
229         void dump(char const * str) const;
230         /// moves on
231         void setSelection(MathIterator const & where, size_type n);
232         ///
233         void insert(char c);
234         ///
235         void insert(string const & str);
236         /// lock/unlock inset
237         void insetToggle();
238
239         /// hack for reveal codes
240         void markInsert();
241         void markErase();
242         //void handleExtern(string const & arg);
243
244         ///
245         friend class Selection;
246
247
248 private:
249         /// injects content of a cell into parent
250         void pullArg();
251         /// moves cursor index one cell to the left
252         bool idxLeft();
253         /// moves cursor index one cell to the right
254         bool idxRight();
255         /// moves cursor to end of last cell of current line
256         bool idxLineLast();
257         /// moves cursor position one cell to the left
258         bool posLeft();
259         /// moves cursor position one cell to the right
260         bool posRight();
261         /// moves position somehow up or down
262         bool goUpDown(bool up);
263         /// moves position into box
264         bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh);
265         /// are we in a nucleus of a script inset?
266         bool inNucleus() const;
267
268
269         /// grab grid marked by anchor and current cursor 
270         MathGridInset grabSelection() const;
271         /// erase the selected part and re-sets the cursor
272         void eraseSelection();
273         /// guess what
274         MathGridInset grabAndEraseSelection();
275
276         ///
277         string macroName() const;
278         ///
279         MathInset::difference_type macroNamePos() const;
280         /// can we enter the inset?
281         bool openable(MathAtom const &, bool selection) const;
282         /// write access to cursor cell position
283         pos_type & pos();
284         /// write access to cursor cell index
285         idx_type & idx();
286
287         /// path of positions the cursor had to go if it were leaving each inset
288         MathIterator Cursor_;
289         /// path of positions the anchor had to go if it were leaving each inset
290         mutable MathIterator Anchor_;
291         /// pointer to enclsing LyX inset
292         InsetFormulaBase * formula_;
293         // Selection stuff
294         /// text code of last char entered
295         //MathTextCodes lastcode_;
296         /// do we allow autocorrection
297         bool autocorrect_;
298         /// do we currently select
299         bool selection_;
300         /// are we entering a macro name?
301         bool macromode_;
302         /// are we targeting a certain x coordinate, if so, which one?
303         int targetx_;
304 };
305
306 extern MathCursor * mathcursor;
307 void releaseMathCursor(BufferView * bv);
308
309 #endif