using PaintDotNet.Measurement.HistoryMementos; using System.Collections.Generic; namespace PaintDotNet.Measurement.HistoryFunctions { public sealed class FlattenFunction : HistoryFunction { public static string StaticName { get { return PdnResources.GetString("FlattenFunction.Name"); } } public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace) { object savedSelection = null; List actions = new List(); if (!historyWorkspace.GetSelection().IsEmpty) { savedSelection = historyWorkspace.GetSelection().Save(); DeselectFunction da = new DeselectFunction(); HistoryMemento hm = da.Execute(historyWorkspace); actions.Add(hm); } ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(rdha); CompoundHistoryMemento chm = new CompoundHistoryMemento( StaticName, PdnResources.GetImageResource("Icons.MenuImageFlattenIcon.png"), actions); // TODO: we can save memory here by serializing, then flattening on to an existing layer Document flat = historyWorkspace.GetDocument().Flatten(); EnterCriticalRegion(); historyWorkspace.SetDocument(flat); if (savedSelection != null) { SelectionHistoryMemento shm = new SelectionHistoryMemento(null, null, historyWorkspace); historyWorkspace.GetSelection().Restore(savedSelection); chm.PushNewAction(shm); } return chm; } public FlattenFunction() : base(PaintDotNet.Measurement.Enum.ActionFlags.None) { } } }