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 containing 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 last possible paragraph in this inset
153 par_type lastpar() const;
154 /// return the position within the paragraph
155 pos_type pos() const { return current().pos(); }
156 /// return the position within the paragraph
157 pos_type & pos() { return current().pos(); }
158 /// return the last position within the paragraph
159 pos_type lastpos() const;
160 /// return the display row of the cursor with in the current par
161 row_type crow() const;
162 /// return the display row of the cursor with in the current par
163 row_type lastcrow() const;
165 /// return the number of embedded cells
166 size_t nargs() const;
167 /// return the number of embedded cells
168 size_t ncols() const;
169 /// return the number of embedded cells
170 size_t nrows() const;
171 /// return the grid row of the current cell
172 row_type row() const;
173 /// return the grid row of the current cell
174 col_type col() const;
175 /// the inset just behind the cursor
176 InsetBase * nextInset();
177 /// the inset just in front of the cursor
178 InsetBase * prevInset();
181 // math-specific part
183 /// return the mathed cell this cursor is in
184 MathArray const & cell() const;
185 /// return the mathed cell this cursor is in
187 /// the mathatom left of the cursor
188 MathAtom const & prevAtom() const;
189 /// the mathatom left of the cursor
190 MathAtom & prevAtom();
191 /// the mathatom right of the cursor
192 MathAtom const & nextAtom() const;
193 /// the mathatom right of the cursor
194 MathAtom & nextAtom();
195 /// auto-correct mode
196 bool autocorrect() const { return autocorrect_; }
197 /// auto-correct mode
198 bool & autocorrect() { return autocorrect_; }
199 /// are we entering a macro name?
200 bool macromode() const { return macromode_; }
201 /// are we entering a macro name?
202 bool & macromode() { return macromode_; }
205 // text-specific part
207 bool boundary() const { return current().boundary(); }
209 bool & boundary() { return current().boundary(); }
211 Paragraph & paragraph();
213 Paragraph const & paragraph() const;
215 LyXText * text() const;
217 InsetBase * innerInsetOfType(int code) const;
219 InsetTabular * innerInsetTabular() const;
221 LyXText * innerText() const;
223 CursorSlice const & innerTextSlice() const;
224 /// returns x,y position
225 void getPos(int & x, int & y) const;
226 /// returns cursor dimension
227 void getDim(int & asc, int & desc) const;
232 /// move one step to the left
234 /// move one step to the right
237 /// write acess to target x position of cursor
239 /// return target x position of cursor
240 int x_target() const;
241 /// clear target x position of cursor
244 /// access to selection anchor
245 CursorSlice & anchor();
246 /// access to selection anchor
247 CursorSlice const & anchor() const;
248 /// cache the absolute coordinate from the top inset
250 /// sets anchor to cursor position
252 /// access to owning BufferView
253 BufferView & bv() const;
254 /// get some interesting description of current position
255 void info(std::ostream & os);
256 /// are we in math mode (2), text mode (1) or unsure (0)?
262 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
265 /// mainly used as stack, but wee need random access
266 std::vector<CursorSlice> cursor_;
267 /// the anchor position
268 std::vector<CursorSlice> anchor_;
278 * The target x position of the cursor. This is used for when
279 * we have text like :
281 * blah blah blah blah| blah blah blah
283 * blah blah blah blah blah blah
285 * When we move onto row 3, we would like to be vertically aligned
286 * with where we were in row 1, despite the fact that row 2 is
290 // do we have a selection?
292 // are we on the way to get one?
296 // math specific stuff that could be promoted to "global" later
298 /// do we allow autocorrection
300 /// are we entering a macro name?
304 ///////////////////////////////////////////////////////////////////
306 // The part below is the non-integrated rest of the original math
307 // cursor. This should be either generalized for texted or moved
308 // back to the math insets.
310 ///////////////////////////////////////////////////////////////////
314 void insert(MathAtom const &);
316 void insert(MathArray const &);
318 void insert2(std::string const &);
319 /// return false for empty math insets
321 /// return false for empty math insets
323 /// called for LFUN_HOME etc
325 /// called for LFUN_END etc
327 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
329 /// called for LFUN_LEFT etc
331 /// called for LFUN_UP etc
333 /// called for LFUN_DOWN etc
338 void plainInsert(MathAtom const & at);
340 void niceInsert(MathAtom const & at);
342 void niceInsert(std::string const & str);
344 /// in pixels from top of screen
345 void setScreenPos(int x, int y);
346 /// in pixels from left of screen
348 /// return the next enclosing grid inset and the cursor's index in it
349 MathGridInset * enclosingGrid(idx_type & idx) const;
350 /// adjust anchor position after deletions/insertions
351 void adjust(pos_type from, int diff);
353 MathHullInset * formula() const;
354 /// current offset in the current cell
358 bool interpret(char);
359 /// interpret name a name of a macro
360 void macroModeClose();
361 /// are we currently typing the name of a macro?
362 bool inMacroMode() const;
363 /// get access to the macro we are currently typing
364 MathUnknownInset * activeMacro();
365 /// are we currently typing '#1' or '#2' or...?
366 bool inMacroArgMode() const;
368 /// replace selected stuff with at, placing the former
369 // selection in given cell of atom
370 void handleNest(MathAtom const & at, int cell = 0);
371 /// remove this as soon as LyXFunc::getStatus is "localized"
372 //inline std::string getLastCode() { return "mathnormal"; }
374 bool isInside(InsetBase const *);
380 /// make sure cursor position is valid
382 /// mark current cursor trace for redraw
385 /// returns the normalized anchor of the selection
386 CursorSlice normalAnchor();
391 void insert(std::string const & str);
392 /// lock/unlock inset
395 /// hack for reveal codes
398 /// injects content of a cell into parent
400 /// split font inset etc
401 void handleFont(std::string const & font);
403 void releaseMathCursor();
404 /// are we in mathed?
405 bool inMathed() const;
407 /// display a message
408 void message(std::string const & msg) const;
409 /// display an error message
410 void errorMessage(std::string const & msg) const;
413 /// moves cursor index one cell to the left
415 /// moves cursor index one cell to the right
417 /// moves cursor to end of last cell of current line
419 /// moves position somehow up or down
420 bool goUpDown(bool up);
421 /// moves position closest to (x, y) in given box
422 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
423 /// moves position closest to (x, y) in current cell
424 void bruteFind2(int x, int y);
425 /// are we in a nucleus of a script inset?
428 /// the name of the macro we are currently inputting
429 std::string macroName();
430 /// where in the curent cell does the macro name start?
432 /// can we enter the inset?
433 bool openable(MathAtom const &);
436 #endif // LYXCURSOR_H