~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/UndoManager.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections;
 
6
 
 
7
namespace HexEditor.Util
 
8
{
 
9
        /// <summary>
 
10
        /// Description of UndoManager.
 
11
        /// </summary>
 
12
        public class UndoManager
 
13
        {
 
14
                Stack UndoStack;
 
15
                Stack RedoStack;
 
16
                
 
17
                public event EventHandler<UndoEventArgs> ActionUndone;
 
18
                public event EventHandler<UndoEventArgs> ActionRedone;
 
19
                
 
20
                public UndoManager()
 
21
                {
 
22
                        UndoStack = new Stack();
 
23
                        RedoStack = new Stack();
 
24
                }
 
25
                
 
26
                /// <summary>
 
27
                /// Determines if there's any further step to undo.
 
28
                /// </summary>
 
29
                public bool CanUndo {
 
30
                        get { return (UndoStack.Count > 0); }
 
31
                }
 
32
                
 
33
                /// <summary>
 
34
                /// Determines if there's any further step to redo.
 
35
                /// </summary>
 
36
                public bool CanRedo {
 
37
                        get { return (RedoStack.Count > 0); }
 
38
                }
 
39
                
 
40
                /// <summary>
 
41
                /// Adds a step to the stack.
 
42
                /// </summary>
 
43
                /// <param name="step">The step to add.</param>
 
44
                internal void AddUndoStep(UndoStep step)
 
45
                {
 
46
                        UndoStack.Push(step);
 
47
                        RedoStack.Clear();
 
48
                        
 
49
                        EventHandler<UndoEventArgs> temp = ActionUndone;
 
50
                        if (temp != null)
 
51
                                temp(this, new UndoEventArgs(step, false));
 
52
                }
 
53
                
 
54
                internal void AddOverwriteStep(int start, byte[] bytes, byte[] oldBytes)
 
55
                {
 
56
                        this.AddUndoStep(new UndoStep(bytes, oldBytes, start, UndoAction.Overwrite));
 
57
                }
 
58
                
 
59
                internal void AddInsertStep(int start, byte[] bytes)
 
60
                {
 
61
                        this.AddUndoStep(new UndoStep(bytes, null, start, UndoAction.Insert));
 
62
                }
 
63
                
 
64
                internal void AddRemoveStep(int start, byte[] bytes)
 
65
                {
 
66
                        this.AddUndoStep(new UndoStep(bytes, null, start, UndoAction.Remove));
 
67
                }
 
68
                
 
69
                /// <summary>
 
70
                /// Undoes the last step.
 
71
                /// </summary>
 
72
                /// <param name="buffer">Buffer to use</param>
 
73
                /// <remarks>Used internally, don't use!</remarks>
 
74
                internal UndoStep Undo(ref BufferManager buffer)
 
75
                {
 
76
                        if (UndoStack.Count > 0) {
 
77
                                UndoStep step = (UndoStep)UndoStack.Peek();
 
78
                                RedoStack.Push(step);
 
79
                                UndoStack.Pop();
 
80
                                switch (step.Action) {
 
81
                                        case UndoAction.Insert :
 
82
                                                buffer.SetBytes(step.Start, step.GetBytes(), false);
 
83
                                                break;
 
84
                                        case UndoAction.Remove :
 
85
                                                buffer.RemoveBytes(step.Start, step.GetBytes().Length);
 
86
                                                break;
 
87
                                        case UndoAction.Overwrite :
 
88
                                                buffer.SetBytes(step.Start, step.GetOldBytes(), true);
 
89
                                                break;
 
90
                                }
 
91
                                EventHandler<UndoEventArgs> temp = ActionUndone;
 
92
                                if (temp != null)
 
93
                                        temp(this, new UndoEventArgs(step, true));
 
94
                                
 
95
                                return step;
 
96
                        }
 
97
                        return null;
 
98
                }
 
99
                
 
100
                /// <summary>
 
101
                /// Redoes the last step.
 
102
                /// </summary>
 
103
                /// <param name="buffer">Buffer to use</param>
 
104
                /// <remarks>Used internally, don't use!</remarks>
 
105
                internal UndoStep Redo(ref BufferManager buffer)
 
106
                {
 
107
                        if (RedoStack.Count > 0) {
 
108
                                UndoStep step = (UndoStep)RedoStack.Peek();
 
109
                                UndoStack.Push(step);
 
110
                                RedoStack.Pop();
 
111
                                switch (step.Action) {
 
112
                                        case UndoAction.Insert :
 
113
                                                buffer.RemoveBytes(step.Start, step.GetBytes().Length);
 
114
                                                break;
 
115
                                        case UndoAction.Remove :
 
116
                                                buffer.SetBytes(step.Start, step.GetBytes(), false);
 
117
                                                break;
 
118
                                        case UndoAction.Overwrite :
 
119
                                                buffer.SetBytes(step.Start, step.GetBytes(), true);
 
120
                                                break;
 
121
                                }
 
122
                                EventHandler<UndoEventArgs> temp = ActionRedone;
 
123
                                if (temp != null)
 
124
                                        temp(this, new UndoEventArgs(step, false));
 
125
                                
 
126
                                return step;
 
127
                        }
 
128
                        return null;
 
129
                }
 
130
        }
 
131
}