]> git.lyx.org Git - lyx.git/blob - src/cursor.h
Hopefully temporary fix for the Tabular crash problem. Of course, this is not the...
[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 "dispatchresult.h"
16 #include "dociterator.h"
17
18 #include <iosfwd>
19 #include <vector>
20
21 class Buffer;
22 class BufferView;
23 class FuncStatus;
24 class FuncRequest;
25 class LyXFont;
26
27 // these should go
28 class InsetMathUnknown;
29 class Encoding;
30
31
32 /// The cursor class describes the position of a cursor within a document.
33
34 // The public inheritance should go in favour of a suitable data member
35 // (or maybe private inheritance) at some point of time.
36 class LCursor : public DocIterator {
37 public:
38         /// create the cursor of a BufferView
39         explicit LCursor(BufferView & bv);
40
41         /// dispatch from innermost inset upwards
42         void dispatch(FuncRequest const & cmd);
43         /// get the resut of the last dispatch
44         DispatchResult result() const;
45         /// add a new cursor slice
46         void push(InsetBase & inset);
47         /// add a new cursor slice, place cursor on left end
48         void pushLeft(InsetBase & inset);
49         /// pop one level off the cursor
50         void pop();
51         /// pop one slice off the cursor stack and go left
52         bool popLeft();
53         /// pop one slice off the cursor stack and go right
54         bool popRight();
55         /// make sure we are outside of given inset
56         void leaveInset(InsetBase const & inset);
57         /// sets cursor part
58         void setCursor(DocIterator const & it);
59
60         //
61         // selection
62         //
63         /// selection active?
64         bool selection() const { return selection_; }
65         /// selection active?
66         bool & selection() { return selection_; }
67         /// did we place the anchor?
68         bool mark() const { return mark_; }
69         /// did we place the anchor?
70         bool & mark() { return mark_; }
71         ///
72         void setSelection();
73         /// set selection at given position
74         void setSelection(DocIterator const & where, size_t n);
75         ///
76         void clearSelection();
77         /// access start of selection
78         CursorSlice selBegin() const;
79         /// access end of selection
80         CursorSlice selEnd() const;
81         /// access start of selection
82         DocIterator selectionBegin() const;
83         /// access start of selection
84         DocIterator selectionEnd() const;
85         ///
86         void selHandle(bool selecting);
87         //
88         lyx::docstring selectionAsString(bool label) const;
89         ///
90         std::string currentState();
91
92         /// auto-correct mode
93         bool autocorrect() const { return autocorrect_; }
94         /// auto-correct mode
95         bool & autocorrect() { return autocorrect_; }
96         /// are we entering a macro name?
97         bool macromode() const { return macromode_; }
98         /// are we entering a macro name?
99         bool & macromode() { return macromode_; }
100         /// returns x,y position
101         void getPos(int & x, int & y) const;
102
103         //
104         // common part
105         //
106         /// move one step to the left
107         bool posLeft();
108         /// move one step to the right
109         bool posRight();
110
111         /// insert an inset
112         void insert(InsetBase *);
113         /// insert a single char
114         void insert(lyx::char_type c);
115         /// insert a string
116         void insert(std::string const & str);
117
118         /// in pixels from left of screen
119         int targetX() const;
120         /// write acess to target x position of cursor
121         int & x_target();
122         /// return target x position of cursor
123         int x_target() const;
124         /// set targetX in current position
125         void setTargetX();
126         /// clear target x position of cursor
127         void clearTargetX();
128
129         /// access to normalized selection anchor
130         CursorSlice anchor() const;
131         /// sets anchor to cursor position
132         void resetAnchor();
133         /// access to owning BufferView
134         BufferView & bv() const;
135         /// access to owning Buffer
136         Buffer & buffer() const;
137         /// get some interesting description of top position
138         void info(std::ostream & os) const;
139         /// are we in math mode (2), text mode (1) or unsure (0)?
140         int currentMode();
141         /// reset cursor bottom to the beginning of the given inset
142         // (sort of 'chroot' environment...)
143         void reset(InsetBase &);
144         /// for spellchecking
145         void replaceWord(std::string const & replacestring);
146         /**
147          * the event was not (yet) dispatched.
148          *
149          * Should only be called by an inset's doDispatch() method. It means:
150          * I, the doDispatch() method of InsetFoo, hereby declare that I am
151          * not able to handle that request and trust my parent will do the
152          * Right Thing (even if my getStatus partner said that I can do it).
153          * It is sort of a kludge that should be used only rarely...
154          */
155         void undispatched();
156         /// the event was already dispatched
157         void dispatched();
158         /// call update() when done
159         void needsUpdate();
160         /**
161          * don't call update() when done
162          *
163          * Should only be called by an inset's doDispatch() method. It means:
164          * I handled that request and I can reassure you that the screen does
165          * not need to be re-drawn and all entries in the coord cache stay
166          * valid (and there are no other things to put in the coord cache).
167          * This is a fairly rare event as well and only some optimization.
168          * Not using noUpdate() should never be wrong.
169          */
170         void noUpdate();
171         /// fix cursor in circumstances that should never happen
172         void fixIfBroken();
173
174         /// output
175         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
176
177 public:
178         ///
179         BufferView * bv_;
180 //private:
181         /// the anchor position
182         DocIterator anchor_;
183
184         ///
185         DispatchResult disp_;
186
187
188 private:
189         /**
190          * The target x position of the cursor. This is used for when
191          * we have text like :
192          *
193          * blah blah blah blah| blah blah blah
194          * blah blah blah
195          * blah blah blah blah blah blah
196          *
197          * When we move onto row 3, we would like to be vertically aligned
198          * with where we were in row 1, despite the fact that row 2 is
199          * shorter than x()
200          */
201         int x_target_;
202         /// do we have a selection?
203         bool selection_;
204         /// are we on the way to get one?
205         bool mark_;
206         /// If true, we are behind the previous char, otherwise we are in front
207         // of the next char. This only make a difference when we are in front
208         // of a big inset spanning a whole row and computing coordinates for
209         // displaying the cursor.
210         bool logicalpos_;
211
212 private:
213
214         //
215         // math specific stuff that could be promoted to "global" later
216         //
217         /// do we allow autocorrection
218         bool autocorrect_;
219         /// are we entering a macro name?
220         bool macromode_;
221
222
223 ///////////////////////////////////////////////////////////////////
224 //
225 // The part below is the non-integrated rest of the original math
226 // cursor. This should be either generalized for texted or moved
227 // back to the math insets.
228 //
229 ///////////////////////////////////////////////////////////////////
230
231 public:
232         ///
233         void insert(MathAtom const &);
234         ///
235         void insert(MathArray const &);
236         /// return false for empty math insets
237         bool erase();
238         /// return false for empty math insets
239         bool backspace();
240         /// called for LFUN_UP etc
241         bool up();
242         /// called for LFUN_DOWN etc
243         bool down();
244         ///
245         void plainErase();
246         ///
247         void plainInsert(MathAtom const & at);
248         ///
249         void niceInsert(MathAtom const & at);
250         ///
251         void niceInsert(std::string const & str);
252
253         /// in pixels from top of screen
254         void setScreenPos(int x, int y);
255         /// current offset in the top cell
256
257         /// interpret name a name of a macro. Returns true if
258         /// something got inserted.
259         bool macroModeClose();
260         /// are we currently typing the name of a macro?
261         bool inMacroMode() const;
262         /// get access to the macro we are currently typing
263         InsetMathUnknown * activeMacro();
264
265         /// replace selected stuff with at, placing the former
266         // selection in given cell of atom
267         void handleNest(MathAtom const & at, int cell = 0);
268         ///
269         bool isInside(InsetBase const *);
270
271         /// make sure cursor position is valid
272         void normalize();
273         /// mark current cursor trace for redraw
274         void touch();
275
276         /// hack for reveal codes
277         void markInsert();
278         void markErase();
279         /// injects content of a cell into parent
280         void pullArg();
281         /// split font inset etc
282         void handleFont(std::string const & font);
283
284         /// display a message
285         void message(lyx::docstring const & msg) const;
286         /// display an error message
287         void errorMessage(lyx::docstring const & msg) const;
288         ///
289         std::string getPossibleLabel();
290
291         /// moves position somehow up or down
292         bool goUpDown(bool up);
293
294         /// the name of the macro we are currently inputting
295         std::string macroName();
296         /// where in the curent cell does the macro name start?
297         int macroNamePos();
298         /// can we enter the inset?
299         bool openable(MathAtom const &) const;
300         ///
301         Encoding const * getEncoding() const;
302         /// font at cursor position
303         LyXFont getFont() const;
304 };
305
306
307 #endif // LYXCURSOR_H