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