]> git.lyx.org Git - lyx.git/blob - src/cursor.h
59519bbb865c2e7c23d69ca4d495aac2ad4a5ff5
[lyx.git] / src / cursor.h
1 // -*- C++ -*-
2 /**
3  * \file cursor.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef CURSOR_H
13 #define CURSOR_H
14
15 #include "cursor_slice.h"
16
17 #include <iosfwd>
18 #include <vector>
19
20 class BufferView;
21 class UpdatableInset;
22 class MathAtom;
23 class DispatchResult;
24 class FuncRequest;
25 class InsetTabular;
26 class LyXText;
27 class Paragraph;
28
29
30 // these should go
31 class MathHullInset;
32 class PainterInfo;
33 class MathUnknownInset;
34 class MathGridInset;
35
36 // this is used for traversing math insets
37 typedef std::vector<CursorSlice> CursorBase;
38 /// move on one step
39 void increment(CursorBase &);
40 ///
41 CursorBase ibegin(InsetBase * p);
42 ///
43 CursorBase iend(InsetBase * p);
44
45
46 /**
47  * The cursor class describes the position of a cursor within a document.
48  */
49
50 class LCursor {
51 public:
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;
62
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
72         void pop();
73         /// pop one slice off the cursor stack and go left
74         bool popLeft();
75         /// pop one slice off the cursor stack and go right
76         bool popRight();
77         /// restrict cursor nesting to given size
78         void pop(int depth);
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(); }
85
86         //
87         // selection
88         //
89         /// selection active?
90         bool selection() const { return selection_; }
91         /// selection active?
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_; }
97         ///
98         void setSelection();
99         /// set selection at given position
100         void setSelection(CursorBase const & where, size_t n);
101         ///
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;
111         ///
112         std::string grabSelection();
113         ///
114         void eraseSelection();
115         ///
116         std::string grabAndEraseSelection();
117         // other selection methods
118         ///
119         void selCopy();
120         ///
121         void selCut();
122         ///
123         void selDel();
124         /// pastes n-th element of cut buffer
125         void selPaste(size_t n);
126         ///
127         void selHandle(bool selecting);
128         /// start selection
129         void selStart();
130         /// clear selection
131         void selClear();
132         /// clears or deletes selection depending on lyxrc setting
133         void selClearOrDel();
134         ///
135         void paste(std::string const & data);
136
137         //
138         // access to the 'current' cursor slice
139         //
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;
168
169         //
170         // math-specific part
171         //
172         /// return the mathed cell this cursor is in
173         MathArray const & cell() const;
174         /// return the mathed cell this cursor is in
175         MathArray & cell();
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_; }
192
193         //
194         // text-specific part
195         ///
196         bool boundary() const { return current().boundary(); }
197         ///
198         Paragraph & paragraph();
199         ///
200         Paragraph const & paragraph() const;
201         ///
202         InsetBase * innerInsetOfType(int code) const;
203         ///
204         InsetTabular * innerInsetTabular() const;
205         ///
206         LyXText * innerText() const;
207         ///
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;
213
214         //
215         // common part
216         //
217         /// move one step to the left
218         bool posLeft();
219         /// move one step to the right
220         bool posRight();
221
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;
226
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
232         void updatePos();
233         /// sets anchor to cursor position
234         void resetAnchor(); 
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)?
240         int currentMode();
241         /// reset cursor
242         void reset();
243
244         /// output
245         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
246 public:
247 //private:
248         /// mainly used as stack, but wee need random access
249         std::vector<CursorSlice> cursor_;
250         /// the anchor position
251         std::vector<CursorSlice> anchor_;
252
253 private:
254         /// don't implement this
255         void operator=(LCursor const &);
256         /// don't implement this
257         LCursor(LCursor const &);
258
259         ///
260         BufferView * const bv_;
261         /// current slice
262         int current_;
263         ///
264         int cached_y_;
265         /**
266          * The target x position of the cursor. This is used for when
267          * we have text like :
268          *
269          * blah blah blah blah| blah blah blah
270          * blah blah blah
271          * blah blah blah blah blah blah
272          *
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
275          * shorter than x()
276          */
277         int x_target_;
278         // do we have a selection?
279         bool selection_;
280         // are we on the way to get one?
281         bool mark_;
282
283         //
284         // math specific stuff that could be promoted to "global" later
285         //
286         /// do we allow autocorrection
287         bool autocorrect_;
288         /// are we entering a macro name?
289         bool macromode_;
290
291
292 ///////////////////////////////////////////////////////////////////
293 //
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.
297 //
298 ///////////////////////////////////////////////////////////////////
299
300 public:
301         ///
302         void insert(MathAtom const &);
303         ///
304         void insert(MathArray const &);
305         ///
306         void insert2(std::string const &);
307         /// return false for empty math insets
308         bool erase();
309         /// return false for empty math insets
310         bool backspace();
311         /// called for LFUN_HOME etc
312         bool home();
313         /// called for LFUN_END etc
314         bool end();
315         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
316         bool right();
317         /// called for LFUN_LEFT etc
318         bool left();
319         /// called for LFUN_UP etc
320         bool up();
321         /// called for LFUN_DOWN etc
322         bool down();
323         ///
324         void plainErase();
325         ///
326         void plainInsert(MathAtom const & at);
327         ///
328         void niceInsert(MathAtom const & at);
329         ///
330         void niceInsert(std::string const & str);
331
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
337         int targetX() const;
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);
342         ///
343         MathHullInset * formula() const;
344         /// current offset in the current cell
345         ///
346         bool script(bool);
347         ///
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;
357
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"; }
365         ///
366         bool isInside(InsetBase const *);
367         ///
368         char valign();
369         ///
370         char halign();
371
372         /// make sure cursor position is valid
373         void normalize();
374         /// mark current cursor trace for redraw
375         void touch();
376
377         /// returns the normalized anchor of the selection
378         CursorSlice normalAnchor();
379
380         ///
381         void insert(char c);
382         ///
383         void insert(std::string const & str);
384         /// lock/unlock inset
385         void lockToggle();
386
387         /// hack for reveal codes
388         void markInsert();
389         void markErase();
390         /// injects content of a cell into parent
391         void pullArg();
392         /// split font inset etc
393         void handleFont(std::string const & font);
394
395         void releaseMathCursor();
396
397         bool inMathed() const;
398
399 private:
400         /// moves cursor index one cell to the left
401         bool idxLeft();
402         /// moves cursor index one cell to the right
403         bool idxRight();
404         /// moves cursor to end of last cell of current line
405         bool idxLineLast();
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?
413         bool inNucleus();
414
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?
418         int macroNamePos();
419         /// can we enter the inset?
420         bool openable(MathAtom const &);
421 };
422
423 #endif // LYXCURSOR_H