support Wayland GTK backend in Window.GetHandle

This adds support for getting a Wayland `wl_surface` from
`Window.GetHandle`. The behavior of the function is now to return either
an X11 window ID or Wayland `wl_surface` if running a GTK build on those
backends, return 0 on any other GTK backend, or else return the C++
`GetHandle` result unmodified. This is the simplest fix for the Wayland
issue without worrying about breaking existing user code, as this
function already failed for any non-X11 GTK build. Returning 0 on
non-X11/Wayland GTK platforms sidesteps the issue of making sure e.g.
GTK Mac GetHandle returns the same type as native Cocoa Mac GetHandle.
This commit is contained in:
unawarez
2024-06-12 18:19:22 -05:00
parent 78938da121
commit a7b3401ed5
2 changed files with 48 additions and 26 deletions

View File

@@ -136,6 +136,16 @@ def run():
m1.find('externalLeading').out = True
c.find('GetHandle').type = 'wxUIntPtr*'
c.find('GetHandle').detailedDoc = [
"""The returned value differs from the C++ version of GetHandle when \
running on the GTK port. When running on Wayland with GTK, this \
function returns a `wl_surface` pointer for the native OS window \
containing the widget. On X11 with GTK, this returns the X window \
ID for the containing window. On any other backend with GTK, this \
function returns 0.\n\n"""
"""On some platforms this may return 0 if the window has not yet been shown."""
]
c.find('GetHandle').setCppCode("return new wxUIntPtr(wxPyGetWinHandle(self));")
c.addCppMethod('void*', 'GetGtkWidget', '()', """\

View File

@@ -1,32 +1,15 @@
#ifdef __WXMSW__
#include <wx/msw/private.h>
#endif
#ifdef __WXGTK__
#include <gdk/gdkx.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#ifdef __WXGTK3__
// Unlike GDK_WINDOW_XWINDOW, GDK_WINDOW_XID can't handle a NULL, so check 1st
static XID GetXWindow(const wxWindow* wxwin) {
if ((wxwin)->m_wxwindow) {
if (gtk_widget_get_window((wxwin)->m_wxwindow))
return GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_wxwindow));
return 0;
}
else {
if (gtk_widget_get_window((wxwin)->m_widget))
return GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_widget));
return 0;
}
}
#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
GDK_WINDOW_XWINDOW((wxwin)->m_wxwindow->window) : \
GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
#endif // ifdef __WXGTK__
@@ -36,11 +19,40 @@ wxUIntPtr wxPyGetWinHandle(const wxWindow* win)
#ifdef __WXMSW__
return (wxUIntPtr)win->GetHandle();
#endif
#if defined(__WXGTK__) || defined(__WXX11__)
return (wxUIntPtr)GetXWindow(win);
#ifdef __WXX11__
return (wxUIntPtr)win->GetHandle();
#endif
#ifdef __WXMAC__
return (wxUIntPtr)win->GetHandle();
#endif
#ifdef __WXGTK__
GtkWidget *gtk_widget = win->GetHandle();
if (!gtk_widget) {
return 0;
};
// gtk_widget_get_window disappears in GTK4; then it will be via
// gtk_widget_get_native() -> gtk_native_get_surface().
GdkWindow *window = gtk_widget_get_window(gtk_widget);
if (!window) {
return 0;
}
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_WINDOW(window)) {
return (wxUIntPtr)gdk_x11_window_get_xid(window);
}
#endif
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_WINDOW(window)) {
return (wxUIntPtr)gdk_wayland_window_get_wl_surface(window);
}
#endif
// Returning 0 on any other backend using GTK.
// This is less confusing than returning something else that might
// mismatch C++ GetHandle.
#endif
return 0;
}