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