From 6b3fd04e24d20530b276de5307cbc6737b0c6a4d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 17 Oct 2021 01:00:19 +0100 Subject: [PATCH 1/3] Initialize wxGenericImageList members in default ctor Don't leave m_useMask and m_scaleFactor uninitialized -- even if this probably doesn't matter, call Create() to make sure they have well-defined values for the default-constructed objects. --- include/wx/generic/imaglist.h | 2 +- src/generic/imaglist.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/wx/generic/imaglist.h b/include/wx/generic/imaglist.h index a720c36a2d..b29c495571 100644 --- a/include/wx/generic/imaglist.h +++ b/include/wx/generic/imaglist.h @@ -22,7 +22,7 @@ class WXDLLIMPEXP_FWD_CORE wxColour; class WXDLLIMPEXP_CORE wxGenericImageList: public wxObject { public: - wxGenericImageList() { } + wxGenericImageList(); wxGenericImageList( int width, int height, bool mask = true, int initialCount = 1 ); virtual ~wxGenericImageList(); bool Create( int width, int height, bool mask = true, int initialCount = 1 ); diff --git a/src/generic/imaglist.cpp b/src/generic/imaglist.cpp index b72dd29d51..83e5173ab8 100644 --- a/src/generic/imaglist.cpp +++ b/src/generic/imaglist.cpp @@ -29,6 +29,11 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject); wxIMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList); +wxGenericImageList::wxGenericImageList() +{ + Create(0, 0, false); +} + wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount ) { (void)Create(width, height, mask, initialCount); From a66e5cdb38c89e2c13b6adb384c882a9d3b0c2bd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 17 Oct 2021 01:01:26 +0100 Subject: [PATCH 2/3] Add wxImageList::Destroy() This will be useful in wxMSW implementation and seems to make sense to have as a public function, as long as we have Create(). --- include/wx/generic/imaglist.h | 1 + include/wx/msw/imaglist.h | 3 +++ interface/wx/imaglist.h | 20 +++++++++++++++++++- src/generic/imaglist.cpp | 9 ++++++++- src/msw/imaglist.cpp | 7 ++++++- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/wx/generic/imaglist.h b/include/wx/generic/imaglist.h index b29c495571..c2da30e487 100644 --- a/include/wx/generic/imaglist.h +++ b/include/wx/generic/imaglist.h @@ -26,6 +26,7 @@ public: wxGenericImageList( int width, int height, bool mask = true, int initialCount = 1 ); virtual ~wxGenericImageList(); bool Create( int width, int height, bool mask = true, int initialCount = 1 ); + void Destroy(); virtual int GetImageCount() const; virtual bool GetSize( int index, int &width, int &height ) const; diff --git a/include/wx/msw/imaglist.h b/include/wx/msw/imaglist.h index f86654eb89..44078ef53a 100644 --- a/include/wx/msw/imaglist.h +++ b/include/wx/msw/imaglist.h @@ -58,6 +58,9 @@ public: // initialNumber is the initial number of images to reserve. bool Create(int width, int height, bool mask = true, int initialNumber = 1); + // Destroys the image list, Create() may then be called again later. + void Destroy(); + // Adds a bitmap, and optionally a mask bitmap. // Note that wxImageList creates *new* bitmaps, so you may delete // 'bitmap' and 'mask' after calling Add. diff --git a/interface/wx/imaglist.h b/interface/wx/imaglist.h index a0536b7bc9..9d60830add 100644 --- a/interface/wx/imaglist.h +++ b/interface/wx/imaglist.h @@ -124,11 +124,29 @@ public: int Add(const wxIcon& icon); /** - Initializes the list. See wxImageList() for details. + Initializes the list. + + See wxImageList() for details. + + This function can be called only once after creating the object using + its default ctor or after calling Destroy(). */ bool Create(int width, int height, bool mask = true, int initialCount = 1); + /** + Destroys the current list. + + This function resets the object to its initial state and does more than + just RemoveAll() in the native wxMSW version. + + After calling it, Create() may be called again to recreate the image + list, e.g. using a different size. + + @since 3.1.6 + */ + void Destroy(); + /** Draws a specified image onto a device context. diff --git a/src/generic/imaglist.cpp b/src/generic/imaglist.cpp index 83e5173ab8..de346d3373 100644 --- a/src/generic/imaglist.cpp +++ b/src/generic/imaglist.cpp @@ -39,9 +39,16 @@ wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int in (void)Create(width, height, mask, initialCount); } -wxGenericImageList::~wxGenericImageList() +void wxGenericImageList::Destroy() { (void)RemoveAll(); + + // Make it invalid. + m_size = wxSize(0, 0); +} + +wxGenericImageList::~wxGenericImageList() +{ } int wxGenericImageList::GetImageCount() const diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index 65997291fc..ac20046311 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -100,7 +100,7 @@ bool wxImageList::Create(int width, int height, bool mask, int initial) return m_hImageList != 0; } -wxImageList::~wxImageList() +void wxImageList::Destroy() { if ( m_hImageList ) { @@ -109,6 +109,11 @@ wxImageList::~wxImageList() } } +wxImageList::~wxImageList() +{ + Destroy(); +} + // ---------------------------------------------------------------------------- // wxImageList attributes // ---------------------------------------------------------------------------- From 999340f288a14356640ddbd3c506ed6f23bb7840 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 17 Oct 2021 15:15:52 +0100 Subject: [PATCH 3/3] Check that wxImageList::Create() is not called more than once Or at least not until Destroy() is called. --- include/wx/msw/imaglist.h | 1 + src/msw/imaglist.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/wx/msw/imaglist.h b/include/wx/msw/imaglist.h index 44078ef53a..2291407a2f 100644 --- a/include/wx/msw/imaglist.h +++ b/include/wx/msw/imaglist.h @@ -32,6 +32,7 @@ public: // from icons), and the initial size of the list. wxImageList(int width, int height, bool mask = true, int initialCount = 1) { + m_hImageList = 0; Create(width, height, mask, initialCount); } virtual ~wxImageList(); diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index ac20046311..de3b4fe564 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -73,6 +73,8 @@ wxImageList::wxImageList() // Creates an image list bool wxImageList::Create(int width, int height, bool mask, int initial) { + wxASSERT_MSG( m_hImageList == NULL, "Recreating existing wxImageList?" ); + // Prevent from storing negative dimensions m_size = wxSize(wxMax(width, 0), wxMax(height, 0)); UINT flags = 0;