From cfc91b62b0d508ad1b763e76f81c260b20376172 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 14 Mar 2021 19:37:11 -0400 Subject: [PATCH] window: Add a way to disable F10 shortcut This is needed for terminal emulators. Fixes: #3727 --- gtk/gtkwindow.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtkwindow.h | 6 ++++ 2 files changed, 84 insertions(+) diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c index 4b03d18044..2aa6053494 100644 --- a/gtk/gtkwindow.c +++ b/gtk/gtkwindow.c @@ -246,6 +246,8 @@ typedef struct int surface_height; GdkCursor *resize_cursor; + + GtkEventController *menubar_controller; } GtkWindowPrivate; enum { @@ -278,6 +280,7 @@ enum { PROP_DEFAULT_WIDGET, PROP_FOCUS_WIDGET, PROP_CHILD, + PROP_HANDLE_MENUBAR_ACCEL, /* Readonly properties */ PROP_IS_ACTIVE, @@ -1002,6 +1005,20 @@ gtk_window_class_init (GtkWindowClass *klass) GTK_TYPE_WIDGET, GTK_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY); + /** + * GtkWindow:handle-menubar-accel: (attributes org.gtk.Property.get=gtk_window_get_handle_menubar_accel org.gtk.Property.set=gtk_window_set_handle_menubar_accel) + * + * Whether the window frame should handle F10 for activating + * menubars. + * + * Since: 4.2 + */ + window_props[PROP_HANDLE_MENUBAR_ACCEL] = + g_param_spec_boolean ("handle-menubar-accel", + P_("Handle Menubar accels"), + P_("Whether the window should handle F10"), + TRUE, + GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); g_object_class_install_properties (gobject_class, LAST_ARG, window_props); @@ -1656,6 +1673,8 @@ gtk_window_init (GtkWindow *window) gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut); gtk_event_controller_set_name (controller, "gtk-window-menubar-accel"); gtk_widget_add_controller (widget, controller); + + priv->menubar_controller = controller; } static void @@ -1768,6 +1787,9 @@ gtk_window_set_property (GObject *object, case PROP_CHILD: gtk_window_set_child (window, g_value_get_object (value)); break; + case PROP_HANDLE_MENUBAR_ACCEL: + gtk_window_set_handle_menubar_accel (window, g_value_get_boolean (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -1848,6 +1870,9 @@ gtk_window_get_property (GObject *object, case PROP_CHILD: g_value_set_object (value, gtk_window_get_child (window)); break; + case PROP_HANDLE_MENUBAR_ACCEL: + g_value_set_boolean (value, gtk_window_get_handle_menubar_accel (window)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -6721,3 +6746,56 @@ gtk_window_grab_notify (GtkWindow *window, from_grab); } } + +/** + * gtk_window_set_handle_menubar_accel: (attributes org.gtk.Method.set_property=handle-menubar-accel) + * @window: a #GtkWindow + * @handle_menubar_accel: %TRUE to make @window handle F10 + * + * Sets whether this window should react to F10 key presses + * by activating a menubar it contains. + * + * Since: 4.2 + */ +void +gtk_window_set_handle_menubar_accel (GtkWindow *window, + gboolean handle_menubar_accel) +{ + GtkWindowPrivate *priv = gtk_window_get_instance_private (window); + GtkPropagationPhase phase; + + g_return_if_fail (GTK_IS_WINDOW (window)); + + phase = handle_menubar_accel ? GTK_PHASE_CAPTURE : GTK_PHASE_NONE; + + if (gtk_event_controller_get_propagation_phase (priv->menubar_controller) == phase) + return; + + gtk_event_controller_set_propagation_phase (priv->menubar_controller, phase); + + g_object_notify_by_pspec (G_OBJECT (window), window_props[PROP_HANDLE_MENUBAR_ACCEL]); +} + +/** + * gtk_window_get_handle_menubar_accel: (attributes org.gtk.Method.get_property=handle-menubar-accel) + * @window: a #GtkWindow + * + * Returns whether this window reacts to F10 key presses by + * activating a menubar it contains. + * + * Returns: %TRUE if the window handles F10 + * + * Since: 4.2 + */ +gboolean +gtk_window_get_handle_menubar_accel (GtkWindow *window) +{ + GtkWindowPrivate *priv = gtk_window_get_instance_private (window); + GtkPropagationPhase phase; + + g_return_val_if_fail (GTK_IS_WINDOW (window), TRUE); + + phase = gtk_event_controller_get_propagation_phase (priv->menubar_controller); + + return phase == GTK_PHASE_CAPTURE; +} diff --git a/gtk/gtkwindow.h b/gtk/gtkwindow.h index 347eee2312..a7e9a5bc4f 100644 --- a/gtk/gtkwindow.h +++ b/gtk/gtkwindow.h @@ -251,6 +251,12 @@ void gtk_window_destroy (GtkWindow *window); GDK_AVAILABLE_IN_ALL void gtk_window_set_interactive_debugging (gboolean enable); +GDK_AVAILABLE_IN_4_2 +void gtk_window_set_handle_menubar_accel (GtkWindow *window, + gboolean handle_menubar_accel); +GDK_AVAILABLE_IN_4_2 +gboolean gtk_window_get_handle_menubar_accel (GtkWindow *window); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkWindow, g_object_unref) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkWindowGroup, g_object_unref)