diff --git a/src/msw/anybutton.cpp b/src/msw/anybutton.cpp index b62d516aa0..fd05bd5c24 100644 --- a/src/msw/anybutton.cpp +++ b/src/msw/anybutton.cpp @@ -106,13 +106,27 @@ class wxButtonImageData: public wxObject { public: wxButtonImageData(wxWindow* btn, const wxBitmapBundle& normalBundle) + : m_btn(btn) { m_bitmapSize = normalBundle.GetPreferredSizeFor(btn); m_bitmapBundles[wxAnyButton::State_Normal] = normalBundle; + + // React to DPI changes in the future. + m_btn->Bind(wxEVT_DPI_CHANGED, &wxButtonImageData::OnDPIChanged, this); } - virtual ~wxButtonImageData() { } + virtual ~wxButtonImageData() + { + m_btn->Unbind(wxEVT_DPI_CHANGED, &wxButtonImageData::OnDPIChanged, this); + } + + virtual void OnDPIChanged(wxDPIChangedEvent& event) + { + event.Skip(); + + m_bitmapSize = m_bitmapBundles[wxAnyButton::State_Normal].GetPreferredSizeFor(m_btn); + } // Bitmap can be set either explicitly, when the bitmap for the given state // is specified by the application, or implicitly, when the bitmap for some @@ -165,6 +179,9 @@ protected: wxBitmapBundle m_bitmapBundles[wxAnyButton::State_Max]; + // the button we're associated with + wxWindow* const m_btn; + wxDECLARE_NO_COPY_CLASS(wxButtonImageData); }; @@ -181,12 +198,8 @@ public: wxODButtonImageData(wxAnyButton *btn, const wxBitmapBundle& bitmapBundle) : wxButtonImageData(btn, bitmapBundle) { - SetBitmap(GetBitmapFromBundle(bitmapBundle), - wxAnyButton::State_Normal); -#if wxUSE_IMAGE - SetBitmap(GetBitmapFromBundle(bitmapBundle).ConvertToDisabled(), - wxAnyButton::State_Disabled); -#endif + InitImageList(); + m_dir = wxLEFT; // we use margins when we have both bitmap and text, but when we have @@ -229,6 +242,34 @@ public: } private: + void InitImageList() + { + const wxBitmap bitmap = m_bitmapBundles[wxAnyButton::State_Normal].GetBitmap(m_bitmapSize); + + for ( int n = 0; n < wxAnyButton::State_Max; n++ ) + { + wxBitmap stateBitmap = m_bitmapBundles[n].GetBitmap(m_bitmapSize); + if ( !stateBitmap.IsOk() ) + { +#if wxUSE_IMAGE + if ( n == wxAnyButton::State_Disabled ) + stateBitmap = bitmap.ConvertToDisabled(); + else +#endif // wxUSE_IMAGE + stateBitmap = bitmap; + } + + SetBitmap(stateBitmap, (wxAnyButton::State)n); + } + } + + void OnDPIChanged(wxDPIChangedEvent& event) wxOVERRIDE + { + wxButtonImageData::OnDPIChanged(event); + + InitImageList(); + } + // just store the values passed to us to be able to retrieve them later // from the drawing code wxBitmap m_bitmaps[wxAnyButton::State_Max]; @@ -253,8 +294,7 @@ public: // we must be constructed with the size of our images as we need to create // the image list wxXPButtonImageData(wxAnyButton *btn, const wxBitmapBundle& bitmapBundle) - : wxButtonImageData(btn, bitmapBundle), - m_btn(btn) + : wxButtonImageData(btn, bitmapBundle) { InitImageList(); @@ -265,9 +305,6 @@ public: m_data.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT; UpdateImageInfo(); - - // React to DPI changes in the future. - btn->Bind(wxEVT_DPI_CHANGED, &wxXPButtonImageData::OnDPIChanged, this); } virtual wxBitmap GetBitmap(wxAnyButton::State which) const wxOVERRIDE @@ -383,7 +420,7 @@ private: stateBitmap = bitmap; } - m_iml.Add(bitmap); + m_iml.Add(stateBitmap); } // In addition to the states supported by wxWidgets such as normal, @@ -409,13 +446,9 @@ private: } } - void OnDPIChanged(wxDPIChangedEvent& event) + void OnDPIChanged(wxDPIChangedEvent& event) wxOVERRIDE { - event.Skip(); - - // We need to recreate the image list using the new size and re-add all - // bitmaps to it. - m_bitmapSize = m_bitmapBundles[wxAnyButton::State_Normal].GetPreferredSizeFor(m_btn); + wxButtonImageData::OnDPIChanged(event); m_iml.Destroy(); InitImageList(); @@ -430,10 +463,6 @@ private: // store the rest of the data in BCM_SETIMAGELIST-friendly form BUTTON_IMAGELIST m_data; - // the button we're associated with - wxWindow* const m_btn; - - wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData); wxDECLARE_ABSTRACT_CLASS(wxXPButtonImageData); }; @@ -817,7 +846,8 @@ void wxAnyButton::DoSetBitmap(const wxBitmapBundle& bitmapBundle, State which) // Check if we already had bitmaps of different size. if ( m_imageData && - bitmapBundle.GetDefaultSize() != m_imageData->GetBitmapSize() ) + bitmapBundle.GetDefaultSize() != + m_imageData->GetBitmapBundle(State_Normal).GetDefaultSize() ) { wxASSERT_MSG( which == State_Normal, "Must set normal bitmap with the new size first" );