openpdn /src/HistoryStack.cs

Language C# Lines 380
MD5 Hash 95768f816ba36a6b5a0ebbefa91cc701 Estimated Cost $7,045 (why?)
Repository https://bitbucket.org/tuldok89/openpdn View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using PaintDotNet.HistoryMementos;
using System;
using System.Collections.Generic;

namespace PaintDotNet
{
    /// <summary>
    /// The HistoryStack class for the History "concept".  
    /// Serves as the undo and redo stacks.  
    /// </summary>
    [Serializable]
    internal class HistoryStack
    {
        private readonly DocumentWorkspace _documentWorkspace;
        private int _stepGroupDepth;
        private int _isExecutingMemento; // 0 -> false, >0 -> true

        public bool IsExecutingMemento
        {
            get
            {
                return _isExecutingMemento > 0;
            }
        }

        private void PushExecutingMemento()
        {
            ++_isExecutingMemento;
        }

        private void PopExecutingMemento()
        {
            --_isExecutingMemento;
        }

        public List<HistoryMemento> UndoStack { get; private set; }

        public List<HistoryMemento> RedoStack { get; private set; }

        public void BeginStepGroup()
        {
            ++_stepGroupDepth;
        }

        public void EndStepGroup()
        {
            --_stepGroupDepth;

            if (_stepGroupDepth == 0)
            {
                OnFinishedStepGroup();
            }
        }

        public event EventHandler FinishedStepGroup;
        protected void OnFinishedStepGroup()
        {
            if (FinishedStepGroup != null)
            {
                FinishedStepGroup(this, EventArgs.Empty);
            }
        }

        public event EventHandler SteppedBackward;
        protected void OnSteppedBackward()
        {
            if (SteppedBackward != null)
            {
                SteppedBackward(this, EventArgs.Empty);
            }
        }

        public event EventHandler SteppedForward;
        protected void OnSteppedForward()
        {
            if (SteppedForward != null)
            {
                SteppedForward(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Event handler for when a new history memento has been added.
        /// </summary>
        public event EventHandler NewHistoryMemento;
        protected void OnNewHistoryMemento()
        {
            if (NewHistoryMemento != null)
            {
                NewHistoryMemento(this, EventArgs.Empty);
            }
        }
                
        /// <summary>
        /// Event handler for when changes have been made to the history.
        /// </summary>
        public event EventHandler Changed;
        protected void OnChanged()
        {
            if (Changed != null)
            {
                Changed(this, EventArgs.Empty);
            }
        }

        public event EventHandler Changing;
        protected void OnChanging()
        {
            if (Changing != null)
            {
                Changing(this, EventArgs.Empty);
            }
        }

        public event EventHandler HistoryFlushed;
        protected void OnHistoryFlushed()
        {
            if (HistoryFlushed != null)
            {
                HistoryFlushed(this, EventArgs.Empty);
            }
        }

        public event ExecutingHistoryMementoEventHandler ExecutingHistoryMemento;
        protected void OnExecutingHistoryMemento(ExecutingHistoryMementoEventArgs e)
        {
            if (ExecutingHistoryMemento != null)
            {
                ExecutingHistoryMemento(this, e);
            }
        }

        public event ExecutedHistoryMementoEventHandler ExecutedHistoryMemento;
        protected void OnExecutedHistoryMemento(ExecutedHistoryMementoEventArgs e)
        {
            if (ExecutedHistoryMemento != null)
            {
                ExecutedHistoryMemento(this, e);
            }
        }

        public void PerformChanged()
        {
            OnChanged();
        }

        public HistoryStack(DocumentWorkspace documentWorkspace)
        {
            _documentWorkspace = documentWorkspace;
            UndoStack = new List<HistoryMemento>();
            RedoStack = new List<HistoryMemento>();
        }

        private HistoryStack(
            IEnumerable<HistoryMemento> undoStack,
            IEnumerable<HistoryMemento> redoStack)
        {
            UndoStack = new List<HistoryMemento>(undoStack);
            RedoStack = new List<HistoryMemento>(redoStack);
        }

        /// <summary>
        /// When the user does something new, it will clear out the redo stack.
        /// </summary>
        public void PushNewMemento(HistoryMemento value)
        {
            Utility.GCFullCollect();

            OnChanging();

            ClearRedoStack();
            UndoStack.Add(value);
            OnNewHistoryMemento();

            OnChanged();

            value.Flush();
            Utility.GCFullCollect();
        }

        /// <summary>
        /// Takes one item from the redo stack, "redoes" it, then places the redo
        /// memento object to the top of the undo stack.
        /// </summary>
        public void StepForward()
        {
            PushExecutingMemento();

            try
            {
                StepForwardImpl();
            }

            finally
            {
                PopExecutingMemento();
            }
        }

        private void StepForwardImpl()
        {
            HistoryMemento topMemento = RedoStack[0];
            var asToolHistoryMemento = topMemento as ToolHistoryMemento;

            if (asToolHistoryMemento != null && asToolHistoryMemento.ToolType != _documentWorkspace.GetToolType())
            {
                _documentWorkspace.SetToolFromType(asToolHistoryMemento.ToolType);
                StepForward();
            }
            else
            {
                OnChanging();

                var ehaea1 = new ExecutingHistoryMementoEventArgs(topMemento, true, false);

                if (asToolHistoryMemento == null && topMemento.SeriesGuid != Guid.Empty)
                {
                    ehaea1.SuspendTool = true;
                }

                OnExecutingHistoryMemento(ehaea1);

                if (ehaea1.SuspendTool)
                {
                    _documentWorkspace.PushNullTool();
                }
            
                HistoryMemento redoMemento = RedoStack[0];

                // Possibly useful invariant here:
                //     ehaea1.HistoryMemento.SeriesGuid == ehaea2.HistoryMemento.SeriesGuid == ehaea3.HistoryMemento.SeriesGuid
                var ehaea2 = new ExecutingHistoryMementoEventArgs(redoMemento, false, ehaea1.SuspendTool);
                OnExecutingHistoryMemento(ehaea2);

                HistoryMemento undoMemento = redoMemento.PerformUndo();
            
                RedoStack.RemoveAt(0);
                UndoStack.Add(undoMemento);

                var ehaea3 = new ExecutedHistoryMementoEventArgs(undoMemento);
                OnExecutedHistoryMemento(ehaea3);

                OnChanged();
                OnSteppedForward();

                undoMemento.Flush();

                if (ehaea1.SuspendTool)
                {
                    _documentWorkspace.PopNullTool();
                }       
            }

            if (_stepGroupDepth == 0)
            {
                OnFinishedStepGroup();
            }
        }

        /// <summary>
        /// Undoes the top of the undo stack, then places the redo memento object to the
        /// top of the redo stack.
        /// </summary>
        public void StepBackward()
        {
            PushExecutingMemento();

            try
            {
                StepBackwardImpl();
            }

            finally
            {
                PopExecutingMemento();
            }
        }

        private void StepBackwardImpl()
        {
            HistoryMemento topMemento = UndoStack[UndoStack.Count - 1];
            var asToolHistoryMemento = topMemento as ToolHistoryMemento;

            if (asToolHistoryMemento != null && asToolHistoryMemento.ToolType != _documentWorkspace.GetToolType())
            {
                _documentWorkspace.SetToolFromType(asToolHistoryMemento.ToolType);
                StepBackward();
            }
            else
            {
                OnChanging();

                var ehaea1 = new ExecutingHistoryMementoEventArgs(topMemento, true, false);

                if (asToolHistoryMemento == null && topMemento.SeriesGuid == Guid.Empty)
                {
                    ehaea1.SuspendTool = true;
                }

                OnExecutingHistoryMemento(ehaea1);

                if (ehaea1.SuspendTool)
                {
                    _documentWorkspace.PushNullTool();
                }

                HistoryMemento undoMemento = UndoStack[UndoStack.Count - 1];

                var ehaea2 = new ExecutingHistoryMementoEventArgs(undoMemento, false, ehaea1.SuspendTool);
                OnExecutingHistoryMemento(ehaea2);

                HistoryMemento redoMemento = UndoStack[UndoStack.Count - 1].PerformUndo();
                UndoStack.RemoveAt(UndoStack.Count - 1);
                RedoStack.Insert(0, redoMemento);

                // Possibly useful invariant here:
                //     ehaea1.HistoryMemento.SeriesGuid == ehaea2.HistoryMemento.SeriesGuid == ehaea3.HistoryMemento.SeriesGuid
                var ehaea3 = new ExecutedHistoryMementoEventArgs(redoMemento);
                OnExecutedHistoryMemento(ehaea3);

                OnChanged();
                OnSteppedBackward();

                redoMemento.Flush();

                if (ehaea1.SuspendTool)
                {
                    _documentWorkspace.PopNullTool();
                }
            }

            if (_stepGroupDepth == 0)
            {
                OnFinishedStepGroup();
            }
        }

        public void ClearAll()
        {
            OnChanging();

            foreach (HistoryMemento ha in UndoStack)
            {
                ha.Flush();
            }

            foreach (HistoryMemento ha in RedoStack)
            {
                ha.Flush();
            }

            UndoStack = new List<HistoryMemento>();
            RedoStack = new List<HistoryMemento>();
            OnChanged();
            OnHistoryFlushed();
        }

        public void ClearRedoStack()
        {
            foreach (HistoryMemento ha in RedoStack)
            {
                ha.Flush();
            }

            OnChanging();
            RedoStack = new List<HistoryMemento>();
            OnChanged();
        }
    }
}
Back to Top