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