From b717d0c8d50cca18325ab9f2e2cba49e34d02323 Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Thu, 25 Jul 2019 10:35:12 -0700 Subject: [PATCH] Add GTK-specific implementation of wxDisplay::GetFromWindow() The generic implementation indirectly uses ClientToScreen(), which won't work with Wayland, and causes debug warnings when used before window is realized. --- src/gtk/display.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/gtk/display.cpp b/src/gtk/display.cpp index 7ab2c8d36b..b1bdacb196 100644 --- a/src/gtk/display.cpp +++ b/src/gtk/display.cpp @@ -9,6 +9,9 @@ #include "wx/wxprec.h" #include "wx/private/display.h" +#if wxUSE_DISPLAY + #include "wx/window.h" +#endif #include "wx/gtk/private/wrapgtk.h" #ifdef GDK_WINDOWING_X11 @@ -67,6 +70,7 @@ public: virtual wxDisplayImpl* CreateDisplay(unsigned n) wxOVERRIDE; virtual unsigned GetCount() wxOVERRIDE; virtual int GetFromPoint(const wxPoint& pt) wxOVERRIDE; + virtual int GetFromWindow(const wxWindow* win) wxOVERRIDE; }; wxDisplayImpl* wxDisplayFactoryGTK::CreateDisplay(unsigned n) @@ -95,6 +99,26 @@ int wxDisplayFactoryGTK::GetFromPoint(const wxPoint& pt) } return wxNOT_FOUND; } + +int wxDisplayFactoryGTK::GetFromWindow(const wxWindow* win) +{ + if (win && win->m_widget) + { + GdkDisplay* display = gtk_widget_get_display(win->m_widget); + GdkMonitor* monitor; + if (GdkWindow* window = gtk_widget_get_window(win->m_widget)) + monitor = gdk_display_get_monitor_at_window(display, window); + else + monitor = gdk_display_get_primary_monitor(display); + + for (unsigned i = gdk_display_get_n_monitors(display); i--;) + { + if (gdk_display_get_monitor(display, i) == monitor) + return i; + } + } + return wxNOT_FOUND; +} #endif // wxUSE_DISPLAY wxDisplayImplGTK::wxDisplayImplGTK(unsigned i) @@ -251,6 +275,7 @@ public: virtual wxDisplayImpl* CreateDisplay(unsigned n) wxOVERRIDE; virtual unsigned GetCount() wxOVERRIDE; virtual int GetFromPoint(const wxPoint& pt) wxOVERRIDE; + virtual int GetFromWindow(const wxWindow* win) wxOVERRIDE; }; wxGCC_WARNING_SUPPRESS(deprecated-declarations) @@ -275,6 +300,20 @@ int wxDisplayFactoryGTK::GetFromPoint(const wxPoint& pt) monitor = wxNOT_FOUND; return monitor; } + +int wxDisplayFactoryGTK::GetFromWindow(const wxWindow* win) +{ + int monitor = wxNOT_FOUND; + if (win && win->m_widget) + { + GdkScreen* screen = gtk_widget_get_screen(win->m_widget); + if (GdkWindow* window = gtk_widget_get_window(win->m_widget)) + monitor = gdk_screen_get_monitor_at_window(screen, window); + else + monitor = gdk_screen_get_primary_monitor(screen); + } + return monitor; +} #endif // wxUSE_DISPLAY wxDisplayImplGTK::wxDisplayImplGTK(unsigned i)