]> git.lyx.org Git - lyx.git/blob - src/cursor.h
add romanian splash; add svglobal layout (+fixes to svjour) [bug 1430]; update CREDITS
[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 grid row of the current cell
194         col_type col() const;
195         /// the inset just behind the cursor
196         InsetBase * nextInset();
197         /// the inset just in front of the cursor
198         InsetBase * prevInset();
199         /// the inset just in front of the cursor
200         InsetBase const * prevInset() const;
201
202         //
203         // math-specific part
204         //
205         /// return the mathed cell this cursor is in
206         MathArray const & cell() const;
207         /// return the mathed cell this cursor is in
208         MathArray & cell();
209         /// the mathatom left of the cursor
210         MathAtom const & prevAtom() const;
211         /// the mathatom left of the cursor
212         MathAtom & prevAtom();
213         /// the mathatom right of the cursor
214         MathAtom const & nextAtom() const;
215         /// the mathatom right of the cursor
216         MathAtom & nextAtom();
217         /// auto-correct mode
218         bool autocorrect() const { return autocorrect_; }
219         /// auto-correct mode
220         bool & autocorrect() { return autocorrect_; }
221         /// are we entering a macro name?
222         bool macromode() const { return macromode_; }
223         /// are we entering a macro name?
224         bool & macromode() { return macromode_; }
225
226         //
227         // text-specific part
228         /// see comment for boundary_ below
229         bool boundary() const { return current().boundary(); }
230         /// see comment for boundary_ below
231         bool & boundary() { return current().boundary(); }
232         /// the paragraph we're in
233         Paragraph & paragraph();
234         /// the paragraph we're in
235         Paragraph const & paragraph() const;
236         /// the row in the paragraph we're in
237         Row & textRow();
238         /// the row in the paragraph we're in
239         Row const & textRow() const;
240         ///
241         LyXText * text() const;
242         ///
243         InsetBase * innerInsetOfType(int code) const;
244         ///
245         InsetTabular * innerInsetTabular() const;
246         ///
247         LyXText * innerText() const;
248         ///
249         CursorSlice const & innerTextSlice() const;
250         /// returns x,y position
251         void getPos(int & x, int & y) const;
252         /// returns cursor dimension
253         void getDim(int & asc, int & desc) const;
254
255         //
256         // common part
257         //
258         /// move one step to the left
259         bool posLeft();
260         /// move one step to the right
261         bool posRight();
262
263         /// insert an inset
264         void insert(InsetBase *);
265         /// insert a single char
266         void insert(char c);
267         /// insert a string
268         void insert(std::string const & str);
269
270         /// write acess to target x position of cursor
271         int & x_target();
272         /// return target x position of cursor
273         int x_target() const;
274         /// clear target x position of cursor
275         void clearTargetX();
276
277         /// access to selection anchor
278         CursorSlice & anchor();
279         /// access to selection anchor
280         CursorSlice const & anchor() const;
281         /// cache the absolute coordinate from the top inset
282         void updatePos();
283         /// sets anchor to cursor position
284         void resetAnchor(); 
285         /// access to owning BufferView
286         BufferView & bv() const; 
287         /// get some interesting description of current position
288         void info(std::ostream & os) const;
289         /// are we in math mode (2), text mode (1) or unsure (0)?
290         int currentMode();
291         /// reset cursor
292         void reset();
293         /// for spellchecking
294         void replaceWord(std::string const & replacestring);
295         /// update our view
296         void update();
297         ///
298         void dispatched(dispatch_result_t res);
299         void notdispatched();
300         void noupdate();
301
302         /// output
303         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
304 public:
305 //private:
306         /// mainly used as stack, but wee need random access
307         std::vector<CursorSlice> cursor_;
308         /// the anchor position
309         std::vector<CursorSlice> anchor_;
310         
311         /// 
312         DispatchResult disp_;
313
314 private:
315         ///
316         BufferView * bv_;
317         /// current slice
318         int current_;
319         ///
320         int cached_y_;
321         /**
322          * The target x position of the cursor. This is used for when
323          * we have text like :
324          *
325          * blah blah blah blah| blah blah blah
326          * blah blah blah
327          * blah blah blah blah blah blah
328          *
329          * When we move onto row 3, we would like to be vertically aligned
330          * with where we were in row 1, despite the fact that row 2 is
331          * shorter than x()
332          */
333         int x_target_;
334         // do we have a selection?
335         bool selection_;
336         // are we on the way to get one?
337         bool mark_;
338
339         //
340         // math specific stuff that could be promoted to "global" later
341         //
342         /// do we allow autocorrection
343         bool autocorrect_;
344         /// are we entering a macro name?
345         bool macromode_;
346
347
348 ///////////////////////////////////////////////////////////////////
349 //
350 // The part below is the non-integrated rest of the original math
351 // cursor. This should be either generalized for texted or moved
352 // back to the math insets.
353 //
354 ///////////////////////////////////////////////////////////////////
355
356 public:
357         ///
358         void insert(MathAtom const &);
359         ///
360         void insert(MathArray const &);
361         /// return false for empty math insets
362         bool erase();
363         /// return false for empty math insets
364         bool backspace();
365         /// called for LFUN_HOME etc
366         bool home();
367         /// called for LFUN_END etc
368         bool end();
369         /// called for LFUN_RIGHT and LFUN_RIGHTSEL
370         bool right();
371         /// called for LFUN_LEFT etc
372         bool left();
373         /// called for LFUN_UP etc
374         bool up();
375         /// called for LFUN_DOWN etc
376         bool down();
377         ///
378         void plainErase();
379         ///
380         void plainInsert(MathAtom const & at);
381         ///
382         void niceInsert(MathAtom const & at);
383         ///
384         void niceInsert(std::string const & str);
385
386         /// in pixels from top of screen
387         void setScreenPos(int x, int y);
388         /// in pixels from left of screen
389         int targetX() const;
390         /// return the next enclosing grid inset and the cursor's index in it
391         MathGridInset * enclosingGrid(idx_type & idx) const;
392         /// adjust anchor position after deletions/insertions
393         void adjust(pos_type from, int diff);
394         ///
395         MathHullInset * formula() const;
396         /// current offset in the current cell
397         ///
398         bool script(bool);
399         ///
400         bool interpret(char);
401         /// interpret name a name of a macro
402         void macroModeClose();
403         /// are we currently typing the name of a macro?
404         bool inMacroMode() const;
405         /// get access to the macro we are currently typing
406         MathUnknownInset * activeMacro();
407         /// are we currently typing '#1' or '#2' or...?
408         bool inMacroArgMode() const;
409
410         /// replace selected stuff with at, placing the former
411         // selection in given cell of atom
412         void handleNest(MathAtom const & at, int cell = 0);
413         /// remove this as soon as LyXFunc::getStatus is "localized"
414         //inline std::string getLastCode() { return "mathnormal"; }
415         ///
416         bool isInside(InsetBase const *);
417         ///
418         char valign();
419         ///
420         char halign();
421
422         /// make sure cursor position is valid
423         void normalize();
424         /// mark current cursor trace for redraw
425         void touch();
426
427         /// returns the normalized anchor of the selection
428         CursorSlice normalAnchor();
429
430         /// lock/unlock inset
431         void lockToggle();
432
433         /// hack for reveal codes
434         void markInsert();
435         void markErase();
436         /// injects content of a cell into parent
437         void pullArg();
438         /// split font inset etc
439         void handleFont(std::string const & font);
440
441         /// are we in mathed?
442         bool inMathed() const;
443         /// are we in texted?
444         bool inTexted() const;
445
446         /// display a message
447         void message(std::string const & msg) const;
448         /// display an error message
449         void errorMessage(std::string const & msg) const;
450         ///
451         std::string getPossibleLabel();
452
453 private:
454         /// moves cursor index one cell to the left
455         bool idxLeft();
456         /// moves cursor index one cell to the right
457         bool idxRight();
458         /// moves cursor to end of last cell of current line
459         bool idxLineLast();
460         /// moves position somehow up or down
461         bool goUpDown(bool up);
462         /// moves position closest to (x, y) in given box
463         bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
464         /// moves position closest to (x, y) in current cell
465         void bruteFind2(int x, int y);
466         /// are we in a nucleus of a script inset?
467         bool inNucleus();
468
469         /// the name of the macro we are currently inputting
470         std::string macroName();
471         /// where in the curent cell does the macro name start?
472         int macroNamePos();
473         /// can we enter the inset?
474         bool openable(MathAtom const &);
475 };
476
477 #endif // LYXCURSOR_H