4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "cursor_slice.h"
16 #include "dispatchresult.h"
35 class MathUnknownInset;
39 // only needed for gcc 2.95, remove when support terminated
40 template <typename A, typename B>
41 bool ptr_cmp(A const * a, B const * b)
47 // this is used for traversing math insets
48 typedef std::vector<CursorSlice> CursorBase;
50 void increment(CursorBase &);
52 CursorBase ibegin(InsetBase * p);
54 CursorBase iend(InsetBase * p);
58 * The cursor class describes the position of a cursor within a document.
63 /// type for cell number in inset
64 typedef CursorSlice::idx_type idx_type;
65 /// type for paragraph numbers positions within a cell
66 typedef CursorSlice::par_type par_type;
67 /// type for cursor positions within a cell
68 typedef CursorSlice::pos_type pos_type;
69 /// type for row indices
70 typedef CursorSlice::row_type row_type;
71 /// type for col indices
72 typedef CursorSlice::col_type col_type;
74 /// create the cursor of a BufferView
75 explicit LCursor(BufferView & bv);
76 /// dispatch from innermost inset upwards
77 DispatchResult dispatch(FuncRequest const & cmd);
78 /// are we willing to handle this event?
79 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
81 /// add a new cursor slice
82 void push(InsetBase * inset);
83 /// add a new cursor slice, place cursor on left end
84 void pushLeft(InsetBase * inset);
85 /// pop one level off the cursor
87 /// pop one slice off the cursor stack and go left
89 /// pop one slice off the cursor stack and go right
91 /// restrict cursor nesting to given size
93 /// access to current cursor slice
94 CursorSlice & current();
95 /// access to current cursor slice
96 CursorSlice const & current() const;
97 /// how many nested insets do we have?
98 size_t depth() const { return cursor_.size(); }
99 /// depth of current slice
100 int currentDepth() const { return current_; }
105 /// selection active?
106 bool selection() const { return selection_; }
107 /// selection active?
108 bool & selection() { return selection_; }
109 /// did we place the anchor?
110 bool mark() const { return mark_; }
111 /// did we place the anchor?
112 bool & mark() { return mark_; }
115 /// set selection at given position
116 void setSelection(CursorBase const & where, size_t n);
118 void clearSelection();
119 /// access start of selection
120 CursorSlice & selBegin();
121 /// access start of selection
122 CursorSlice const & selBegin() const;
123 /// access end of selection
124 CursorSlice & selEnd();
125 /// access end of selection
126 CursorSlice const & selEnd() const;
128 std::string grabSelection();
130 void eraseSelection();
132 std::string grabAndEraseSelection();
133 // other selection methods
140 /// pastes n-th element of cut buffer
141 void selPaste(size_t n);
143 void selHandle(bool selecting);
148 /// clears or deletes selection depending on lyxrc setting
149 void selClearOrDel();
151 std::string selectionAsString(bool label) const;
153 void paste(std::string const & data);
155 std::string currentState();
158 // access to the 'current' cursor slice
160 /// the containing inset
161 InsetBase * inset() const { return current().inset(); }
162 /// return the cell of the inset this cursor is in
163 idx_type idx() const { return current().idx(); }
164 /// return the cell of the inset this cursor is in
165 idx_type & idx() { return current().idx(); }
166 /// return the last possible cell in this inset
167 idx_type lastidx() const;
168 /// return the paragraph this cursor is in
169 par_type par() const { return current().par(); }
170 /// return the paragraph this cursor is in
171 par_type & par() { return current().par(); }
172 /// return the last possible paragraph in this inset
173 par_type lastpar() const;
174 /// return the position within the paragraph
175 pos_type pos() const { return current().pos(); }
176 /// return the position within the paragraph
177 pos_type & pos() { return current().pos(); }
178 /// return the last position within the paragraph
179 pos_type lastpos() const;
180 /// return the display row of the cursor with in the current par
181 row_type crow() const;
182 /// return the display row of the cursor with in the current par
183 row_type lastcrow() const;
185 /// return the number of embedded cells
186 size_t nargs() const;
187 /// return the number of embedded cells
188 size_t ncols() const;
189 /// return the number of embedded cells
190 size_t nrows() const;
191 /// return the grid row of the current cell
192 row_type row() const;
193 /// return the grid row of the current cell
194 col_type col() const;
195 /// the inset just behind the cursor
196 InsetBase * nextInset();
197 /// the inset just in front of the cursor
198 InsetBase * prevInset();
199 /// the inset just in front of the cursor
200 InsetBase const * prevInset() const;
203 // math-specific part
205 /// return the mathed cell this cursor is in
206 MathArray const & cell() const;
207 /// return the mathed cell this cursor is in
209 /// the mathatom left of the cursor
210 MathAtom const & prevAtom() const;
211 /// the mathatom left of the cursor
212 MathAtom & prevAtom();
213 /// the mathatom right of the cursor
214 MathAtom const & nextAtom() const;
215 /// the mathatom right of the cursor
216 MathAtom & nextAtom();
217 /// auto-correct mode
218 bool autocorrect() const { return autocorrect_; }
219 /// auto-correct mode
220 bool & autocorrect() { return autocorrect_; }
221 /// are we entering a macro name?
222 bool macromode() const { return macromode_; }
223 /// are we entering a macro name?
224 bool & macromode() { return macromode_; }
227 // text-specific part
228 /// see comment for boundary_ below
229 bool boundary() const { return current().boundary(); }
230 /// see comment for boundary_ below
231 bool & boundary() { return current().boundary(); }
232 /// the paragraph we're in
233 Paragraph & paragraph();
234 /// the paragraph we're in
235 Paragraph const & paragraph() const;
236 /// the row in the paragraph we're in
238 /// the row in the paragraph we're in
239 Row const & textRow() const;
241 LyXText * text() const;
243 InsetBase * innerInsetOfType(int code) const;
245 InsetTabular * innerInsetTabular() const;
247 LyXText * innerText() const;
249 CursorSlice const & innerTextSlice() const;
250 /// returns x,y position
251 void getPos(int & x, int & y) const;
252 /// returns cursor dimension
253 void getDim(int & asc, int & desc) const;
258 /// move one step to the left
260 /// move one step to the right
264 void insert(InsetBase *);
265 /// insert a single char
268 void insert(std::string const & str);
270 /// write acess to target x position of cursor
272 /// return target x position of cursor
273 int x_target() const;
274 /// clear target x position of cursor
277 /// access to selection anchor
278 CursorSlice & anchor();
279 /// access to selection anchor
280 CursorSlice const & anchor() const;
281 /// cache the absolute coordinate from the top inset
283 /// sets anchor to cursor position
285 /// access to owning BufferView
286 BufferView & bv() const;
287 /// get some interesting description of current position
288 void info(std::ostream & os) const;
289 /// are we in math mode (2), text mode (1) or unsure (0)?
293 /// for spellchecking
294 void replaceWord(std::string const & replacestring);
298 void dispatched(dispatch_result_t res);
299 void notdispatched();
303 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
306 /// mainly used as stack, but wee need random access
307 std::vector<CursorSlice> cursor_;
308 /// the anchor position
309 std::vector<CursorSlice> anchor_;
312 DispatchResult disp_;
322 * The target x position of the cursor. This is used for when
323 * we have text like :
325 * blah blah blah blah| blah blah blah
327 * blah blah blah blah blah blah
329 * When we move onto row 3, we would like to be vertically aligned
330 * with where we were in row 1, despite the fact that row 2 is
334 // do we have a selection?
336 // are we on the way to get one?
340 // math specific stuff that could be promoted to "global" later
342 /// do we allow autocorrection
344 /// are we entering a macro name?
348 ///////////////////////////////////////////////////////////////////
350 // The part below is the non-integrated rest of the original math
351 // cursor. This should be either generalized for texted or moved
352 // back to the math insets.
354 ///////////////////////////////////////////////////////////////////
358 void insert(MathAtom const &);
360 void insert(MathArray const &);
361 /// return false for empty math insets
363 /// return false for empty math insets
365 /// called for LFUN_HOME etc
367 /// called for LFUN_END etc
369 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
371 /// called for LFUN_LEFT etc
373 /// called for LFUN_UP etc
375 /// called for LFUN_DOWN etc
380 void plainInsert(MathAtom const & at);
382 void niceInsert(MathAtom const & at);
384 void niceInsert(std::string const & str);
386 /// in pixels from top of screen
387 void setScreenPos(int x, int y);
388 /// in pixels from left of screen
390 /// return the next enclosing grid inset and the cursor's index in it
391 MathGridInset * enclosingGrid(idx_type & idx) const;
392 /// adjust anchor position after deletions/insertions
393 void adjust(pos_type from, int diff);
395 MathHullInset * formula() const;
396 /// current offset in the current cell
400 bool interpret(char);
401 /// interpret name a name of a macro
402 void macroModeClose();
403 /// are we currently typing the name of a macro?
404 bool inMacroMode() const;
405 /// get access to the macro we are currently typing
406 MathUnknownInset * activeMacro();
407 /// are we currently typing '#1' or '#2' or...?
408 bool inMacroArgMode() const;
410 /// replace selected stuff with at, placing the former
411 // selection in given cell of atom
412 void handleNest(MathAtom const & at, int cell = 0);
413 /// remove this as soon as LyXFunc::getStatus is "localized"
414 //inline std::string getLastCode() { return "mathnormal"; }
416 bool isInside(InsetBase const *);
422 /// make sure cursor position is valid
424 /// mark current cursor trace for redraw
427 /// returns the normalized anchor of the selection
428 CursorSlice normalAnchor();
430 /// lock/unlock inset
433 /// hack for reveal codes
436 /// injects content of a cell into parent
438 /// split font inset etc
439 void handleFont(std::string const & font);
441 /// are we in mathed?
442 bool inMathed() const;
443 /// are we in texted?
444 bool inTexted() const;
446 /// display a message
447 void message(std::string const & msg) const;
448 /// display an error message
449 void errorMessage(std::string const & msg) const;
451 std::string getPossibleLabel();
454 /// moves cursor index one cell to the left
456 /// moves cursor index one cell to the right
458 /// moves cursor to end of last cell of current line
460 /// moves position somehow up or down
461 bool goUpDown(bool up);
462 /// moves position closest to (x, y) in given box
463 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
464 /// moves position closest to (x, y) in current cell
465 void bruteFind2(int x, int y);
466 /// are we in a nucleus of a script inset?
469 /// the name of the macro we are currently inputting
470 std::string macroName();
471 /// where in the curent cell does the macro name start?
473 /// can we enter the inset?
474 bool openable(MathAtom const &);
477 #endif // LYXCURSOR_H