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