]> git.lyx.org Git - features.git/blob - src/mathed/math_cursor.h
7e4994de4c1df3ad6db1f3e3e51bb7be0c6e37e5
[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 "cursor_slice.h"
17 #include "math_inset.h"
18 #include "math_data.h"
19
20 #include <string>
21
22 class InsetFormulaBase;
23 class BufferView;
24 class PainterInfo;
25 class MathUnknownInset;
26
27 /**
28
29 [Have a look at math_inset.h first]
30
31 The MathCursor is different from the kind of cursor used in the Outer
32 World. It contains a stack of CursorSlice, each of which is made
33 up of a inset pointer, an index and a position offset, marking a path from
34 this formula's MathHullInset to the current position.
35
36 */
37
38
39 class MathCursor {
40 public:
41         /// short of anything else reasonable
42         typedef size_t             size_type;
43         /// type for column numbers
44         typedef ptrdiff_t          difference_type;
45         /// type for cursor positions within a cell
46         typedef lyx::pos_type      pos_type;
47         /// type for cell indices
48         typedef size_t             idx_type;
49         /// type for row numbers
50         typedef size_t             row_type;
51         /// type for column numbers
52         typedef size_t             col_type;
53
54         ///
55         explicit MathCursor(BufferView *, InsetFormulaBase *, bool left);
56         ///
57         ~MathCursor();
58         ///
59         void insert(BufferView & bv, MathAtom const &);
60         ///
61         void insert(BufferView & bv, MathArray const &);
62         ///
63         void insert2(BufferView & bv, std::string const &);
64         ///
65         void paste(BufferView & bv, std::string const & data);
66         /// return false for empty math insets
67         bool erase(BufferView & bv);
68         /// return false for empty math insets
69         bool backspace(BufferView & bv);
70         /// called for LFUN_HOME etc
71         bool home(BufferView & bv, bool sel = false);
72         /// called for LFUN_END etc
73         bool end(BufferView & bv, bool sel = false);
74         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
75         bool right(BufferView & bv, bool sel = false);
76         /// called for LFUN_LEFT etc
77         bool left(BufferView & bv, bool sel = false);
78         /// called for LFUN_UP etc
79         bool up(BufferView & bv, bool sel = false);
80         /// called for LFUN_DOWN etc
81         bool down(BufferView & bv, bool sel = false);
82         /// Put the cursor in the first position
83         void first(BufferView & bv);
84         /// Put the cursor in the last position
85         void last(BufferView & bv);
86         /// move to next cell in current inset
87         void idxNext(BufferView & bv);
88         /// move to previous cell in current inset
89         void idxPrev(BufferView & bv);
90         ///
91         void plainErase(BufferView & bv);
92         ///
93         void plainInsert(BufferView & bv, MathAtom const & at);
94         ///
95         void niceInsert(BufferView & bv, MathAtom const & at);
96         ///
97         void niceInsert(BufferView & bv, std::string const & str);
98
99         /// in pixels from top of screen
100         void setScreenPos(BufferView & bv, int x, int y);
101         /// in pixels from top of screen
102         void getScreenPos(BufferView & bv, int & x, int & y) const;
103         /// in pixels from left of screen
104         int targetX(BufferView & bv) const;
105         /// return the next enclosing grid inset and the cursor's index in it
106         MathGridInset * enclosingGrid(BufferView & bv, idx_type & idx) const;
107         /// go up to enclosing grid
108         void popToEnclosingGrid(BufferView & bv);
109         /// go up to the hull inset
110         void popToEnclosingHull(BufferView & bv);
111         /// go up to the hull inset
112         void popToHere(BufferView & bv, MathInset const * p);
113         /// adjust anchor position after deletions/insertions
114         void adjust(BufferView & bv, pos_type from, difference_type diff);
115         ///
116         InsetFormulaBase * formula() const;
117         /// current offset in the current cell
118         ///
119         bool script(BufferView & bv, bool);
120         ///
121         bool interpret(BufferView & bv, char);
122         /// interpret name a name of a macro
123         void macroModeClose(BufferView & bv);
124         /// are we currently typing the name of a macro?
125         bool inMacroMode(BufferView & bv) const;
126         /// get access to the macro we are currently typing
127         MathUnknownInset * activeMacro(BufferView & bv);
128         /// get access to the macro we are currently typing
129         MathUnknownInset const * activeMacro(BufferView & bv) const;
130         /// are we currently typing '#1' or '#2' or...?
131         bool inMacroArgMode(BufferView & bv) const;
132         /// are we in math mode (1), text mode (-1) or unsure?
133         MathInset::mode_type currentMode(BufferView & bv) const;
134
135         // Local selection methods
136         ///
137         bool selection() const;
138         ///
139         void selCopy(BufferView & bv);
140         ///
141         void selCut(BufferView & bv);
142         ///
143         void selDel(BufferView & bv);
144         /// pastes n-th element of cut buffer
145         void selPaste(BufferView & bv, size_t n);
146         ///
147         void selHandle(BufferView & bv, bool);
148         ///
149         void selStart(BufferView & bv);
150         ///
151         void selClear(BufferView & bv);
152         /// clears or deletes selection depending on lyxrc setting
153         void selClearOrDel(BufferView & bv);
154         /// draws light-blue selection background
155         void drawSelection(PainterInfo & pi) const;
156         /// replace selected stuff with at, placing the former
157         // selection in given cell of atom
158         void handleNest(BufferView & bv, MathAtom const & at, int cell = 0);
159         /// remove this as soon as LyXFunc::getStatus is "localized"
160         std::string getLastCode() const { return "mathnormal"; }
161         ///
162         bool isInside(MathInset const *) const;
163         ///
164         char valign(BufferView & bv) const;
165         ///
166         char halign(BufferView & bv) const;
167
168         /// make sure cursor position is valid
169         void normalize(BufferView & bv);
170         /// mark current cursor trace for redraw
171         void touch();
172
173         /// enter a MathInset
174         void push(BufferView & bv, MathAtom & par);
175         /// enter a MathInset from the front
176         void pushLeft(BufferView & bv, MathAtom & par);
177         /// enter a MathInset from the back
178         void pushRight(BufferView & bv, MathAtom & par);
179         /// leave current MathInset to the left
180         bool popLeft(BufferView & bv);
181         /// leave current MathInset to the left
182         bool popRight(BufferView & bv);
183
184         ///
185         bool hasPrevAtom(BufferView & bv) const;
186         ///
187         bool hasNextAtom(BufferView & bv) const;
188         ///
189         MathAtom const & prevAtom(BufferView & bv) const;
190         ///
191         MathAtom & prevAtom(BufferView & bv);
192         ///
193         MathAtom const & nextAtom(BufferView & bv) const;
194         ///
195         MathAtom & nextAtom(BufferView & bv);
196
197         /// returns the selection
198         void getSelection(BufferView & bv, CursorSlice &, CursorSlice &) const;
199         /// returns the normalized anchor of the selection
200         CursorSlice normalAnchor(BufferView & bv) const;
201
202         /// how deep are we nested?
203         unsigned depth(BufferView & bv) const;
204         /// describe the situation
205         std::string info(BufferView & bv) const;
206         /// dump selection information for debugging
207         void seldump(char const * str) const;
208         /// dump selection information for debugging
209         void dump(char const * str) const;
210         /// moves on
211         void setSelection(BufferView & bv, CursorBase const & where, size_type n);
212         /// grab selection marked by anchor and current cursor
213         std::string grabSelection(BufferView & bv) const;
214         /// guess what
215         std::string grabAndEraseSelection(BufferView & bv);
216         ///
217         void insert(BufferView & bv, char c);
218         ///
219         void insert(BufferView & bv, std::string const & str);
220         /// lock/unlock inset
221         void insetToggle(BufferView & bv);
222
223         /// hack for reveal codes
224         void markInsert(BufferView & bv);
225         void markErase(BufferView & bv);
226         /// injects content of a cell into parent
227         void pullArg(BufferView & bv);
228         /// split font inset etc
229         void handleFont(BufferView & bv, std::string const & font);
230         ///
231         DispatchResult dispatch(BufferView & bv, FuncRequest const & cmd);
232 private:
233         /// moves cursor index one cell to the left
234         bool idxLeft(BufferView & bv);
235         /// moves cursor index one cell to the right
236         bool idxRight(BufferView & bv);
237         /// moves cursor to end of last cell of current line
238         bool idxLineLast(BufferView & bv);
239         /// moves cursor position one cell to the left
240         bool posLeft(BufferView & bv);
241         /// moves cursor position one cell to the right
242         bool posRight(BufferView & bv);
243         /// moves position somehow up or down
244         bool goUpDown(BufferView & bv, bool up);
245         /// moves position closest to (x, y) in given box
246         bool bruteFind(BufferView & bv,
247                 int x, int y, int xlow, int xhigh, int ylow, int yhigh);
248         /// moves position closest to (x, y) in current cell
249         void bruteFind2(BufferView & bv, int x, int y);
250         /// are we in a nucleus of a script inset?
251         bool inNucleus(BufferView & bv) const;
252
253         /// erase the selected part and re-sets the cursor
254         void eraseSelection(BufferView & bv);
255
256         /// the name of the macro we are currently inputting
257         std::string macroName(BufferView & bv) const;
258         /// where in the curent cell does the macro name start?
259         difference_type macroNamePos(BufferView & bv) const;
260         /// can we enter the inset?
261         bool openable(MathAtom const &, bool selection) const;
262
263         /// pointer to enclsing LyX inset
264         InsetFormulaBase * formula_;
265         // Selection stuff
266         /// text code of last char entered
267         //MathTextCodes lastcode_;
268         /// do we allow autocorrection
269         bool autocorrect_;
270         /// do we currently select
271         bool selection_;
272         /// are we entering a macro name?
273         bool macromode_;
274 };
275
276 extern MathCursor * mathcursor;
277 void releaseMathCursor(BufferView & bv);
278
279 #endif