]> git.lyx.org Git - lyx.git/blob - src/Cursor.h
HTML for math fonts.
[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 LCURSOR_H
13 #define LCURSOR_H
14
15 #include "DispatchResult.h"
16 #include "DocIterator.h"
17 #include "Font.h"
18 #include "Undo.h"
19
20 #include "mathed/MathParser_flags.h"
21
22 #include <vector>
23
24
25 namespace lyx {
26
27 class Buffer;
28 class BufferView;
29 class FuncStatus;
30 class FuncRequest;
31 class Row;
32
33 // these should go
34 class InsetMathUnknown;
35 class Encoding;
36
37
38 /// The cursor class describes the position of a cursor within a document.
39
40 // The public inheritance should go in favour of a suitable data member
41 // (or maybe private inheritance) at some point of time.
42 class Cursor : public DocIterator
43 {
44 public:
45         /// create the cursor of a BufferView
46         explicit Cursor(BufferView & bv);
47
48         /// returns true if we made a decision
49         bool getStatus(FuncRequest const & cmd, FuncStatus & flag) const;
50         /// dispatch from innermost inset upwards
51         void dispatch(FuncRequest const & cmd);
52         /// get the resut of the last dispatch
53         DispatchResult result() const;
54         /// add a new cursor slice
55         void push(Inset & inset);
56         /// add a new cursor slice, place cursor at front (move backwards)
57         void pushBackward(Inset & inset);
58         /// pop one level off the cursor
59         void pop();
60         /// pop one slice off the cursor stack and go backwards
61         bool popBackward();
62         /// pop one slice off the cursor stack and go forward
63         bool popForward();
64         /// make sure we are outside of given inset
65         void leaveInset(Inset const & inset);
66         /// sets cursor part
67         void setCursor(DocIterator const & it);
68         /// sets the cursor to the normalized selection anchor
69         void setCursorToAnchor();
70
71         ///
72         void setCurrentFont();
73
74         //
75         // selection
76         //
77         /// selection active?
78         bool selection() const { return selection_; }
79         /// set selection;
80         void setSelection(bool sel) { selection_ = sel; }
81         /// do we have a multicell selection?
82         bool selIsMultiCell() const 
83                 { return selection_ && selBegin().idx() != selEnd().idx(); }
84         /// do we have a multiline selection?
85         bool selIsMultiLine() const 
86                 { return selection_ && selBegin().pit() != selEnd().pit(); }
87         /// 
88         void setWordSelection(bool set) { word_selection_ = set; }
89         ///
90         bool wordSelection() { return word_selection_; }
91         /// did we place the anchor?
92         bool mark() const { return mark_; }
93         /// did we place the anchor?
94         void setMark(bool mark) { mark_ = mark; }
95         ///
96         void setSelection();
97         /// set selection at given position
98         void setSelection(DocIterator const & where, int n);
99         ///
100         void clearSelection();
101         /// access start of selection
102         CursorSlice selBegin() const;
103         /// access end of selection
104         CursorSlice selEnd() const;
105         /// access start of selection
106         DocIterator selectionBegin() const;
107         /// access start of selection
108         DocIterator selectionEnd() const;
109         /**
110          * Update the selection status and save permanent
111          * selection if needed.
112          * @param selecting the new selection status
113          * @return whether the selection status has changed
114          */
115         bool selHandle(bool selecting);
116         ///
117         docstring selectionAsString(bool label) const;
118         ///
119         docstring currentState() const;
120
121         /// auto-correct mode
122         bool autocorrect() const { return autocorrect_; }
123         /// auto-correct mode
124         bool & autocorrect() { return autocorrect_; }
125         /// are we entering a macro name?
126         bool macromode() const { return macromode_; }
127         /// are we entering a macro name?
128         bool & macromode() { return macromode_; }
129         /// returns x,y position
130         void getPos(int & x, int & y) const;
131         /// return logical positions between which the cursor is situated
132         /**
133          * If the cursor is at the edge of a row, the position which is "over the 
134          * edge" will be returned as -1.
135          */
136         void getSurroundingPos(pos_type & left_pos, pos_type & right_pos);
137         /// the row in the paragraph we're in
138         Row const & textRow() const;
139
140         //
141         // common part
142         //
143         /// move one step backwards
144         bool posBackward();
145         /// move one step forward
146         bool posForward();
147         /// move visually one step to the right
148         /**
149          * @note: This method may move into an inset unless skip_inset == true.
150          * @note: This method may move into a new paragraph.
151          * @note: This method may move out of the current slice.
152          * @return: true if moved, false if not moved
153          */
154         bool posVisRight(bool skip_inset = false);
155         /// move visually one step to the left
156         /**
157          * @note: This method may move into an inset unless skip_inset == true.
158          * @note: This method may move into a new paragraph.
159          * @note: This method may move out of the current slice.
160          * @return: true if moved, false if not moved
161          */
162         bool posVisLeft(bool skip_inset = false);
163         /// move visually to next/previous row
164         /**
165          * Assuming we were to keep moving left (right) from the current cursor
166          * position, place the cursor at the rightmost (leftmost) edge of the 
167          * new row to which we would move according to visual-mode cursor movement.
168          * This could be either the next or the previous row, depending on the
169          * direction in which we're moving, and whether we're in an LTR or RTL 
170          * paragraph. 
171          * @note: The new position may even be in a new paragraph.
172          * @note: This method will not move out of the current slice.
173          * @return: false if not moved (no more rows to move to in given direction)
174          * @return: true if moved
175          */
176         bool posVisToNewRow(bool movingLeft);
177         /// move to right or left extremity of the current row
178         void posVisToRowExtremity(bool left);
179
180         /// insert an inset
181         void insert(Inset *);
182         /// insert a single char
183         void insert(char_type c);
184         /// insert a string
185         void insert(docstring const & str);
186
187         /// FIXME: rename to something sensible showing difference to x_target()
188         /// in pixels from left of screen, set to current position if unset
189         int targetX() const;
190         /// set the targetX to x
191         void setTargetX(int x);
192         /// return targetX or -1 if unset
193         int x_target() const;
194         /// set targetX to current position
195         void setTargetX();
196         /// clear targetX, i.e. set it to -1
197         void clearTargetX();
198         /// set offset to actual position - targetX
199         void updateTextTargetOffset();
200         /// distance between actual and targeted position during last up/down in text
201         int textTargetOffset() const;
202
203         /// access to normalized selection anchor
204         CursorSlice anchor() const;
205         /// sets anchor to cursor position
206         void resetAnchor();
207         /// access to owning BufferView
208         BufferView & bv() const;
209         /// get some interesting description of top position
210         void info(odocstream & os) const;
211         /// are we in math mode (2), text mode (1) or unsure (0)?
212         int currentMode();
213         /// reset cursor bottom to the beginning of the top inset
214         // (sort of 'chroot' environment...)
215         void reset();
216         /// for spellchecking
217         void replaceWord(std::string const & replacestring);
218         /**
219          * the event was not (yet) dispatched.
220          *
221          * Should only be called by an inset's doDispatch() method. It means:
222          * I, the doDispatch() method of InsetFoo, hereby declare that I am
223          * not able to handle that request and trust my parent will do the
224          * Right Thing (even if my getStatus partner said that I can do it).
225          * It is sort of a kludge that should be used only rarely...
226          */
227         void undispatched();
228         /// the event was already dispatched
229         void dispatched();
230         /// Set which update should be done
231         void updateFlags(Update::flags f);
232         /**
233          * don't call update() when done
234          *
235          * Should only be called by an inset's doDispatch() method. It means:
236          * I handled that request and I can reassure you that the screen does
237          * not need to be re-drawn and all entries in the coord cache stay
238          * valid (and there are no other things to put in the coord cache).
239          * This is a fairly rare event as well and only some optimization.
240          * Not using noUpdate() should never be wrong.
241          */
242         void noUpdate();
243         /// fix cursor in circumstances that should never happen.
244         /// \retval true if a fix occured.
245         bool fixIfBroken();
246
247         /// output
248         friend std::ostream & operator<<(std::ostream & os, Cursor const & cur);
249         friend LyXErr & operator<<(LyXErr & os, Cursor const & cur);
250
251         ///
252         bool textUndo();
253         ///
254         bool textRedo();
255
256         /// makes sure the next operation will be stored
257         void finishUndo() const;
258
259         /// open a new group of undo operations. Groups can be nested.
260         void beginUndoGroup() const;
261
262         /// end the current undo group
263         void endUndoGroup() const;
264
265         /// The general case: prepare undo for an arbitrary range.
266         void recordUndo(UndoKind kind, pit_type from, pit_type to) const;
267
268         /// Convenience: prepare undo for the range between 'from' and cursor.
269         void recordUndo(UndoKind kind, pit_type from) const;
270
271         /// Convenience: prepare undo for the single paragraph or cell
272         /// containing the cursor
273         void recordUndo(UndoKind kind = ATOMIC_UNDO) const;
274
275         /// Convenience: prepare undo for the inset containing the cursor
276         void recordUndoInset(UndoKind kind = ATOMIC_UNDO) const;
277
278         /// Convenience: prepare undo for the whole buffer
279         void recordUndoFullDocument() const;
280
281         /// Convenience: prepare undo for the selected paragraphs or cells
282         void recordUndoSelection() const;
283
284         ///
285         void checkBufferStructure();
286
287 public:
288         ///
289         BufferView * bv_;
290 //private:
291         /// the anchor position
292         DocIterator anchor_;
293         
294         ///
295         mutable DispatchResult disp_;
296         ///
297         DocIterator const & beforeDispatchCursor() const { return beforeDispatchCursor_; }
298         ///
299         void saveBeforeDispatchPosXY();
300
301 private:
302         /**
303          * The target x position of the cursor. This is used for when
304          * we have text like :
305          *
306          * blah blah blah blah| blah blah blah
307          * blah blah blah
308          * blah blah blah blah blah blah
309          *
310          * When we move onto row 3, we would like to be vertically aligned
311          * with where we were in row 1, despite the fact that row 2 is
312          * shorter than x()
313          */
314         int x_target_;
315         /// if a x_target cannot be hit exactly in a text, put the difference here
316         int textTargetOffset_;
317         /// do we have a selection?
318         bool selection_;
319         /// are we on the way to get one?
320         bool mark_;
321         /// are we in word-selection mode? This is set when double clicking.
322         bool word_selection_;
323         /// If true, we are behind the previous char, otherwise we are in front
324         // of the next char. This only make a difference when we are in front
325         // of a big inset spanning a whole row and computing coordinates for
326         // displaying the cursor.
327         bool logicalpos_;
328         /// position before dispatch started
329         DocIterator beforeDispatchCursor_;
330         /// cursor screen coordinates before dispatch started
331         int beforeDispatchPosX_;
332         int beforeDispatchPosY_;
333
334
335 // FIXME: make them private.
336 public:
337         /// the current font settings
338         Font current_font;
339         /// the current font
340         Font real_current_font;
341
342 private:
343
344         //
345         // math specific stuff that could be promoted to "global" later
346         //
347         /// do we allow autocorrection
348         bool autocorrect_;
349         /// are we entering a macro name?
350         bool macromode_;
351
352
353 ///////////////////////////////////////////////////////////////////
354 //
355 // The part below is the non-integrated rest of the original math
356 // cursor. This should be either generalized for texted or moved
357 // back to the math insets.
358 //
359 ///////////////////////////////////////////////////////////////////
360
361 public:
362         ///
363         void insert(MathAtom const &);
364         ///
365         void insert(MathData const &);
366         /// return false for empty math insets
367         bool erase();
368         /// return false for empty math insets
369         bool backspace();
370         /// move the cursor up by sending an internal LFUN_UP
371         /// return true if fullscreen update is needed
372         bool up();
373         /// move the cursor up by sending an internal LFUN_DOWN,
374         /// return true if fullscreen update is needed
375         bool down();
376         /// whether the cursor is either at the first or last row
377         bool atFirstOrLastRow(bool up);
378         /// move up/down in a text inset, called for LFUN_UP/DOWN,
379         /// return true if successful, updateNeeded set to true if fullscreen
380         /// update is needed, otherwise it's not touched
381         bool upDownInText(bool up, bool & updateNeeded);
382         /// move up/down in math or any non text inset, call for LFUN_UP/DOWN
383         /// return true if successful
384         bool upDownInMath(bool up);
385         ///
386         void plainErase();
387         ///
388         void plainInsert(MathAtom const & at);
389         ///
390         void niceInsert(MathAtom const & at);
391         ///
392         void niceInsert(docstring const & str, Parse::flags f = Parse::NORMAL,
393                         bool enter = true);
394
395         /// in pixels from top of screen
396         void setScreenPos(int x, int y);
397         /// current offset in the top cell
398
399         /// interpret name a name of a macro. Returns true if
400         /// something got inserted.
401         bool macroModeClose();
402         /// are we currently typing the name of a macro?
403         bool inMacroMode() const;
404         /// get access to the macro we are currently typing
405         InsetMathUnknown * activeMacro();
406         /// get access to the macro we are currently typing
407         InsetMathUnknown const * activeMacro() const;
408
409         /// replace selected stuff with at, placing the former
410         // selection in given cell of atom
411         void handleNest(MathAtom const & at, int cell = 0);
412         ///
413         bool isInside(Inset const *) const;
414
415         /// make sure cursor position is valid
416         /// FIXME: It does a subset of fixIfBroken. Maybe merge them?
417         void normalize();
418         /// mark current cursor trace for redraw
419         void touch();
420
421         /// hack for reveal codes
422         void markInsert();
423         void markErase();
424         /// injects content of a cell into parent
425         void pullArg();
426         /// split font inset etc
427         void handleFont(std::string const & font);
428
429         /// display a message
430         void message(docstring const & msg) const;
431         /// display an error message
432         void errorMessage(docstring const & msg) const;
433         ///
434         docstring getPossibleLabel() const;
435
436         /// the name of the macro we are currently inputting
437         docstring macroName();
438         /// where in the curent cell does the macro name start?
439         int macroNamePos();
440         /// can we enter the inset?
441         bool openable(MathAtom const &) const;
442         ///
443         Encoding const * getEncoding() const;
444         /// font at cursor position
445         Font getFont() const;
446 };
447
448
449 /**
450  * Notifies all insets which appear in old, but not in cur. And then
451  * notify all insets which appear in cur, but not in old.
452  * Make sure that the cursor old is valid, i.e. all inset pointers
453  * point to valid insets! Use Cursor::fixIfBroken if necessary.
454  */
455 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur);
456
457
458 } // namespace lyx
459
460 #endif // LYXLCURSOR_H