]> git.lyx.org Git - features.git/blob - src/mathed/math_cursor.h
some integer type changes for inset unification
[features.git] / src / mathed / math_cursor.h
1 // -*- C++ -*-
2 /**
3  * \file math_cursor.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_CURSOR
14 #define MATH_CURSOR
15
16 #include "math_inset.h"
17 #include "math_data.h"
18 #include "math_iterator.h"
19 #include "support/types.h"
20
21 #include <string>
22
23
24 class InsetFormulaBase;
25 class BufferView;
26 class PainterInfo;
27 class MathUnknownInset;
28
29 /**
30
31 [Have a look at math_inset.h first]
32
33 The MathCursor is different from the kind of cursor used in the Outer
34 World. It contains a stack of CursorSlice, each of which is made
35 up of a inset pointer, an index and a position offset, marking a path from
36 this formula's MathHullInset to the current position.
37
38 */
39
40
41 class MathCursor {
42 public:
43         /// short of anything else reasonable
44         typedef size_t             size_type;
45         /// type for column numbers
46         typedef ptrdiff_t          difference_type;
47         /// type for cursor positions within a cell
48         typedef lyx::pos_type      pos_type;
49         /// type for cell indices
50         typedef size_t             idx_type;
51         /// type for row numbers
52         typedef size_t             row_type;
53         /// type for column numbers
54         typedef size_t             col_type;
55
56         ///
57         explicit MathCursor(InsetFormulaBase *, bool left);
58         ///
59         ~MathCursor();
60         ///
61         void insert(MathAtom const &);
62         ///
63         void insert(MathArray const &);
64         ///
65         void insert2(std::string const &);
66         ///
67         void paste(std::string const & data);
68         /// return false for empty math insets
69         bool erase();
70         /// return false for empty math insets
71         bool backspace();
72         /// called for LFUN_HOME etc
73         bool home(bool sel = false);
74         /// called for LFUN_END etc
75         bool end(bool sel = false);
76         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
77         bool right(bool sel = false);
78         /// called for LFUN_LEFT etc
79         bool left(bool sel = false);
80         /// called for LFUN_UP etc
81         bool up(bool sel = false);
82         /// called for LFUN_DOWN etc
83         bool down(bool sel = false);
84         /// Put the cursor in the first position
85         void first();
86         /// Put the cursor in the last position
87         void last();
88         /// move to next cell in current inset
89         void idxNext();
90         /// move to previous cell in current inset
91         void idxPrev();
92         ///
93         void plainErase();
94         ///
95         void plainInsert(MathAtom const &);
96         ///
97         void niceInsert(MathAtom const &);
98         ///
99         void niceInsert(std::string const &);
100
101         /// in pixels from top of screen
102         void setPos(int x, int y);
103         /// in pixels from top of screen
104         void getPos(int & x, int & y) const;
105         /// in pixels from left of screen
106         int targetX() const;
107         /// current inset
108         MathInset * inset() const;
109         /// return the next enclosing grid inset and the cursor's index in it
110         MathGridInset * enclosingGrid(idx_type & idx) const;
111         /// go up to enclosing grid
112         void popToEnclosingGrid();
113         /// go up to the hull inset
114         void popToEnclosingHull();
115         /// go up to the hull inset
116         void popToHere(MathInset const * p);
117         /// adjust anchor position after deletions/insertions
118         void adjust(pos_type from, difference_type diff);
119         ///
120         InsetFormulaBase * formula() const;
121         /// current offset in the current cell
122         pos_type pos() const;
123         /// current cell
124         idx_type idx() const;
125         /// size of current cell
126         size_type size() const;
127         ///
128         bool script(bool);
129         ///
130         bool interpret(char);
131         /// interpret name a name of a macro
132         void macroModeClose();
133         /// are we currently typing the name of a macro?
134         bool inMacroMode() const;
135         /// get access to the macro we are currently typing
136         MathUnknownInset * activeMacro();
137         /// get access to the macro we are currently typing
138         MathUnknownInset const * activeMacro() const;
139         /// are we currently typing '#1' or '#2' or...?
140         bool inMacroArgMode() const;
141         /// are we in math mode (1), text mode (-1) or unsure?
142         MathInset::mode_type currentMode() const;
143
144         // Local selection methods
145         ///
146         bool selection() const;
147         ///
148         void selCopy();
149         ///
150         void selCut();
151         ///
152         void selDel();
153         /// pastes n-th element of cut buffer
154         void selPaste(size_t n);
155         ///
156         void selHandle(bool);
157         ///
158         void selStart();
159         ///
160         void selClear();
161         /// clears or deletes selection depending on lyxrc setting
162         void selClearOrDel();
163         /// draws light-blue selection background
164         void drawSelection(PainterInfo & pi) const;
165         /// replace selected stuff with at, placing the former
166         // selection in given cell of atom
167         void handleNest(MathAtom const & at, int cell = 0);
168         /// remove this as soon as LyXFunc::getStatus is "localized"
169         std::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(CursorSlice &, CursorSlice &) const;
212         /// returns the normalized anchor of the selection
213         CursorSlice normalAnchor() const;
214
215         /// reference to the last item of the path, i.e. "The Cursor"
216         CursorSlice & cursor();
217         /// reference to the last item of the path, i.e. "The Cursor"
218         CursorSlice const & cursor() const;
219         /// how deep are we nested?
220         unsigned depth() const;
221         /// describe the situation
222         std::string info() const;
223         /// dump selection information for debugging
224         void seldump(char const * str) const;
225         /// dump selection information for debugging
226         void dump(char const * str) const;
227         /// moves on
228         void setSelection(MathIterator const & where, size_type n);
229         /// grab selection marked by anchor and current cursor
230         std::string grabSelection() const;
231         /// guess what
232         std::string grabAndEraseSelection();
233         ///
234         void insert(char c);
235         ///
236         void insert(std::string const & str);
237         /// lock/unlock inset
238         void insetToggle();
239
240         /// hack for reveal codes
241         void markInsert();
242         void markErase();
243         /// injects content of a cell into parent
244         void pullArg();
245         /// split font inset etc
246         void handleFont(std::string const & font);
247         ///
248         DispatchResult
249         dispatch(FuncRequest const & cmd);
250 private:
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 closest to (x, y) in given box
264         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
265         /// moves position closest to (x, y) in current cell
266         void bruteFind2(int x, int y);
267         /// are we in a nucleus of a script inset?
268         bool inNucleus() const;
269
270         /// erase the selected part and re-sets the cursor
271         void eraseSelection();
272
273         /// the name of the macro we are currently inputting
274         std::string macroName() const;
275         /// where in the curent cell does the macro name start?
276         difference_type macroNamePos() const;
277         /// can we enter the inset?
278         bool openable(MathAtom const &, bool selection) const;
279         /// write access to cursor cell position
280         pos_type & pos();
281         /// write access to cursor cell index
282         idx_type & idx();
283
284         /// path of positions the cursor had to go if it were leaving each inset
285         MathIterator Cursor_;
286         /// path of positions the anchor had to go if it were leaving each inset
287         mutable MathIterator Anchor_;
288         /// pointer to enclsing LyX inset
289         InsetFormulaBase * formula_;
290         // Selection stuff
291         /// text code of last char entered
292         //MathTextCodes lastcode_;
293         /// do we allow autocorrection
294         bool autocorrect_;
295         /// do we currently select
296         bool selection_;
297         /// are we entering a macro name?
298         bool macromode_;
299         /// are we targeting a certain x coordinate, if so, which one?
300         int targetx_;
301 };
302
303 extern MathCursor * mathcursor;
304 void releaseMathCursor(BufferView * bv);
305
306 #endif