Support DPI scaling in custom bitmap buttons

This commit is contained in:
Jon Evans 2023-06-03 11:32:36 -04:00
parent bdee545841
commit 8f69336ad1
4 changed files with 28 additions and 19 deletions

View File

@ -92,16 +92,17 @@ void BITMAP_BUTTON::SetPadding( int aPadding )
} }
void BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp ) void BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
{ {
m_normalBitmap = aBmp; m_normalBitmap = aBmp;
m_unadjustedMinSize = aBmp.GetSize(); m_unadjustedMinSize = aBmp.GetPreferredBitmapSizeFor( this );
SetMinSize( wxSize( aBmp.GetWidth() + ( m_padding * 2 ), aBmp.GetHeight() + ( m_padding * 2 ) ) ); SetMinSize( wxSize( m_unadjustedMinSize.GetWidth() + ( m_padding * 2 ),
m_unadjustedMinSize.GetHeight() + ( m_padding * 2 ) ) );
} }
void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmap& aBmp ) void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmapBundle& aBmp )
{ {
m_disabledBitmap = aBmp; m_disabledBitmap = aBmp;
} }
@ -272,11 +273,11 @@ void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
} }
} }
const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap; const wxBitmapBundle& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
// Draw the bitmap with the upper-left corner offset by the padding // Draw the bitmap with the upper-left corner offset by the padding
if( bmp.IsOk() ) if( bmp.IsOk() )
dc.DrawBitmap( bmp, m_padding, m_padding, true ); dc.DrawBitmap( bmp.GetBitmapFor( this ), m_padding, m_padding, true );
// Draw the badge // Draw the badge
if( m_showBadge ) if( m_showBadge )

View File

@ -73,11 +73,12 @@ void STD_BITMAP_BUTTON::onThemeChanged( wxSysColourChangedEvent &aEvent )
} }
void STD_BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp ) void STD_BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
{ {
m_bitmap = aBmp; m_bitmap = aBmp;
wxSize size = aBmp.GetPreferredBitmapSizeFor( this );
SetMinSize( wxSize( m_bitmap.GetWidth() + 8, m_bitmap.GetHeight() + 8 ) ); SetMinSize( wxSize( size.GetWidth() + 8, size.GetHeight() + 8 ) );
} }
@ -205,14 +206,21 @@ void STD_BITMAP_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
if( m_bitmap.IsOk() ) if( m_bitmap.IsOk() )
{ {
r1.x = ( size.GetWidth() - m_bitmap.GetWidth() ) / 2; wxSize bmpSize = m_bitmap.GetPreferredBitmapSizeFor( this );
r1.x = ( size.GetWidth() - bmpSize.GetWidth() ) / 2;
if( r1.x < 0 ) if( r1.x < 0 )
r1.x = 0; r1.x = 0;
r1.y += ( size.GetHeight() - m_bitmap.GetHeight() ) / 2; r1.y += ( size.GetHeight() - bmpSize.GetHeight() ) / 2;
dc.DrawBitmap( m_bIsEnable ? m_bitmap : m_bitmap.ConvertToDisabled(), r1.x, r1.y, true ); wxBitmap bm = m_bitmap.GetBitmapFor( this );
if( !m_bIsEnable )
bm.ConvertToDisabled();
dc.DrawBitmap( bm, r1.x, r1.y, true );
} }
} }

View File

@ -25,7 +25,7 @@
#ifndef BITMAP_BUTTON_H_ #ifndef BITMAP_BUTTON_H_
#define BITMAP_BUTTON_H_ #define BITMAP_BUTTON_H_
#include <wx/bitmap.h> #include <wx/bmpbndl.h>
#include <wx/panel.h> #include <wx/panel.h>
#include <wx/colour.h> #include <wx/colour.h>
@ -63,14 +63,14 @@ public:
* *
* @param aBmp is the enabled bitmap. * @param aBmp is the enabled bitmap.
*/ */
void SetBitmap( const wxBitmap& aBmp ); void SetBitmap( const wxBitmapBundle& aBmp );
/** /**
* Set the bitmap shown when the button is disabled. * Set the bitmap shown when the button is disabled.
* *
* @param aBmp is the disabled bitmap. * @param aBmp is the disabled bitmap.
*/ */
void SetDisabledBitmap( const wxBitmap& aBmp ); void SetDisabledBitmap( const wxBitmapBundle& aBmp );
/** /**
* Enable the button. * Enable the button.
@ -142,8 +142,8 @@ protected:
} }
private: private:
wxBitmap m_normalBitmap; wxBitmapBundle m_normalBitmap;
wxBitmap m_disabledBitmap; wxBitmapBundle m_disabledBitmap;
bool m_isRadioButton; bool m_isRadioButton;
bool m_showBadge; bool m_showBadge;

View File

@ -25,7 +25,7 @@
#ifndef STD_BITMAP_BUTTON_H #ifndef STD_BITMAP_BUTTON_H
#define STD_BITMAP_BUTTON_H #define STD_BITMAP_BUTTON_H
#include <wx/bitmap.h> #include <wx/bmpbndl.h>
#include <wx/panel.h> #include <wx/panel.h>
class wxButton; class wxButton;
@ -51,7 +51,7 @@ public:
~STD_BITMAP_BUTTON(); ~STD_BITMAP_BUTTON();
void SetBitmap( const wxBitmap& aBmp ); void SetBitmap( const wxBitmapBundle& aBmp );
bool Enable( bool aEnable = true ) override; bool Enable( bool aEnable = true ) override;
protected: protected:
@ -66,7 +66,7 @@ protected:
private: private:
int m_stateButton = 0; int m_stateButton = 0;
bool m_bIsEnable = true; bool m_bIsEnable = true;
wxBitmap m_bitmap; wxBitmapBundle m_bitmap;
}; };
#endif /*STD_BITMAP_BUTTON_H*/ #endif /*STD_BITMAP_BUTTON_H*/