public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { AnchorEdge initialAnchor = AppSettings.Instance.Workspace.LastCanvasSizeAnchorEdge.Value; Document document = ResizeDocument(documentWorkspace, initialAnchor, documentWorkspace.ToolSettings.SecondaryColor.Value, true, true); if (document != null) { using (new PushNullToolMode(documentWorkspace)) { List <HistoryMemento> actions = new List <HistoryMemento>(2); SelectionHistoryMemento item = new SelectionHistoryMemento(null, null, documentWorkspace); actions.Add(item); if (document.DpuUnit != MeasurementUnit.Pixel) { AppSettings.Instance.Workspace.LastNonPixelUnits.Value = document.DpuUnit; if (documentWorkspace.AppWorkspace.Units != MeasurementUnit.Pixel) { documentWorkspace.AppWorkspace.Units = document.DpuUnit; } } ReplaceDocumentHistoryMemento memento2 = new ReplaceDocumentHistoryMemento(null, null, documentWorkspace); actions.Add(memento2); documentWorkspace.Document = document; return(new CompoundHistoryMemento(StaticName, StaticImage, actions)); } } return(null); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { SelectionData state = null; SegmentedList <HistoryMemento> actions = new SegmentedList <HistoryMemento>(); if (!historyWorkspace.Selection.IsEmpty) { state = historyWorkspace.Selection.Save(); HistoryMemento memento3 = new DeselectFunction().Execute(historyWorkspace); actions.Add(memento3); } ReplaceDocumentHistoryMemento item = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(item); CompoundHistoryMemento memento2 = new CompoundHistoryMemento(StaticName, PdnResources.GetImageResource("Icons.MenuImageFlattenIcon.png"), actions); Document document = RetryManager.RunMemorySensitiveOperation <Document>(() => historyWorkspace.Document.Flatten()); base.EnterCriticalRegion(); historyWorkspace.Document = document; if (state != null) { SelectionHistoryMemento newHA = new SelectionHistoryMemento(null, null, historyWorkspace); historyWorkspace.Selection.Restore(state); memento2.AddMemento(newHA); } return(memento2); }
private HistoryMemento DoCanvasResize(DocumentWorkspace documentWorkspace, Size newLayerSize) { Document document; int activeLayerIndex = documentWorkspace.ActiveLayerIndex; Size newSize = new Size(Math.Max(newLayerSize.Width, documentWorkspace.Document.Width), Math.Max(newLayerSize.Height, documentWorkspace.Document.Height)); try { using (new WaitCursorChanger(documentWorkspace)) { CleanupManager.RequestCleanup(); document = CanvasSizeAction.ResizeDocument(documentWorkspace.Document, newSize, AnchorEdge.TopLeft, documentWorkspace.ToolSettings.SecondaryColor.Value); } } catch (OutOfMemoryException exception) { ExceptionDialog.ShowErrorDialog(documentWorkspace, PdnResources.GetString("ImportFromFileAction.AskForCanvasResize.OutOfMemory"), exception); document = null; } if (document == null) { return(null); } HistoryMemento memento = new ReplaceDocumentHistoryMemento(string.Empty, null, documentWorkspace); using (new WaitCursorChanger(documentWorkspace)) { documentWorkspace.Document = document; } documentWorkspace.ActiveLayer = (Layer)documentWorkspace.Document.Layers[activeLayerIndex]; return(memento); }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { AnchorEdge initialEdge = SettingNames.GetLastCanvasSizeAnchorEdge(); Document newDoc = ResizeDocument( documentWorkspace.FindForm(), documentWorkspace.Document, documentWorkspace.Document.Size, initialEdge, documentWorkspace.AppWorkspace.AppEnvironment.SecondaryColor, true, true); if (newDoc != null) { using (new PushNullToolMode(documentWorkspace)) { ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(StaticName, StaticImage, documentWorkspace); documentWorkspace.Document = newDoc; return(rdha); } } else { return(null); } }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { GeometryList cachedClippingMask = historyWorkspace.Selection.GetCachedClippingMask(); if (historyWorkspace.Selection.IsEmpty || (cachedClippingMask.Bounds.Area < 1.0)) { return(null); } Document document = historyWorkspace.Document; List <HistoryMemento> mementos = new List <HistoryMemento>(document.Layers.Count); RectInt32 b = cachedClippingMask.Bounds.GetInt32Bound(1E-05); RectInt32 sourceRect = RectInt32.Intersect(document.Bounds(), b); Document document2 = new Document(sourceRect.Width, sourceRect.Height); document2.ReplaceMetadataFrom(document); RectInt32 rect = new RectInt32(0, 0, sourceRect.Width, sourceRect.Height); IRenderer <ColorAlpha8> cachedClippingMaskRenderer = historyWorkspace.Selection.GetCachedClippingMaskRenderer(historyWorkspace.ToolSettings.Selection.RenderingQuality.Value); IRenderer <ColorAlpha8> newClipMaskRenderer = new ClippedRenderer <ColorAlpha8>(cachedClippingMaskRenderer, sourceRect); SelectionHistoryMemento item = new SelectionHistoryMemento(null, null, historyWorkspace); mementos.Add(item); base.EnterCriticalRegion(); int count = document.Layers.Count; while (document.Layers.Count > 0) { BitmapLayer layer = (BitmapLayer)document.Layers[0]; Surface croppedSurface = layer.Surface.CreateWindow(sourceRect); BitmapLayer newLayer = RetryManager.RunMemorySensitiveOperation <BitmapLayer>(() => new BitmapLayer(croppedSurface)); newLayer.LoadProperties(layer.SaveProperties()); HistoryMemento deleteLayerMemento = new DeleteLayerFunction(0).Execute(historyWorkspace); mementos.Add(deleteLayerMemento); Task task = Task.Factory.StartNew(delegate { deleteLayerMemento.Flush(); }, TaskCreationOptions.LongRunning); Parallel.ForEach <RectInt32>(rect.GetTiles(TilingStrategy.Tiles, 7), delegate(RectInt32 newTileRect) { ISurface <ColorBgra> surface = newLayer.Surface.CreateWindow(newTileRect); IRenderer <ColorAlpha8> mask = new ClippedRenderer <ColorAlpha8>(newClipMaskRenderer, newTileRect); surface.MultiplyAlphaChannel(mask); }); document2.Layers.Add(newLayer); task.Wait(); if (document2.Layers.Count > count) { ExceptionUtil.ThrowInternalErrorException("newDocument.Layers.Count > oldLayerCount"); } } ReplaceDocumentHistoryMemento memento2 = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); mementos.Add(memento2); historyWorkspace.Document = document2; return(HistoryMemento.Combine(HistoryMementoName, HistoryMementoImage, mementos)); }
private HistoryMemento DoCanvasResize(DocumentWorkspace documentWorkspace, Size newLayerSize) { HistoryMemento retHA; int layerIndex = documentWorkspace.ActiveLayerIndex; Size newSize = new Size(Math.Max(newLayerSize.Width, documentWorkspace.Document.Width), Math.Max(newLayerSize.Height, documentWorkspace.Document.Height)); Document newDoc; try { using (new WaitCursorChanger(documentWorkspace)) { Utility.GCFullCollect(); newDoc = CanvasSizeAction.ResizeDocument(documentWorkspace.Document, newSize, AnchorEdge.TopLeft, documentWorkspace.AppWorkspace.AppEnvironment.SecondaryColor); } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ImportFromFileAction.AskForCanvasResize.OutOfMemory")); newDoc = null; } if (newDoc == null) { retHA = null; } else { retHA = new ReplaceDocumentHistoryMemento(string.Empty, null, documentWorkspace); using (new WaitCursorChanger(documentWorkspace)) { documentWorkspace.Document = newDoc; } documentWorkspace.ActiveLayer = (Layer)documentWorkspace.Document.Layers[layerIndex]; } return(retHA); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { object savedSelection = null; List <HistoryMemento> actions = new List <HistoryMemento>(); if (!historyWorkspace.Selection.IsEmpty) { savedSelection = historyWorkspace.Selection.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, ImageResource.Get("Icons.MenuImageFlattenIcon.png"), actions); // TODO: we can save memory here by serializing, then flattening on to an existing layer Document flat = historyWorkspace.Document.Flatten(); EnterCriticalRegion(); historyWorkspace.Document = flat; if (savedSelection != null) { SelectionHistoryMemento shm = new SelectionHistoryMemento(null, null, historyWorkspace); historyWorkspace.Selection.Restore(savedSelection); chm.PushNewAction(shm); } return(chm); }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { AnchorEdge initialEdge = PdnSettings.GetLastCanvasSizeAnchorEdge(); Document newDoc = ResizeDocument( documentWorkspace.FindForm(), documentWorkspace.Document, documentWorkspace.Document.Size, initialEdge, documentWorkspace.AppWorkspace.AppEnvironment.SecondaryColor, true, true); if (newDoc != null) { using (new PushNullToolMode(documentWorkspace)) { if (newDoc.DpuUnit != MeasurementUnit.Pixel) { Settings.CurrentUser.SetString(PdnSettings.LastNonPixelUnits, newDoc.DpuUnit.ToString()); if (documentWorkspace.AppWorkspace.Units != MeasurementUnit.Pixel) { documentWorkspace.AppWorkspace.Units = newDoc.DpuUnit; } } ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(StaticName, StaticImage, documentWorkspace); documentWorkspace.Document = newDoc; return(rdha); } } else { return(null); } }
private bool PerformActionImpl() { PointInt32 num2; RectInt32 num3; if (this.clipData == null) { try { using (new WaitCursorChanger(this.documentWorkspace)) { CleanupManager.RequestCleanup(); this.clipData = PdnClipboard.GetDataObject(); } } catch (OutOfMemoryException exception) { ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory"), exception); return(false); } catch (Exception exception2) { ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.TransferFromClipboard"), exception2); return(false); } } bool handled = false; if (this.documentWorkspace.Tool != null) { this.documentWorkspace.Tool.PerformPaste(this.clipData, out handled); } if (handled) { return(true); } if (this.maskedSurface == null) { try { using (new WaitCursorChanger(this.documentWorkspace)) { this.maskedSurface = ClipboardUtil.GetClipboardImage(this.documentWorkspace, this.clipData); } } catch (OutOfMemoryException exception3) { ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory"), exception3); return(false); } catch (Exception exception4) { ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.TransferFromClipboard"), exception4); return(false); } } if (this.maskedSurface == null) { MessageBoxUtil.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.NoImage")); return(false); } RectInt32 cachedGeometryMaskScansBounds = this.maskedSurface.GetCachedGeometryMaskScansBounds(); if ((cachedGeometryMaskScansBounds.Width > this.documentWorkspace.Document.Width) || (cachedGeometryMaskScansBounds.Height > this.documentWorkspace.Document.Height)) { Surface surface; try { using (new WaitCursorChanger(this.documentWorkspace)) { surface = CreateThumbnail(this.maskedSurface); } } catch (OutOfMemoryException) { surface = null; } DialogResult result = ShowExpandCanvasTaskDialog(this.documentWorkspace, surface); int activeLayerIndex = this.documentWorkspace.ActiveLayerIndex; ColorBgra background = this.documentWorkspace.ToolSettings.SecondaryColor.Value; if (result != DialogResult.Cancel) { if (result != DialogResult.Yes) { if (result != DialogResult.No) { throw ExceptionUtil.InvalidEnumArgumentException <DialogResult>(result, "dr"); } goto Label_031D; } using (new PushNullToolMode(this.documentWorkspace)) { int width = Math.Max(cachedGeometryMaskScansBounds.Width, this.documentWorkspace.Document.Width); Size newSize = new Size(width, Math.Max(cachedGeometryMaskScansBounds.Height, this.documentWorkspace.Document.Height)); Document document = CanvasSizeAction.ResizeDocument(this.documentWorkspace.Document, newSize, AnchorEdge.TopLeft, background); if (document == null) { return(false); } SelectionHistoryMemento memento = new SelectionHistoryMemento(null, null, this.documentWorkspace); ReplaceDocumentHistoryMemento memento2 = new ReplaceDocumentHistoryMemento(CanvasSizeAction.StaticName, CanvasSizeAction.StaticImage, this.documentWorkspace); this.documentWorkspace.Document = document; HistoryMemento[] actions = new HistoryMemento[] { memento, memento2 }; CompoundHistoryMemento memento3 = new CompoundHistoryMemento(CanvasSizeAction.StaticName, CanvasSizeAction.StaticImage, actions); this.documentWorkspace.History.PushNewMemento(memento3); this.documentWorkspace.ActiveLayer = (Layer)this.documentWorkspace.Document.Layers[activeLayerIndex]; goto Label_031D; } } return(false); } Label_031D: num3 = this.documentWorkspace.Document.Bounds(); RectDouble visibleDocumentRect = this.documentWorkspace.VisibleDocumentRect; RectInt32? nullable = visibleDocumentRect.Int32Inset(); RectDouble num5 = nullable.HasValue ? ((RectDouble)nullable.Value) : visibleDocumentRect; RectInt32 num6 = num5.Int32Bound; if (num5.Contains(cachedGeometryMaskScansBounds)) { num2 = new PointInt32(0, 0); } else { int num12; int num13; int num16; int num17; if (cachedGeometryMaskScansBounds.X < num5.Left) { num12 = -cachedGeometryMaskScansBounds.X + num6.X; } else if (cachedGeometryMaskScansBounds.Right > num6.Right) { num12 = (-cachedGeometryMaskScansBounds.X + num6.Right) - cachedGeometryMaskScansBounds.Width; } else { num12 = 0; } if (cachedGeometryMaskScansBounds.Y < num5.Top) { num13 = -cachedGeometryMaskScansBounds.Y + num6.Y; } else if (cachedGeometryMaskScansBounds.Bottom > num6.Bottom) { num13 = (-cachedGeometryMaskScansBounds.Y + num6.Bottom) - cachedGeometryMaskScansBounds.Height; } else { num13 = 0; } PointInt32 num14 = new PointInt32(num12, num13); RectInt32 num15 = new RectInt32(cachedGeometryMaskScansBounds.X + num14.X, cachedGeometryMaskScansBounds.Y + num14.Y, cachedGeometryMaskScansBounds.Width, cachedGeometryMaskScansBounds.Height); if (num15.X < 0) { num16 = num12 - num15.X; } else { num16 = num12; } if (num15.Y < 0) { num17 = num13 - num15.Y; } else { num17 = num13; } PointInt32 num18 = new PointInt32(num16, num17); RectInt32 rect = new RectInt32(cachedGeometryMaskScansBounds.X + num18.X, cachedGeometryMaskScansBounds.Y + num18.Y, cachedGeometryMaskScansBounds.Width, cachedGeometryMaskScansBounds.Height); if (num3.Contains(rect)) { num2 = num18; } else { PointInt32 num20 = num18; if (rect.Right > num3.Right) { int num21 = rect.Right - num3.Right; int num22 = Math.Min(num21, rect.Left); num20.X -= num22; } if (rect.Bottom > num3.Bottom) { int num23 = rect.Bottom - num3.Bottom; int num24 = Math.Min(num23, rect.Top); num20.Y -= num24; } num2 = num20; } } RectInt32 b = this.documentWorkspace.VisibleDocumentRect.Int32Bound; RectInt32 a = new RectInt32(cachedGeometryMaskScansBounds.X + num2.X, cachedGeometryMaskScansBounds.Y + num2.Y, cachedGeometryMaskScansBounds.Width, cachedGeometryMaskScansBounds.Height); bool hasZeroArea = RectInt32.Intersect(a, b).HasZeroArea; MoveTool.BeginPaste(this.documentWorkspace, PdnResources.GetString("CommonAction.Paste"), PdnResources.GetImageResource("Icons.MenuEditPasteIcon.png"), this.maskedSurface.SurfaceReadOnly, this.maskedSurface.GeometryMask, num2); if (hasZeroArea) { PointInt32 num25 = new PointInt32(b.Left + (b.Width / 2), b.Top + (b.Height / 2)); PointInt32 num26 = new PointInt32(a.Left + (a.Width / 2), a.Top + (a.Height / 2)); SizeInt32 num27 = new SizeInt32(num26.X - num25.X, num26.Y - num25.Y); PointDouble documentScrollPosition = this.documentWorkspace.DocumentScrollPosition; PointDouble num29 = new PointDouble(documentScrollPosition.X + num27.Width, documentScrollPosition.Y + num27.Height); this.documentWorkspace.DocumentScrollPosition = num29; } return(true); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { GeometryList cachedClippingMask = historyWorkspace.Selection.GetCachedClippingMask(); if (historyWorkspace.Selection.IsEmpty || (cachedClippingMask.Bounds.Area < 1.0)) { return(null); } Document oldDocument = historyWorkspace.Document; RectInt32 b = cachedClippingMask.Bounds.GetInt32Bound(1E-05); RectInt32 oldClipBounds = RectInt32.Intersect(oldDocument.Bounds(), b); Document document = new Document(oldClipBounds.Width, oldClipBounds.Height); document.ReplaceMetadataFrom(oldDocument); RectInt32 newClipBounds = new RectInt32(0, 0, oldClipBounds.Width, oldClipBounds.Height); SelectionRenderingQuality quality = historyWorkspace.ToolSettings.Selection.RenderingQuality.Value; Result <IRenderer <ColorAlpha8> > oldClipMaskRendererLazy = historyWorkspace.Selection.GetCachedLazyClippingMaskRenderer(quality); LazyResult <ClippedRenderer <ColorAlpha8> > newClipMaskRendererLazy = LazyResult.New <ClippedRenderer <ColorAlpha8> >(() => new ClippedRenderer <ColorAlpha8>(oldClipMaskRendererLazy.Value, oldClipBounds), LazyThreadSafetyMode.ExecutionAndPublication, new VistaCriticalSection(0)); SelectionData selectionData = historyWorkspace.Selection.Save(); System.Threading.Tasks.Task <SelectionHistoryMemento> task = System.Threading.Tasks.Task.Factory.StartNew <SelectionHistoryMemento>(() => new SelectionHistoryMemento(null, null, historyWorkspace, selectionData)); System.Threading.Tasks.Task <ReplaceDocumentHistoryMemento> task2 = System.Threading.Tasks.Task.Factory.StartNew <ReplaceDocumentHistoryMemento>(() => new ReplaceDocumentHistoryMemento(null, null, historyWorkspace, oldDocument), TaskCreationOptions.LongRunning); int count = oldDocument.Layers.Count; System.Threading.Tasks.Task <BitmapLayer>[] items = new System.Threading.Tasks.Task <BitmapLayer> [count]; for (int i = 0; i < count; i++) { int iP = i; items[iP] = System.Threading.Tasks.Task.Factory.StartNew <BitmapLayer>(delegate { if (iP == 0) { newClipMaskRendererLazy.EnsureEvaluated(); } BitmapLayer layer = (BitmapLayer)oldDocument.Layers[iP]; Surface croppedSurface = layer.Surface.CreateWindow(oldClipBounds); BitmapLayer newLayer = RetryManager.RunMemorySensitiveOperation <BitmapLayer>(() => new BitmapLayer(croppedSurface)); newLayer.LoadProperties(layer.SaveProperties()); Parallel.ForEach <RectInt32>(newClipBounds.GetTiles(TilingStrategy.Tiles, 7), delegate(RectInt32 newTileRect) { ISurface <ColorBgra> surface = newLayer.Surface.CreateWindow(newTileRect); IRenderer <ColorAlpha8> mask = new ClippedRenderer <ColorAlpha8>(newClipMaskRendererLazy.Value, newTileRect); surface.MultiplyAlphaChannel(mask); }); return(newLayer); }); } List <TupleStruct <System.Threading.Tasks.Task, double> > collection = new List <TupleStruct <System.Threading.Tasks.Task, double> >(); collection.AddTuple <System.Threading.Tasks.Task, double>(task, 0.1); collection.AddTuple <System.Threading.Tasks.Task, double>(task2, 1.0); for (int j = 0; j < items.Length; j++) { collection.AddTuple <System.Threading.Tasks.Task, double>(items[j], 0.1); } PaintDotNet.Threading.Tasks.Task <Unit> task3 = historyWorkspace.TaskManager.CreateFrameworkTasksWrapper(collection); historyWorkspace.WaitWithProgress(task3, HistoryMementoImage, HistoryMementoName, PdnResources.GetString("Effects.ApplyingDialog.Description")); document.Layers.AddRange <Layer>(items.Select <System.Threading.Tasks.Task <BitmapLayer>, BitmapLayer>(t => t.Result)); SelectionHistoryMemento result = task.Result; ReplaceDocumentHistoryMemento memento2 = task2.Result; base.EnterCriticalRegion(); historyWorkspace.Document = document; HistoryMemento[] mementos = new HistoryMemento[] { result, memento2 }; return(HistoryMemento.Combine(HistoryMementoName, HistoryMementoImage, mementos)); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { if (historyWorkspace.Selection.IsEmpty) { return(null); } else { PdnRegion selectionRegion = historyWorkspace.Selection.CreateRegion(); if (selectionRegion.GetArea() == 0) { selectionRegion.Dispose(); return(null); } SelectionHistoryMemento sha = new SelectionHistoryMemento(StaticName, null, historyWorkspace); ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(StaticName, null, historyWorkspace); Rectangle boundingBox; Rectangle[] inverseRegionRects = null; boundingBox = Utility.GetRegionBounds(selectionRegion); using (PdnRegion inverseRegion = new PdnRegion(boundingBox)) { inverseRegion.Exclude(selectionRegion); inverseRegionRects = Utility.TranslateRectangles( inverseRegion.GetRegionScansReadOnlyInt(), -boundingBox.X, -boundingBox.Y); } selectionRegion.Dispose(); selectionRegion = null; Document oldDocument = historyWorkspace.Document; // TODO: serialize this to disk so we don't *have* to store the full thing Document newDocument = new Document(boundingBox.Width, boundingBox.Height); // copy the document's meta data over newDocument.ReplaceMetaDataFrom(oldDocument); foreach (Layer layer in oldDocument.Layers) { if (layer is BitmapLayer) { BitmapLayer oldLayer = (BitmapLayer)layer; Surface croppedSurface = oldLayer.Surface.CreateWindow(boundingBox); BitmapLayer newLayer = new BitmapLayer(croppedSurface); ColorBgra clearWhite = ColorBgra.White.NewAlpha(0); foreach (Rectangle rect in inverseRegionRects) { newLayer.Surface.Clear(rect, clearWhite); } newLayer.LoadProperties(oldLayer.SaveProperties()); newDocument.Layers.Add(newLayer); } else { throw new InvalidOperationException("Crop does not support Layers that are not BitmapLayers"); } } CompoundHistoryMemento cha = new CompoundHistoryMemento( StaticName, PdnResources.GetImageResource("Icons.MenuImageCropIcon.png"), new HistoryMemento[] { sha, rdha }); EnterCriticalRegion(); historyWorkspace.Document = newDocument; return(cha); } }
/// <summary> /// Pastes from the clipboard into the document. /// </summary> /// <returns>true if the paste operation completed, false if there was an error or if it was cancelled for some reason</returns> public bool PerformAction() { SurfaceForClipboard surfaceForClipboard = null; IDataObject clipData = null; try { Utility.GCFullCollect(); clipData = Clipboard.GetDataObject(); } catch (ExternalException) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.TransferFromClipboard")); return(false); } catch (OutOfMemoryException) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory")); return(false); } // First "ask" the current tool if it wants to handle it bool handledByTool = false; if (this.documentWorkspace.Tool != null) { this.documentWorkspace.Tool.PerformPaste(clipData, out handledByTool); } if (handledByTool) { return(true); } if (clipData.GetDataPresent(typeof(SurfaceForClipboard))) { try { Utility.GCFullCollect(); surfaceForClipboard = clipData.GetData(typeof(SurfaceForClipboard)) as SurfaceForClipboard; } catch (OutOfMemoryException) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory")); return(false); } } if (surfaceForClipboard != null && surfaceForClipboard.MaskedSurface.IsDisposed) { // Have been getting crash reports where sfc contains a disposed MaskedSurface ... surfaceForClipboard = null; } if (surfaceForClipboard == null && (clipData.GetDataPresent(DataFormats.Bitmap, true) || clipData.GetDataPresent(DataFormats.EnhancedMetafile, true))) { Image image; try { Utility.GCFullCollect(); image = clipData.GetData(DataFormats.Bitmap, true) as Image; if (image == null) { image = SystemLayer.Clipboard.GetEmfFromClipboard(this.documentWorkspace); } } catch (OutOfMemoryException) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory")); return(false); } // Sometimes we get weird errors if we're in, say, 16-bit mode but the image was copied // to the clipboard in 32-bit mode if (image == null) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.NotRecognized")); return(false); } MaskedSurface maskedSurface = null; try { Utility.GCFullCollect(); Bitmap bitmap; Surface surface = null; if (image is Bitmap) { bitmap = (Bitmap)image; image = null; } else { bitmap = new Bitmap(image); image.Dispose(); image = null; } surface = Surface.CopyFromBitmap(bitmap); bitmap.Dispose(); bitmap = null; maskedSurface = new MaskedSurface(surface, new PdnRegion(surface.Bounds)); surface.Dispose(); surface = null; } catch (Exception) { Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory")); return(false); } surfaceForClipboard = new SurfaceForClipboard(maskedSurface); } if (surfaceForClipboard == null || surfaceForClipboard.MaskedSurface == null) { // silently fail: like what if a program overwrote the clipboard in between the time // we enabled the "Paste" menu item and the user actually clicked paste? // it could happen! Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("PasteAction.Error.NoImage")); return(false); } // If the image is larger than the document, ask them if they'd like to make the image larger first Rectangle bounds = surfaceForClipboard.Bounds; if (bounds.Width > this.documentWorkspace.Document.Width || bounds.Height > this.documentWorkspace.Document.Height) { Surface thumb; try { using (new WaitCursorChanger(this.documentWorkspace)) { thumb = CreateThumbnail(surfaceForClipboard); } } catch (OutOfMemoryException) { thumb = null; } DialogResult dr = ShowExpandCanvasTaskDialog(this.documentWorkspace, thumb); int layerIndex = this.documentWorkspace.ActiveLayerIndex; switch (dr) { case DialogResult.Yes: Size newSize = new Size(Math.Max(bounds.Width, this.documentWorkspace.Document.Width), Math.Max(bounds.Height, this.documentWorkspace.Document.Height)); Document newDoc = CanvasSizeAction.ResizeDocument( this.documentWorkspace.Document, newSize, AnchorEdge.TopLeft, this.documentWorkspace.AppWorkspace.AppEnvironment.SecondaryColor); if (newDoc == null) { return(false); // user clicked cancel! } else { HistoryMemento rdha = new ReplaceDocumentHistoryMemento( CanvasSizeAction.StaticName, CanvasSizeAction.StaticImage, this.documentWorkspace); this.documentWorkspace.Document = newDoc; this.documentWorkspace.History.PushNewMemento(rdha); this.documentWorkspace.ActiveLayer = (Layer)this.documentWorkspace.Document.Layers[layerIndex]; } break; case DialogResult.No: break; case DialogResult.Cancel: return(false); default: throw new InvalidEnumArgumentException("Internal error: DialogResult was neither Yes, No, nor Cancel"); } } // Decide where to paste to: If the paste is within bounds of the document, do as normal // Otherwise, center it. Rectangle docBounds = this.documentWorkspace.Document.Bounds; Rectangle intersect1 = Rectangle.Intersect(docBounds, bounds); bool doMove = intersect1 != bounds; //intersect1.IsEmpty; Point pasteOffset; if (doMove) { pasteOffset = new Point(-bounds.X + (docBounds.Width / 2) - (bounds.Width / 2), -bounds.Y + (docBounds.Height / 2) - (bounds.Height / 2)); } else { pasteOffset = new Point(0, 0); } // Paste to the place it was originally copied from (for PDN-to-PDN transfers) // and then if its not pasted within the viewable rectangle we pan to that location RectangleF visibleDocRectF = this.documentWorkspace.VisibleDocumentRectangleF; Rectangle visibleDocRect = Utility.RoundRectangle(visibleDocRectF); Rectangle bounds2 = new Rectangle(new Point(bounds.X + pasteOffset.X, bounds.Y + pasteOffset.Y), bounds.Size); Rectangle intersect2 = Rectangle.Intersect(bounds2, visibleDocRect); bool doPan = intersect2.IsEmpty; this.documentWorkspace.SetTool(null); this.documentWorkspace.SetToolFromType(typeof(MoveTool)); ((MoveTool)this.documentWorkspace.Tool).PasteMouseDown(surfaceForClipboard, pasteOffset); if (doPan) { Point centerPtView = new Point(visibleDocRect.Left + (visibleDocRect.Width / 2), visibleDocRect.Top + (visibleDocRect.Height / 2)); Point centerPtPasted = new Point(bounds2.Left + (bounds2.Width / 2), bounds2.Top + (bounds2.Height / 2)); Size delta = new Size(centerPtPasted.X - centerPtView.X, centerPtPasted.Y - centerPtView.Y); PointF docScrollPos = this.documentWorkspace.DocumentScrollPositionF; PointF newDocScrollPos = new PointF(docScrollPos.X + delta.Width, docScrollPos.Y + delta.Height); this.documentWorkspace.DocumentScrollPositionF = newDocScrollPos; } return(true); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { int height; int width; string str; string str2; switch (this.rotation) { case RotateType.Clockwise90: case RotateType.CounterClockwise90: height = historyWorkspace.Document.Height; width = historyWorkspace.Document.Width; break; case RotateType.Rotate180: height = historyWorkspace.Document.Width; width = historyWorkspace.Document.Height; break; default: throw ExceptionUtil.InvalidEnumArgumentException <RotateType>(this.rotation, "this.rotation"); } switch (this.rotation) { case RotateType.Clockwise90: str = "Icons.MenuImageRotate90CWIcon.png"; str2 = PdnResources.GetString("RotateAction.90CW"); break; case RotateType.CounterClockwise90: str = "Icons.MenuImageRotate90CCWIcon.png"; str2 = PdnResources.GetString("RotateAction.90CCW"); break; case RotateType.Rotate180: str = "Icons.MenuImageRotate180Icon.png"; str2 = PdnResources.GetString("RotateAction.180"); break; default: throw ExceptionUtil.InvalidEnumArgumentException <RotateType>(this.rotation, "this.rotation"); } string name = string.Format(PdnResources.GetString("RotateAction.HistoryMementoName.Format"), StaticName, str2); ImageResource imageResource = PdnResources.GetImageResource(str); List <HistoryMemento> actions = new List <HistoryMemento>(); Document document = new Document(height, width); if (!historyWorkspace.Selection.IsEmpty) { DeselectFunction function = new DeselectFunction(); base.EnterCriticalRegion(); HistoryMemento memento3 = function.Execute(historyWorkspace); actions.Add(memento3); } ReplaceDocumentHistoryMemento item = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(item); document.ReplaceMetadataFrom(historyWorkspace.Document); for (int i = 0; i < historyWorkspace.Document.Layers.Count; i++) { Layer at = historyWorkspace.Document.Layers.GetAt(i); if (!(at is BitmapLayer)) { throw new InvalidOperationException("Cannot Rotate non-BitmapLayers"); } Layer layer2 = this.RotateLayer((BitmapLayer)at, this.rotation, height, width); document.Layers.Add(layer2); } CompoundHistoryMemento memento2 = new CompoundHistoryMemento(name, imageResource, actions); base.EnterCriticalRegion(); historyWorkspace.Document = document; return(memento2); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { int newWidth; int newHeight; // Get new width and Height switch (rotation) { case RotateType.Clockwise90: case RotateType.CounterClockwise90: newWidth = historyWorkspace.Document.Height; newHeight = historyWorkspace.Document.Width; break; case RotateType.Rotate180: newWidth = historyWorkspace.Document.Width; newHeight = historyWorkspace.Document.Height; break; default: throw new InvalidEnumArgumentException("invalid RotateType"); } // Figure out which icon and text to use string iconResName; string suffix; switch (rotation) { case RotateType.Rotate180: iconResName = "Icons.MenuImageRotate180Icon.png"; suffix = PdnResources.GetString("RotateAction.180"); break; case RotateType.Clockwise90: iconResName = "Icons.MenuImageRotate90CWIcon.png"; suffix = PdnResources.GetString("RotateAction.90CW"); break; case RotateType.CounterClockwise90: iconResName = "Icons.MenuImageRotate90CCWIcon.png"; suffix = PdnResources.GetString("RotateAction.90CCW"); break; default: throw new InvalidEnumArgumentException("invalid RotateType"); } // Initialize the new Doc string haNameFormat = PdnResources.GetString("RotateAction.HistoryMementoName.Format"); string haName = string.Format(haNameFormat, StaticName, suffix); ImageResource haImage = PdnResources.GetImageResource(iconResName); List <HistoryMemento> actions = new List <HistoryMemento>(); // do the memory allocation now: if this fails, we can still bail out cleanly Document newDoc = new Document(newWidth, newHeight); if (!historyWorkspace.Selection.IsEmpty) { DeselectFunction da = new DeselectFunction(); EnterCriticalRegion(); HistoryMemento hm = da.Execute(historyWorkspace); actions.Add(hm); } ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(rdha); newDoc.ReplaceMetaDataFrom(historyWorkspace.Document); // TODO: serialize oldDoc to disk, and let the GC purge it if needed OnProgress(0.0); for (int i = 0; i < historyWorkspace.Document.Layers.Count; ++i) { Layer layer = historyWorkspace.Document.Layers.GetAt(i); double progressStart = 100.0 * ((double)i / (double)historyWorkspace.Document.Layers.Count); double progressEnd = 100.0 * ((double)(i + 1) / (double)historyWorkspace.Document.Layers.Count); if (layer is BitmapLayer) { Layer nl = RotateLayer((BitmapLayer)layer, rotation, newWidth, newHeight, progressStart, progressEnd); newDoc.Layers.Add(nl); } else { throw new InvalidOperationException("Cannot Rotate non-BitmapLayers"); } if (this.PleaseCancel) { break; } OnProgress(progressEnd); } CompoundHistoryMemento chm = new CompoundHistoryMemento( haName, haImage, actions); if (this.PleaseCancel) { chm = null; } else { EnterCriticalRegion(); historyWorkspace.Document = newDoc; } return(chm); }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { int newWidth; int newHeight; double newDpu; MeasurementUnit newDpuUnit; string resamplingAlgorithm = Settings.CurrentUser.GetString(SettingNames.LastResamplingMethod, ResamplingAlgorithm.SuperSampling.ToString()); ResamplingAlgorithm alg; try { alg = (ResamplingAlgorithm)Enum.Parse(typeof(ResamplingAlgorithm), resamplingAlgorithm, true); } catch { alg = ResamplingAlgorithm.SuperSampling; } bool maintainAspect = Settings.CurrentUser.GetBoolean(SettingNames.LastMaintainAspectRatio, true); using (ResizeDialog rd = new ResizeDialog()) { rd.OriginalSize = documentWorkspace.Document.Size; rd.OriginalDpuUnit = documentWorkspace.Document.DpuUnit; rd.OriginalDpu = documentWorkspace.Document.DpuX; rd.ImageHeight = documentWorkspace.Document.Height; rd.ImageWidth = documentWorkspace.Document.Width; rd.ResamplingAlgorithm = alg; rd.LayerCount = documentWorkspace.Document.Layers.Count; rd.Units = rd.OriginalDpuUnit; rd.Resolution = documentWorkspace.Document.DpuX; rd.Units = SettingNames.GetLastNonPixelUnits(); rd.ConstrainToAspect = maintainAspect; DialogResult result = rd.ShowDialog(documentWorkspace); if (result == DialogResult.Cancel) { return(null); } Settings.CurrentUser.SetString(SettingNames.LastResamplingMethod, rd.ResamplingAlgorithm.ToString()); Settings.CurrentUser.SetBoolean(SettingNames.LastMaintainAspectRatio, rd.ConstrainToAspect); newDpuUnit = rd.Units; newWidth = rd.ImageWidth; newHeight = rd.ImageHeight; newDpu = rd.Resolution; alg = rd.ResamplingAlgorithm; if (newDpuUnit != MeasurementUnit.Pixel) { Settings.CurrentUser.SetString(SettingNames.LastNonPixelUnits, newDpuUnit.ToString()); if (documentWorkspace.AppWorkspace.Units != MeasurementUnit.Pixel) { documentWorkspace.AppWorkspace.Units = newDpuUnit; } } // if the new size equals the old size, there's really no point in doing anything if (documentWorkspace.Document.Size == new Size(rd.ImageWidth, rd.ImageHeight) && documentWorkspace.Document.DpuX == newDpu && documentWorkspace.Document.DpuUnit == newDpuUnit) { return(null); } } HistoryMemento ha; if (newWidth == documentWorkspace.Document.Width && newHeight == documentWorkspace.Document.Height) { // Only adjusting Dpu or DpuUnit ha = new MetaDataHistoryMemento(StaticName, StaticImage, documentWorkspace); documentWorkspace.Document.DpuUnit = newDpuUnit; documentWorkspace.Document.DpuX = newDpu; documentWorkspace.Document.DpuY = newDpu; } else { try { using (new WaitCursorChanger(documentWorkspace)) { ha = new ReplaceDocumentHistoryMemento(StaticName, StaticImage, documentWorkspace); } Document newDocument = new Document(newWidth, newHeight); newDocument.ReplaceMetaDataFrom(documentWorkspace.Document); newDocument.DpuUnit = newDpuUnit; newDocument.DpuX = newDpu; newDocument.DpuY = newDpu; ResizeProgressDialog rpd = new ResizeProgressDialog(documentWorkspace, newDocument, documentWorkspace.Document, new Size(newWidth, newHeight), alg); Utility.GCFullCollect(); bool result = rpd.DoResize(); if (!result) { return(null); } documentWorkspace.Document = newDocument; } catch (WorkerThreadException ex) { if (ex.InnerException is OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ResizeAction.PerformAction.OutOfMemory")); return(null); } else { throw; } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ResizeAction.PerformAction.OutOfMemory")); return(null); } } return(ha); }