From 9d455ca3998dbf824a0b917337cd92ebefa4de17 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sun, 22 Oct 2023 15:40:05 -0400 Subject: [PATCH] Fix rendering of ACTION_TOOLBAR_PALETTE buttons at non-normal sizes --- common/tool/action_toolbar.cpp | 3 +++ common/widgets/bitmap_button.cpp | 34 ++++++++++++++++++++----- common/widgets/wx_aui_art_providers.cpp | 2 +- include/widgets/bitmap_button.h | 6 ++++- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/common/tool/action_toolbar.cpp b/common/tool/action_toolbar.cpp index 8b5156ceca..8276c35797 100644 --- a/common/tool/action_toolbar.cpp +++ b/common/tool/action_toolbar.cpp @@ -120,6 +120,7 @@ void ACTION_TOOLBAR_PALETTE::AddAction( const TOOL_ACTION& aAction ) int padding = ( m_buttonSize.GetWidth() - bmpWidth ) / 2; int size = Pgm().GetCommonSettings()->m_Appearance.toolbar_icon_size; wxSize bmSize( size, size ); + bmSize *= KIPLATFORM::UI::GetPixelScaleFactor( m_parent ); BITMAP_BUTTON* button = new BITMAP_BUTTON( m_panel, aAction.GetUIId(), wxDefaultPosition, bmSize ); @@ -129,6 +130,8 @@ void ACTION_TOOLBAR_PALETTE::AddAction( const TOOL_ACTION& aAction ) button->SetPadding( padding ); button->SetToolTip( aAction.GetTooltip() ); button->AcceptDragInAsClick(); + button->SetIsToolbarButton(); + button->SetBitmapCentered(); m_buttons[aAction.GetUIId()] = button; diff --git a/common/widgets/bitmap_button.cpp b/common/widgets/bitmap_button.cpp index 4c5499e979..b7b634a7b0 100644 --- a/common/widgets/bitmap_button.cpp +++ b/common/widgets/bitmap_button.cpp @@ -23,6 +23,8 @@ */ #include +#include +#include #include #include #include @@ -41,6 +43,7 @@ BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& m_badgeTextColor( wxColor( wxT( "white" ) ) ), m_buttonState( 0 ), m_padding( 0 ), + m_isToolbarButton( false ), m_acceptDraggedInClicks( false ), m_centerBitmap( false ) { @@ -62,6 +65,7 @@ BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxBitmap& m_badgeTextColor( wxColor( wxT( "white" ) ) ), m_buttonState( 0 ), m_padding( 5 ), + m_isToolbarButton( false ), m_acceptDraggedInClicks( false ), m_centerBitmap( false ) { @@ -101,11 +105,7 @@ void BITMAP_BUTTON::SetPadding( int aPadding ) void BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp ) { m_normalBitmap = aBmp; -#ifndef __WXMSW__ - m_unadjustedMinSize = m_normalBitmap.GetDefaultSize(); -#else m_unadjustedMinSize = m_normalBitmap.GetPreferredBitmapSizeFor( this ); -#endif SetMinSize( wxSize( m_unadjustedMinSize.GetWidth() + ( m_padding * 2 ), m_unadjustedMinSize.GetHeight() + ( m_padding * 2 ) ) ); @@ -286,12 +286,32 @@ void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent ) const wxBitmapBundle& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap; wxPoint drawBmpPos( m_padding, m_padding ); - wxBitmap bmpImg = bmp.GetBitmapFor( this ); + wxBitmap bmpImg; + double scale = KIPLATFORM::UI::GetPixelScaleFactor( this ); + int size; + wxSize bmSize; + + if( m_isToolbarButton ) + { + size = Pgm().GetCommonSettings()->m_Appearance.toolbar_icon_size; + bmSize = wxSize( size, size ); + bmpImg = bmp.GetBitmap( bmSize * scale ); + + // wxBitmapBundle::GetBitmap thinks we need this rescaled to match the base size + if( bmpImg.IsOk() ) + bmpImg.SetScaleFactor( scale ); + } + else + { + bmpImg = bmp.GetBitmapFor( this ); + bmSize = bmpImg.GetSize(); + } + if( m_centerBitmap ) { // dont let it go negative if bmp is larger than the button - int x = std::max( ( rect.width - bmpImg.GetWidth() ) / 2, 0 ); - int y = std::max( ( rect.height - bmpImg.GetHeight() ) / 2, 0 ); + int x = std::max( ( rect.width - bmSize.x ) / 2, 0 ); + int y = std::max( ( rect.height - bmSize.y ) / 2, 0 ); drawBmpPos = wxPoint( x, y ); } diff --git a/common/widgets/wx_aui_art_providers.cpp b/common/widgets/wx_aui_art_providers.cpp index 87d02dd2dc..c8ca833991 100644 --- a/common/widgets/wx_aui_art_providers.cpp +++ b/common/widgets/wx_aui_art_providers.cpp @@ -104,7 +104,7 @@ void WX_AUI_TOOLBAR_ART::DrawButton( wxDC& aDc, wxWindow* aWindow, const wxAuiTo int bmpX = 0, bmpY = 0; int textX = 0, textY = 0; - double scale = KIPLATFORM::UI::GetPixelScaleFactor( aWindow ) ; + double scale = KIPLATFORM::UI::GetPixelScaleFactor( aWindow ); const wxBitmapBundle& bundle = ( aItem.GetState() & wxAUI_BUTTON_STATE_DISABLED ) ? aItem.GetDisabledBitmapBundle() : aItem.GetBitmapBundle(); diff --git a/include/widgets/bitmap_button.h b/include/widgets/bitmap_button.h index 5b731d04c0..05ced04548 100644 --- a/include/widgets/bitmap_button.h +++ b/include/widgets/bitmap_button.h @@ -115,11 +115,14 @@ public: m_badgeTextColor = aBadgeTextColor; } - void SetBitmapCentered( bool aCentered ) + void SetBitmapCentered( bool aCentered = true ) { m_centerBitmap = aCentered; } + void SetIsToolbarButton( bool aIsToolbar = true ) { m_isToolbarButton = aIsToolbar; } + bool IsToolbarButton() const { return m_isToolbarButton; } + protected: void setupEvents(); @@ -159,6 +162,7 @@ private: int m_buttonState; int m_padding; wxSize m_unadjustedMinSize; + bool m_isToolbarButton; ///< Accept mouse-up as click even if mouse-down happened outside of the control bool m_acceptDraggedInClicks;