]> git.lyx.org Git - lyx.git/blob - src/Undo.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Undo.h
1 // -*- C++ -*-
2 /**
3  * \file Undo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Jürgen Vigna
12  * \author Abdelrazak Younes
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #ifndef UNDO_H
18 #define UNDO_H
19
20 #include "support/types.h"
21
22 namespace lyx {
23
24 class Buffer;
25 class CursorData;
26 class Inset;
27
28 /// This is used to combine consecutive undo recordings of the same kind.
29 enum UndoKind {
30         /**
31         * Insert something - these will combine to one big chunk
32         * when many inserts come after each other.
33         */
34         INSERT_UNDO,
35         /**
36         * Delete something - these will combine to one big chunk
37         * when many deletes come after each other.
38         */
39         DELETE_UNDO,
40         /// Atomic - each of these will have its own entry in the stack
41         ATOMIC_UNDO
42 };
43
44
45 class Undo
46 {
47         /// noncopyable
48         Undo(Undo const &);
49         void operator=(Undo const &);
50 public:
51
52         Undo(Buffer &);
53
54         ~Undo();
55
56         /// Clear out all undo/redo contents.
57         void clear();
58
59         /// this will undo the last action - returns false if no undo possible
60         bool undoAction(CursorData &);
61
62         /// this will redo the last undo - returns false if no redo possible
63         bool redoAction(CursorData &);
64
65         /// End a sequence of INSERT_UNDO or DELETE_UNDO type of undo
66         /// operations (grouping of consecutive characters insertion/deletion).
67         void finishUndo();
68
69         ///
70         bool hasUndoStack() const;
71         ///
72         bool hasRedoStack() const;
73
74         /// Mark all the elements of the undo and redo stacks as dirty
75         void markDirty();
76
77         /// open a new group of undo operations.
78         /**
79          *  Groups can be nested. Such a nested group e.g. { {} {} } is undone in
80          *  a single step. This means you can add a group whenever you are not sure.
81          */
82         void beginUndoGroup();
83         /// open a new group as above and specify a cursor to set as cur_before
84         /// of the group's undo elements.
85         /**
86          * This cursor takes precedence over what is passed to recordUndo.
87          * In the case of nested groups, only the first cur_before is
88          * taken into account. The cursor is reset at the end of the
89          * top-level group.
90          */
91         void beginUndoGroup(CursorData const & cur_before);
92         /// end the current undo group.
93         void endUndoGroup();
94         /// end the current undo group and set UndoElement::cur_after if necessary.
95         void endUndoGroup(CursorData const & cur_after);
96         /// end abruptly the current group and create a new one wih the same nesting level
97         void splitUndoGroup(CursorData const & cur);
98         /// return true if an undo group is open and contains at least one element
99         bool activeUndoGroup() const;
100
101         /// The general case: record undo information for an arbitrary range.
102         /**
103          * Record undo information - call with the current cursor and
104          * the 'other end' of the range of changed paragraphs. So we
105          * give an inclusive range. This is called before you make the
106          * changes to the paragraph, and it will record the original
107          * information of the paragraphs in the undo stack.
108          * Kind of undo is always ATOMIC_UNDO.
109          */
110         void recordUndo(CursorData const & cur, pit_type from, pit_type to);
111
112         /// Convenience: record undo information for the single
113         /// paragraph or cell containing the cursor.
114         void recordUndo(CursorData const & cur, UndoKind kind = ATOMIC_UNDO);
115
116         /// prepare undo for the inset containing the cursor
117         void recordUndoInset(CursorData const & cur, Inset const * inset);
118
119         /// Convenience: record undo for buffer parameters
120         void recordUndoBufferParams(CursorData const & cur);
121
122         /// Convenience: prepare undo for the whole buffer
123         void recordUndoFullBuffer(CursorData const & cur);
124
125 private:
126         struct Private;
127         Private * const d;
128 };
129
130
131 /** Helper class to simplify the use of undo groups across several buffers.
132  *
133  *  The undo group is open when the object is instantiated or when
134  *  the buffer is reset; it is then ended as the object goes out of
135  *  scope (see below)
136  */
137 class UndoGroupHelper {
138 public:
139         // Begin a new undo group for buffer \c buf.
140         UndoGroupHelper(Buffer * buf);
141         // End all active undo groups.
142         ~UndoGroupHelper();
143
144         // Begin if needed an undo group for buffer \c buf.
145         void resetBuffer(Buffer * buf);
146
147 private:
148         class Impl;
149         Impl * const d;
150 };
151
152
153
154 } // namespace lyx
155
156 #endif // UNDO_H