From 09cbec0373a00c753f2160a44edbd1b421f3c166 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 4 Aug 2021 21:09:04 +0100 Subject: [PATCH] Allow calling wxListCtrl::SetImageList() before Create() in wxMSW This already worked with the generic version, but silently failed with wxMSW, so make it work with wxMSW too as it doesn't cost much and makes wxListCtrl behave in the same way under all platforms. Also document that SetImageList() can be used before the window is created. --- interface/wx/listctrl.h | 5 +++++ src/msw/listctrl.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/interface/wx/listctrl.h b/interface/wx/listctrl.h index f8d45031a2..adb528f533 100644 --- a/interface/wx/listctrl.h +++ b/interface/wx/listctrl.h @@ -1110,6 +1110,11 @@ public: This method does not take ownership of the image list, you have to delete it yourself. + Note that, unlike for most of the other methods of this class, it is + possible to call this function before the corresponding window is + created, i.e. do it in a constructor of a class derived from wxListCtrl + before calling Create(). + @see AssignImageList() */ void SetImageList(wxImageList* imageList, int which); diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 6b4be8208b..922a9809d8 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -320,6 +320,15 @@ bool wxListCtrl::Create(wxWindow *parent, if ( HasFlag(wxLC_LIST) ) m_colCount = 1; + // If SetImageList() had been called before the control was created, take + // it into account now. + if ( m_imageListNormal ) + ListView_SetImageList(GetHwnd(), GetHimagelistOf(m_imageListNormal), LVSIL_NORMAL); + if ( m_imageListSmall ) + ListView_SetImageList(GetHwnd(), GetHimagelistOf(m_imageListSmall), LVSIL_SMALL); + if ( m_imageListState ) + ListView_SetImageList(GetHwnd(), GetHimagelistOf(m_imageListState), LVSIL_STATE); + return true; } @@ -1551,6 +1560,13 @@ void wxListCtrl::SetImageList(wxImageList *imageList, int which) m_imageListState = imageList; m_ownsImageListState = false; } + + // It's possible that this function is called before the control is + // created, don't do anything else in this case -- the image list will be + // really set after creating it. + if ( !GetHwnd() ) + return; + (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); // For ComCtl32 prior 6.0 we need to re-assign all existing