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(BufferView & bv, MathAtom const &);
61 void insert(BufferView & bv, MathArray const &);
63 void insert2(BufferView & bv, std::string const &);
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);
91 void plainErase(BufferView & bv);
93 void plainInsert(BufferView & bv, MathAtom const & at);
95 void niceInsert(BufferView & bv, MathAtom const & at);
97 void niceInsert(BufferView & bv, std::string const & str);
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);
116 InsetFormulaBase * formula() const;
117 /// current offset in the current cell
119 bool script(BufferView & bv, bool);
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;
135 // Local selection methods
137 bool selection() const;
139 void selCopy(BufferView & bv);
141 void selCut(BufferView & bv);
143 void selDel(BufferView & bv);
144 /// pastes n-th element of cut buffer
145 void selPaste(BufferView & bv, size_t n);
147 void selHandle(BufferView & bv, bool);
149 void selStart(BufferView & bv);
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"; }
162 bool isInside(MathInset const *) const;
164 char valign(BufferView & bv) const;
166 char halign(BufferView & bv) const;
168 /// make sure cursor position is valid
169 void normalize(BufferView & bv);
170 /// mark current cursor trace for redraw
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);
185 bool hasPrevAtom(BufferView & bv) const;
187 bool hasNextAtom(BufferView & bv) const;
189 MathAtom const & prevAtom(BufferView & bv) const;
191 MathAtom & prevAtom(BufferView & bv);
193 MathAtom const & nextAtom(BufferView & bv) const;
195 MathAtom & nextAtom(BufferView & bv);
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;
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;
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;
215 std::string grabAndEraseSelection(BufferView & bv);
217 void insert(BufferView & bv, char c);
219 void insert(BufferView & bv, std::string const & str);
220 /// lock/unlock inset
221 void insetToggle(BufferView & bv);
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);
231 DispatchResult dispatch(BufferView & bv, FuncRequest const & cmd);
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;
253 /// erase the selected part and re-sets the cursor
254 void eraseSelection(BufferView & bv);
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;
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);