]> git.lyx.org Git - lyx.git/blob - src/cursor.h
0d316b2fac98667e6bdc4f9976207b7ab99395b3
[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 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;
164
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();
179
180         //
181         // math-specific part
182         //
183         /// return the mathed cell this cursor is in
184         MathArray const & cell() const;
185         /// return the mathed cell this cursor is in
186         MathArray & cell();
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_; }
203
204         //
205         // text-specific part
206         ///
207         bool boundary() const { return current().boundary(); }
208         ///
209         bool & boundary() { return current().boundary(); }
210         ///
211         Paragraph & paragraph();
212         ///
213         Paragraph const & paragraph() const;
214         ///
215         LyXText * text() const;
216         ///
217         InsetBase * innerInsetOfType(int code) const;
218         ///
219         InsetTabular * innerInsetTabular() const;
220         ///
221         LyXText * innerText() const;
222         ///
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;
228
229         //
230         // common part
231         //
232         /// move one step to the left
233         bool posLeft();
234         /// move one step to the right
235         bool posRight();
236
237         /// write acess to target x position of cursor
238         int & x_target();
239         /// return target x position of cursor
240         int x_target() const;
241         /// clear target x position of cursor
242         void clearTargetX();
243
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
249         void updatePos();
250         /// sets anchor to cursor position
251         void resetAnchor(); 
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)?
257         int currentMode();
258         /// reset cursor
259         void reset();
260
261         /// output
262         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
263 public:
264 //private:
265         /// mainly used as stack, but wee need random access
266         std::vector<CursorSlice> cursor_;
267         /// the anchor position
268         std::vector<CursorSlice> anchor_;
269
270 private:
271         ///
272         BufferView * bv_;
273         /// current slice
274         int current_;
275         ///
276         int cached_y_;
277         /**
278          * The target x position of the cursor. This is used for when
279          * we have text like :
280          *
281          * blah blah blah blah| blah blah blah
282          * blah blah blah
283          * blah blah blah blah blah blah
284          *
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
287          * shorter than x()
288          */
289         int x_target_;
290         // do we have a selection?
291         bool selection_;
292         // are we on the way to get one?
293         bool mark_;
294
295         //
296         // math specific stuff that could be promoted to "global" later
297         //
298         /// do we allow autocorrection
299         bool autocorrect_;
300         /// are we entering a macro name?
301         bool macromode_;
302
303
304 ///////////////////////////////////////////////////////////////////
305 //
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.
309 //
310 ///////////////////////////////////////////////////////////////////
311
312 public:
313         ///
314         void insert(MathAtom const &);
315         ///
316         void insert(MathArray const &);
317         ///
318         void insert2(std::string const &);
319         /// return false for empty math insets
320         bool erase();
321         /// return false for empty math insets
322         bool backspace();
323         /// called for LFUN_HOME etc
324         bool home();
325         /// called for LFUN_END etc
326         bool end();
327         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
328         bool right();
329         /// called for LFUN_LEFT etc
330         bool left();
331         /// called for LFUN_UP etc
332         bool up();
333         /// called for LFUN_DOWN etc
334         bool down();
335         ///
336         void plainErase();
337         ///
338         void plainInsert(MathAtom const & at);
339         ///
340         void niceInsert(MathAtom const & at);
341         ///
342         void niceInsert(std::string const & str);
343
344         /// in pixels from top of screen
345         void setScreenPos(int x, int y);
346         /// in pixels from left of screen
347         int targetX() const;
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);
352         ///
353         MathHullInset * formula() const;
354         /// current offset in the current cell
355         ///
356         bool script(bool);
357         ///
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;
367
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"; }
373         ///
374         bool isInside(InsetBase const *);
375         ///
376         char valign();
377         ///
378         char halign();
379
380         /// make sure cursor position is valid
381         void normalize();
382         /// mark current cursor trace for redraw
383         void touch();
384
385         /// returns the normalized anchor of the selection
386         CursorSlice normalAnchor();
387
388         ///
389         void insert(char c);
390         ///
391         void insert(std::string const & str);
392         /// lock/unlock inset
393         void lockToggle();
394
395         /// hack for reveal codes
396         void markInsert();
397         void markErase();
398         /// injects content of a cell into parent
399         void pullArg();
400         /// split font inset etc
401         void handleFont(std::string const & font);
402
403         void releaseMathCursor();
404
405         bool inMathed() const;
406
407 private:
408         /// moves cursor index one cell to the left
409         bool idxLeft();
410         /// moves cursor index one cell to the right
411         bool idxRight();
412         /// moves cursor to end of last cell of current line
413         bool idxLineLast();
414         /// moves position somehow up or down
415         bool goUpDown(bool up);
416         /// moves position closest to (x, y) in given box
417         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
418         /// moves position closest to (x, y) in current cell
419         void bruteFind2(int x, int y);
420         /// are we in a nucleus of a script inset?
421         bool inNucleus();
422
423         /// the name of the macro we are currently inputting
424         std::string macroName();
425         /// where in the curent cell does the macro name start?
426         int macroNamePos();
427         /// can we enter the inset?
428         bool openable(MathAtom const &);
429 };
430
431 #endif // LYXCURSOR_H