Skip to content

Commit 2d21ecb

Browse files
authored
GTK: Open the dialog window on the same GdkScreen as the parent (#145)
Setting the default GdkDisplay doesn't actually cause a newly opened dialog window to use it. The more correct way seems to be to make the dialog window use the same GdkScreen as the parent. Each GdkScreen maps to one GdkDisplay, so this guarantees that the dialog window uses the same GdkDisplay as the parent too.
1 parent 7020935 commit 2d21ecb

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

src/nfd_gtk.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,7 @@ gint RunDialogWithFocus(GtkDialog* dialog) {
384384
// Gets the GdkWindow from the given window handle. This function might fail even if parentWindow
385385
// is set correctly, since it calls some failable GDK functions. If it fails, it will return
386386
// nullptr. The caller is responsible for freeing ths returned GdkWindow, if not nullptr.
387-
GdkWindow* GetAllocNativeWindowHandle(const nfdwindowhandle_t& parentWindow,
388-
GdkDisplayManager*& outDisplayManager,
389-
GdkDisplay*& outDisplay) {
387+
GdkWindow* GetAllocNativeWindowHandle(const nfdwindowhandle_t& parentWindow) {
390388
switch (parentWindow.type) {
391389
#if defined(GDK_WINDOWING_X11)
392390
case NFD_WINDOW_HANDLE_TYPE_X11: {
@@ -434,8 +432,6 @@ GdkWindow* GetAllocNativeWindowHandle(const nfdwindowhandle_t& parentWindow,
434432
gdk_set_allowed_backends(NULL);
435433
}
436434
if (!x11_display) return nullptr;
437-
outDisplayManager = display_manager;
438-
outDisplay = x11_display;
439435
GdkWindow* gdk_window = gdk_x11_window_foreign_new_for_display(x11_display, x11_handle);
440436
return gdk_window;
441437
}
@@ -452,39 +448,29 @@ void RealizedSignalHandler(GtkWidget* window, void* userdata) {
452448

453449
struct NativeWindowParenter {
454450
NativeWindowParenter(GtkWidget* widget, const nfdwindowhandle_t& parentWindow) noexcept
455-
: widget(widget), displayManager(nullptr) {
456-
GdkDisplay* new_display = nullptr;
457-
parent = GetAllocNativeWindowHandle(parentWindow, displayManager, new_display);
451+
: widget(widget) {
452+
parent = GetAllocNativeWindowHandle(parentWindow);
458453

459454
if (parent) {
460-
/* set the handler to the realize signal to set the transient GDK parent */
455+
// set the handler to the realize signal to set the transient GDK parent
461456
handlerID = g_signal_connect(G_OBJECT(widget),
462457
"realize",
463458
G_CALLBACK(RealizedSignalHandler),
464459
static_cast<void*>(parent));
465460

466-
/* Set the default display to a display that we know is X11 (so that realizing the file
467-
* dialog will use it) */
468-
/* Note: displayManager here must be non-null since parent is non-null */
469-
originalDisplay = gdk_display_manager_get_default_display(displayManager);
470-
gdk_display_manager_set_default_display(displayManager, new_display);
461+
// make the dialog window use the same GtkScreen as the parent (so that parenting works)
462+
gtk_window_set_screen(GTK_WINDOW(widget), gdk_window_get_screen(parent));
471463
}
472464
}
473465
~NativeWindowParenter() {
474466
if (parent) {
475-
/* Set the default display back to whatever it was, to be nice */
476-
/* Note: displayManager here must be non-null since parent is non-null */
477-
gdk_display_manager_set_default_display(displayManager, originalDisplay);
478-
479-
/* unset the handler and delete the parent GdkWindow */
467+
// unset the handler and delete the parent GdkWindow
480468
g_signal_handler_disconnect(G_OBJECT(widget), handlerID);
481469
g_object_unref(parent);
482470
}
483471
}
484472
GtkWidget* const widget;
485473
GdkWindow* parent;
486-
GdkDisplayManager* displayManager;
487-
GdkDisplay* originalDisplay;
488474
gulong handlerID;
489475
};
490476

0 commit comments

Comments
 (0)