]> git.lyx.org Git - features.git/blob - src/mathed/math_cursor.h
shift small stuff from math cursor to cursor
[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(LCursor & cur, MathAtom const &);
60         ///
61         void insert(LCursor & cur, MathArray const &);
62         ///
63         void insert2(LCursor & cur, std::string const &);
64         ///
65         void paste(LCursor & cur, std::string const & data);
66         /// return false for empty math insets
67         bool erase(LCursor & cur);
68         /// return false for empty math insets
69         bool backspace(LCursor & cur);
70         /// called for LFUN_HOME etc
71         bool home(LCursor & cur, bool sel = false);
72         /// called for LFUN_END etc
73         bool end(LCursor & cur, bool sel = false);
74         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
75         bool right(LCursor & cur, bool sel = false);
76         /// called for LFUN_LEFT etc
77         bool left(LCursor & cur, bool sel = false);
78         /// called for LFUN_UP etc
79         bool up(LCursor & cur, bool sel = false);
80         /// called for LFUN_DOWN etc
81         bool down(LCursor & cur, bool sel = false);
82         /// Put the cursor in the first position
83         void first(LCursor & cur);
84         /// Put the cursor in the last position
85         void last(LCursor & cur);
86         /// move to next cell in current inset
87         void idxNext(LCursor & bv);
88         /// move to previous cell in current inset
89         void idxPrev(LCursor & bv);
90         ///
91         void plainErase(LCursor & cur);
92         ///
93         void plainInsert(LCursor & cur, MathAtom const & at);
94         ///
95         void niceInsert(LCursor & cur, MathAtom const & at);
96         ///
97         void niceInsert(LCursor & cur, std::string const & str);
98
99         /// in pixels from top of screen
100         void setScreenPos(LCursor & cur, int x, int y);
101         /// in pixels from top of screen
102         void getScreenPos(LCursor & cur, int & x, int & y) const;
103         /// in pixels from left of screen
104         int targetX(LCursor & cur) const;
105         /// return the next enclosing grid inset and the cursor's index in it
106         MathGridInset * enclosingGrid(LCursor & cur, idx_type & idx) const;
107         /// go up to enclosing grid
108         void popToEnclosingGrid(LCursor & cur);
109         /// go up to the hull inset
110         void popToEnclosingHull(LCursor & cur);
111         /// go up to the hull inset
112         void popToHere(LCursor & cur, MathInset const * p);
113         /// adjust anchor position after deletions/insertions
114         void adjust(LCursor & cur, pos_type from, difference_type diff);
115         ///
116         InsetFormulaBase * formula() const;
117         /// current offset in the current cell
118         ///
119         bool script(LCursor & cur, bool);
120         ///
121         bool interpret(LCursor & cur, char);
122         /// interpret name a name of a macro
123         void macroModeClose(LCursor & cur);
124         /// are we currently typing the name of a macro?
125         bool inMacroMode(LCursor & cur) const;
126         /// get access to the macro we are currently typing
127         MathUnknownInset * activeMacro(LCursor & cur);
128         /// get access to the macro we are currently typing
129         MathUnknownInset const * activeMacro(LCursor & cur) const;
130         /// are we currently typing '#1' or '#2' or...?
131         bool inMacroArgMode(LCursor & cur) const;
132         /// are we in math mode (1), text mode (-1) or unsure?
133         MathInset::mode_type currentMode(LCursor & cur) const;
134
135         // Local selection methods
136         ///
137         bool selection() const;
138         ///
139         void selCopy(LCursor & cur);
140         ///
141         void selCut(LCursor & cur);
142         ///
143         void selDel(LCursor & cur);
144         /// pastes n-th element of cut buffer
145         void selPaste(LCursor & cur, size_t n);
146         ///
147         void selHandle(LCursor & cur, bool);
148         ///
149         void selStart(LCursor & cur);
150         ///
151         void selClear(LCursor & cur);
152         /// clears or deletes selection depending on lyxrc setting
153         void selClearOrDel(LCursor & cur);
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(LCursor & cur, 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(LCursor & cur) const;
165         ///
166         char halign(LCursor & cur) const;
167
168         /// make sure cursor position is valid
169         void normalize(LCursor & cur);
170         /// mark current cursor trace for redraw
171         void touch();
172
173         /// enter a MathInset
174         void push(LCursor & cur, MathAtom & par);
175         /// enter a MathInset from the front
176         void pushLeft(LCursor & cur, MathAtom & par);
177         /// enter a MathInset from the back
178         void pushRight(LCursor & cur, MathAtom & par);
179         /// leave current MathInset to the left
180         bool popLeft(LCursor & cur);
181         /// leave current MathInset to the left
182         bool popRight(LCursor & cur);
183
184         /// returns the selection
185         void getSelection(LCursor & cur, CursorSlice &, CursorSlice &) const;
186         /// returns the normalized anchor of the selection
187         CursorSlice normalAnchor(LCursor & cur) const;
188
189         /// describe the situation
190         std::string info(LCursor & cur) const;
191         /// dump selection information for debugging
192         void seldump(char const * str) const;
193         /// dump selection information for debugging
194         void dump(char const * str) const;
195         /// moves on
196         void setSelection(LCursor & cur, CursorBase const & where, size_type n);
197         /// grab selection marked by anchor and current cursor
198         std::string grabSelection(LCursor & cur) const;
199         /// guess what
200         std::string grabAndEraseSelection(LCursor & cur);
201         ///
202         void insert(LCursor & cur, char c);
203         ///
204         void insert(LCursor & cur, std::string const & str);
205         /// lock/unlock inset
206         void insetToggle(LCursor & cur);
207
208         /// hack for reveal codes
209         void markInsert(LCursor & cur);
210         void markErase(LCursor & cur);
211         /// injects content of a cell into parent
212         void pullArg(LCursor & cur);
213         /// split font inset etc
214         void handleFont(LCursor & cur, std::string const & font);
215         ///
216         DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
217 private:
218         /// moves cursor index one cell to the left
219         bool idxLeft(LCursor & bv);
220         /// moves cursor index one cell to the right
221         bool idxRight(LCursor & bv);
222         /// moves cursor to end of last cell of current line
223         bool idxLineLast(LCursor & bv);
224         /// moves position somehow up or down
225         bool goUpDown(LCursor & cur, bool up);
226         /// moves position closest to (x, y) in given box
227         bool bruteFind(LCursor & cur,
228                 int x, int y, int xlow, int xhigh, int ylow, int yhigh);
229         /// moves position closest to (x, y) in current cell
230         void bruteFind2(LCursor & cur, int x, int y);
231         /// are we in a nucleus of a script inset?
232         bool inNucleus(LCursor & cur) const;
233
234         /// erase the selected part and re-sets the cursor
235         void eraseSelection(LCursor & cur);
236
237         /// the name of the macro we are currently inputting
238         std::string macroName(LCursor & cur) const;
239         /// where in the curent cell does the macro name start?
240         difference_type macroNamePos(LCursor & cur) const;
241         /// can we enter the inset?
242         bool openable(MathAtom const &, bool selection) const;
243
244         /// pointer to enclsing LyX inset
245         InsetFormulaBase * formula_;
246         // Selection stuff
247         /// text code of last char entered
248         //MathTextCodes lastcode_;
249         /// do we allow autocorrection
250         bool autocorrect_;
251         /// do we currently select
252         bool selection_;
253         /// are we entering a macro name?
254         bool macromode_;
255 };
256
257 extern MathCursor * mathcursor;
258 void releaseMathCursor(BufferView & bv);
259
260 #endif