diff --git a/include/wx/window.h b/include/wx/window.h index d8a00f199f..c9b8869895 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -2100,6 +2100,9 @@ extern WXDLLIMPEXP_CORE wxWindow *wxGetActiveWindow(); // get the (first) top level parent window WXDLLIMPEXP_CORE wxWindow* wxGetTopLevelParent(wxWindowBase *win); +// Return a string with platform-dependent description of the window. +extern WXDLLIMPEXP_CORE wxString wxDumpWindow(wxWindowBase* win); + #if wxUSE_ACCESSIBILITY // ---------------------------------------------------------------------------- // accessible object for windows diff --git a/interface/wx/window.h b/interface/wx/window.h index 2918be982a..93186d4189 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -4251,5 +4251,22 @@ wxWindow* wxGetActiveWindow(); */ wxWindow* wxGetTopLevelParent(wxWindow* window); +/** + Return a string with human-readable platform-specific description of + the window useful for diagnostic purposes. + + The string returned from this function doesn't have any fixed form and can + vary between different wxWidgets ports and versions, but contains some + useful description of the window and uniquely identifies it. This can be + useful to include in debug or tracing messages. + + @param window Window pointer which is allowed to be @NULL. + + @header{wx/window.h} + + @since 3.1.6 + */ +wxString wxDumpWindow(wxWindow* window); + //@} diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 42621feea4..2d1e13bb32 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -3661,6 +3661,31 @@ wxWindow* wxGetTopLevelParent(wxWindowBase *win_) return win; } +wxString wxDumpWindow(wxWindowBase* win) +{ + if ( !win ) + return wxString::FromAscii("[no window]"); + + wxString s = wxString::Format("%s@%p (", + win->GetClassInfo()->GetClassName(), win); + + wxString label = win->GetLabel(); + if ( label.empty() ) + label = win->GetName(); + + s += wxString::Format("\"%s\"", label); + + // Under MSW the HWND can be useful to find the window in Spy++ or similar + // program, so include it in the dump as well. +#ifdef __WXMSW__ + s += wxString::Format(", HWND=%p", win->GetHandle()); +#endif // __WXMSW__ + + s += ")"; + + return s; +} + #if wxUSE_ACCESSIBILITY // ---------------------------------------------------------------------------- // accessible object for windows diff --git a/src/gtk/window.cpp b/src/gtk/window.cpp index b9fc42d760..dbee540948 100644 --- a/src/gtk/window.cpp +++ b/src/gtk/window.cpp @@ -309,26 +309,6 @@ static wxPoint gs_lastGesturePoint; // the trace mask used for the focus debugging messages #define TRACE_FOCUS wxT("focus") -#if wxUSE_LOG_TRACE -// Function used to dump a brief description of a window. -static -wxString wxDumpWindow(wxWindowGTK* win) -{ - if ( !win ) - return "(no window)"; - - wxString s = wxString::Format("%s(%p", - win->GetClassInfo()->GetClassName(), win); - - wxString label = win->GetLabel(); - if ( !label.empty() ) - s += wxString::Format(", \"%s\"", label); - s += ")"; - - return s; -} -#endif // wxUSE_LOG_TRACE - // A handy function to run from under gdb to show information about the given // GtkWidget. Right now it only shows its type, we could enhance it to show // more information later but this is already pretty useful. diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 7f34361392..537d091595 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -51,9 +51,6 @@ // debugging helpers // ---------------------------------------------------------------------------- -// This one is defined in window_osx.cpp. -extern wxString wxDumpWindow(wxWindowMac* win); - // These functions are called from the code but are also useful in the debugger // (especially wxDumpNSView(), as selectors can be printed out directly anyhow), // so make them just static instead of putting them in an anonymous namespace diff --git a/src/osx/window_osx.cpp b/src/osx/window_osx.cpp index d03e3b2467..99cbfe1c7c 100644 --- a/src/osx/window_osx.cpp +++ b/src/osx/window_osx.cpp @@ -181,28 +181,6 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxBlindPlateWindow, wxWindow); wxBEGIN_EVENT_TABLE(wxBlindPlateWindow, wxWindow) wxEND_EVENT_TABLE() -// ---------------------------------------------------------------------------- -// debug helpers -// ---------------------------------------------------------------------------- - -// Function used to dump a brief description of a window. -extern -wxString wxDumpWindow(wxWindowMac* win) -{ - if ( !win ) - return "(no window)"; - - wxString s = wxString::Format("%s(%p", - win->GetClassInfo()->GetClassName(), win); - - wxString label = win->GetLabel(); - if ( !label.empty() ) - s += wxString::Format(", \"%s\"", label); - s += ")"; - - return s; -} - // ---------------------------------------------------------------------------- // constructors and such // ---------------------------------------------------------------------------- diff --git a/tests/misc/guifuncs.cpp b/tests/misc/guifuncs.cpp index c47b941d6a..1461a03147 100644 --- a/tests/misc/guifuncs.cpp +++ b/tests/misc/guifuncs.cpp @@ -201,3 +201,16 @@ TEST_CASE("GUI::FindWindowAtPoint", "[guifuncs]") INFO("Point over disabled child controls still corresponds to this child"); CHECK( GetLabelOfWindowAtPoint(parent, 31, 111) == btn3->GetLabel() ); } + +TEST_CASE("wxWindow::Dump", "[window]") +{ + CHECK_NOTHROW( wxDumpWindow(NULL) ); + + wxScopedPtr + button(new wxButton(wxTheApp->GetTopWindow(), wxID_ANY, "bloordyblop")); + + const std::string s = wxDumpWindow(button.get()).utf8_string(); + + CHECK_THAT( s, Catch::Contains("wxButton") ); + CHECK_THAT( s, Catch::Contains("bloordyblop") ); +}