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