From 547e02d868ba7bc5bca1bcf97779ade419104f89 Mon Sep 17 00:00:00 2001 From: Nathan Brown Date: Tue, 11 Oct 2022 20:50:13 -0700 Subject: [PATCH] allow surface to be resized (cherry picked from commit b936c1004d542c5033c4a721f412588c316a1134) --- src/Greenshot.Base/Interfaces/ISurface.cs | 2 ++ .../Interfaces/SurfaceExpandedEventHandler.cs | 27 +++++++++++++++++++ src/Greenshot.Editor/Drawing/Surface.cs | 22 +++++++++++++++ src/Greenshot.Editor/Forms/ImageEditorForm.cs | 9 +++++++ 4 files changed, 60 insertions(+) create mode 100644 src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs diff --git a/src/Greenshot.Base/Interfaces/ISurface.cs b/src/Greenshot.Base/Interfaces/ISurface.cs index 4b39fb9b8..8ea5881c8 100644 --- a/src/Greenshot.Base/Interfaces/ISurface.cs +++ b/src/Greenshot.Base/Interfaces/ISurface.cs @@ -40,6 +40,7 @@ namespace Greenshot.Base.Interfaces event SurfaceMessageEventHandler SurfaceMessage; event SurfaceDrawingModeEventHandler DrawingModeChanged; event SurfaceElementEventHandler MovingElementChanged; + event SurfaceExpandedEventHandler SurfaceExpanded; event SurfaceForegroundColorEventHandler ForegroundColorChanged; event SurfaceBackgroundColorEventHandler BackgroundColorChanged; event SurfaceLineThicknessEventHandler LineThicknessChanged; @@ -203,6 +204,7 @@ namespace Greenshot.Base.Interfaces void RemoveElement(IDrawableContainer elementToRemove, bool makeUndoable = true, bool invalidate = true, bool generateEvents = true); void SendMessageEvent(object source, SurfaceMessageTyp messageType, string message); + void ResizeCanvas(int left, int right, int top, int bottom); void ApplyBitmapEffect(IEffect effect); void RemoveCursor(); bool HasCursor { get; } diff --git a/src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs b/src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs new file mode 100644 index 000000000..b34f2179d --- /dev/null +++ b/src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs @@ -0,0 +1,27 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: https://getgreenshot.org/ + * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using System; + +namespace Greenshot.Base.Interfaces +{ + public delegate void SurfaceExpandedEventHandler(object sender, EventArgs e); +} \ No newline at end of file diff --git a/src/Greenshot.Editor/Drawing/Surface.cs b/src/Greenshot.Editor/Drawing/Surface.cs index f050374a6..73fe91daa 100644 --- a/src/Greenshot.Editor/Drawing/Surface.cs +++ b/src/Greenshot.Editor/Drawing/Surface.cs @@ -105,6 +105,14 @@ namespace Greenshot.Editor.Drawing remove => _surfaceSizeChanged -= value; } + [NonSerialized] private SurfaceExpandedEventHandler _surfaceExpanded; + + public event SurfaceExpandedEventHandler SurfaceExpanded + { + add => _surfaceExpanded += value; + remove => _surfaceExpanded -= value; + } + [NonSerialized] private SurfaceMessageEventHandler _surfaceMessage; public event SurfaceMessageEventHandler SurfaceMessage @@ -1032,6 +1040,20 @@ namespace Greenshot.Editor.Drawing Invalidate(); } + /// + /// Set the canvas to a new size using the given bounds. + /// + /// + /// + /// + /// + public void ResizeCanvas(int left, int right, int top, int bottom) + { + var resizeEffect = new ResizeCanvasEffect(left, right, top, bottom); + ApplyBitmapEffect(resizeEffect); + _surfaceExpanded(this, null); + } + /// /// Apply a bitmap effect to the surface /// diff --git a/src/Greenshot.Editor/Forms/ImageEditorForm.cs b/src/Greenshot.Editor/Forms/ImageEditorForm.cs index 87c3813c7..db18f831a 100644 --- a/src/Greenshot.Editor/Forms/ImageEditorForm.cs +++ b/src/Greenshot.Editor/Forms/ImageEditorForm.cs @@ -246,6 +246,7 @@ namespace Greenshot.Editor.Forms _surface.MovingElementChanged += delegate { RefreshEditorControls(); }; _surface.DrawingModeChanged += Surface_DrawingModeChanged; _surface.SurfaceSizeChanged += SurfaceSizeChanged; + _surface.SurfaceExpanded += SurfaceExpanded; _surface.SurfaceMessage += SurfaceMessageReceived; _surface.ForegroundColorChanged += ForegroundColorChanged; _surface.BackgroundColorChanged += BackgroundColorChanged; @@ -566,6 +567,14 @@ namespace Greenshot.Editor.Forms AlignCanvasPositionAfterResize(); } + /// + /// Used when expanding the surface in one direction to accomodate shifting an object to one side. + /// + private void SurfaceExpanded(object sender, EventArgs e) + { + UpdateUndoRedoSurfaceDependencies(); + } + public ISurface Surface { get { return _surface; }