diff --git a/gtk+-2/pinentry-gtk-2.c b/gtk+-2/pinentry-gtk-2.c --- a/gtk+-2/pinentry-gtk-2.c +++ b/gtk+-2/pinentry-gtk-2.c @@ -554,6 +554,45 @@ return button; } +static void +move_window_to_active_monitor (GtkWindow *win) +{ + GdkScreen *screen; + GdkWindow *active_window; + GdkRectangle rect; + int monitor, width, height, cx, cy; + + screen = gdk_screen_get_default (); + + /* No need to bother if there's only one monitor. */ + if (gdk_screen_get_n_monitors (screen) == 1) + return; + + active_window = gdk_screen_get_active_window (screen); + if (active_window) + { + monitor = gdk_screen_get_monitor_at_window (screen, active_window); + gdk_display_get_pointer (gdk_screen_get_display (screen), NULL, + &cx, &cy, NULL); + + /* We need to act only if the cursor is not on the same monitor + * than the focused window. */ + if (gdk_screen_get_monitor_at_point (screen, cx, cy) != monitor) + { + gdk_screen_get_monitor_geometry (screen, monitor, &rect); + + /* We should use GDK_GRAVITY_CENTER so that we don't have + * to care about the window's size, but I could not make it + * to work... */ + gtk_window_get_size (win, &width, &height); + gtk_window_move (win, + rect.x + rect.width / 2 - width / 2, + rect.y + rect.height / 2 - height / 2); + } + + g_object_unref (active_window); + } +} static GtkWidget * create_window (pinentry_t ctx) @@ -889,6 +928,7 @@ gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER); gtk_window_set_keep_above (GTK_WINDOW (win), TRUE); gtk_widget_show_all (win); + move_window_to_active_monitor (GTK_WINDOW (win)); gtk_window_present (GTK_WINDOW (win)); /* Make sure it has the focus. */ if (pinentry->timeout > 0)