From 455a45f5a8925573e0d2355cfb6f7ecedce6fc99 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 22 Sep 2018 14:33:29 +0200 Subject: [PATCH] Always create wxStaticBitmap in the widgets sample Not creating it when the default image wasn't found resulted in a crash, so always create wxStaticBitmap using a fallback bitmap if necessary. Also add a wxCHECK_RET to prevent the crash from happening in a similar situation in the future. --- samples/widgets/statbmp.cpp | 30 +++++++++++++++++++++--------- samples/widgets/widgets.cpp | 7 +++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/samples/widgets/statbmp.cpp b/samples/widgets/statbmp.cpp index b31b3a0370..97ebb97030 100644 --- a/samples/widgets/statbmp.cpp +++ b/samples/widgets/statbmp.cpp @@ -35,6 +35,7 @@ #include "wx/textctrl.h" #endif +#include "wx/artprov.h" #include "wx/filename.h" #include "wx/generic/statbmpg.h" @@ -126,28 +127,39 @@ void StatBmpWidgetsPage::RecreateWidget() { wxDELETE(m_statbmp); - wxString filepath = m_filepicker->GetPath(); - if ( filepath.empty() ) - return; + wxBitmap bmp; - wxImage image(filepath); - if (! image.IsOk() ) + wxString filepath = m_filepicker->GetPath(); + if ( !filepath.empty() ) { - wxLogMessage("Reading image from file '%s' failed.", filepath.c_str()); - return; + wxImage image(filepath); + if ( image.IsOk() ) + { + bmp = image; + } + else + { + wxLogMessage("Reading image from file '%s' failed.", filepath.c_str()); + } + } + + if ( !bmp.IsOk() ) + { + // Show at least something. + bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE); } long style = GetAttrs().m_defaultFlags; if (m_radio->GetSelection() == 0) { - m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxBitmap(image), + m_statbmp = new wxStaticBitmap(this, wxID_ANY, bmp, wxDefaultPosition, wxDefaultSize, style); } else { - m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, wxBitmap(image), + m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, bmp, wxDefaultPosition, wxDefaultSize, style); } diff --git a/samples/widgets/widgets.cpp b/samples/widgets/widgets.cpp index 5df2d6972c..c14cadcc26 100644 --- a/samples/widgets/widgets.cpp +++ b/samples/widgets/widgets.cpp @@ -680,8 +680,11 @@ void WidgetsFrame::ConnectToWidgetEvents() it != widgets.end(); ++it ) { - (*it)->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this); - (*it)->Bind(wxEVT_KILL_FOCUS, &WidgetsFrame::OnWidgetFocus, this); + wxWindow* const w = *it; + wxCHECK_RET(w, "NULL widget"); + + w->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this); + w->Bind(wxEVT_KILL_FOCUS, &WidgetsFrame::OnWidgetFocus, this); } }