4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
10 * Full author contact details are available in file CREDITS.
16 #include "cursor_slice.h"
17 #include "math_inset.h"
18 #include "math_data.h"
22 class InsetFormulaBase;
25 class MathUnknownInset;
29 [Have a look at math_inset.h first]
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.
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;
55 explicit MathCursor(BufferView *, InsetFormulaBase *, bool left);
59 void insert(LCursor & cur, MathAtom const &);
61 void insert(LCursor & cur, MathArray const &);
63 void insert2(LCursor & cur, std::string const &);
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);
91 void plainErase(LCursor & cur);
93 void plainInsert(LCursor & cur, MathAtom const & at);
95 void niceInsert(LCursor & cur, MathAtom const & at);
97 void niceInsert(LCursor & cur, std::string const & str);
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);
116 InsetFormulaBase * formula() const;
117 /// current offset in the current cell
119 bool script(LCursor & cur, bool);
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;
135 // Local selection methods
137 bool selection() const;
139 void selCopy(LCursor & cur);
141 void selCut(LCursor & cur);
143 void selDel(LCursor & cur);
144 /// pastes n-th element of cut buffer
145 void selPaste(LCursor & cur, size_t n);
147 void selHandle(LCursor & cur, bool);
149 void selStart(LCursor & cur);
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"; }
162 bool isInside(MathInset const *) const;
164 char valign(LCursor & cur) const;
166 char halign(LCursor & cur) const;
168 /// make sure cursor position is valid
169 void normalize(LCursor & cur);
170 /// mark current cursor trace for redraw
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);
185 bool hasPrevAtom(LCursor & cur) const;
187 bool hasNextAtom(LCursor & cur) const;
189 MathAtom const & prevAtom(LCursor & cur) const;
191 MathAtom & prevAtom(LCursor & cur);
193 MathAtom const & nextAtom(LCursor & cur) const;
195 MathAtom & nextAtom(LCursor & cur);
197 /// returns the selection
198 void getSelection(LCursor & cur, CursorSlice &, CursorSlice &) const;
199 /// returns the normalized anchor of the selection
200 CursorSlice normalAnchor(LCursor & cur) const;
202 /// how deep are we nested?
203 unsigned depth(LCursor & cur) const;
204 /// describe the situation
205 std::string info(LCursor & cur) 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;
211 void setSelection(LCursor & cur, CursorBase const & where, size_type n);
212 /// grab selection marked by anchor and current cursor
213 std::string grabSelection(LCursor & cur) const;
215 std::string grabAndEraseSelection(LCursor & cur);
217 void insert(LCursor & cur, char c);
219 void insert(LCursor & cur, std::string const & str);
220 /// lock/unlock inset
221 void insetToggle(LCursor & cur);
223 /// hack for reveal codes
224 void markInsert(LCursor & cur);
225 void markErase(LCursor & cur);
226 /// injects content of a cell into parent
227 void pullArg(LCursor & cur);
228 /// split font inset etc
229 void handleFont(LCursor & cur, std::string const & font);
231 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd);
233 /// moves cursor index one cell to the left
234 bool idxLeft(LCursor & bv);
235 /// moves cursor index one cell to the right
236 bool idxRight(LCursor & bv);
237 /// moves cursor to end of last cell of current line
238 bool idxLineLast(LCursor & bv);
239 /// moves cursor position one cell to the left
240 bool posLeft(LCursor & cur);
241 /// moves cursor position one cell to the right
242 bool posRight(LCursor & cur);
243 /// moves position somehow up or down
244 bool goUpDown(LCursor & cur, bool up);
245 /// moves position closest to (x, y) in given box
246 bool bruteFind(LCursor & cur,
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(LCursor & cur, int x, int y);
250 /// are we in a nucleus of a script inset?
251 bool inNucleus(LCursor & cur) const;
253 /// erase the selected part and re-sets the cursor
254 void eraseSelection(LCursor & cur);
256 /// the name of the macro we are currently inputting
257 std::string macroName(LCursor & cur) const;
258 /// where in the curent cell does the macro name start?
259 difference_type macroNamePos(LCursor & cur) const;
260 /// can we enter the inset?
261 bool openable(MathAtom const &, bool selection) const;
263 /// pointer to enclsing LyX inset
264 InsetFormulaBase * formula_;
266 /// text code of last char entered
267 //MathTextCodes lastcode_;
268 /// do we allow autocorrection
270 /// do we currently select
272 /// are we entering a macro name?
276 extern MathCursor * mathcursor;
277 void releaseMathCursor(BufferView & bv);