From: Vadim Zeitlin Date: Wed, 3 Jul 2013 00:31:49 +0000 (+0000) Subject: Add a helper wxApp::GetValidTraits() method. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/76015a6bb79156d8af1b5e8b4b8e46b4e192d18f Add a helper wxApp::GetValidTraits() method. This method always returns some valid traits, even if we don't have wxTheApp (which is possible in the console applications) or if its GetTraits() was overridden to return NULL (which shouldn't be, but still guard against this). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74349 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/app.h b/include/wx/app.h index a0fe4afee1..b2d11cce40 100644 --- a/include/wx/app.h +++ b/include/wx/app.h @@ -228,6 +228,14 @@ public: // for it static wxAppTraits *GetTraitsIfExists(); + // Return some valid traits object. + // + // This method checks if we have wxTheApp and returns its traits if it does + // exist and the traits are non-NULL, similarly to GetTraitsIfExists(), but + // falls back to wxConsoleAppTraits to ensure that it always returns + // something valid. + static wxAppTraits& GetValidTraits(); + // returns the main event loop instance, i.e. the event loop which is started // by OnRun() and which dispatches all events sent from the native toolkit // to the application (except when new event loops are temporarily set-up). diff --git a/src/common/appbase.cpp b/src/common/appbase.cpp index 5014ca25df..b8cba4ff88 100644 --- a/src/common/appbase.cpp +++ b/src/common/appbase.cpp @@ -304,6 +304,15 @@ wxAppTraits *wxAppConsoleBase::GetTraitsIfExists() return app ? app->GetTraits() : NULL; } +/* static */ +wxAppTraits& wxAppConsoleBase::GetValidTraits() +{ + static wxConsoleAppTraits s_traitsConsole; + wxAppTraits* const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; + + return traits ? *traits : s_traitsConsole; +} + // ---------------------------------------------------------------------------- // wxEventLoop redirection // ---------------------------------------------------------------------------- diff --git a/src/common/evtloopcmn.cpp b/src/common/evtloopcmn.cpp index 8564f52517..2399911f68 100644 --- a/src/common/evtloopcmn.cpp +++ b/src/common/evtloopcmn.cpp @@ -124,15 +124,9 @@ wxEventLoopBase::AddSourceForFD(int fd, wxEventLoopSourceHandler *handler, int flags) { - // Ensure that we have some valid traits. - wxConsoleAppTraits traitsConsole; - wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; - if ( !traits ) - traits = &traitsConsole; - - // And delegate to the event loop sources manager defined by it. + // Delegate to the event loop sources manager defined by it. wxEventLoopSourcesManagerBase* const - manager = traits->GetEventLoopSourcesManager(); + manager = wxApp::GetValidTraits().GetEventLoopSourcesManager(); wxCHECK_MSG( manager, NULL, wxS("Must have wxEventLoopSourcesManager") ); return manager->AddSourceForFD(fd, handler, flags);