From 80bb72468c8d62ac85a3be31e1ed07f09415d04b Mon Sep 17 00:00:00 2001 From: Alexander Koshelev Date: Tue, 18 Jan 2022 16:29:00 +0300 Subject: [PATCH] Use wxBitmapBundle in wxComboCtrl instead of wxBitmap Replace wxBitmap with wxBitmapBundle in wxComboCtrl to make it show the bitmap appropriate to the current DPI scaling. Also add an event handler updating bitmaps on DPI change. Closes #22032. --- include/wx/combo.h | 32 ++++++++++++++++++-------------- interface/wx/combo.h | 24 ++++++++++++------------ src/common/combocmn.cpp | 34 ++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/include/wx/combo.h b/include/wx/combo.h index c9910e8486..727c26ecb9 100644 --- a/include/wx/combo.h +++ b/include/wx/combo.h @@ -43,7 +43,7 @@ #include "wx/control.h" #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags -#include "wx/bitmap.h" // wxBitmap used by-value +#include "wx/bmpbndl.h" #include "wx/textentry.h" #include "wx/time.h" // needed for wxMilliClock_t @@ -351,11 +351,11 @@ public: // bmpHover: drawn when cursor hovers on button. This is ignored on platforms // that do not generally display hover differently. // bmpDisabled: drawn when combobox is disabled. - void SetButtonBitmaps( const wxBitmap& bmpNormal, + void SetButtonBitmaps( const wxBitmapBundle& bmpNormal, bool pushButtonBg = false, - const wxBitmap& bmpPressed = wxNullBitmap, - const wxBitmap& bmpHover = wxNullBitmap, - const wxBitmap& bmpDisabled = wxNullBitmap ); + const wxBitmapBundle& bmpPressed = wxBitmapBundle(), + const wxBitmapBundle& bmpHover = wxBitmapBundle(), + const wxBitmapBundle& bmpDisabled = wxBitmapBundle() ); #if WXWIN_COMPATIBILITY_2_8 // @@ -433,11 +433,11 @@ public: (m_windowStyle & wxCB_READONLY) ); } - // These methods return references to appropriate dropbutton bitmaps - const wxBitmap& GetBitmapNormal() const { return m_bmpNormal; } - const wxBitmap& GetBitmapPressed() const { return m_bmpPressed; } - const wxBitmap& GetBitmapHover() const { return m_bmpHover; } - const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; } + // These methods return appropriate dropbutton bitmaps + wxBitmap GetBitmapNormal() const { return m_bmpNormal.GetBitmapFor(this); } + wxBitmap GetBitmapPressed() const { return m_bmpPressed.GetBitmapFor(this); } + wxBitmap GetBitmapHover() const { return m_bmpHover.GetBitmapFor(this); } + wxBitmap GetBitmapDisabled() const { return m_bmpDisabled.GetBitmapFor(this); } // Set custom style flags for embedded wxTextCtrl. Usually must be used // with two-step creation, before Create() call. @@ -585,6 +585,10 @@ protected: void OnPopupMouseEvent(wxMouseEvent& event); + // This function can be used as event handle for wxEVT_DPI_CHANGED event + // and simply recalculates button size when it happens. + void WXHandleDPIChanged(wxDPIChangedEvent& event); + // Set customization flags (directs how wxComboCtrlBase helpers behave) void Customize( wxUint32 flags ) { m_iFlags |= flags; } @@ -699,10 +703,10 @@ protected: int m_btnWidDefault; // custom dropbutton bitmaps - wxBitmap m_bmpNormal; - wxBitmap m_bmpPressed; - wxBitmap m_bmpHover; - wxBitmap m_bmpDisabled; + wxBitmapBundle m_bmpNormal; + wxBitmapBundle m_bmpPressed; + wxBitmapBundle m_bmpHover; + wxBitmapBundle m_bmpDisabled; // area used by the button wxSize m_btnSize; diff --git a/interface/wx/combo.h b/interface/wx/combo.h index c772aec966..44b1d1cf7b 100644 --- a/interface/wx/combo.h +++ b/interface/wx/combo.h @@ -483,33 +483,33 @@ public: Returns disabled button bitmap that has been set with SetButtonBitmaps(). - @return A reference to the disabled state bitmap. + @return The disabled state bitmap. */ - const wxBitmap& GetBitmapDisabled() const; + wxBitmap GetBitmapDisabled() const; /** Returns button mouse hover bitmap that has been set with SetButtonBitmaps(). - @return A reference to the mouse hover state bitmap. + @return The mouse hover state bitmap. */ - const wxBitmap& GetBitmapHover() const; + wxBitmap GetBitmapHover() const; /** Returns default button bitmap that has been set with SetButtonBitmaps(). - @return A reference to the normal state bitmap. + @return The normal state bitmap. */ - const wxBitmap& GetBitmapNormal() const; + wxBitmap GetBitmapNormal() const; /** Returns depressed button bitmap that has been set with SetButtonBitmaps(). - @return A reference to the depressed state bitmap. + @return The depressed state bitmap. */ - const wxBitmap& GetBitmapPressed() const; + wxBitmap GetBitmapPressed() const; /** Returns current size of the dropdown button. @@ -697,11 +697,11 @@ public: @param bmpDisabled Disabled button image. */ - void SetButtonBitmaps(const wxBitmap& bmpNormal, + void SetButtonBitmaps(const wxBitmapBundle& bmpNormal, bool pushButtonBg = false, - const wxBitmap& bmpPressed = wxNullBitmap, - const wxBitmap& bmpHover = wxNullBitmap, - const wxBitmap& bmpDisabled = wxNullBitmap); + const wxBitmapBundle& bmpPressed = wxBitmapBundle(), + const wxBitmapBundle& bmpHover = wxBitmapBundle(), + const wxBitmapBundle& bmpDisabled = wxBitmapBundle()); /** Sets size and position of dropdown button. diff --git a/src/common/combocmn.cpp b/src/common/combocmn.cpp index d6007631c0..80408c6677 100644 --- a/src/common/combocmn.cpp +++ b/src/common/combocmn.cpp @@ -701,6 +701,16 @@ void wxComboCtrlBase::OnPopupMouseEvent( wxMouseEvent& event ) } } +void wxComboCtrlBase::WXHandleDPIChanged(wxDPIChangedEvent& event) +{ + // Ensure it is really recalculated. + m_btnSize = wxDefaultSize; + // And calculate it again + m_btnSize = GetButtonSize(); + + event.Skip(); +} + // ---------------------------------------------------------------------------- // wxComboCtrlTextCtrl // ---------------------------------------------------------------------------- @@ -786,6 +796,8 @@ void wxComboCtrlBase::Init() // Let's make it so that the popup control will not receive mouse // events until mouse left button has been up. m_blockEventsToPopup = true; + + Bind(wxEVT_DPI_CHANGED, &wxComboCtrlBase::WXHandleDPIChanged, this); } bool wxComboCtrlBase::Create(wxWindow *parent, @@ -1019,8 +1031,9 @@ void wxComboCtrlBase::CalculateAreas( int btnWidth ) // button width is set to default and blank button bg is not drawn if ( m_bmpNormal.IsOk() ) { - int bmpReqWidth = m_bmpNormal.GetWidth(); - int bmpReqHeight = m_bmpNormal.GetHeight(); + wxSize bmpReqSize = m_bmpNormal.GetPreferredLogicalSizeFor(this); + int bmpReqWidth = bmpReqSize.GetWidth(); + int bmpReqHeight = bmpReqSize.GetHeight(); // If drawing blank button background, we need to add some margin. if ( m_blankButtonBg ) @@ -1533,7 +1546,7 @@ void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags ) { // Draw bitmap - wxBitmap* pBmp; + wxBitmapBundle* pBmp; if ( !enabled ) pBmp = &m_bmpDisabled; @@ -1556,9 +1569,10 @@ void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags ) } // Draw bitmap centered in drawRect - dc.DrawBitmap(*pBmp, - drawRect.x + (drawRect.width-pBmp->GetWidth())/2, - drawRect.y + (drawRect.height-pBmp->GetHeight())/2, + wxBitmap currentBmp = pBmp->GetBitmapFor(this); + dc.DrawBitmap(currentBmp, + drawRect.x + (drawRect.width-currentBmp.GetLogicalWidth())/2, + drawRect.y + (drawRect.height-currentBmp.GetLogicalHeight())/2, true); } } @@ -2390,11 +2404,11 @@ wxSize wxComboCtrlBase::GetButtonSize() return retSize; } -void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal, +void wxComboCtrlBase::SetButtonBitmaps( const wxBitmapBundle& bmpNormal, bool blankButtonBg, - const wxBitmap& bmpPressed, - const wxBitmap& bmpHover, - const wxBitmap& bmpDisabled ) + const wxBitmapBundle& bmpPressed, + const wxBitmapBundle& bmpHover, + const wxBitmapBundle& bmpDisabled ) { m_bmpNormal = bmpNormal; m_blankButtonBg = blankButtonBg;