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"
34 class MathUnknownInset;
38 // only needed for gcc 2.95, remove when support terminated
39 template <typename A, typename B>
40 bool ptr_cmp(A const * a, B const * b)
46 // this is used for traversing math insets
47 typedef std::vector<CursorSlice> CursorBase;
49 void increment(CursorBase &);
51 CursorBase ibegin(InsetBase * p);
53 CursorBase iend(InsetBase * p);
57 * The cursor class describes the position of a cursor within a document.
62 /// type for cell number in inset
63 typedef CursorSlice::idx_type idx_type;
64 /// type for paragraph numbers positions within a cell
65 typedef CursorSlice::par_type par_type;
66 /// type for cursor positions within a cell
67 typedef CursorSlice::pos_type pos_type;
68 /// type for row indices
69 typedef CursorSlice::row_type row_type;
70 /// type for col indices
71 typedef CursorSlice::col_type col_type;
73 /// create the cursor of a BufferView
74 explicit LCursor(BufferView & bv);
75 /// dispatch from innermost inset upwards
76 DispatchResult dispatch(FuncRequest const & cmd);
77 /// add a new cursor slice
78 void push(InsetBase * inset);
79 /// add a new cursor slice, place cursor on left end
80 void pushLeft(InsetBase * inset);
81 /// pop one level off the cursor
83 /// pop one slice off the cursor stack and go left
85 /// pop one slice off the cursor stack and go right
87 /// restrict cursor nesting to given size
89 /// access to current cursor slice
90 CursorSlice & current();
91 /// access to current cursor slice
92 CursorSlice const & current() const;
93 /// how many nested insets do we have?
94 size_t depth() const { return cursor_.size(); }
95 /// depth of current slice
96 int currentDepth() const { return current_; }
101 /// selection active?
102 bool selection() const { return selection_; }
103 /// selection active?
104 bool & selection() { return selection_; }
105 /// did we place the anchor?
106 bool mark() const { return mark_; }
107 /// did we place the anchor?
108 bool & mark() { return mark_; }
111 /// set selection at given position
112 void setSelection(CursorBase const & where, size_t n);
114 void clearSelection();
115 /// access start of selection
116 CursorSlice & selBegin();
117 /// access start of selection
118 CursorSlice const & selBegin() const;
119 /// access end of selection
120 CursorSlice & selEnd();
121 /// access end of selection
122 CursorSlice const & selEnd() const;
124 std::string grabSelection();
126 void eraseSelection();
128 std::string grabAndEraseSelection();
129 // other selection methods
136 /// pastes n-th element of cut buffer
137 void selPaste(size_t n);
139 void selHandle(bool selecting);
144 /// clears or deletes selection depending on lyxrc setting
145 void selClearOrDel();
147 std::string selectionAsString(bool label) const;
149 void paste(std::string const & data);
151 std::string currentState();
154 // access to the 'current' cursor slice
156 /// the containing inset
157 InsetBase * inset() const { return current().inset(); }
158 /// return the cell of the inset this cursor is in
159 idx_type idx() const { return current().idx(); }
160 /// return the cell of the inset this cursor is in
161 idx_type & idx() { return current().idx(); }
162 /// return the last possible cell in this inset
163 idx_type lastidx() const;
164 /// return the paragraph this cursor is in
165 par_type par() const { return current().par(); }
166 /// return the paragraph this cursor is in
167 par_type & par() { return current().par(); }
168 /// return the last possible paragraph in this inset
169 par_type lastpar() const;
170 /// return the position within the paragraph
171 pos_type pos() const { return current().pos(); }
172 /// return the position within the paragraph
173 pos_type & pos() { return current().pos(); }
174 /// return the last position within the paragraph
175 pos_type lastpos() const;
176 /// return the display row of the cursor with in the current par
177 row_type crow() const;
178 /// return the display row of the cursor with in the current par
179 row_type lastcrow() const;
181 /// return the number of embedded cells
182 size_t nargs() const;
183 /// return the number of embedded cells
184 size_t ncols() const;
185 /// return the number of embedded cells
186 size_t nrows() const;
187 /// return the grid row of the current cell
188 row_type row() const;
189 /// return the grid row of the current cell
190 col_type col() const;
191 /// the inset just behind the cursor
192 InsetBase * nextInset();
193 /// the inset just in front of the cursor
194 InsetBase * prevInset();
195 /// the inset just in front of the cursor
196 InsetBase const * prevInset() const;
199 // math-specific part
201 /// return the mathed cell this cursor is in
202 MathArray const & cell() const;
203 /// return the mathed cell this cursor is in
205 /// the mathatom left of the cursor
206 MathAtom const & prevAtom() const;
207 /// the mathatom left of the cursor
208 MathAtom & prevAtom();
209 /// the mathatom right of the cursor
210 MathAtom const & nextAtom() const;
211 /// the mathatom right of the cursor
212 MathAtom & nextAtom();
213 /// auto-correct mode
214 bool autocorrect() const { return autocorrect_; }
215 /// auto-correct mode
216 bool & autocorrect() { return autocorrect_; }
217 /// are we entering a macro name?
218 bool macromode() const { return macromode_; }
219 /// are we entering a macro name?
220 bool & macromode() { return macromode_; }
223 // text-specific part
224 /// see comment for boundary_ below
225 bool boundary() const { return current().boundary(); }
226 /// see comment for boundary_ below
227 bool & boundary() { return current().boundary(); }
228 /// the paragraph we're in
229 Paragraph & paragraph();
230 /// the paragraph we're in
231 Paragraph const & paragraph() const;
232 /// the row in the paragraph we're in
234 /// the row in the paragraph we're in
235 Row const & textRow() const;
237 LyXText * text() const;
239 InsetBase * innerInsetOfType(int code) const;
241 InsetTabular * innerInsetTabular() const;
243 LyXText * innerText() const;
245 CursorSlice const & innerTextSlice() const;
246 /// returns x,y position
247 void getPos(int & x, int & y) const;
248 /// returns cursor dimension
249 void getDim(int & asc, int & desc) const;
254 /// move one step to the left
256 /// move one step to the right
260 void insert(InsetBase *);
261 /// insert a single char
264 void insert(std::string const & str);
266 /// write acess to target x position of cursor
268 /// return target x position of cursor
269 int x_target() const;
270 /// clear target x position of cursor
273 /// access to selection anchor
274 CursorSlice & anchor();
275 /// access to selection anchor
276 CursorSlice const & anchor() const;
277 /// cache the absolute coordinate from the top inset
279 /// sets anchor to cursor position
281 /// access to owning BufferView
282 BufferView & bv() const;
283 /// get some interesting description of current position
284 void info(std::ostream & os) const;
285 /// are we in math mode (2), text mode (1) or unsure (0)?
289 /// for spellchecking
290 void replaceWord(std::string const & replacestring);
294 void dispatched(dispatch_result_t res);
295 void notdispatched();
299 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
302 /// mainly used as stack, but wee need random access
303 std::vector<CursorSlice> cursor_;
304 /// the anchor position
305 std::vector<CursorSlice> anchor_;
308 DispatchResult disp_;
318 * The target x position of the cursor. This is used for when
319 * we have text like :
321 * blah blah blah blah| blah blah blah
323 * blah blah blah blah blah blah
325 * When we move onto row 3, we would like to be vertically aligned
326 * with where we were in row 1, despite the fact that row 2 is
330 // do we have a selection?
332 // are we on the way to get one?
336 // math specific stuff that could be promoted to "global" later
338 /// do we allow autocorrection
340 /// are we entering a macro name?
344 ///////////////////////////////////////////////////////////////////
346 // The part below is the non-integrated rest of the original math
347 // cursor. This should be either generalized for texted or moved
348 // back to the math insets.
350 ///////////////////////////////////////////////////////////////////
354 void insert(MathAtom const &);
356 void insert(MathArray const &);
357 /// return false for empty math insets
359 /// return false for empty math insets
361 /// called for LFUN_HOME etc
363 /// called for LFUN_END etc
365 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
367 /// called for LFUN_LEFT etc
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
396 bool interpret(char);
397 /// interpret name a name of a macro
398 void macroModeClose();
399 /// are we currently typing the name of a macro?
400 bool inMacroMode() const;
401 /// get access to the macro we are currently typing
402 MathUnknownInset * activeMacro();
403 /// are we currently typing '#1' or '#2' or...?
404 bool inMacroArgMode() const;
406 /// replace selected stuff with at, placing the former
407 // selection in given cell of atom
408 void handleNest(MathAtom const & at, int cell = 0);
409 /// remove this as soon as LyXFunc::getStatus is "localized"
410 //inline std::string getLastCode() { return "mathnormal"; }
412 bool isInside(InsetBase const *);
418 /// make sure cursor position is valid
420 /// mark current cursor trace for redraw
423 /// returns the normalized anchor of the selection
424 CursorSlice normalAnchor();
426 /// lock/unlock inset
429 /// hack for reveal codes
432 /// injects content of a cell into parent
434 /// split font inset etc
435 void handleFont(std::string const & font);
437 /// are we in mathed?
438 bool inMathed() const;
439 /// are we in texted?
440 bool inTexted() const;
442 /// display a message
443 void message(std::string const & msg) const;
444 /// display an error message
445 void errorMessage(std::string const & msg) const;
447 std::string getPossibleLabel();
450 /// moves cursor index one cell to the left
452 /// moves cursor index one cell to the right
454 /// moves cursor to end of last cell of current line
456 /// moves position somehow up or down
457 bool goUpDown(bool up);
458 /// moves position closest to (x, y) in given box
459 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
460 /// moves position closest to (x, y) in current cell
461 void bruteFind2(int x, int y);
462 /// are we in a nucleus of a script inset?
465 /// the name of the macro we are currently inputting
466 std::string macroName();
467 /// where in the curent cell does the macro name start?
469 /// can we enter the inset?
470 bool openable(MathAtom const &);
473 #endif // LYXCURSOR_H