diff --git a/include/wx/app.h b/include/wx/app.h index 33810ef719..45e35ef7d3 100644 --- a/include/wx/app.h +++ b/include/wx/app.h @@ -622,6 +622,10 @@ public: // there are none, will return NULL) virtual wxWindow *GetTopWindow() const; + // convenient helper which is safe to use even if there is no wxApp at + // all, it will just return NULL in this case + static wxWindow *GetMainTopWindow(); + // control the exit behaviour: by default, the program will exit the // main loop (and so, usually, terminate) when the last top-level // program window is deleted. Beware that if you disable this behaviour diff --git a/interface/wx/app.h b/interface/wx/app.h index 073148dcd5..de56c7e987 100644 --- a/interface/wx/app.h +++ b/interface/wx/app.h @@ -859,6 +859,18 @@ public: */ bool GetUseBestVisual() const; + /** + Returns a pointer to the top application window if any. + + This function is safe to call even before creating, or after + destroying, the application object, as it simply returns @NULL if it + doesn't exist. Otherwise it's equivalent to calling + @c wxTheApp->GetTopWindow(). + + @since 3.1.5 + */ + static wxWindow* GetMainTopWindow(); + /** Returns a pointer to the top window. diff --git a/src/common/appcmn.cpp b/src/common/appcmn.cpp index 3ab92d0dd0..d257599f42 100644 --- a/src/common/appcmn.cpp +++ b/src/common/appcmn.cpp @@ -189,6 +189,14 @@ wxWindow* wxAppBase::GetTopWindow() const return window; } +/* static */ +wxWindow* wxAppBase::GetMainTopWindow() +{ + const wxAppBase* const app = static_cast(GetInstance()); + + return app ? app->GetTopWindow() : NULL; +} + wxVideoMode wxAppBase::GetDisplayMode() const { return wxVideoMode(); diff --git a/src/common/dlgcmn.cpp b/src/common/dlgcmn.cpp index 7c5b6da04e..dfc23046ca 100644 --- a/src/common/dlgcmn.cpp +++ b/src/common/dlgcmn.cpp @@ -183,8 +183,8 @@ wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const wxGetTopLevelParent(wxGetActiveWindow())); // and finally the application main window - if ( !parent && wxTheApp ) - parent = CheckIfCanBeUsedAsParent(wxTheApp->GetTopWindow()); + if ( !parent ) + parent = CheckIfCanBeUsedAsParent(wxApp::GetMainTopWindow()); return parent; } diff --git a/src/common/iconbndl.cpp b/src/common/iconbndl.cpp index eadaad6988..077f09fd56 100644 --- a/src/common/iconbndl.cpp +++ b/src/common/iconbndl.cpp @@ -264,7 +264,7 @@ wxIcon wxIconBundle::GetIcon(const wxSize& size, int flags) const sysY = 0; if ( flags & FALLBACK_SYSTEM ) { - wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + wxWindow* win = wxApp::GetMainTopWindow(); sysX = wxSystemSettings::GetMetric(wxSYS_ICON_X, win); sysY = wxSystemSettings::GetMetric(wxSYS_ICON_Y, win); } diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index b469097468..110a5d69c3 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -104,7 +104,7 @@ float wxSizerFlags::DoGetDefaultBorderInPx() // We also have to use the DPI for the monitor showing the top window here // as we don't have any associated window -- but, again, without changes // in the API, there is nothing we can do about this. - const wxWindow* const win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* const win = wxApp::GetMainTopWindow(); static wxPrivate::DpiDependentValue s_defaultBorderInPx; if ( s_defaultBorderInPx.HasChanged(win) ) { diff --git a/src/generic/aboutdlgg.cpp b/src/generic/aboutdlgg.cpp index bb824780f5..a0ecb03030 100644 --- a/src/generic/aboutdlgg.cpp +++ b/src/generic/aboutdlgg.cpp @@ -81,10 +81,10 @@ wxString wxAboutDialogInfo::GetDescriptionAndCredits() const wxIcon wxAboutDialogInfo::GetIcon() const { wxIcon icon = m_icon; - if ( !icon.IsOk() && wxTheApp ) + if ( !icon.IsOk() ) { const wxTopLevelWindow * const - tlw = wxDynamicCast(wxTheApp->GetTopWindow(), wxTopLevelWindow); + tlw = wxDynamicCast(wxApp::GetMainTopWindow(), wxTopLevelWindow); if ( tlw ) icon = tlw->GetIcon(); } diff --git a/src/html/m_image.cpp b/src/html/m_image.cpp index cf1836a86c..4123efcffb 100644 --- a/src/html/m_image.cpp +++ b/src/html/m_image.cpp @@ -691,8 +691,8 @@ TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA") #if defined(__WXOSX_COCOA__) // Try to find a 2x resolution image with @2x appended before the file extension. wxWindow* win = m_WParser->GetWindowInterface() ? m_WParser->GetWindowInterface()->GetHTMLWindow() : NULL; - if (!win && wxTheApp) - win = wxTheApp->GetTopWindow(); + if (!win) + win = wxApp::GetMainTopWindow(); if (win && win->GetContentScaleFactor() > 1.0) { if (tmp.Find('.') != wxNOT_FOUND) diff --git a/src/msw/artmsw.cpp b/src/msw/artmsw.cpp index 2a3cfe1657..4e08e4abd1 100644 --- a/src/msw/artmsw.cpp +++ b/src/msw/artmsw.cpp @@ -326,7 +326,7 @@ wxBitmap wxWindowsArtProvider::CreateBitmap(const wxArtID& id, /*static*/ wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client) { - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); if ( client == wxART_TOOLBAR ) { return wxWindow::FromDIP(wxSize(24, 24), win); diff --git a/src/msw/cursor.cpp b/src/msw/cursor.cpp index 9f3920fe05..e37727a59d 100644 --- a/src/msw/cursor.cpp +++ b/src/msw/cursor.cpp @@ -109,13 +109,13 @@ public: wxCoord wxCursorRefData::GetStandardWidth() { - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); return wxSystemSettings::GetMetric(wxSYS_CURSOR_X, win); } wxCoord wxCursorRefData::GetStandardHeight() { - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); return wxSystemSettings::GetMetric(wxSYS_CURSOR_Y, win); } diff --git a/src/msw/gdiimage.cpp b/src/msw/gdiimage.cpp index db16d2405b..8bb2cf6ecf 100644 --- a/src/msw/gdiimage.cpp +++ b/src/msw/gdiimage.cpp @@ -471,7 +471,7 @@ bool wxICOFileHandler::LoadIcon(wxIcon *icon, else #endif // were we asked for a large icon? - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); if ( desiredWidth == wxGetSystemMetrics(SM_CXICON, win) && desiredHeight == wxGetSystemMetrics(SM_CYICON, win) ) { @@ -666,7 +666,7 @@ wxSize wxGetHiconSize(HICON hicon) if ( !size.x ) { // use default icon size on this hardware - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); size.x = wxGetSystemMetrics(SM_CXICON, win); size.y = wxGetSystemMetrics(SM_CYICON, win); } diff --git a/src/msw/helpchm.cpp b/src/msw/helpchm.cpp index c8207268a4..b998d061a4 100644 --- a/src/msw/helpchm.cpp +++ b/src/msw/helpchm.cpp @@ -69,8 +69,8 @@ HTMLHELP GetHtmlHelpFunction() // fall back to the top level app window and then the desktop if it's NULL static HWND GetSuitableHWND(wxWindow *win) { - if ( !win && wxTheApp ) - win = wxTheApp->GetTopWindow(); + if ( !win ) + win = wxApp::GetMainTopWindow(); return win ? GetHwndOf(win) : ::GetDesktopWindow(); } diff --git a/src/msw/msgdlg.cpp b/src/msw/msgdlg.cpp index 58dd7a3c5e..36c543f27e 100644 --- a/src/msw/msgdlg.cpp +++ b/src/msw/msgdlg.cpp @@ -393,7 +393,7 @@ void wxMessageDialog::AdjustButtonLabels() /* static */ wxFont wxMessageDialog::GetMessageFont() { - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); const wxNativeFontInfo info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); diff --git a/src/msw/settings.cpp b/src/msw/settings.cpp index 42152ffb36..f4b33df25a 100644 --- a/src/msw/settings.cpp +++ b/src/msw/settings.cpp @@ -179,7 +179,7 @@ wxFont wxSystemSettingsNative::GetFont(wxSystemFont index) // for most (simple) controls, e.g. buttons and such but other // controls may prefer to use lfStatusFont or lfCaptionFont if it // is more appropriate for them - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); const wxNativeFontInfo info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); @@ -337,7 +337,7 @@ extern wxFont wxGetCCDefaultFont() // font which is also used for the icon titles and not the stock default // GUI font LOGFONT lf; - const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; + const wxWindow* win = wxApp::GetMainTopWindow(); if ( wxSystemParametersInfo ( SPI_GETICONTITLELOGFONT, diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 8eb40c461a..2c5e6b8051 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -4236,7 +4236,7 @@ bool wxWindowMSW::HandleEndSession(bool endSession, long logOff) return false; // only send once - if ( (this != wxTheApp->GetTopWindow()) ) + if ( this != wxApp::GetMainTopWindow() ) return false; wxCloseEvent event(wxEVT_END_SESSION, wxID_ANY); @@ -4773,10 +4773,11 @@ static wxSize GetWindowDPI(HWND hwnd) } /*extern*/ -int wxGetSystemMetrics(int nIndex, const wxWindow* win) +int wxGetSystemMetrics(int nIndex, const wxWindow* window) { #if wxUSE_DYNLIB_CLASS - const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win; + if ( !window ) + window = wxApp::GetMainTopWindow(); if ( window ) { @@ -4798,14 +4799,14 @@ int wxGetSystemMetrics(int nIndex, const wxWindow* win) } } #else - wxUnusedVar(win); + wxUnusedVar(window); #endif // wxUSE_DYNLIB_CLASS return ::GetSystemMetrics(nIndex); } /*extern*/ -bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, const wxWindow* win) +bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, const wxWindow* window) { // Note that we can't use SystemParametersInfoForDpi() in non-Unicode build // because it always works with wide strings and we'd have to check for all @@ -4813,7 +4814,8 @@ bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWi // for them, and convert the returned value to ANSI after the call. Instead // of doing all this, just don't use it at all in the deprecated ANSI build. #if wxUSE_DYNLIB_CLASS && wxUSE_UNICODE - const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win; + if ( !window ) + window = wxApp::GetMainTopWindow(); if ( window ) { @@ -4838,7 +4840,7 @@ bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWi } } #else - wxUnusedVar(win); + wxUnusedVar(window); #endif // wxUSE_DYNLIB_CLASS return ::SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni) == TRUE;