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