Show images set with SetImages() in wxListbook too

Override OnImagesChanged() rather than SetImageList() in wxListbook to
handle the images set with SetImages() correctly too.
This commit is contained in:
Vadim Zeitlin
2021-11-12 16:50:02 +00:00
parent 3bd28ef9e8
commit 31cd4b0aea
2 changed files with 24 additions and 18 deletions

View File

@@ -73,7 +73,6 @@ public:
virtual int SetSelection(size_t n) wxOVERRIDE { return DoSetSelection(n, SetSelection_SendEvent); }
virtual int ChangeSelection(size_t n) wxOVERRIDE { return DoSetSelection(n); }
virtual int HitTest(const wxPoint& pt, long *flags = NULL) const wxOVERRIDE;
virtual void SetImageList(wxImageList *imageList) wxOVERRIDE;
virtual bool DeleteAllPages() wxOVERRIDE;
@@ -82,14 +81,13 @@ public:
protected:
virtual wxWindow *DoRemovePage(size_t page) wxOVERRIDE;
virtual void OnImagesChanged() wxOVERRIDE;
void UpdateSelectedPage(size_t newsel) wxOVERRIDE;
wxBookCtrlEvent* CreatePageChangingEvent() const wxOVERRIDE;
void MakeChangedEvent(wxBookCtrlEvent &event) wxOVERRIDE;
// Get the correct wxListCtrl flags to use depending on our own flags.
long GetListCtrlFlags() const;
// event handlers
void OnListSelected(wxListEvent& event);
void OnSize(wxSizeEvent& event);
@@ -98,6 +96,12 @@ private:
// this should be called when we need to be relaid out
void UpdateSize();
// Get the correct wxListCtrl flags to use depending on our own flags.
long GetListCtrlFlags(bool hasImages) const;
// Update list control wxLC_ICON flag depending on whether we have images.
void SyncListCtrlIconFlag(bool hasImages);
wxDECLARE_EVENT_TABLE();
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxListbook);

View File

@@ -86,7 +86,7 @@ wxListbook::Create(wxWindow *parent,
wxID_ANY,
wxDefaultPosition,
wxDefaultSize,
GetListCtrlFlags()
GetListCtrlFlags(HasImages())
);
if ( GetListView()->InReportView() )
@@ -103,7 +103,7 @@ wxListbook::Create(wxWindow *parent,
// wxListCtrl flags
// ----------------------------------------------------------------------------
long wxListbook::GetListCtrlFlags() const
long wxListbook::GetListCtrlFlags(bool hasImages) const
{
// We'd like to always use wxLC_ICON mode but it doesn't work with the
// native wxListCtrl under MSW unless we do have icons for all the items,
@@ -115,7 +115,7 @@ long wxListbook::GetListCtrlFlags() const
// case there.
long flags = IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP;
if ( GetImageList() )
if ( hasImages )
{
flags |= wxLC_ICON;
}
@@ -252,27 +252,24 @@ bool wxListbook::SetPageImage(size_t n, int imageId)
}
// ----------------------------------------------------------------------------
// image list stuff
// images support
// ----------------------------------------------------------------------------
void wxListbook::SetImageList(wxImageList *imageList)
void wxListbook::OnImagesChanged()
{
const long flagsOld = GetListCtrlFlags();
wxBookCtrlBase::SetImageList(imageList);
const long flagsNew = GetListCtrlFlags();
wxListView * const list = GetListView();
// We may need to change the list control mode if the image list presence
// has changed.
if ( flagsNew != flagsOld )
const bool hasImages = HasImages();
if ( list->HasFlag(wxLC_ICON) != hasImages )
{
// Preserve the selection which is lost when changing the mode
const int oldSel = GetSelection();
list->SetWindowStyleFlag(flagsNew);
// Note that we can't just ToggleWindowStyle(wxLC_ICON) here because we
// may also need to turn off wxLC_REPORT under MSW.
list->SetWindowStyleFlag(GetListCtrlFlags(hasImages));
if ( list->InReportView() )
list->InsertColumn(0, wxS("Pages"));
@@ -281,7 +278,12 @@ void wxListbook::SetImageList(wxImageList *imageList)
SetSelection(oldSel);
}
list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
// We also need to propagate the actual images to use to the list control.
const Images& images = GetImages();
if ( !images.empty() )
list->SetNormalImages(images);
else
list->SetImageList(GetImageList(), wxIMAGE_LIST_NORMAL);
}
// ----------------------------------------------------------------------------