From 1cad6badb795df1b83440261b528108cfc992bfd Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 7 Oct 2023 23:14:16 +0300 Subject: [PATCH 63/63] gtk4: Disable unit commands while inputline in focus This prevents units from taking attempts to write chat messages as commands to themselves. See osdn #48815 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/chatline.c | 30 ++++++++++++++++++++++++++++++ client/gui-gtk-4.0/menu.c | 11 ++++++++++- client/gui-gtk-4.0/menu.h | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/client/gui-gtk-4.0/chatline.c b/client/gui-gtk-4.0/chatline.c index 65a9b1fa6d..682480bbdb 100644 --- a/client/gui-gtk-4.0/chatline.c +++ b/client/gui-gtk-4.0/chatline.c @@ -46,6 +46,7 @@ #include "colors.h" #include "gui_main.h" #include "gui_stuff.h" +#include "menu.h" #include "pages.h" #include "chatline.h" @@ -90,6 +91,28 @@ bool inputline_is_visible(void) return gtk_widget_get_mapped(toolkit.entry); } +/**********************************************************************//** + Inputline has lost focus +**************************************************************************/ +static gboolean il_lost_focus(GtkEventControllerFocus *controller, + gpointer data) +{ + real_menus_update(); + + return TRUE; +} + +/**********************************************************************//** + Inputline has gained focus +**************************************************************************/ +static gboolean il_gained_focus(GtkEventControllerFocus *controller, + gpointer data) +{ + menus_disable_unit_commands(); + + return TRUE; +} + /**********************************************************************//** Helper function to determine if a given client input line is intended as a "plain" public message. Note that messages prefixed with : are a @@ -1322,6 +1345,7 @@ void chatline_init(void) GdkRGBA color; int grid_col = 0; GtkEventController *chat_controller; + GtkEventController *focus_controller; /* Chatline history. */ if (!history_list) { @@ -1344,6 +1368,12 @@ void chatline_init(void) gtk_widget_set_margin_start(entry, 2); gtk_widget_set_margin_top(entry, 2); gtk_widget_set_hexpand(entry, TRUE); + focus_controller = GTK_EVENT_CONTROLLER(gtk_event_controller_focus_new()); + g_signal_connect(focus_controller, "enter", + G_CALLBACK(il_gained_focus), NULL); + g_signal_connect(focus_controller, "leave", + G_CALLBACK(il_lost_focus), NULL); + gtk_widget_add_controller(entry, focus_controller); toolkit.entry = entry; hgrid = gtk_grid_new(); diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c index 6aeb4feded..3b68bda923 100644 --- a/client/gui-gtk-4.0/menu.c +++ b/client/gui-gtk-4.0/menu.c @@ -3424,6 +3424,7 @@ void real_menus_update(void) i = 0; j = 0; + /* Add the new action entries grouped by target kind. */ for (tgt_kind_group = 0; tgt_kind_group < ATK_COUNT; tgt_kind_group++) { action_noninternal_iterate(act_id) { @@ -3799,7 +3800,7 @@ void real_menus_update(void) can_units_do_activity_client(punits, ACTIVITY_TRANSFORM)); menu_entry_set_sensitive(map, "FORTIFY", can_units_do_activity_client(punits, - ACTIVITY_FORTIFYING)); + ACTIVITY_FORTIFYING)); menu_entry_set_sensitive(map, "PARADROP", can_units_do(punits, can_unit_paradrop)); menu_entry_set_sensitive(map, "PILLAGE", @@ -4139,3 +4140,11 @@ void enable_menus(bool enable) * been already created is not noticed. */ g_object_notify(G_OBJECT(gtk_settings_get_default()), "gtk-shell-shows-menubar"); } + +/**********************************************************************//** + Disable all unit related commands. +**************************************************************************/ +void menus_disable_unit_commands(void) +{ + menu_entry_group_set_sensitive(G_ACTION_MAP(gui_app()), MGROUP_UNIT, FALSE); +} diff --git a/client/gui-gtk-4.0/menu.h b/client/gui-gtk-4.0/menu.h index f2761b9d2b..88a5029369 100644 --- a/client/gui-gtk-4.0/menu.h +++ b/client/gui-gtk-4.0/menu.h @@ -21,5 +21,6 @@ void enable_menus(bool enable); void menus_set_initial_toggle_values(void); +void menus_disable_unit_commands(void); #endif /* FC__MENU_H */ -- 2.40.1