]> git.lyx.org Git - lyx.git/blob - src/cursor.h
5071dcec846db01a2c3e66829ac11137626c5caa
[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 class Row;
29
30
31 // these should go
32 class MathHullInset;
33 class MathUnknownInset;
34 class MathGridInset;
35
36
37 // only needed for gcc 2.95, remove when support terminated
38 template <typename A, typename B>
39 bool ptr_cmp(A const * a, B const * b)
40 {
41         return a == b;
42 }
43
44
45 // this is used for traversing math insets
46 typedef std::vector<CursorSlice> CursorBase;
47 /// move on one step
48 void increment(CursorBase &);
49 ///
50 CursorBase ibegin(InsetBase * p);
51 ///
52 CursorBase iend(InsetBase * p);
53
54
55 /**
56  * The cursor class describes the position of a cursor within a document.
57  */
58
59 class LCursor {
60 public:
61         /// type for cell number in inset
62         typedef CursorSlice::idx_type idx_type;
63         /// type for paragraph numbers positions within a cell
64         typedef CursorSlice::par_type par_type;
65         /// type for cursor positions within a cell
66         typedef CursorSlice::pos_type pos_type;
67         /// type for row indices
68         typedef CursorSlice::row_type row_type;
69         /// type for col indices
70         typedef CursorSlice::col_type col_type;
71
72         /// create the cursor of a BufferView
73         explicit LCursor(BufferView & bv);
74         /// dispatch from innermost inset upwards
75         DispatchResult dispatch(FuncRequest const & cmd);
76         /// add a new cursor slice
77         void push(InsetBase * inset);
78         /// add a new cursor slice, place cursor on left end
79         void pushLeft(InsetBase * inset);
80         /// pop one level off the cursor
81         void pop();
82         /// pop one slice off the cursor stack and go left
83         bool popLeft();
84         /// pop one slice off the cursor stack and go right
85         bool popRight();
86         /// restrict cursor nesting to given size
87         void pop(int depth);
88         /// access to current cursor slice
89         CursorSlice & current();
90         /// access to current cursor slice
91         CursorSlice const & current() const;
92         /// how many nested insets do we have?
93         size_t depth() const { return cursor_.size(); }
94         /// depth of current slice
95         int currentDepth() const { return current_; }
96
97         //
98         // selection
99         //
100         /// selection active?
101         bool selection() const { return selection_; }
102         /// selection active?
103         bool & selection() { return selection_; }
104         /// did we place the anchor?
105         bool mark() const { return mark_; }
106         /// did we place the anchor?
107         bool & mark() { return mark_; }
108         ///
109         void setSelection();
110         /// set selection at given position
111         void setSelection(CursorBase const & where, size_t n);
112         ///
113         void clearSelection();
114         /// access start of selection
115         CursorSlice & selBegin();
116         /// access start of selection
117         CursorSlice const & selBegin() const;
118         /// access end of selection
119         CursorSlice & selEnd();
120         /// access end of selection
121         CursorSlice const & selEnd() const;
122         ///
123         std::string grabSelection();
124         ///
125         void eraseSelection();
126         ///
127         std::string grabAndEraseSelection();
128         // other selection methods
129         ///
130         void selCopy();
131         ///
132         void selCut();
133         ///
134         void selDel();
135         /// pastes n-th element of cut buffer
136         void selPaste(size_t n);
137         ///
138         void selHandle(bool selecting);
139         /// start selection
140         void selStart();
141         /// clear selection
142         void selClear();
143         /// clears or deletes selection depending on lyxrc setting
144         void selClearOrDel();
145         //
146         std::string selectionAsString(bool label) const;
147         ///
148         void paste(std::string const & data);
149         ///
150         std::string currentState();
151
152         //
153         // access to the 'current' cursor slice
154         //
155         /// the containing inset
156         InsetBase * inset() const { return current().inset(); }
157         /// return the cell of the inset this cursor is in
158         idx_type idx() const { return current().idx(); }
159         /// return the cell of the inset this cursor is in
160         idx_type & idx() { return current().idx(); }
161         /// return the last possible cell in this inset
162         idx_type lastidx() const { return current().lastidx(); }
163         /// return the paragraph this cursor is in
164         par_type par() const { return current().par(); }
165         /// return the paragraph this cursor is in
166         par_type & par() { return current().par(); }
167         /// return the last possible paragraph in this inset
168         par_type lastpar() const;
169         /// return the position within the paragraph
170         pos_type pos() const { return current().pos(); }
171         /// return the position within the paragraph
172         pos_type & pos() { return current().pos(); }
173         /// return the last position within the paragraph
174         pos_type lastpos() const;
175         /// return the display row of the cursor with in the current par
176         row_type crow() const;
177         /// return the display row of the cursor with in the current par
178         row_type lastcrow() const;
179
180         /// return the number of embedded cells
181         size_t nargs() const;
182         /// return the number of embedded cells
183         size_t ncols() const;
184         /// return the number of embedded cells
185         size_t nrows() const;
186         /// return the grid row of the current cell
187         row_type row() const;
188         /// return the grid row of the current cell
189         col_type col() const;
190         /// the inset just behind the cursor
191         InsetBase * nextInset();
192         /// the inset just in front of the cursor
193         InsetBase * prevInset();
194         /// the inset just in front of the cursor
195         InsetBase const * prevInset() const;
196
197         //
198         // math-specific part
199         //
200         /// return the mathed cell this cursor is in
201         MathArray const & cell() const;
202         /// return the mathed cell this cursor is in
203         MathArray & cell();
204         /// the mathatom left of the cursor
205         MathAtom const & prevAtom() const;
206         /// the mathatom left of the cursor
207         MathAtom & prevAtom();
208         /// the mathatom right of the cursor
209         MathAtom const & nextAtom() const;
210         /// the mathatom right of the cursor
211         MathAtom & nextAtom();
212         /// auto-correct mode
213         bool autocorrect() const { return autocorrect_; }
214         /// auto-correct mode
215         bool & autocorrect() { return autocorrect_; }
216         /// are we entering a macro name?
217         bool macromode() const { return macromode_; }
218         /// are we entering a macro name?
219         bool & macromode() { return macromode_; }
220
221         //
222         // text-specific part
223         /// see comment for boundary_ below
224         bool boundary() const { return current().boundary(); }
225         /// see comment for boundary_ below
226         bool & boundary() { return current().boundary(); }
227         /// the paragraph we're in
228         Paragraph & paragraph();
229         /// the paragraph we're in
230         Paragraph const & paragraph() const;
231         /// the row in the paragraph we're in
232         Row & textRow();
233         /// the row in the paragraph we're in
234         Row const & textRow() const;
235         ///
236         LyXText * text() const;
237         ///
238         InsetBase * innerInsetOfType(int code) const;
239         ///
240         InsetTabular * innerInsetTabular() const;
241         ///
242         LyXText * innerText() const;
243         ///
244         CursorSlice const & innerTextSlice() const;
245         /// returns x,y position
246         void getPos(int & x, int & y) const;
247         /// returns cursor dimension
248         void getDim(int & asc, int & desc) const;
249
250         //
251         // common part
252         //
253         /// move one step to the left
254         bool posLeft();
255         /// move one step to the right
256         bool posRight();
257
258         /// insert an inset
259         void insert(InsetBase *);
260         /// insert a single char
261         void insert(char c);
262         /// insert a string
263         void insert(std::string const & str);
264
265         /// write acess to target x position of cursor
266         int & x_target();
267         /// return target x position of cursor
268         int x_target() const;
269         /// clear target x position of cursor
270         void clearTargetX();
271
272         /// access to selection anchor
273         CursorSlice & anchor();
274         /// access to selection anchor
275         CursorSlice const & anchor() const;
276         /// cache the absolute coordinate from the top inset
277         void updatePos();
278         /// sets anchor to cursor position
279         void resetAnchor(); 
280         /// access to owning BufferView
281         BufferView & bv() const; 
282         /// get some interesting description of current position
283         void info(std::ostream & os) const;
284         /// are we in math mode (2), text mode (1) or unsure (0)?
285         int currentMode();
286         /// reset cursor
287         void reset();
288         /// for spellchecking
289         void replaceWord(std::string const & replacestring);
290         /// update our view
291         void update();
292
293         /// output
294         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
295 public:
296 //private:
297         /// mainly used as stack, but wee need random access
298         std::vector<CursorSlice> cursor_;
299         /// the anchor position
300         std::vector<CursorSlice> anchor_;
301
302 private:
303         ///
304         BufferView * bv_;
305         /// current slice
306         int current_;
307         ///
308         int cached_y_;
309         /**
310          * The target x position of the cursor. This is used for when
311          * we have text like :
312          *
313          * blah blah blah blah| blah blah blah
314          * blah blah blah
315          * blah blah blah blah blah blah
316          *
317          * When we move onto row 3, we would like to be vertically aligned
318          * with where we were in row 1, despite the fact that row 2 is
319          * shorter than x()
320          */
321         int x_target_;
322         // do we have a selection?
323         bool selection_;
324         // are we on the way to get one?
325         bool mark_;
326
327         //
328         // math specific stuff that could be promoted to "global" later
329         //
330         /// do we allow autocorrection
331         bool autocorrect_;
332         /// are we entering a macro name?
333         bool macromode_;
334
335
336 ///////////////////////////////////////////////////////////////////
337 //
338 // The part below is the non-integrated rest of the original math
339 // cursor. This should be either generalized for texted or moved
340 // back to the math insets.
341 //
342 ///////////////////////////////////////////////////////////////////
343
344 public:
345         ///
346         void insert(MathAtom const &);
347         ///
348         void insert(MathArray const &);
349         /// return false for empty math insets
350         bool erase();
351         /// return false for empty math insets
352         bool backspace();
353         /// called for LFUN_HOME etc
354         bool home();
355         /// called for LFUN_END etc
356         bool end();
357         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
358         bool right();
359         /// called for LFUN_LEFT etc
360         bool left();
361         /// called for LFUN_UP etc
362         bool up();
363         /// called for LFUN_DOWN etc
364         bool down();
365         ///
366         void plainErase();
367         ///
368         void plainInsert(MathAtom const & at);
369         ///
370         void niceInsert(MathAtom const & at);
371         ///
372         void niceInsert(std::string const & str);
373
374         /// in pixels from top of screen
375         void setScreenPos(int x, int y);
376         /// in pixels from left of screen
377         int targetX() const;
378         /// return the next enclosing grid inset and the cursor's index in it
379         MathGridInset * enclosingGrid(idx_type & idx) const;
380         /// adjust anchor position after deletions/insertions
381         void adjust(pos_type from, int diff);
382         ///
383         MathHullInset * formula() const;
384         /// current offset in the current cell
385         ///
386         bool script(bool);
387         ///
388         bool interpret(char);
389         /// interpret name a name of a macro
390         void macroModeClose();
391         /// are we currently typing the name of a macro?
392         bool inMacroMode() const;
393         /// get access to the macro we are currently typing
394         MathUnknownInset * activeMacro();
395         /// are we currently typing '#1' or '#2' or...?
396         bool inMacroArgMode() const;
397
398         /// replace selected stuff with at, placing the former
399         // selection in given cell of atom
400         void handleNest(MathAtom const & at, int cell = 0);
401         /// remove this as soon as LyXFunc::getStatus is "localized"
402         //inline std::string getLastCode() { return "mathnormal"; }
403         ///
404         bool isInside(InsetBase const *);
405         ///
406         char valign();
407         ///
408         char halign();
409
410         /// make sure cursor position is valid
411         void normalize();
412         /// mark current cursor trace for redraw
413         void touch();
414
415         /// returns the normalized anchor of the selection
416         CursorSlice normalAnchor();
417
418         /// lock/unlock inset
419         void lockToggle();
420
421         /// hack for reveal codes
422         void markInsert();
423         void markErase();
424         /// injects content of a cell into parent
425         void pullArg();
426         /// split font inset etc
427         void handleFont(std::string const & font);
428
429         void releaseMathCursor();
430         /// are we in mathed?
431         bool inMathed() const;
432         /// are we in texted?
433         bool inTexted() const;
434
435         /// display a message
436         void message(std::string const & msg) const;
437         /// display an error message
438         void errorMessage(std::string const & msg) const;
439
440 private:
441         /// moves cursor index one cell to the left
442         bool idxLeft();
443         /// moves cursor index one cell to the right
444         bool idxRight();
445         /// moves cursor to end of last cell of current line
446         bool idxLineLast();
447         /// moves position somehow up or down
448         bool goUpDown(bool up);
449         /// moves position closest to (x, y) in given box
450         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
451         /// moves position closest to (x, y) in current cell
452         void bruteFind2(int x, int y);
453         /// are we in a nucleus of a script inset?
454         bool inNucleus();
455
456         /// the name of the macro we are currently inputting
457         std::string macroName();
458         /// where in the curent cell does the macro name start?
459         int macroNamePos();
460         /// can we enter the inset?
461         bool openable(MathAtom const &);
462 };
463
464 #endif // LYXCURSOR_H