]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
'fix' stupid compiler warnings
[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
21 #include "math_inset.h"
22 #include "math_iterator.h"
23 #include "LString.h"
24
25 class InsetFormulaBase;
26 class BufferView;
27 class PainterInfo;
28 class MathUnknownInset;
29
30 /**
31
32 [Have a look at math_inset.h first]
33
34 The MathCursor is different from the kind of cursor used in the Outer
35 World. It contains a stack of CursorPos, each of which is made
36 up of a inset pointer, an index and a position offset, marking a path from
37 this formula's MathHullInset to the current position.
38
39 */
40
41
42 class MathCursor {
43 public:
44         /// short of anything else reasonable
45         typedef MathInset::size_type       size_type;
46         /// type for column numbers
47         typedef MathArray::difference_type difference_type;
48         /// type for cursor positions within a cell
49         typedef MathInset::pos_type        pos_type;
50         /// type for cell indices
51         typedef MathInset::idx_type        idx_type;
52         /// type for row numbers
53         typedef MathInset::row_type        row_type;
54         /// type for column numbers
55         typedef MathInset::col_type        col_type;
56
57         ///
58         explicit MathCursor(InsetFormulaBase *, bool left);
59         ///
60         ~MathCursor();
61         ///
62         void insert(MathAtom const &);
63         ///
64         void insert(MathArray const &);
65         ///
66         void insert2(string const &);
67         ///
68         void paste(string const & data);
69         /// return false for empty math insets
70         bool erase();
71         /// return false for empty math insets
72         bool 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         void niceInsert(string const &);
101
102         /// in pixels from top of screen
103         void setPos(int x, int y);
104         /// in pixels from top of screen
105         void getPos(int & x, int & y) const;
106         /// in pixels from left of screen
107         int targetX() const;
108         /// current inset
109         MathInset * inset() const;
110         /// return the next enclosing grid inset and the cursor's index in it
111         MathGridInset * enclosingGrid(idx_type & idx) const;
112         /// go up to enclosing grid
113         void popToEnclosingGrid();
114         /// go up to the hull inset
115         void popToEnclosingHull();
116         /// go up to the hull inset
117         void popToHere(MathInset const * p);
118         /// adjust anchor position after deletions/insertions
119         void adjust(pos_type from, difference_type diff);
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(char);
132         /// interpret name a name of a macro
133         void macroModeClose();
134         /// are we currently typing the name of a macro?
135         bool inMacroMode() const;
136         /// get access to the macro we are currently typing
137         MathUnknownInset * activeMacro();
138         /// get access to the macro we are currently typing
139         MathUnknownInset const * activeMacro() 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         /// pastes n-th element of cut buffer
155         void selPaste(size_t n);
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         /// draws light-blue selection background
165         void drawSelection(PainterInfo & pi) 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(CursorPos &, CursorPos &) const;
212         /// returns the normalized anchor of the selection
213         CursorPos normalAnchor() const;
214
215         /// reference to the last item of the path, i.e. "The Cursor"
216         CursorPos & cursor();
217         /// reference to the last item of the path, i.e. "The Cursor"
218         CursorPos const & cursor() const;
219         /// how deep are we nested?
220         unsigned depth() const;
221
222         /// local dispatcher
223         dispatch_result 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         /// grab selection marked by anchor and current cursor
233         string grabSelection() const;
234         /// guess what
235         string grabAndEraseSelection();
236         ///
237         void insert(char c);
238         ///
239         void insert(string const & str);
240         /// lock/unlock inset
241         void insetToggle();
242
243         /// hack for reveal codes
244         void markInsert();
245         void markErase();
246         /// injects content of a cell into parent
247         void pullArg();
248         /// split font inset etc
249         void handleFont(string const & font);
250
251 private:
252         /// moves cursor index one cell to the left
253         bool idxLeft();
254         /// moves cursor index one cell to the right
255         bool idxRight();
256         /// moves cursor to end of last cell of current line
257         bool idxLineLast();
258         /// moves cursor position one cell to the left
259         bool posLeft();
260         /// moves cursor position one cell to the right
261         bool posRight();
262         /// moves position somehow up or down
263         bool goUpDown(bool up);
264         /// moves position closest to (x, y) in given box
265         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
266         /// moves position closest to (x, y) in current cell
267         void bruteFind2(int x, int y);
268         /// are we in a nucleus of a script inset?
269         bool inNucleus() const;
270
271         /// erase the selected part and re-sets the cursor
272         void eraseSelection();
273
274         /// the name of the macro we are currently inputting
275         string macroName() const;
276         /// where in the curent cell does the macro name start?
277         difference_type macroNamePos() const;
278         /// can we enter the inset?
279         bool openable(MathAtom const &, bool selection) const;
280         /// write access to cursor cell position
281         pos_type & pos();
282         /// write access to cursor cell index
283         idx_type & idx();
284
285         /// path of positions the cursor had to go if it were leaving each inset
286         MathIterator Cursor_;
287         /// path of positions the anchor had to go if it were leaving each inset
288         mutable MathIterator Anchor_;
289         /// pointer to enclsing LyX inset
290         InsetFormulaBase * formula_;
291         // Selection stuff
292         /// text code of last char entered
293         //MathTextCodes lastcode_;
294         /// do we allow autocorrection
295         bool autocorrect_;
296         /// do we currently select
297         bool selection_;
298         /// are we entering a macro name?
299         bool macromode_;
300         /// are we targeting a certain x coordinate, if so, which one?
301         int targetx_;
302 };
303
304 extern MathCursor * mathcursor;
305 void releaseMathCursor(BufferView * bv);
306
307 #endif