1 | // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Diagnostics; |
---|
21 | using ICSharpCode.AvalonEdit.Utils; |
---|
22 | |
---|
23 | namespace ICSharpCode.AvalonEdit.Document |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// This class stacks the last x operations from the undostack and makes |
---|
27 | /// one undo/redo operation from it. |
---|
28 | /// </summary> |
---|
29 | sealed class UndoOperationGroup : IUndoableOperationWithContext |
---|
30 | { |
---|
31 | IUndoableOperation[] undolist; |
---|
32 | |
---|
33 | public UndoOperationGroup(Deque<IUndoableOperation> stack, int numops) |
---|
34 | { |
---|
35 | if (stack == null) { |
---|
36 | throw new ArgumentNullException("stack"); |
---|
37 | } |
---|
38 | |
---|
39 | Debug.Assert(numops > 0 , "UndoOperationGroup : numops should be > 0"); |
---|
40 | Debug.Assert(numops <= stack.Count); |
---|
41 | |
---|
42 | undolist = new IUndoableOperation[numops]; |
---|
43 | for (int i = 0; i < numops; ++i) { |
---|
44 | undolist[i] = stack.PopBack(); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | public void Undo() |
---|
49 | { |
---|
50 | for (int i = 0; i < undolist.Length; ++i) { |
---|
51 | undolist[i].Undo(); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | public void Undo(UndoStack stack) |
---|
56 | { |
---|
57 | for (int i = 0; i < undolist.Length; ++i) { |
---|
58 | stack.RunUndo(undolist[i]); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | public void Redo() |
---|
63 | { |
---|
64 | for (int i = undolist.Length - 1; i >= 0; --i) { |
---|
65 | undolist[i].Redo(); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public void Redo(UndoStack stack) |
---|
70 | { |
---|
71 | for (int i = undolist.Length - 1; i >= 0; --i) { |
---|
72 | stack.RunRedo(undolist[i]); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|