]> git.lyx.org Git - lyx.git/blob - src/cursor.h
Enable LyX to start up under Cygwin.
[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 Point;
26
27 // these should go
28 class MathUnknownInset;
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         /**
46          * \returns true if this function made a definitive decision on
47          * whether the inset at this cursor position wants to handle the
48          * request \p cmd or not. The result of this decision is put into
49          * \p status.
50          */
51         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
52
53         /// add a new cursor slice
54         void push(InsetBase & inset);
55         /// add a new cursor slice, place cursor on left end
56         void pushLeft(InsetBase & inset);
57         /// pop one level off the cursor
58         void pop();
59         /// pop one slice off the cursor stack and go left
60         bool popLeft();
61         /// pop one slice off the cursor stack and go right
62         bool popRight();
63         /// sets cursor part
64         void setCursor(DocIterator const & it);
65
66         //
67         // selection
68         //
69         /// selection active?
70         bool selection() const { return selection_; }
71         /// selection active?
72         bool & selection() { return selection_; }
73         /// did we place the anchor?
74         bool mark() const { return mark_; }
75         /// did we place the anchor?
76         bool & mark() { return mark_; }
77         ///
78         void setSelection();
79         /// set selection at given position
80         void setSelection(DocIterator const & where, size_t n);
81         ///
82         void clearSelection();
83         /// access start of selection
84         CursorSlice selBegin() const;
85         /// access end of selection
86         CursorSlice selEnd() const;
87         /// access start of selection
88         DocIterator selectionBegin() const;
89         /// access start of selection
90         DocIterator selectionEnd() const;
91         ///
92         void selHandle(bool selecting);
93         //
94         std::string selectionAsString(bool label) const;
95         ///
96         void paste(std::string const & data);
97         ///
98         std::string currentState();
99
100         /// auto-correct mode
101         bool autocorrect() const { return autocorrect_; }
102         /// auto-correct mode
103         bool & autocorrect() { return autocorrect_; }
104         /// are we entering a macro name?
105         bool macromode() const { return macromode_; }
106         /// are we entering a macro name?
107         bool & macromode() { return macromode_; }
108         /// returns x,y position
109         void getPos(int & x, int & y) const;
110         /// returns cursor dimension
111         void getDim(int & asc, int & desc) const;
112
113         //
114         // common part
115         //
116         /// move one step to the left
117         bool posLeft();
118         /// move one step to the right
119         bool posRight();
120
121         /// insert an inset
122         void insert(InsetBase *);
123         /// insert a single char
124         void insert(char c);
125         /// insert a string
126         void insert(std::string const & str);
127
128         /// in pixels from left of screen
129         int targetX() const;
130         /// write acess to target x position of cursor
131         int & x_target();
132         /// return target x position of cursor
133         int x_target() const;
134         /// set targetX in current position
135         void setTargetX();
136         /// clear target x position of cursor
137         void clearTargetX();
138
139         /// access to normalized selection anchor
140         CursorSlice anchor() const;
141         /// sets anchor to cursor position
142         void resetAnchor();
143         /// access to owning BufferView
144         BufferView & bv() const;
145         /// access to owning Buffer
146         Buffer & buffer() const;
147         /// get some interesting description of top position
148         void info(std::ostream & os) const;
149         /// are we in math mode (2), text mode (1) or unsure (0)?
150         int currentMode();
151         /// reset cursor bottom to the beginning of the given inset
152         // (sort of 'chroot' environment...)
153         void reset(InsetBase &);
154         /// for spellchecking
155         void replaceWord(std::string const & replacestring);
156         /// the event was not (yet) dispatched
157         void undispatched();
158         /// the event was already dispatched
159         void dispatched();
160         /// call update() when done
161         void needsUpdate();
162         /// don't call update() when done
163         void noUpdate();
164
165         /// output
166         friend std::ostream & operator<<(std::ostream & os, LCursor const & cur);
167
168 public:
169         ///
170         BufferView * bv_;
171 //private:
172         /// the anchor position
173         DocIterator anchor_;
174
175         ///
176         DispatchResult disp_;
177
178
179 private:
180         /**
181          * The target x position of the cursor. This is used for when
182          * we have text like :
183          *
184          * blah blah blah blah| blah blah blah
185          * blah blah blah
186          * blah blah blah blah blah blah
187          *
188          * When we move onto row 3, we would like to be vertically aligned
189          * with where we were in row 1, despite the fact that row 2 is
190          * shorter than x()
191          */
192         int x_target_;
193         /// do we have a selection?
194         bool selection_;
195         /// are we on the way to get one?
196         bool mark_;
197
198 private:
199
200         //
201         // math specific stuff that could be promoted to "global" later
202         //
203         /// do we allow autocorrection
204         bool autocorrect_;
205         /// are we entering a macro name?
206         bool macromode_;
207
208
209 ///////////////////////////////////////////////////////////////////
210 //
211 // The part below is the non-integrated rest of the original math
212 // cursor. This should be either generalized for texted or moved
213 // back to the math insets.
214 //
215 ///////////////////////////////////////////////////////////////////
216
217 public:
218         ///
219         void insert(MathAtom const &);
220         ///
221         void insert(MathArray const &);
222         /// return false for empty math insets
223         bool erase();
224         /// return false for empty math insets
225         bool backspace();
226         /// called for LFUN_UP etc
227         bool up();
228         /// called for LFUN_DOWN etc
229         bool down();
230         ///
231         void plainErase();
232         ///
233         void plainInsert(MathAtom const & at);
234         ///
235         void niceInsert(MathAtom const & at);
236         ///
237         void niceInsert(std::string const & str);
238
239         /// in pixels from top of screen
240         void setScreenPos(int x, int y);
241         /// current offset in the top cell
242         /// interpret name a name of a macro
243         void macroModeClose();
244         /// are we currently typing the name of a macro?
245         bool inMacroMode() const;
246         /// get access to the macro we are currently typing
247         MathUnknownInset * activeMacro();
248
249         /// replace selected stuff with at, placing the former
250         // selection in given cell of atom
251         void handleNest(MathAtom const & at, int cell = 0);
252         ///
253         bool isInside(InsetBase const *);
254
255         /// make sure cursor position is valid
256         void normalize();
257         /// mark current cursor trace for redraw
258         void touch();
259
260         /// hack for reveal codes
261         void markInsert();
262         void markErase();
263         /// injects content of a cell into parent
264         void pullArg();
265         /// split font inset etc
266         void handleFont(std::string const & font);
267
268         /// display a message
269         void message(std::string const & msg) const;
270         /// display an error message
271         void errorMessage(std::string const & msg) const;
272         ///
273         std::string getPossibleLabel();
274
275         /// moves position somehow up or down
276         bool goUpDown(bool up);
277
278         /// the name of the macro we are currently inputting
279         std::string macroName();
280         /// where in the curent cell does the macro name start?
281         int macroNamePos();
282         /// can we enter the inset?
283         bool openable(MathAtom const &) const;
284         ///
285         Encoding const * getEncoding() const;
286 };
287
288
289
290 #endif // LYXCURSOR_H