Skip to content

Commit

Permalink
Make screenshot function cropping above highlighted controls an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Jul 31, 2024
1 parent 3d6140b commit e0844b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/util/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()));
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/util/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e0844b2

Please sign in to comment.