using PaintDotNet.Measurement.HistoryMementos; using System; namespace PaintDotNet.Measurement.HistoryFunctions { public sealed class DuplicateLayerFunction : HistoryFunction { private int layerIndex; public static string StaticName { get { return PdnResources.GetString("DuplicateLayer.HistoryMementoName"); } } public static ImageResource StaticImage { get { return PdnResources.GetImageResource("Icons.MenuLayersDuplicateLayerIcon.png"); } } public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace) { if (this.layerIndex < 0 || this.layerIndex >= historyWorkspace.GetDocument().Layers.Count) { throw new ArgumentOutOfRangeException("layerIndex = " + layerIndex + ", expected [0, " + historyWorkspace.GetDocument().Layers.Count + ")"); } Layer newLayer = null; newLayer = (Layer)historyWorkspace.GetActiveLayer().Clone(); newLayer.IsBackground = false; int newIndex = 1 + this.layerIndex; HistoryMemento ha = new NewLayerHistoryMemento( StaticName, StaticImage, historyWorkspace, newIndex); EnterCriticalRegion(); historyWorkspace.GetDocument().Layers.Insert(newIndex, newLayer); newLayer.Invalidate(); return ha; } public DuplicateLayerFunction(int layerIndex) : base(PaintDotNet.Measurement.Enum.ActionFlags.None) { this.layerIndex = layerIndex; } } }