Skip to content

Commit

Permalink
Merge pull request #2561 from unawarez/master
Browse files Browse the repository at this point in the history
Add Wayland support for Window.GetHandle
  • Loading branch information
swt2c authored Oct 3, 2024
2 parents 31b687f + a7b3401 commit b6b28ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
10 changes: 10 additions & 0 deletions etg/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '()', """\
Expand Down
64 changes: 38 additions & 26 deletions src/window_ex.cpp
Original file line number Diff line number Diff line change
@@ -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__



Expand All @@ -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;
}

0 comments on commit b6b28ee

Please sign in to comment.