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