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 Paragraph & paragraph();
200 Paragraph const & paragraph() const;
202 InsetBase * innerInsetOfType(int code) const;
204 InsetTabular * innerInsetTabular() const;
206 LyXText * innerText() const;
208 CursorSlice const & innerTextSlice() const;
209 /// returns x,y position
210 void getPos(int & x, int & y) const;
211 /// returns cursor dimension
212 void getDim(int & asc, int & desc) const;
217 /// move one step to the left
219 /// move one step to the right
222 /// set target x position of cursor
223 void x_target(int x);
224 /// return target x position of cursor
225 int x_target() const;
227 /// access to selection anchor
228 CursorSlice & anchor();
229 /// access to selection anchor
230 CursorSlice const & anchor() const;
231 /// cache the absolute coordinate from the top inset
233 /// sets anchor to cursor position
235 /// access to owning BufferView
236 BufferView & bv() const;
237 /// get some interesting description of current position
238 void info(std::ostream & os);
239 /// are we in math mode (2), text mode (1) or unsure (0)?
245 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
248 /// mainly used as stack, but wee need random access
249 std::vector<CursorSlice> cursor_;
250 /// the anchor position
251 std::vector<CursorSlice> anchor_;
254 /// don't implement this
255 void operator=(LCursor const &);
256 /// don't implement this
257 LCursor(LCursor const &);
260 BufferView * const bv_;
266 * The target x position of the cursor. This is used for when
267 * we have text like :
269 * blah blah blah blah| blah blah blah
271 * blah blah blah blah blah blah
273 * When we move onto row 3, we would like to be vertically aligned
274 * with where we were in row 1, despite the fact that row 2 is
278 // do we have a selection?
280 // are we on the way to get one?
284 // math specific stuff that could be promoted to "global" later
286 /// do we allow autocorrection
288 /// are we entering a macro name?
292 ///////////////////////////////////////////////////////////////////
294 // The part below is the non-integrated rest of the original math
295 // cursor. This should be either generalized for texted or moved
296 // back to the math insets.
298 ///////////////////////////////////////////////////////////////////
302 void insert(MathAtom const &);
304 void insert(MathArray const &);
306 void insert2(std::string const &);
307 /// return false for empty math insets
309 /// return false for empty math insets
311 /// called for LFUN_HOME etc
313 /// called for LFUN_END etc
315 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
317 /// called for LFUN_LEFT etc
319 /// called for LFUN_UP etc
321 /// called for LFUN_DOWN etc
326 void plainInsert(MathAtom const & at);
328 void niceInsert(MathAtom const & at);
330 void niceInsert(std::string const & str);
332 /// in pixels from top of screen
333 void setScreenPos(int x, int y);
334 /// in pixels from top of screen
335 void getScreenPos(int & x, int & y) const;
336 /// in pixels from left of screen
338 /// return the next enclosing grid inset and the cursor's index in it
339 MathGridInset * enclosingGrid(idx_type & idx) const;
340 /// adjust anchor position after deletions/insertions
341 void adjust(pos_type from, int diff);
343 MathHullInset * formula() const;
344 /// current offset in the current cell
348 bool interpret(char);
349 /// interpret name a name of a macro
350 void macroModeClose();
351 /// are we currently typing the name of a macro?
352 bool inMacroMode() const;
353 /// get access to the macro we are currently typing
354 MathUnknownInset * activeMacro();
355 /// are we currently typing '#1' or '#2' or...?
356 bool inMacroArgMode() const;
358 /// draws light-blue selection background
359 void drawSelection(PainterInfo & pi);
360 /// replace selected stuff with at, placing the former
361 // selection in given cell of atom
362 void handleNest(MathAtom const & at, int cell = 0);
363 /// remove this as soon as LyXFunc::getStatus is "localized"
364 //inline std::string getLastCode() { return "mathnormal"; }
366 bool isInside(InsetBase const *);
372 /// make sure cursor position is valid
374 /// mark current cursor trace for redraw
377 /// returns the normalized anchor of the selection
378 CursorSlice normalAnchor();
383 void insert(std::string const & str);
384 /// lock/unlock inset
387 /// hack for reveal codes
390 /// injects content of a cell into parent
392 /// split font inset etc
393 void handleFont(std::string const & font);
395 void releaseMathCursor();
397 bool inMathed() const;
400 /// moves cursor index one cell to the left
402 /// moves cursor index one cell to the right
404 /// moves cursor to end of last cell of current line
406 /// moves position somehow up or down
407 bool goUpDown(bool up);
408 /// moves position closest to (x, y) in given box
409 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
410 /// moves position closest to (x, y) in current cell
411 void bruteFind2(int x, int y);
412 /// are we in a nucleus of a script inset?
415 /// the name of the macro we are currently inputting
416 std::string macroName();
417 /// where in the curent cell does the macro name start?
419 /// can we enter the inset?
420 bool openable(MathAtom const &);
423 #endif // LYXCURSOR_H