]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.h
the monster patch
[lyx.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(MathAtom const &);
60         ///
61         void insert(MathArray const &);
62         ///
63         void insert2(std::string const &);
64         ///
65         void paste(std::string const & data);
66         /// return false for empty math insets
67         bool erase();
68         /// return false for empty math insets
69         bool backspace();
70         /// called for LFUN_HOME etc
71         bool home(bool sel = false);
72         /// called for LFUN_END etc
73         bool end(bool sel = false);
74         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
75         bool right(bool sel = false);
76         /// called for LFUN_LEFT etc
77         bool left(bool sel = false);
78         /// called for LFUN_UP etc
79         bool up(bool sel = false);
80         /// called for LFUN_DOWN etc
81         bool down(bool sel = false);
82         /// Put the cursor in the first position
83         void first();
84         /// Put the cursor in the last position
85         void last();
86         /// move to next cell in current inset
87         void idxNext();
88         /// move to previous cell in current inset
89         void idxPrev();
90         ///
91         void plainErase();
92         ///
93         void plainInsert(MathAtom const &);
94         ///
95         void niceInsert(MathAtom const &);
96         ///
97         void niceInsert(std::string const &);
98
99         /// in pixels from top of screen
100         void setScreenPos(int x, int y);
101         /// in pixels from top of screen
102         void getScreenPos(int & x, int & y) const;
103         /// in pixels from left of screen
104         int targetX() const;
105         /// current inset
106         MathInset * inset() const;
107         /// return the next enclosing grid inset and the cursor's index in it
108         MathGridInset * enclosingGrid(idx_type & idx) const;
109         /// go up to enclosing grid
110         void popToEnclosingGrid();
111         /// go up to the hull inset
112         void popToEnclosingHull();
113         /// go up to the hull inset
114         void popToHere(MathInset const * p);
115         /// adjust anchor position after deletions/insertions
116         void adjust(pos_type from, difference_type diff);
117         ///
118         InsetFormulaBase * formula() const;
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(char);
129         /// interpret name a name of a macro
130         void macroModeClose();
131         /// are we currently typing the name of a macro?
132         bool inMacroMode() const;
133         /// get access to the macro we are currently typing
134         MathUnknownInset * activeMacro();
135         /// get access to the macro we are currently typing
136         MathUnknownInset const * activeMacro() const;
137         /// are we currently typing '#1' or '#2' or...?
138         bool inMacroArgMode() const;
139         /// are we in math mode (1), text mode (-1) or unsure?
140         MathInset::mode_type currentMode() const;
141
142         // Local selection methods
143         ///
144         bool selection() const;
145         ///
146         void selCopy();
147         ///
148         void selCut();
149         ///
150         void selDel();
151         /// pastes n-th element of cut buffer
152         void selPaste(size_t n);
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         /// draws light-blue selection background
162         void drawSelection(PainterInfo & pi) const;
163         /// replace selected stuff with at, placing the former
164         // selection in given cell of atom
165         void handleNest(MathAtom const & at, int cell = 0);
166         /// remove this as soon as LyXFunc::getStatus is "localized"
167         std::string getLastCode() const { return "mathnormal"; }
168         ///
169         bool isInside(MathInset const *) const;
170         ///
171         char valign() const;
172         ///
173         char halign() const;
174
175         /// make sure cursor position is valid
176         void normalize();
177         /// mark current cursor trace for redraw
178         void touch();
179         ///
180         UpdatableInset * asHyperActiveInset() const;
181
182         /// enter a MathInset
183         void push(MathAtom & par);
184         /// enter a MathInset from the front
185         void pushLeft(MathAtom & par);
186         /// enter a MathInset from the back
187         void pushRight(MathAtom & par);
188         /// leave current MathInset to the left
189         bool popLeft();
190         /// leave current MathInset to the left
191         bool popRight();
192
193         ///
194         MathArray & array() const;
195         ///
196         bool hasPrevAtom() const;
197         ///
198         bool hasNextAtom() const;
199         ///
200         MathAtom const & prevAtom() const;
201         ///
202         MathAtom & prevAtom();
203         ///
204         MathAtom const & nextAtom() const;
205         ///
206         MathAtom & nextAtom();
207
208         /// returns the selection
209         void getSelection(CursorSlice &, CursorSlice &) const;
210         /// returns the normalized anchor of the selection
211         CursorSlice normalAnchor() const;
212
213         /// reference to the last item of the path, i.e. "The Cursor"
214         CursorSlice & cursor();
215         /// reference to the last item of the path, i.e. "The Cursor"
216         CursorSlice const & cursor() const;
217         /// how deep are we nested?
218         unsigned depth() const;
219         /// describe the situation
220         std::string info() const;
221         /// dump selection information for debugging
222         void seldump(char const * str) const;
223         /// dump selection information for debugging
224         void dump(char const * str) const;
225         /// moves on
226         void setSelection(CursorBase const & where, size_type n);
227         /// grab selection marked by anchor and current cursor
228         std::string grabSelection() const;
229         /// guess what
230         std::string grabAndEraseSelection();
231         ///
232         void insert(char c);
233         ///
234         void insert(std::string const & str);
235         /// lock/unlock inset
236         void insetToggle();
237
238         /// hack for reveal codes
239         void markInsert();
240         void markErase();
241         /// injects content of a cell into parent
242         void pullArg();
243         /// split font inset etc
244         void handleFont(std::string const & font);
245         ///
246         DispatchResult
247         dispatch(FuncRequest const & cmd);
248 private:
249         /// moves cursor index one cell to the left
250         bool idxLeft();
251         /// moves cursor index one cell to the right
252         bool idxRight();
253         /// moves cursor to end of last cell of current line
254         bool idxLineLast();
255         /// moves cursor position one cell to the left
256         bool posLeft();
257         /// moves cursor position one cell to the right
258         bool posRight();
259         /// moves position somehow up or down
260         bool goUpDown(bool up);
261         /// moves position closest to (x, y) in given box
262         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
263         /// moves position closest to (x, y) in current cell
264         void bruteFind2(int x, int y);
265         /// are we in a nucleus of a script inset?
266         bool inNucleus() const;
267
268         /// erase the selected part and re-sets the cursor
269         void eraseSelection();
270
271         /// the name of the macro we are currently inputting
272         std::string macroName() const;
273         /// where in the curent cell does the macro name start?
274         difference_type macroNamePos() const;
275         /// can we enter the inset?
276         bool openable(MathAtom const &, bool selection) const;
277         /// write access to cursor cell position
278         pos_type & pos();
279         /// write access to cursor cell index
280         idx_type & idx();
281
282         /// pointer to enclsing LyX inset
283         InsetFormulaBase * formula_;
284         // Selection stuff
285         /// text code of last char entered
286         //MathTextCodes lastcode_;
287         /// do we allow autocorrection
288         bool autocorrect_;
289         /// do we currently select
290         bool selection_;
291         /// are we entering a macro name?
292         bool macromode_;
293         ///
294         BufferView * bv_;
295 };
296
297 extern MathCursor * mathcursor;
298 void releaseMathCursor(BufferView & bv);
299
300 #endif