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;
37 // only needed for gcc 2.95, remove when support terminated
38 template <typename A, typename B>
39 bool ptr_cmp(A const * a, B const * b)
45 // this is used for traversing math insets
46 typedef std::vector<CursorSlice> CursorBase;
48 void increment(CursorBase &);
50 CursorBase ibegin(InsetBase * p);
52 CursorBase iend(InsetBase * p);
56 * The cursor class describes the position of a cursor within a document.
61 /// type for cell number in inset
62 typedef CursorSlice::idx_type idx_type;
63 /// type for paragraph numbers positions within a cell
64 typedef CursorSlice::par_type par_type;
65 /// type for cursor positions within a cell
66 typedef CursorSlice::pos_type pos_type;
67 /// type for row indices
68 typedef CursorSlice::row_type row_type;
69 /// type for col indices
70 typedef CursorSlice::col_type col_type;
72 /// create the cursor of a BufferView
73 explicit LCursor(BufferView & bv);
74 /// dispatch from innermost inset upwards
75 DispatchResult dispatch(FuncRequest const & cmd);
76 /// add a new cursor slice
77 void push(InsetBase * inset);
78 /// add a new cursor slice, place cursor on left end
79 void pushLeft(InsetBase * inset);
80 /// pop one level off the cursor
82 /// pop one slice off the cursor stack and go left
84 /// pop one slice off the cursor stack and go right
86 /// restrict cursor nesting to given size
88 /// access to current cursor slice
89 CursorSlice & current();
90 /// access to current cursor slice
91 CursorSlice const & current() const;
92 /// how many nested insets do we have?
93 size_t depth() const { return cursor_.size(); }
94 /// depth of current slice
95 int currentDepth() const { return current_; }
100 /// selection active?
101 bool selection() const { return selection_; }
102 /// selection active?
103 bool & selection() { return selection_; }
104 /// did we place the anchor?
105 bool mark() const { return mark_; }
106 /// did we place the anchor?
107 bool & mark() { return mark_; }
110 /// set selection at given position
111 void setSelection(CursorBase const & where, size_t n);
113 void clearSelection();
114 /// access start of selection
115 CursorSlice & selBegin();
116 /// access start of selection
117 CursorSlice const & selBegin() const;
118 /// access end of selection
119 CursorSlice & selEnd();
120 /// access end of selection
121 CursorSlice const & selEnd() const;
123 std::string grabSelection();
125 void eraseSelection();
127 std::string grabAndEraseSelection();
128 // other selection methods
135 /// pastes n-th element of cut buffer
136 void selPaste(size_t n);
138 void selHandle(bool selecting);
143 /// clears or deletes selection depending on lyxrc setting
144 void selClearOrDel();
146 std::string selectionAsString(bool label) const;
148 void paste(std::string const & data);
151 // access to the 'current' cursor slice
153 /// the containing inset
154 InsetBase * inset() const { return current().inset(); }
155 /// return the cell of the inset this cursor is in
156 idx_type idx() const { return current().idx(); }
157 /// return the cell of the inset this cursor is in
158 idx_type & idx() { return current().idx(); }
159 /// return the last possible cell in this inset
160 idx_type lastidx() const { return current().lastidx(); }
161 /// return the paragraph this cursor is in
162 par_type par() const { return current().par(); }
163 /// return the paragraph this cursor is in
164 par_type & par() { return current().par(); }
165 /// return the last possible paragraph in this inset
166 par_type lastpar() const;
167 /// return the position within the paragraph
168 pos_type pos() const { return current().pos(); }
169 /// return the position within the paragraph
170 pos_type & pos() { return current().pos(); }
171 /// return the last position within the paragraph
172 pos_type lastpos() const;
173 /// return the display row of the cursor with in the current par
174 row_type crow() const;
175 /// return the display row of the cursor with in the current par
176 row_type lastcrow() const;
178 /// return the number of embedded cells
179 size_t nargs() const;
180 /// return the number of embedded cells
181 size_t ncols() const;
182 /// return the number of embedded cells
183 size_t nrows() const;
184 /// return the grid row of the current cell
185 row_type row() const;
186 /// return the grid row of the current cell
187 col_type col() const;
188 /// the inset just behind the cursor
189 InsetBase * nextInset();
190 /// the inset just in front of the cursor
191 InsetBase * prevInset();
194 // math-specific part
196 /// return the mathed cell this cursor is in
197 MathArray const & cell() const;
198 /// return the mathed cell this cursor is in
200 /// the mathatom left of the cursor
201 MathAtom const & prevAtom() const;
202 /// the mathatom left of the cursor
203 MathAtom & prevAtom();
204 /// the mathatom right of the cursor
205 MathAtom const & nextAtom() const;
206 /// the mathatom right of the cursor
207 MathAtom & nextAtom();
208 /// auto-correct mode
209 bool autocorrect() const { return autocorrect_; }
210 /// auto-correct mode
211 bool & autocorrect() { return autocorrect_; }
212 /// are we entering a macro name?
213 bool macromode() const { return macromode_; }
214 /// are we entering a macro name?
215 bool & macromode() { return macromode_; }
218 // text-specific part
219 /// see comment for boundary_ below
220 bool boundary() const { return current().boundary(); }
221 /// see comment for boundary_ below
222 bool & boundary() { return current().boundary(); }
223 /// the paragraph we're in
224 Paragraph & paragraph();
225 /// the paragraph we're in
226 Paragraph const & paragraph() const;
227 /// the row in the paragraph we're in
229 /// the row in the paragraph we're in
230 Row const & textRow() const;
232 LyXText * text() const;
234 InsetBase * innerInsetOfType(int code) const;
236 InsetTabular * innerInsetTabular() const;
238 LyXText * innerText() const;
240 CursorSlice const & innerTextSlice() const;
241 /// returns x,y position
242 void getPos(int & x, int & y) const;
243 /// returns cursor dimension
244 void getDim(int & asc, int & desc) const;
249 /// move one step to the left
251 /// move one step to the right
255 void insert(InsetBase *);
256 /// insert a single char
259 void insert(std::string const & str);
261 /// write acess to target x position of cursor
263 /// return target x position of cursor
264 int x_target() const;
265 /// clear target x position of cursor
268 /// access to selection anchor
269 CursorSlice & anchor();
270 /// access to selection anchor
271 CursorSlice const & anchor() const;
272 /// cache the absolute coordinate from the top inset
274 /// sets anchor to cursor position
276 /// access to owning BufferView
277 BufferView & bv() const;
278 /// get some interesting description of current position
279 void info(std::ostream & os);
280 /// are we in math mode (2), text mode (1) or unsure (0)?
286 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
289 /// mainly used as stack, but wee need random access
290 std::vector<CursorSlice> cursor_;
291 /// the anchor position
292 std::vector<CursorSlice> anchor_;
302 * The target x position of the cursor. This is used for when
303 * we have text like :
305 * blah blah blah blah| blah blah blah
307 * blah blah blah blah blah blah
309 * When we move onto row 3, we would like to be vertically aligned
310 * with where we were in row 1, despite the fact that row 2 is
314 // do we have a selection?
316 // are we on the way to get one?
320 // math specific stuff that could be promoted to "global" later
322 /// do we allow autocorrection
324 /// are we entering a macro name?
328 ///////////////////////////////////////////////////////////////////
330 // The part below is the non-integrated rest of the original math
331 // cursor. This should be either generalized for texted or moved
332 // back to the math insets.
334 ///////////////////////////////////////////////////////////////////
338 void insert(MathAtom const &);
340 void insert(MathArray const &);
341 /// return false for empty math insets
343 /// return false for empty math insets
345 /// called for LFUN_HOME etc
347 /// called for LFUN_END etc
349 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
351 /// called for LFUN_LEFT etc
353 /// called for LFUN_UP etc
355 /// called for LFUN_DOWN etc
360 void plainInsert(MathAtom const & at);
362 void niceInsert(MathAtom const & at);
364 void niceInsert(std::string const & str);
366 /// in pixels from top of screen
367 void setScreenPos(int x, int y);
368 /// in pixels from left of screen
370 /// return the next enclosing grid inset and the cursor's index in it
371 MathGridInset * enclosingGrid(idx_type & idx) const;
372 /// adjust anchor position after deletions/insertions
373 void adjust(pos_type from, int diff);
375 MathHullInset * formula() const;
376 /// current offset in the current cell
380 bool interpret(char);
381 /// interpret name a name of a macro
382 void macroModeClose();
383 /// are we currently typing the name of a macro?
384 bool inMacroMode() const;
385 /// get access to the macro we are currently typing
386 MathUnknownInset * activeMacro();
387 /// are we currently typing '#1' or '#2' or...?
388 bool inMacroArgMode() const;
390 /// replace selected stuff with at, placing the former
391 // selection in given cell of atom
392 void handleNest(MathAtom const & at, int cell = 0);
393 /// remove this as soon as LyXFunc::getStatus is "localized"
394 //inline std::string getLastCode() { return "mathnormal"; }
396 bool isInside(InsetBase const *);
402 /// make sure cursor position is valid
404 /// mark current cursor trace for redraw
407 /// returns the normalized anchor of the selection
408 CursorSlice normalAnchor();
410 /// lock/unlock inset
413 /// hack for reveal codes
416 /// injects content of a cell into parent
418 /// split font inset etc
419 void handleFont(std::string const & font);
421 void releaseMathCursor();
422 /// are we in mathed?
423 bool inMathed() const;
424 /// are we in texted?
425 bool inTexted() const;
427 /// display a message
428 void message(std::string const & msg) const;
429 /// display an error message
430 void errorMessage(std::string const & msg) const;
433 /// moves cursor index one cell to the left
435 /// moves cursor index one cell to the right
437 /// moves cursor to end of last cell of current line
439 /// moves position somehow up or down
440 bool goUpDown(bool up);
441 /// moves position closest to (x, y) in given box
442 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
443 /// moves position closest to (x, y) in current cell
444 void bruteFind2(int x, int y);
445 /// are we in a nucleus of a script inset?
448 /// the name of the macro we are currently inputting
449 std::string macroName();
450 /// where in the curent cell does the macro name start?
452 /// can we enter the inset?
453 bool openable(MathAtom const &);
456 #endif // LYXCURSOR_H