Fix using wxMemoryDC without a GUI wxApp instance

This used to work, at least in wxMSW, but stopped working after the
(perfectly valid, on their own) changes of 2508efdd6e (Initialize
wxMemoryDC with a default font, 2019-08-13), as this resulted in calling
wxApp::GetTopWindow() that can only be called from the GUI code.

Fix this by adding wxApp::GetGUIInstance() and using it in
GetMainTopWindow(), so that we only call GetTopWindow() if we actually
have a GUI wxApp object on which to call it.

Implement this in terms of a new virtual IsGUI() which seems slightly
better than, although roughly equivalent to, using wxDynamicCast().

Closes https://github.com/wxWidgets/wxWidgets/pull/2617

Closes #19343.
This commit is contained in:
Vadim Zeitlin
2021-12-12 17:05:58 +00:00
parent b9a1931394
commit e3ec9fb124
3 changed files with 51 additions and 4 deletions

View File

@@ -193,7 +193,7 @@ wxWindow* wxAppBase::GetTopWindow() const
/* static */
wxWindow* wxAppBase::GetMainTopWindow()
{
const wxAppBase* const app = static_cast<wxAppBase*>(GetInstance());
const wxAppBase* const app = GetGUIInstance();
return app ? app->GetTopWindow() : NULL;
}