From 19124b685e49f078fe6db665af00a0ada79be9a3 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sun, 4 Feb 2024 15:25:03 -0500 Subject: [PATCH] Make STD_BITMAP_BUTTON always draw a sane button size regardless of bitmap size We need higher resolution images for use with STD_BITMAP_BUTTONs as 4K 250% scaling Windows which is "normal". --- common/widgets/std_bitmap_button.cpp | 37 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/common/widgets/std_bitmap_button.cpp b/common/widgets/std_bitmap_button.cpp index d66a489b54..4ea205d451 100644 --- a/common/widgets/std_bitmap_button.cpp +++ b/common/widgets/std_bitmap_button.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -40,8 +41,21 @@ STD_BITMAP_BUTTON::STD_BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, { if( aSize == wxDefaultSize ) { +#ifndef __WXMSW__ wxSize defaultSize = wxButton::GetDefaultSize( aParent ); - SetMinSize( wxSize( defaultSize.GetWidth() + 1, defaultSize.GetHeight() + 1 ) ); + defaultSize.IncBy( 1 ); +#else + // wxButton::GetDefaultSize does not work on Windows, it's based on some archiac + // ascii text size logic that does not hold true in modern Windows at hi dpi scaling + // instead spawn a button, grab it's size and then toss it :D + wxButton* dummyButton = new wxButton( this, wxID_ANY ); + dummyButton->SetLabelText( "Z" ); + + wxSize defaultSize = dummyButton->GetSize(); + dummyButton->Hide(); + delete dummyButton; +#endif + SetMinSize( defaultSize ); } Bind( wxEVT_PAINT, &STD_BITMAP_BUTTON::OnPaint, this ); @@ -74,11 +88,26 @@ void STD_BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp ) #ifndef __WXMSW__ wxSize size = m_bitmap.GetDefaultSize(); -#else - wxSize size = m_bitmap.GetPreferredBitmapSizeFor( this ); -#endif SetMinSize( wxSize( size.GetWidth() + 8, size.GetHeight() + 8 ) ); +#else + wxSize size = m_bitmap.GetPreferredBitmapSizeFor( this ); + size.IncBy( 8 ); // padding + + // Now adjust the min size but don't reduce it + wxSize minSize = GetMinSize(); + + // only change the width + // we want to keep the height at the original determined button height + // or else forms will get funny + // additionally we prefer to make the button square + if( size.GetWidth() > minSize.GetHeight() ) + minSize.SetWidth( size.GetWidth() ); + else + minSize.SetWidth( minSize.GetHeight() ); + + SetMinSize( minSize ); +#endif }