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"
33 class MathUnknownInset;
36 // this is used for traversing math insets
37 typedef std::vector<CursorSlice> CursorBase;
39 void increment(CursorBase &);
41 CursorBase ibegin(InsetBase * p);
43 CursorBase iend(InsetBase * p);
47 * The cursor class describes the position of a cursor within a document.
52 /// type for cell number in inset
53 typedef CursorSlice::idx_type idx_type;
54 /// type for paragraph numbers positions within a cell
55 typedef CursorSlice::par_type par_type;
56 /// type for cursor positions within a cell
57 typedef CursorSlice::pos_type pos_type;
58 /// type for row indices
59 typedef CursorSlice::row_type row_type;
60 /// type for col indices
61 typedef CursorSlice::col_type col_type;
63 /// create the cursor of a BufferView
64 explicit LCursor(BufferView & bv);
65 /// dispatch from innermost inset upwards
66 DispatchResult dispatch(FuncRequest const & cmd);
67 /// add a new cursor slice
68 void push(InsetBase * inset);
69 /// add a new cursor slice, place cursor on left end
70 void pushLeft(InsetBase * inset);
71 /// pop one level off the cursor
73 /// pop one slice off the cursor stack and go left
75 /// pop one slice off the cursor stack and go right
77 /// restrict cursor nesting to given size
79 /// access to current cursor slice
80 CursorSlice & current();
81 /// access to current cursor slice
82 CursorSlice const & current() const;
83 /// how many nested insets do we have?
84 size_t depth() const { return cursor_.size(); }
85 /// depth of current slice
86 int currentDepth() const { return current_; }
92 bool selection() const { return selection_; }
94 bool & selection() { return selection_; }
95 /// did we place the anchor?
96 bool mark() const { return mark_; }
97 /// did we place the anchor?
98 bool & mark() { return mark_; }
101 /// set selection at given position
102 void setSelection(CursorBase const & where, size_t n);
104 void clearSelection();
105 /// access start of selection
106 CursorSlice & selBegin();
107 /// access start of selection
108 CursorSlice const & selBegin() const;
109 /// access end of selection
110 CursorSlice & selEnd();
111 /// access end of selection
112 CursorSlice const & selEnd() const;
114 std::string grabSelection();
116 void eraseSelection();
118 std::string grabAndEraseSelection();
119 // other selection methods
126 /// pastes n-th element of cut buffer
127 void selPaste(size_t n);
129 void selHandle(bool selecting);
134 /// clears or deletes selection depending on lyxrc setting
135 void selClearOrDel();
137 void paste(std::string const & data);
140 // access to the 'current' cursor slice
142 /// the containing inset
143 InsetBase * inset() const { return current().inset(); }
144 /// return the cell of the inset this cursor is in
145 idx_type idx() const { return current().idx(); }
146 /// return the cell of the inset this cursor is in
147 idx_type & idx() { return current().idx(); }
148 /// return the last possible cell in this inset
149 idx_type lastidx() const { return current().lastidx(); }
150 /// return the paragraph this cursor is in
151 par_type par() const { return current().par(); }
152 /// return the paragraph this cursor is in
153 par_type & par() { return current().par(); }
154 /// return the last possible paragraph in this inset
155 par_type lastpar() const;
156 /// return the position within the paragraph
157 pos_type pos() const { return current().pos(); }
158 /// return the position within the paragraph
159 pos_type & pos() { return current().pos(); }
160 /// return the last position within the paragraph
161 pos_type lastpos() const;
162 /// return the display row of the cursor with in the current par
163 row_type crow() const;
164 /// return the display row of the cursor with in the current par
165 row_type lastcrow() const;
167 /// return the number of embedded cells
168 size_t nargs() const;
169 /// return the number of embedded cells
170 size_t ncols() const;
171 /// return the number of embedded cells
172 size_t nrows() const;
173 /// return the grid row of the current cell
174 row_type row() const;
175 /// return the grid row of the current cell
176 col_type col() const;
177 /// the inset just behind the cursor
178 InsetBase * nextInset();
179 /// the inset just in front of the cursor
180 InsetBase * prevInset();
183 // math-specific part
185 /// return the mathed cell this cursor is in
186 MathArray const & cell() const;
187 /// return the mathed cell this cursor is in
189 /// the mathatom left of the cursor
190 MathAtom const & prevAtom() const;
191 /// the mathatom left of the cursor
192 MathAtom & prevAtom();
193 /// the mathatom right of the cursor
194 MathAtom const & nextAtom() const;
195 /// the mathatom right of the cursor
196 MathAtom & nextAtom();
197 /// auto-correct mode
198 bool autocorrect() const { return autocorrect_; }
199 /// auto-correct mode
200 bool & autocorrect() { return autocorrect_; }
201 /// are we entering a macro name?
202 bool macromode() const { return macromode_; }
203 /// are we entering a macro name?
204 bool & macromode() { return macromode_; }
207 // text-specific part
209 bool boundary() const { return current().boundary(); }
211 bool & boundary() { return current().boundary(); }
213 Paragraph & paragraph();
215 Paragraph const & paragraph() const;
217 LyXText * text() const;
219 InsetBase * innerInsetOfType(int code) const;
221 InsetTabular * innerInsetTabular() const;
223 LyXText * innerText() const;
225 CursorSlice const & innerTextSlice() const;
226 /// returns x,y position
227 void getPos(int & x, int & y) const;
228 /// returns cursor dimension
229 void getDim(int & asc, int & desc) const;
234 /// move one step to the left
236 /// move one step to the right
239 /// write acess to target x position of cursor
241 /// return target x position of cursor
242 int x_target() const;
243 /// clear target x position of cursor
246 /// access to selection anchor
247 CursorSlice & anchor();
248 /// access to selection anchor
249 CursorSlice const & anchor() const;
250 /// cache the absolute coordinate from the top inset
252 /// sets anchor to cursor position
254 /// access to owning BufferView
255 BufferView & bv() const;
256 /// get some interesting description of current position
257 void info(std::ostream & os);
258 /// are we in math mode (2), text mode (1) or unsure (0)?
264 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
267 /// mainly used as stack, but wee need random access
268 std::vector<CursorSlice> cursor_;
269 /// the anchor position
270 std::vector<CursorSlice> anchor_;
280 * The target x position of the cursor. This is used for when
281 * we have text like :
283 * blah blah blah blah| blah blah blah
285 * blah blah blah blah blah blah
287 * When we move onto row 3, we would like to be vertically aligned
288 * with where we were in row 1, despite the fact that row 2 is
292 // do we have a selection?
294 // are we on the way to get one?
298 // math specific stuff that could be promoted to "global" later
300 /// do we allow autocorrection
302 /// are we entering a macro name?
306 ///////////////////////////////////////////////////////////////////
308 // The part below is the non-integrated rest of the original math
309 // cursor. This should be either generalized for texted or moved
310 // back to the math insets.
312 ///////////////////////////////////////////////////////////////////
316 void insert(MathAtom const &);
318 void insert(MathArray const &);
320 void insert2(std::string const &);
321 /// return false for empty math insets
323 /// return false for empty math insets
325 /// called for LFUN_HOME etc
327 /// called for LFUN_END etc
329 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
331 /// called for LFUN_LEFT etc
333 /// called for LFUN_UP etc
335 /// called for LFUN_DOWN etc
340 void plainInsert(MathAtom const & at);
342 void niceInsert(MathAtom const & at);
344 void niceInsert(std::string const & str);
346 /// in pixels from top of screen
347 void setScreenPos(int x, int y);
348 /// in pixels from left of screen
350 /// return the next enclosing grid inset and the cursor's index in it
351 MathGridInset * enclosingGrid(idx_type & idx) const;
352 /// adjust anchor position after deletions/insertions
353 void adjust(pos_type from, int diff);
355 MathHullInset * formula() const;
356 /// current offset in the current cell
360 bool interpret(char);
361 /// interpret name a name of a macro
362 void macroModeClose();
363 /// are we currently typing the name of a macro?
364 bool inMacroMode() const;
365 /// get access to the macro we are currently typing
366 MathUnknownInset * activeMacro();
367 /// are we currently typing '#1' or '#2' or...?
368 bool inMacroArgMode() const;
370 /// replace selected stuff with at, placing the former
371 // selection in given cell of atom
372 void handleNest(MathAtom const & at, int cell = 0);
373 /// remove this as soon as LyXFunc::getStatus is "localized"
374 //inline std::string getLastCode() { return "mathnormal"; }
376 bool isInside(InsetBase const *);
382 /// make sure cursor position is valid
384 /// mark current cursor trace for redraw
387 /// returns the normalized anchor of the selection
388 CursorSlice normalAnchor();
393 void insert(std::string const & str);
394 /// lock/unlock inset
397 /// hack for reveal codes
400 /// injects content of a cell into parent
402 /// split font inset etc
403 void handleFont(std::string const & font);
405 void releaseMathCursor();
406 /// are we in mathed?
407 bool inMathed() const;
409 /// display a message
410 void message(std::string const & msg) const;
411 /// display an error message
412 void errorMessage(std::string const & msg) const;
415 /// moves cursor index one cell to the left
417 /// moves cursor index one cell to the right
419 /// moves cursor to end of last cell of current line
421 /// moves position somehow up or down
422 bool goUpDown(bool up);
423 /// moves position closest to (x, y) in given box
424 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
425 /// moves position closest to (x, y) in current cell
426 void bruteFind2(int x, int y);
427 /// are we in a nucleus of a script inset?
430 /// the name of the macro we are currently inputting
431 std::string macroName();
432 /// where in the curent cell does the macro name start?
434 /// can we enter the inset?
435 bool openable(MathAtom const &);
438 #endif // LYXCURSOR_H