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