Add wxApp::GetMainTopWindow() wrapper

This trivial function just allows to avoid checking if wxTheApp is not
null before calling GetTopWindow() on it.

Replace the existing "wxTheApp && wxTheApp->GetTopWindow()" calls with
wxApp::GetMainTopWindow().

No real changes.
This commit is contained in:
Vadim Zeitlin
2021-03-07 15:25:27 +01:00
parent e9f619e561
commit 69e48f036b
15 changed files with 51 additions and 25 deletions

View File

@@ -622,6 +622,10 @@ public:
// there are none, will return NULL) // there are none, will return NULL)
virtual wxWindow *GetTopWindow() const; 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 // control the exit behaviour: by default, the program will exit the
// main loop (and so, usually, terminate) when the last top-level // main loop (and so, usually, terminate) when the last top-level
// program window is deleted. Beware that if you disable this behaviour // program window is deleted. Beware that if you disable this behaviour

View File

@@ -859,6 +859,18 @@ public:
*/ */
bool GetUseBestVisual() const; 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. Returns a pointer to the top window.

View File

@@ -189,6 +189,14 @@ wxWindow* wxAppBase::GetTopWindow() const
return window; return window;
} }
/* static */
wxWindow* wxAppBase::GetMainTopWindow()
{
const wxAppBase* const app = static_cast<wxAppBase*>(GetInstance());
return app ? app->GetTopWindow() : NULL;
}
wxVideoMode wxAppBase::GetDisplayMode() const wxVideoMode wxAppBase::GetDisplayMode() const
{ {
return wxVideoMode(); return wxVideoMode();

View File

@@ -183,8 +183,8 @@ wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const
wxGetTopLevelParent(wxGetActiveWindow())); wxGetTopLevelParent(wxGetActiveWindow()));
// and finally the application main window // and finally the application main window
if ( !parent && wxTheApp ) if ( !parent )
parent = CheckIfCanBeUsedAsParent(wxTheApp->GetTopWindow()); parent = CheckIfCanBeUsedAsParent(wxApp::GetMainTopWindow());
return parent; return parent;
} }

View File

@@ -264,7 +264,7 @@ wxIcon wxIconBundle::GetIcon(const wxSize& size, int flags) const
sysY = 0; sysY = 0;
if ( flags & FALLBACK_SYSTEM ) if ( flags & FALLBACK_SYSTEM )
{ {
wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; wxWindow* win = wxApp::GetMainTopWindow();
sysX = wxSystemSettings::GetMetric(wxSYS_ICON_X, win); sysX = wxSystemSettings::GetMetric(wxSYS_ICON_X, win);
sysY = wxSystemSettings::GetMetric(wxSYS_ICON_Y, win); sysY = wxSystemSettings::GetMetric(wxSYS_ICON_Y, win);
} }

View File

@@ -104,7 +104,7 @@ float wxSizerFlags::DoGetDefaultBorderInPx()
// We also have to use the DPI for the monitor showing the top window here // 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 // as we don't have any associated window -- but, again, without changes
// in the API, there is nothing we can do about this. // 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<float> s_defaultBorderInPx; static wxPrivate::DpiDependentValue<float> s_defaultBorderInPx;
if ( s_defaultBorderInPx.HasChanged(win) ) if ( s_defaultBorderInPx.HasChanged(win) )
{ {

View File

@@ -81,10 +81,10 @@ wxString wxAboutDialogInfo::GetDescriptionAndCredits() const
wxIcon wxAboutDialogInfo::GetIcon() const wxIcon wxAboutDialogInfo::GetIcon() const
{ {
wxIcon icon = m_icon; wxIcon icon = m_icon;
if ( !icon.IsOk() && wxTheApp ) if ( !icon.IsOk() )
{ {
const wxTopLevelWindow * const const wxTopLevelWindow * const
tlw = wxDynamicCast(wxTheApp->GetTopWindow(), wxTopLevelWindow); tlw = wxDynamicCast(wxApp::GetMainTopWindow(), wxTopLevelWindow);
if ( tlw ) if ( tlw )
icon = tlw->GetIcon(); icon = tlw->GetIcon();
} }

View File

@@ -691,8 +691,8 @@ TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
#if defined(__WXOSX_COCOA__) #if defined(__WXOSX_COCOA__)
// Try to find a 2x resolution image with @2x appended before the file extension. // Try to find a 2x resolution image with @2x appended before the file extension.
wxWindow* win = m_WParser->GetWindowInterface() ? m_WParser->GetWindowInterface()->GetHTMLWindow() : NULL; wxWindow* win = m_WParser->GetWindowInterface() ? m_WParser->GetWindowInterface()->GetHTMLWindow() : NULL;
if (!win && wxTheApp) if (!win)
win = wxTheApp->GetTopWindow(); win = wxApp::GetMainTopWindow();
if (win && win->GetContentScaleFactor() > 1.0) if (win && win->GetContentScaleFactor() > 1.0)
{ {
if (tmp.Find('.') != wxNOT_FOUND) if (tmp.Find('.') != wxNOT_FOUND)

View File

@@ -326,7 +326,7 @@ wxBitmap wxWindowsArtProvider::CreateBitmap(const wxArtID& id,
/*static*/ /*static*/
wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client) wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& client)
{ {
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
if ( client == wxART_TOOLBAR ) if ( client == wxART_TOOLBAR )
{ {
return wxWindow::FromDIP(wxSize(24, 24), win); return wxWindow::FromDIP(wxSize(24, 24), win);

View File

@@ -109,13 +109,13 @@ public:
wxCoord wxCursorRefData::GetStandardWidth() wxCoord wxCursorRefData::GetStandardWidth()
{ {
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
return wxSystemSettings::GetMetric(wxSYS_CURSOR_X, win); return wxSystemSettings::GetMetric(wxSYS_CURSOR_X, win);
} }
wxCoord wxCursorRefData::GetStandardHeight() wxCoord wxCursorRefData::GetStandardHeight()
{ {
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
return wxSystemSettings::GetMetric(wxSYS_CURSOR_Y, win); return wxSystemSettings::GetMetric(wxSYS_CURSOR_Y, win);
} }

View File

@@ -471,7 +471,7 @@ bool wxICOFileHandler::LoadIcon(wxIcon *icon,
else else
#endif #endif
// were we asked for a large icon? // 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) && if ( desiredWidth == wxGetSystemMetrics(SM_CXICON, win) &&
desiredHeight == wxGetSystemMetrics(SM_CYICON, win) ) desiredHeight == wxGetSystemMetrics(SM_CYICON, win) )
{ {
@@ -666,7 +666,7 @@ wxSize wxGetHiconSize(HICON hicon)
if ( !size.x ) if ( !size.x )
{ {
// use default icon size on this hardware // 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.x = wxGetSystemMetrics(SM_CXICON, win);
size.y = wxGetSystemMetrics(SM_CYICON, win); size.y = wxGetSystemMetrics(SM_CYICON, win);
} }

View File

@@ -69,8 +69,8 @@ HTMLHELP GetHtmlHelpFunction()
// fall back to the top level app window and then the desktop if it's NULL // fall back to the top level app window and then the desktop if it's NULL
static HWND GetSuitableHWND(wxWindow *win) static HWND GetSuitableHWND(wxWindow *win)
{ {
if ( !win && wxTheApp ) if ( !win )
win = wxTheApp->GetTopWindow(); win = wxApp::GetMainTopWindow();
return win ? GetHwndOf(win) : ::GetDesktopWindow(); return win ? GetHwndOf(win) : ::GetDesktopWindow();
} }

View File

@@ -393,7 +393,7 @@ void wxMessageDialog::AdjustButtonLabels()
/* static */ /* static */
wxFont wxMessageDialog::GetMessageFont() wxFont wxMessageDialog::GetMessageFont()
{ {
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
const wxNativeFontInfo const wxNativeFontInfo
info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win);

View File

@@ -179,7 +179,7 @@ wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
// for most (simple) controls, e.g. buttons and such but other // for most (simple) controls, e.g. buttons and such but other
// controls may prefer to use lfStatusFont or lfCaptionFont if it // controls may prefer to use lfStatusFont or lfCaptionFont if it
// is more appropriate for them // is more appropriate for them
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
const wxNativeFontInfo const wxNativeFontInfo
info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); 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 // font which is also used for the icon titles and not the stock default
// GUI font // GUI font
LOGFONT lf; LOGFONT lf;
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxApp::GetMainTopWindow();
if ( wxSystemParametersInfo if ( wxSystemParametersInfo
( (
SPI_GETICONTITLELOGFONT, SPI_GETICONTITLELOGFONT,

View File

@@ -4236,7 +4236,7 @@ bool wxWindowMSW::HandleEndSession(bool endSession, long logOff)
return false; return false;
// only send once // only send once
if ( (this != wxTheApp->GetTopWindow()) ) if ( this != wxApp::GetMainTopWindow() )
return false; return false;
wxCloseEvent event(wxEVT_END_SESSION, wxID_ANY); wxCloseEvent event(wxEVT_END_SESSION, wxID_ANY);
@@ -4773,10 +4773,11 @@ static wxSize GetWindowDPI(HWND hwnd)
} }
/*extern*/ /*extern*/
int wxGetSystemMetrics(int nIndex, const wxWindow* win) int wxGetSystemMetrics(int nIndex, const wxWindow* window)
{ {
#if wxUSE_DYNLIB_CLASS #if wxUSE_DYNLIB_CLASS
const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win; if ( !window )
window = wxApp::GetMainTopWindow();
if ( window ) if ( window )
{ {
@@ -4798,14 +4799,14 @@ int wxGetSystemMetrics(int nIndex, const wxWindow* win)
} }
} }
#else #else
wxUnusedVar(win); wxUnusedVar(window);
#endif // wxUSE_DYNLIB_CLASS #endif // wxUSE_DYNLIB_CLASS
return ::GetSystemMetrics(nIndex); return ::GetSystemMetrics(nIndex);
} }
/*extern*/ /*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 // 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 // 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 // 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. // of doing all this, just don't use it at all in the deprecated ANSI build.
#if wxUSE_DYNLIB_CLASS && wxUSE_UNICODE #if wxUSE_DYNLIB_CLASS && wxUSE_UNICODE
const wxWindow* window = (!win && wxTheApp) ? wxTheApp->GetTopWindow() : win; if ( !window )
window = wxApp::GetMainTopWindow();
if ( window ) if ( window )
{ {
@@ -4838,7 +4840,7 @@ bool wxSystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWi
} }
} }
#else #else
wxUnusedVar(win); wxUnusedVar(window);
#endif // wxUSE_DYNLIB_CLASS #endif // wxUSE_DYNLIB_CLASS
return ::SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni) == TRUE; return ::SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni) == TRUE;