From d7cc988db0bde72363f30258e2b5cb769da3564e Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 21 Jul 2022 10:55:29 +0300 Subject: [PATCH 51/51] gtk4: Implement focus enter/leave events See osdn #45181 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/gui_main.c | 15 ++++++++++----- client/gui-gtk-4.0/gui_main.h | 6 ++++-- client/gui-gtk-4.0/gui_stuff.c | 13 +++++++++---- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index 6366fc3e4d..a5b98df083 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -758,7 +758,8 @@ static void move_from_container_to_container(GtkWidget *wdg, /**********************************************************************//** Freeciv window has lost focus **************************************************************************/ -gboolean fc_lost_focus(GtkWidget *w, GdkEvent *ev, gpointer data) +gboolean fc_lost_focus(GtkEventControllerFocus *controller, + gpointer data) { client_focus = FALSE; @@ -768,7 +769,8 @@ gboolean fc_lost_focus(GtkWidget *w, GdkEvent *ev, gpointer data) /**********************************************************************//** Freeciv window has gained focus **************************************************************************/ -gboolean fc_gained_focus(GtkWidget *w, GdkEvent *ev, gpointer data) +gboolean fc_gained_focus(GtkEventControllerFocus *controller, + gpointer data) { client_focus = TRUE; @@ -2009,6 +2011,7 @@ static void activate_gui(GtkApplication *app, gpointer data) { PangoFontDescription *toplevel_font_name; guint sig; + GtkEventController *controller; toplevel = gtk_application_window_new(app); if (vmode.width > 0 && vmode.height > 0) { @@ -2016,10 +2019,12 @@ static void activate_gui(GtkApplication *app, gpointer data) vmode.width, vmode.height); } - g_signal_connect(toplevel, "focus_out_event", - G_CALLBACK(fc_lost_focus), NULL); - g_signal_connect(toplevel, "focus_in_event", + controller = GTK_EVENT_CONTROLLER(gtk_event_controller_focus_new()); + g_signal_connect(controller, "enter", G_CALLBACK(fc_gained_focus), NULL); + g_signal_connect(controller, "leave", + G_CALLBACK(fc_lost_focus), NULL); + gtk_widget_add_controller(toplevel, controller); gtk_widget_realize(toplevel); gtk_widget_set_name(toplevel, "Freeciv"); diff --git a/client/gui-gtk-4.0/gui_main.h b/client/gui-gtk-4.0/gui_main.h index ec3e112f0d..722a61c041 100644 --- a/client/gui-gtk-4.0/gui_main.h +++ b/client/gui-gtk-4.0/gui_main.h @@ -82,7 +82,9 @@ bool is_gui_up(void); bool terminate_signal_processing(void); -gboolean fc_lost_focus(GtkWidget *w, GdkEvent *ev, gpointer data); -gboolean fc_gained_focus(GtkWidget *w, GdkEvent *ev, gpointer data); +gboolean fc_lost_focus(GtkEventControllerFocus *controller, + gpointer data); +gboolean fc_gained_focus(GtkEventControllerFocus *controller, + gpointer data); #endif /* FC__GUI_MAIN_H */ diff --git a/client/gui-gtk-4.0/gui_stuff.c b/client/gui-gtk-4.0/gui_stuff.c index 8d79b32a74..bb1fac4ee8 100644 --- a/client/gui-gtk-4.0/gui_stuff.c +++ b/client/gui-gtk-4.0/gui_stuff.c @@ -278,10 +278,15 @@ void setup_dialog(GtkWidget *shell, GtkWidget *parent) /* Close dialog window on Escape keypress. */ if (GTK_IS_DIALOG(shell)) { - g_signal_connect(shell, "focus_out_event", - G_CALLBACK(fc_lost_focus), NULL); - g_signal_connect(shell, "focus_in_event", - G_CALLBACK(fc_gained_focus), NULL); + GtkEventController *controller; + + controller = GTK_EVENT_CONTROLLER(gtk_event_controller_focus_new()); + g_signal_connect(controller, "enter", + G_CALLBACK(fc_gained_focus), NULL); + g_signal_connect(controller, "leave", + G_CALLBACK(fc_lost_focus), NULL); + gtk_widget_add_controller(shell, controller); + g_signal_connect_after(shell, "close", G_CALLBACK(close_callback), shell); } } -- 2.35.1