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 InsetBase * innerInsetOfType(int code) const;
206 InsetTabular * innerInsetTabular() const;
208 LyXText * innerText() const;
210 CursorSlice const & innerTextSlice() const;
211 /// returns x,y position
212 void getPos(int & x, int & y) const;
213 /// returns cursor dimension
214 void getDim(int & asc, int & desc) const;
219 /// move one step to the left
221 /// move one step to the right
224 /// write acess to target x position of cursor
226 /// return target x position of cursor
227 int x_target() const;
228 /// clear target x position of cursor
231 /// access to selection anchor
232 CursorSlice & anchor();
233 /// access to selection anchor
234 CursorSlice const & anchor() const;
235 /// cache the absolute coordinate from the top inset
237 /// sets anchor to cursor position
239 /// access to owning BufferView
240 BufferView & bv() const;
241 /// get some interesting description of current position
242 void info(std::ostream & os);
243 /// are we in math mode (2), text mode (1) or unsure (0)?
249 friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
252 /// mainly used as stack, but wee need random access
253 std::vector<CursorSlice> cursor_;
254 /// the anchor position
255 std::vector<CursorSlice> anchor_;
265 * The target x position of the cursor. This is used for when
266 * we have text like :
268 * blah blah blah blah| blah blah blah
270 * blah blah blah blah blah blah
272 * When we move onto row 3, we would like to be vertically aligned
273 * with where we were in row 1, despite the fact that row 2 is
277 // do we have a selection?
279 // are we on the way to get one?
283 // math specific stuff that could be promoted to "global" later
285 /// do we allow autocorrection
287 /// are we entering a macro name?
291 ///////////////////////////////////////////////////////////////////
293 // The part below is the non-integrated rest of the original math
294 // cursor. This should be either generalized for texted or moved
295 // back to the math insets.
297 ///////////////////////////////////////////////////////////////////
301 void insert(MathAtom const &);
303 void insert(MathArray const &);
305 void insert2(std::string const &);
306 /// return false for empty math insets
308 /// return false for empty math insets
310 /// called for LFUN_HOME etc
312 /// called for LFUN_END etc
314 /// called for LFUN_RIGHT and LFUN_RIGHTSEL
316 /// called for LFUN_LEFT etc
318 /// called for LFUN_UP etc
320 /// called for LFUN_DOWN etc
325 void plainInsert(MathAtom const & at);
327 void niceInsert(MathAtom const & at);
329 void niceInsert(std::string const & str);
331 /// in pixels from top of screen
332 void setScreenPos(int x, int y);
333 /// in pixels from left of screen
335 /// return the next enclosing grid inset and the cursor's index in it
336 MathGridInset * enclosingGrid(idx_type & idx) const;
337 /// adjust anchor position after deletions/insertions
338 void adjust(pos_type from, int diff);
340 MathHullInset * formula() const;
341 /// current offset in the current cell
345 bool interpret(char);
346 /// interpret name a name of a macro
347 void macroModeClose();
348 /// are we currently typing the name of a macro?
349 bool inMacroMode() const;
350 /// get access to the macro we are currently typing
351 MathUnknownInset * activeMacro();
352 /// are we currently typing '#1' or '#2' or...?
353 bool inMacroArgMode() const;
355 /// draws light-blue selection background
356 void drawSelection(PainterInfo & pi);
357 /// replace selected stuff with at, placing the former
358 // selection in given cell of atom
359 void handleNest(MathAtom const & at, int cell = 0);
360 /// remove this as soon as LyXFunc::getStatus is "localized"
361 //inline std::string getLastCode() { return "mathnormal"; }
363 bool isInside(InsetBase const *);
369 /// make sure cursor position is valid
371 /// mark current cursor trace for redraw
374 /// returns the normalized anchor of the selection
375 CursorSlice normalAnchor();
380 void insert(std::string const & str);
381 /// lock/unlock inset
384 /// hack for reveal codes
387 /// injects content of a cell into parent
389 /// split font inset etc
390 void handleFont(std::string const & font);
392 void releaseMathCursor();
394 bool inMathed() const;
397 /// moves cursor index one cell to the left
399 /// moves cursor index one cell to the right
401 /// moves cursor to end of last cell of current line
403 /// moves position somehow up or down
404 bool goUpDown(bool up);
405 /// moves position closest to (x, y) in given box
406 bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
407 /// moves position closest to (x, y) in current cell
408 void bruteFind2(int x, int y);
409 /// are we in a nucleus of a script inset?
412 /// the name of the macro we are currently inputting
413 std::string macroName();
414 /// where in the curent cell does the macro name start?
416 /// can we enter the inset?
417 bool openable(MathAtom const &);
420 #endif // LYXCURSOR_H