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)
5
using System.Collections;
7
namespace HexEditor.Util
10
/// Description of UndoManager.
12
public class UndoManager
17
public event EventHandler<UndoEventArgs> ActionUndone;
18
public event EventHandler<UndoEventArgs> ActionRedone;
22
UndoStack = new Stack();
23
RedoStack = new Stack();
27
/// Determines if there's any further step to undo.
30
get { return (UndoStack.Count > 0); }
34
/// Determines if there's any further step to redo.
37
get { return (RedoStack.Count > 0); }
41
/// Adds a step to the stack.
43
/// <param name="step">The step to add.</param>
44
internal void AddUndoStep(UndoStep step)
49
EventHandler<UndoEventArgs> temp = ActionUndone;
51
temp(this, new UndoEventArgs(step, false));
54
internal void AddOverwriteStep(int start, byte[] bytes, byte[] oldBytes)
56
this.AddUndoStep(new UndoStep(bytes, oldBytes, start, UndoAction.Overwrite));
59
internal void AddInsertStep(int start, byte[] bytes)
61
this.AddUndoStep(new UndoStep(bytes, null, start, UndoAction.Insert));
64
internal void AddRemoveStep(int start, byte[] bytes)
66
this.AddUndoStep(new UndoStep(bytes, null, start, UndoAction.Remove));
70
/// Undoes the last step.
72
/// <param name="buffer">Buffer to use</param>
73
/// <remarks>Used internally, don't use!</remarks>
74
internal UndoStep Undo(ref BufferManager buffer)
76
if (UndoStack.Count > 0) {
77
UndoStep step = (UndoStep)UndoStack.Peek();
80
switch (step.Action) {
81
case UndoAction.Insert :
82
buffer.SetBytes(step.Start, step.GetBytes(), false);
84
case UndoAction.Remove :
85
buffer.RemoveBytes(step.Start, step.GetBytes().Length);
87
case UndoAction.Overwrite :
88
buffer.SetBytes(step.Start, step.GetOldBytes(), true);
91
EventHandler<UndoEventArgs> temp = ActionUndone;
93
temp(this, new UndoEventArgs(step, true));
101
/// Redoes the last step.
103
/// <param name="buffer">Buffer to use</param>
104
/// <remarks>Used internally, don't use!</remarks>
105
internal UndoStep Redo(ref BufferManager buffer)
107
if (RedoStack.Count > 0) {
108
UndoStep step = (UndoStep)RedoStack.Peek();
109
UndoStack.Push(step);
111
switch (step.Action) {
112
case UndoAction.Insert :
113
buffer.RemoveBytes(step.Start, step.GetBytes().Length);
115
case UndoAction.Remove :
116
buffer.SetBytes(step.Start, step.GetBytes(), false);
118
case UndoAction.Overwrite :
119
buffer.SetBytes(step.Start, step.GetBytes(), true);
122
EventHandler<UndoEventArgs> temp = ActionRedone;
124
temp(this, new UndoEventArgs(step, false));