diff options
author | Sergios - Anestis Kefalidis <sergioskefalidis@gmail.com> | 2021-12-26 20:55:39 +0200 |
---|---|---|
committer | Sergios - Anestis Kefalidis <sergioskefalidis@gmail.com> | 2021-12-26 20:55:39 +0200 |
commit | 96b922e3935f4a88ae1b0ab30a5ab96beb56f936 (patch) | |
tree | 02f6b7e068a2597dd70749e05a5d807333512230 | |
parent | 17b0d557d620fd63cedfe0bbc9abb453d5cfcdd0 (diff) | |
download | libxfce4ui-96b922e3935f4a88ae1b0ab30a5ab96beb56f936.tar.gz |
Add functions for handling accelerators that use the Tab key
GTK+ does not allow the use of the Tab key as an accelerator. xfce_gtk_handle_tab_accels and xfce_gtk_execute_tab_accel can
be used to manually trigger the callback of an action that uses the Tab key as an accelerator.
-rw-r--r-- | docs/reference/libxfce4ui-sections.txt | 2 | ||||
-rw-r--r-- | libxfce4ui/libxfce4ui.symbols | 2 | ||||
-rw-r--r-- | libxfce4ui/xfce-gtk-extensions.c | 76 | ||||
-rw-r--r-- | libxfce4ui/xfce-gtk-extensions.h | 10 |
4 files changed, 90 insertions, 0 deletions
diff --git a/docs/reference/libxfce4ui-sections.txt b/docs/reference/libxfce4ui-sections.txt index f385238..607eb3a 100644 --- a/docs/reference/libxfce4ui-sections.txt +++ b/docs/reference/libxfce4ui-sections.txt @@ -46,6 +46,8 @@ xfce_gtk_accel_group_connect_action_entries xfce_gtk_accel_group_disconnect_action_entries xfce_gtk_get_action_entry_by_id xfce_gtk_translate_action_entries +xfce_gtk_handle_tab_accels +xfce_gtk_execute_tab_accel xfce_gtk_menu_append_seperator xfce_gtk_button_new_mixed xfce_gtk_frame_box_new diff --git a/libxfce4ui/libxfce4ui.symbols b/libxfce4ui/libxfce4ui.symbols index 6b26b85..d62a1a4 100644 --- a/libxfce4ui/libxfce4ui.symbols +++ b/libxfce4ui/libxfce4ui.symbols @@ -95,6 +95,8 @@ xfce_gtk_accel_group_connect_action_entries xfce_gtk_accel_group_disconnect_action_entries xfce_gtk_get_action_entry_by_id xfce_gtk_translate_action_entries +xfce_gtk_handle_tab_accels +xfce_gtk_execute_tab_accel xfce_gtk_menu_append_seperator xfce_gtk_button_new_mixed G_GNUC_MALLOC xfce_gtk_frame_box_new G_GNUC_MALLOC diff --git a/libxfce4ui/xfce-gtk-extensions.c b/libxfce4ui/xfce-gtk-extensions.c index c3faca6..b712d39 100644 --- a/libxfce4ui/xfce-gtk-extensions.c +++ b/libxfce4ui/xfce-gtk-extensions.c @@ -556,6 +556,82 @@ xfce_gtk_translate_action_entries (XfceGtkActionEntry *action_entries, /** + * xfce_gtk_handle_tab_accels + * @key_event : the #GdkEventKey that might trigger a shortcut + * @accel_group : the #GtkAccelGroup that will be get queried + * @data : a pointer of data that will be passed to the callback if a tab-shortcut is found + * @entries : a #XfceGtkActionEntry[] + * @entry_count : the number of entries in @entries + * + * The Tab key is used to navigate the interface by GTK+ so we need to handle shortcuts with the Tab accelerator manually. + * Tab sometimes becomes ISO_Left_Tab (e.g. in Ctrl+Shift+Tab) so check both here. + * + * Return value: a boolean that is TRUE if the event was handled, otherwise it is FALSE +**/ +gboolean +xfce_gtk_handle_tab_accels (GdkEventKey *key_event, + GtkAccelGroup *accel_group, + gpointer data, + XfceGtkActionEntry *entries, + size_t entry_count) +{ + const guint modifiers = key_event->state & gtk_accelerator_get_default_mod_mask (); + + if (G_UNLIKELY (key_event->keyval == GDK_KEY_Tab || key_event->keyval == GDK_KEY_ISO_Left_Tab) && key_event->type == GDK_KEY_PRESS) + { + GtkAccelGroupEntry *group_entries; + guint group_entries_count; + + group_entries = gtk_accel_group_query (accel_group, key_event->keyval, modifiers, &group_entries_count); + if (group_entries_count > 1) + { + g_error ("Found multiple shortcuts that include the Tab key and the same modifiers."); + } + else if (group_entries_count == 1) + { + const gchar *path = g_quark_to_string (group_entries[0].accel_path_quark); + return xfce_gtk_execute_tab_accel (path, data, entries, entry_count); + } + } + + return GDK_EVENT_PROPAGATE; +} + + + +/** + * xfce_gtk_execute_tab_accel + * @accel_path : the accelerator path of the action that we want to activate + * @data : a pointer of data that will be passed to the callback if a tab-shortcut is found + * @entries : a #XfceGtkActionEntry[] + * @entry_count : the number of entries in @entries + * + * Activates the callback function of the #XfceGtkActionEntry that corresponds to @accel_path. If no such action + * exists in @entries, then nothing happens. + * + * Return value: a boolean that is TRUE if the action was found, otherwise it is FALSE +**/ +gboolean +xfce_gtk_execute_tab_accel (const gchar *accel_path, + gpointer data, + XfceGtkActionEntry *entries, + size_t entry_count) +{ + for (size_t i = 0; i < entry_count; i++) + { + if (g_strcmp0 (accel_path, entries[i].accel_path) == 0) + { + ((void (*) (void *)) entries[i].callback) (data); + return GDK_EVENT_STOP; + } + } + + return GDK_EVENT_PROPAGATE; +} + + + +/** * xfce_gtk_button_new_mixed: * @stock_id : the name of the stock item. * @label : the text of the button, with an underscore in front of diff --git a/libxfce4ui/xfce-gtk-extensions.h b/libxfce4ui/xfce-gtk-extensions.h index c564ef7..11e6a1f 100644 --- a/libxfce4ui/xfce-gtk-extensions.h +++ b/libxfce4ui/xfce-gtk-extensions.h @@ -127,6 +127,16 @@ const XfceGtkActionEntry *xfce_gtk_get_action_entry_by_id (const guint id); void xfce_gtk_translate_action_entries (XfceGtkActionEntry *action_entries, guint n_action_entries); +gboolean xfce_gtk_handle_tab_accels (GdkEventKey *key_event, + GtkAccelGroup *accel_group, + gpointer data, + XfceGtkActionEntry *entries, + size_t entry_count); + +gboolean xfce_gtk_execute_tab_accel (const gchar *accel_path, + gpointer data, + XfceGtkActionEntry *entries, + size_t entry_count); void xfce_gtk_menu_append_seperator (GtkMenuShell *menu); GtkWidget *xfce_gtk_button_new_mixed (const gchar *stock_id, |