From e0844b2afb8b1dd6eb875256004dd957cb6420f9 Mon Sep 17 00:00:00 2001 From: Blake-Madden <66873089+Blake-Madden@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:36:44 -0400 Subject: [PATCH] Make screenshot function cropping above highlighted controls an option --- src/util/screenshot.cpp | 28 +++++++++++++++++++++------- src/util/screenshot.h | 3 ++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/util/screenshot.cpp b/src/util/screenshot.cpp index 16535be9..e164ffb1 100644 --- a/src/util/screenshot.cpp +++ b/src/util/screenshot.cpp @@ -655,6 +655,8 @@ bool Screenshot::SaveScreenshot(const wxString& filePath, wxCoord endPointY{ 0 }; + wxPoint startPoint{ 0, 0 }; + if (startIdToHighlight != wxID_ANY || endIdToHighlight != wxID_ANY) { const wxWindow* startWindow = (startIdToHighlight == wxID_ANY) ? @@ -666,7 +668,6 @@ bool Screenshot::SaveScreenshot(const wxString& filePath, of the children relative to its parent. When dealing with client areas, using the screen position of controls will be off because the main dialog's decorations aren't factored into that.*/ - wxPoint startPoint(0, 0); auto startWindowParent = startWindow; while (startWindowParent && startWindowParent != windowToCapture) { @@ -718,7 +719,7 @@ bool Screenshot::SaveScreenshot(const wxString& filePath, const wxWindow* cutoffWindow = windowToCapture->FindWindow(cutoffId); if (cutoffWindow) { - wxPoint cutoffPoint(0, 0); + wxPoint cutoffPoint{ 0, 0 }; auto cutoffWindowParent = cutoffWindow; while (cutoffWindowParent && cutoffWindowParent != windowToCapture) { @@ -727,11 +728,24 @@ bool Screenshot::SaveScreenshot(const wxString& filePath, } wxPoint cutOffEndPoint(cutoffPoint.x + cutoffWindow->GetSize().GetWidth(), cutoffPoint.y + cutoffWindow->GetSize().GetHeight()); - bitmap = bitmap.GetSubBitmap( - wxRect(0, 0, bitmap.GetWidth(), - // if there is something being highlighted, make sure we don't - // cut that off - std::max(cutOffEndPoint.y, endPointY) + wxSizerFlags::GetDefaultBorder())); + + // if cutoff is at the starting point (or above it), + // then crop above the first highlighted control + if (startPoint.y >= cutoffPoint.y) + { + const wxCoord yStart = cutoffPoint.y - wxSizerFlags::GetDefaultBorder(); + bitmap = bitmap.GetSubBitmap( + wxRect(0, yStart, bitmap.GetWidth(), bitmap.GetHeight() - yStart)); + } + // ...otherwise, crop beneath end point + else + { + bitmap = bitmap.GetSubBitmap(wxRect(0, 0, bitmap.GetWidth(), + // if there is something being highlighted, make + // sure we don't cut that off + std::max(cutOffEndPoint.y, endPointY) + + wxSizerFlags::GetDefaultBorder())); + } } } diff --git a/src/util/screenshot.h b/src/util/screenshot.h index 46deacad..c721e9d5 100644 --- a/src/util/screenshot.h +++ b/src/util/screenshot.h @@ -30,7 +30,8 @@ class Screenshot @param startIdToHighlight The (optional) start control to draw a red line around. @param endIdToHighlight The (optional) end control to draw a red line around. @param cutoffId An (optional) ID it cutoff vertically at. - (This will be the last control at the bottom of the screenshot.) + (This will be the last control at the bottom of the screenshot, or top one + if above @c startIdToHighlight.) @returns @c true if image is saved successfully.*/ static bool SaveScreenshot(const wxString& filePath, const wxWindowID startIdToHighlight = wxID_ANY,