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 last row of the current grid
194 row_type lastrow() const { return nrows() - 1; }
195 /// return the grid column of the current cell
196 col_type col() const;
197 /// return the last column of the current grid
198 col_type lastcol() const { return ncols() - 1; }
199 /// the inset just behind the cursor
200 InsetBase * nextInset();
201 /// the inset just in front of the cursor
202 InsetBase * prevInset();
203 /// the inset just in front of the cursor
204 InsetBase const * prevInset() const;
207 // math-specific part
209 /// return the mathed cell this cursor is in
210 MathArray const & cell() const;
211 /// return the mathed cell this cursor is in
213 /// the mathatom left of the cursor
214 MathAtom const & prevAtom() const;
215 /// the mathatom left of the cursor
216 MathAtom & prevAtom();
217 /// the mathatom right of the cursor
218 MathAtom const & nextAtom() const;
219 /// the mathatom right of the cursor
220 MathAtom & nextAtom();
221 /// auto-correct mode
222 bool autocorrect() const { return autocorrect_; }
223 /// auto-correct mode
224 bool & autocorrect() { return autocorrect_; }
225 /// are we entering a macro name?
226 bool macromode() const { return macromode_; }
227 /// are we entering a macro name?
228 bool & macromode() { return macromode_; }
231 // text-specific part
232 /// see comment for boundary_ below
233 bool boundary() const { return current().boundary(); }
234 /// see comment for boundary_ below
235 bool & boundary() { return current().boundary(); }
236 /// the paragraph we're in
237 Paragraph & paragraph();
238 /// the paragraph we're in
239 Paragraph const & paragraph() const;
240 /// the row in the paragraph we're in
242 /// the row in the paragraph we're in
243 Row const & textRow() const;
245 LyXText * text() const;
247 InsetBase * innerInsetOfType(int code) const;
249 InsetTabular * innerInsetTabular() const;
251 LyXText * innerText() const;
253 CursorSlice const & innerTextSlice() const;
254 /// returns x,y position
255 void getPos(int & x, int & y) const;
256 /// returns cursor dimension
257 void getDim(int & asc, int & desc) const;
262 /// move one step to the left
264 /// move one step to the right
268 void insert(InsetBase *);
269 /// insert a single char
272 void insert(std::string const & str);
274 /// write acess to target x position of cursor
276 /// return target x position of cursor
277 int x_target() const;
278 /// clear target x position of cursor
281 /// access to selection anchor
282 CursorSlice & anchor();
283 /// access to selection anchor
284 CursorSlice const & anchor() const;
285 /// cache the absolute coordinate from the top inset
287 /// sets anchor to cursor position
289 /// access to owning BufferView
290 BufferView & bv() const;
291 /// get some interesting description of current position
292 void info(std::ostream & os) const;
293 /// are we in math mode (2), text mode (1) or unsure (0)?
297 /// for spellchecking
298 void replaceWord(std::string const & replacestring);
302 void dispatched(dispatch_result_t res);
303 void notdispatched();
307 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
310 /// mainly used as stack, but wee need random access
311 std::vector<CursorSlice> cursor_;
312 /// the anchor position
313 std::vector<CursorSlice> anchor_;
316 DispatchResult disp_;
326 * The target x position of the cursor. This is used for when
327 * we have text like :
329 * blah blah blah blah| blah blah blah
331 * blah blah blah blah blah blah
333 * When we move onto row 3, we would like to be vertically aligned
334 * with where we were in row 1, despite the fact that row 2 is
338 // do we have a selection?
340 // are we on the way to get one?
344 // math specific stuff that could be promoted to "global" later
346 /// do we allow autocorrection
348 /// are we entering a macro name?
352 ///////////////////////////////////////////////////////////////////
354 // The part below is the non-integrated rest of the original math
355 // cursor. This should be either generalized for texted or moved
356 // back to the math insets.
358 ///////////////////////////////////////////////////////////////////
362 void insert(MathAtom const &);
364 void insert(MathArray const &);
365 /// return false for empty math insets
367 /// return false for empty math insets
369 /// called for LFUN_UP etc
371 /// called for LFUN_DOWN etc
376 void plainInsert(MathAtom const & at);
378 void niceInsert(MathAtom const & at);
380 void niceInsert(std::string const & str);
382 /// in pixels from top of screen
383 void setScreenPos(int x, int y);
384 /// in pixels from left of screen
386 /// return the next enclosing grid inset and the cursor's index in it
387 MathGridInset * enclosingGrid(idx_type & idx) const;
388 /// adjust anchor position after deletions/insertions
389 void adjust(pos_type from, int diff);
391 MathHullInset * formula() const;
392 /// current offset in the current cell
393 /// interpret name a name of a macro
394 void macroModeClose();
395 /// are we currently typing the name of a macro?
396 bool inMacroMode() const;
397 /// get access to the macro we are currently typing
398 MathUnknownInset * activeMacro();
399 /// are we currently typing '#1' or '#2' or...?
400 bool inMacroArgMode() const;
402 /// replace selected stuff with at, placing the former
403 // selection in given cell of atom
404 void handleNest(MathAtom const & at, int cell = 0);
405 /// remove this as soon as LyXFunc::getStatus is "localized"
406 //inline std::string getLastCode() { return "mathnormal"; }
408 bool isInside(InsetBase const *);
414 /// make sure cursor position is valid
416 /// mark current cursor trace for redraw
419 /// returns the normalized anchor of the selection
420 CursorSlice normalAnchor();
422 /// lock/unlock inset
425 /// hack for reveal codes
428 /// injects content of a cell into parent
430 /// split font inset etc
431 void handleFont(std::string const & font);
433 /// are we in mathed?
434 bool inMathed() const;
435 /// are we in texted?
436 bool inTexted() const;
438 /// display a message
439 void message(std::string const & msg) const;
440 /// display an error message
441 void errorMessage(std::string const & msg) const;
443 std::string getPossibleLabel();
445 /// moves position somehow up or down
446 bool goUpDown(bool up);
447 /// moves position closest to (x, y) in given box
448 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
449 /// moves position closest to (x, y) in current cell
450 void bruteFind2(int x, int y);
452 /// the name of the macro we are currently inputting
453 std::string macroName();
454 /// where in the curent cell does the macro name start?
456 /// can we enter the inset?
457 bool openable(MathAtom const &) const;
460 #endif // LYXCURSOR_H