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(); }
90 bool selection() const { return selection_; }
92 bool & selection() { return selection_; }
93 /// did we place the anchor?
94 bool mark() const { return mark_; }
95 /// did we place the anchor?
96 bool & mark() { return mark_; }
99 /// set selection at given position
100 void setSelection(CursorBase const & where, size_t n);
102 void clearSelection();
103 /// access start of selection
104 CursorSlice & selBegin();
105 /// access start of selection
106 CursorSlice const & selBegin() const;
107 /// access end of selection
108 CursorSlice & selEnd();
109 /// access end of selection
110 CursorSlice const & selEnd() const;
112 std::string grabSelection();
114 void eraseSelection();
116 std::string grabAndEraseSelection();
117 // other selection methods
124 /// pastes n-th element of cut buffer
125 void selPaste(size_t n);
127 void selHandle(bool selecting);
132 /// clears or deletes selection depending on lyxrc setting
133 void selClearOrDel();
135 void paste(std::string const & data);
138 // access to the 'current' cursor slice
140 /// the current inset
141 InsetBase * inset() const { return current().inset(); }
142 /// return the cell of the inset this cursor is in
143 idx_type idx() const { return current().idx(); }
144 /// return the cell of the inset this cursor is in
145 idx_type & idx() { return current().idx(); }
146 /// return the last possible cell in this inset
147 idx_type lastidx() const { return current().lastidx(); }
148 /// return the paragraph this cursor is in
149 par_type par() const { return current().par(); }
150 /// return the paragraph this cursor is in
151 par_type & par() { return current().par(); }
152 /// return the position within the paragraph
153 pos_type pos() const { return current().pos(); }
154 /// return the position within the paragraph
155 pos_type & pos() { return current().pos(); }
156 /// return the last position within the paragraph
157 pos_type lastpos() const;
158 /// return the number of embedded cells
159 size_t nargs() const;
160 /// return the number of embedded cells
161 size_t ncols() const;
162 /// return the number of embedded cells
163 size_t nrows() const;
164 /// return the grid row of the current cell
165 row_type row() const;
166 /// return the grid row of the current cell
167 col_type col() const;
170 // math-specific part
172 /// return the mathed cell this cursor is in
173 MathArray const & cell() const;
174 /// return the mathed cell this cursor is in
176 /// the mathatom left of the cursor
177 MathAtom const & prevAtom() const;
178 /// the mathatom left of the cursor
179 MathAtom & prevAtom();
180 /// the mathatom right of the cursor
181 MathAtom const & nextAtom() const;
182 /// the mathatom right of the cursor
183 MathAtom & nextAtom();
184 /// auto-correct mode
185 bool autocorrect() const { return autocorrect_; }
186 /// auto-correct mode
187 bool & autocorrect() { return autocorrect_; }
188 /// are we entering a macro name?
189 bool macromode() const { return macromode_; }
190 /// are we entering a macro name?
191 bool & macromode() { return macromode_; }
194 // text-specific part
196 bool boundary() const { return current().boundary(); }
198 bool & boundary() { return current().boundary(); }
200 Paragraph & paragraph();
202 Paragraph const & paragraph() const;
204 LyXText * text() const;
206 InsetBase * innerInsetOfType(int code) const;
208 InsetTabular * innerInsetTabular() const;
210 LyXText * innerText() const;
212 CursorSlice const & innerTextSlice() const;
213 /// returns x,y position
214 void getPos(int & x, int & y) const;
215 /// returns cursor dimension
216 void getDim(int & asc, int & desc) const;
221 /// move one step to the left
223 /// move one step to the right
226 /// write acess to target x position of cursor
228 /// return target x position of cursor
229 int x_target() const;
230 /// clear target x position of cursor
233 /// access to selection anchor
234 CursorSlice & anchor();
235 /// access to selection anchor
236 CursorSlice const & anchor() const;
237 /// cache the absolute coordinate from the top inset
239 /// sets anchor to cursor position
241 /// access to owning BufferView
242 BufferView & bv() const;
243 /// get some interesting description of current position
244 void info(std::ostream & os);
245 /// are we in math mode (2), text mode (1) or unsure (0)?
251 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
254 /// mainly used as stack, but wee need random access
255 std::vector<CursorSlice> cursor_;
256 /// the anchor position
257 std::vector<CursorSlice> anchor_;
267 * The target x position of the cursor. This is used for when
268 * we have text like :
270 * blah blah blah blah| blah blah blah
272 * blah blah blah blah blah blah
274 * When we move onto row 3, we would like to be vertically aligned
275 * with where we were in row 1, despite the fact that row 2 is
279 // do we have a selection?
281 // are we on the way to get one?
285 // math specific stuff that could be promoted to "global" later
287 /// do we allow autocorrection
289 /// are we entering a macro name?
293 ///////////////////////////////////////////////////////////////////
295 // The part below is the non-integrated rest of the original math
296 // cursor. This should be either generalized for texted or moved
297 // back to the math insets.
299 ///////////////////////////////////////////////////////////////////
303 void insert(MathAtom const &);
305 void insert(MathArray const &);
307 void insert2(std::string const &);
308 /// return false for empty math insets
310 /// return false for empty math insets
312 /// called for LFUN_HOME etc
314 /// called for LFUN_END etc
316 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
318 /// called for LFUN_LEFT etc
320 /// called for LFUN_UP etc
322 /// called for LFUN_DOWN etc
327 void plainInsert(MathAtom const & at);
329 void niceInsert(MathAtom const & at);
331 void niceInsert(std::string const & str);
333 /// in pixels from top of screen
334 void setScreenPos(int x, int y);
335 /// in pixels from left of screen
337 /// return the next enclosing grid inset and the cursor's index in it
338 MathGridInset * enclosingGrid(idx_type & idx) const;
339 /// adjust anchor position after deletions/insertions
340 void adjust(pos_type from, int diff);
342 MathHullInset * formula() const;
343 /// current offset in the current cell
347 bool interpret(char);
348 /// interpret name a name of a macro
349 void macroModeClose();
350 /// are we currently typing the name of a macro?
351 bool inMacroMode() const;
352 /// get access to the macro we are currently typing
353 MathUnknownInset * activeMacro();
354 /// are we currently typing '#1' or '#2' or...?
355 bool inMacroArgMode() const;
357 /// draws light-blue selection background
358 void drawSelection(PainterInfo & pi);
359 /// replace selected stuff with at, placing the former
360 // selection in given cell of atom
361 void handleNest(MathAtom const & at, int cell = 0);
362 /// remove this as soon as LyXFunc::getStatus is "localized"
363 //inline std::string getLastCode() { return "mathnormal"; }
365 bool isInside(InsetBase const *);
371 /// make sure cursor position is valid
373 /// mark current cursor trace for redraw
376 /// returns the normalized anchor of the selection
377 CursorSlice normalAnchor();
382 void insert(std::string const & str);
383 /// lock/unlock inset
386 /// hack for reveal codes
389 /// injects content of a cell into parent
391 /// split font inset etc
392 void handleFont(std::string const & font);
394 void releaseMathCursor();
396 bool inMathed() const;
399 /// moves cursor index one cell to the left
401 /// moves cursor index one cell to the right
403 /// moves cursor to end of last cell of current line
405 /// moves position somehow up or down
406 bool goUpDown(bool up);
407 /// moves position closest to (x, y) in given box
408 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
409 /// moves position closest to (x, y) in current cell
410 void bruteFind2(int x, int y);
411 /// are we in a nucleus of a script inset?
414 /// the name of the macro we are currently inputting
415 std::string macroName();
416 /// where in the curent cell does the macro name start?
418 /// can we enter the inset?
419 bool openable(MathAtom const &);
422 #endif // LYXCURSOR_H