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); }
private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { SegmentedList <HistoryMemento> items = new SegmentedList <HistoryMemento>(); bool flag = true; if (flag && !documentWorkspace.Selection.IsEmpty) { HistoryMemento item = new DeselectFunction().Execute(documentWorkspace); items.Add(item); } if (flag && ((layer.Width > documentWorkspace.Document.Width) || (layer.Height > documentWorkspace.Document.Height))) { HistoryMemento memento3 = this.DoCanvasResize(documentWorkspace, layer.Size); if (memento3 == null) { flag = false; } else { items.Add(memento3); } } if (flag && (layer.Size != documentWorkspace.Document.Size)) { BitmapLayer layer2; try { using (new WaitCursorChanger(documentWorkspace)) { CleanupManager.RequestCleanup(); layer2 = CanvasSizeAction.ResizeLayer(layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException exception) { ExceptionDialog.ShowErrorDialog(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory"), exception); flag = false; layer2 = null; } if (layer2 != null) { layer.Dispose(); layer = layer2; } } if (flag) { NewLayerHistoryMemento memento4 = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); items.Add(memento4); } if (flag) { return(new CompoundHistoryMemento(string.Empty, null, items.ToArrayEx <HistoryMemento>())); } this.Rollback(items); return(null); }
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); }
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); }
private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { HistoryMemento retHA; List <HistoryMemento> historyMementos = new List <HistoryMemento>(); bool success = true; if (success) { if (!documentWorkspace.Selection.IsEmpty) { HistoryMemento ha = new DeselectFunction().Execute(documentWorkspace); historyMementos.Add(ha); } } if (success) { if (layer.Width > documentWorkspace.Document.Width || layer.Height > documentWorkspace.Document.Height) { HistoryMemento ha = DoCanvasResize(documentWorkspace, layer.Size); if (ha == null) { success = false; } else { historyMementos.Add(ha); } } } if (success) { if (layer.Size != documentWorkspace.Document.Size) { BitmapLayer newLayer; try { using (new WaitCursorChanger(documentWorkspace)) { Utility.GCFullCollect(); newLayer = CanvasSizeAction.ResizeLayer((BitmapLayer)layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory")); success = false; newLayer = null; } if (newLayer != null) { layer.Dispose(); layer = newLayer; } } } if (success) { NewLayerHistoryMemento nlha = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); historyMementos.Add(nlha); } if (success) { HistoryMemento[] has = historyMementos.ToArray(); retHA = new CompoundHistoryMemento(string.Empty, null, has); } else { Rollback(historyMementos); retHA = null; } return(retHA); }
/// <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)) { Image image; try { Utility.GCFullCollect(); image = (Image)clipData.GetData(DataFormats.Bitmap); } 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(); } 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) { DialogResult dr = MessageBox.Show( this.documentWorkspace, PdnResources.GetString("PasteAction.Question.ExpandCanvas"), PdnInfo.GetAppName(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); 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); }