[gtk+] Add gtk_application_get_actions_for_accel()
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+] Add gtk_application_get_actions_for_accel()
- Date: Sun, 3 Aug 2014 18:28:30 +0000 (UTC)
commit 7d81d0a3bb3ad8c26d9ca664b3aaafd0b4f3052e
Author: Ryan Lortie <desrt desrt ca>
Date: Sun Aug 3 20:27:51 2014 +0200
Add gtk_application_get_actions_for_accel()
This counterpart to gtk_application_get_accels_for_action() lets you
find out if a particular accelerator has one or more actions associated
with it. This might be useful from an accelerator editor or plugin
system to prevent the the installation of conflicting accelerators.
https://bugzilla.gnome.org/show_bug.cgi?id=721367
docs/reference/gtk/gtk3-sections.txt | 1 +
gtk/gtkapplication.c | 75 ++++++++++++++++++++++++++++++++++
gtk/gtkapplication.h | 4 ++
3 files changed, 80 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index b2a773a..cf17004 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -7415,6 +7415,7 @@ gtk_application_remove_accelerator
gtk_application_list_action_descriptions
gtk_application_get_accels_for_action
gtk_application_set_accels_for_action
+gtk_application_get_actions_for_accel
<SUBSECTION Standard>
GTK_TYPE_APPLICATION
diff --git a/gtk/gtkapplication.c b/gtk/gtkapplication.c
index 6df6d21..88e3efe 100644
--- a/gtk/gtkapplication.c
+++ b/gtk/gtkapplication.c
@@ -446,6 +446,13 @@ accels_get_accels_for_action (Accels *accels,
return result;
}
+static const gchar * const *
+accels_get_actions_for_accel (Accels *accels,
+ const AccelKey *accel_key)
+{
+ return g_hash_table_lookup (accels->accel_to_actions, accel_key);
+}
+
static void
accels_init (Accels *accels)
{
@@ -1691,6 +1698,74 @@ gtk_application_get_accels_for_action (GtkApplication *application,
return accels;
}
+/**
+ * gtk_application_get_actions_for_accel:
+ * @application: a #GtkApplication
+ * @accel: an accelerator that can be parsed by gtk_accelerator_parse()
+ *
+ * Returns the list of actions (possibly empty) that @accel maps to.
+ * Each item in the list is a detailed action name in the usual form.
+ *
+ * This might be useful to discover if an accel already exists in
+ * order to prevent installation of a conflicting accelerator (from
+ * an accelerator editor or a plugin system, for example). Note that
+ * having more than one action per accelerator may not be a bad thing
+ * and might make sense in cases where the actions never appear in the
+ * same context.
+ *
+ * In case there are no actions for a given accelerator, an empty array
+ * is returned. %NULL is never returned.
+ *
+ * It is a programmer error to pass an invalid accelerator string.
+ * If you are unsure, check it with gtk_accelerator_parse() first.
+ *
+ * Returns: (transfer full): a %NULL-terminated array of actions for @accel
+ *
+ * Since: 3.14
+ */
+gchar **
+gtk_application_get_actions_for_accel (GtkApplication *application,
+ const gchar *accel)
+{
+ const gchar * const *actions_and_targets;
+ gchar **detailed_actions;
+ AccelKey accel_key;
+ guint i, n;
+
+ g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
+ g_return_val_if_fail (accel != NULL, NULL);
+
+ gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier);
+
+ if (accel_key.key == 0)
+ {
+ g_critical ("invalid accelerator string '%s'", accel);
+ g_return_val_if_fail (accel_key.key != 0, NULL);
+ }
+
+ actions_and_targets = accels_get_actions_for_accel (&application->priv->accels, &accel_key);
+ n = actions_and_targets ? g_strv_length ((gchar **) actions_and_targets) : 0;
+
+ detailed_actions = g_new0 (gchar *, n + 1);
+
+ for (i = 0; i < n; i++)
+ {
+ const gchar *action_and_target = actions_and_targets[i];
+ const gchar *sep;
+ GVariant *target;
+
+ sep = strrchr (action_and_target, '|');
+ target = g_variant_parse (NULL, action_and_target, sep, NULL, NULL);
+ detailed_actions[i] = g_action_print_detailed_name (sep + 1, target);
+ if (target)
+ g_variant_unref (target);
+ }
+
+ detailed_actions[n] = NULL;
+
+ return detailed_actions;
+}
+
GtkActionMuxer *
gtk_application_get_action_muxer (GtkApplication *application)
{
diff --git a/gtk/gtkapplication.h b/gtk/gtkapplication.h
index 6fe1ff3..b55d4a7 100644
--- a/gtk/gtkapplication.h
+++ b/gtk/gtkapplication.h
@@ -145,6 +145,10 @@ gchar ** gtk_application_list_action_descriptions (GtkApplication
GDK_AVAILABLE_IN_3_12
gchar ** gtk_application_get_accels_for_action (GtkApplication *application,
const gchar
*detailed_action_name);
+GDK_AVAILABLE_IN_3_14
+gchar ** gtk_application_get_actions_for_accel (GtkApplication *application,
+ const gchar *accel);
+
GDK_AVAILABLE_IN_3_12
void gtk_application_set_accels_for_action (GtkApplication *application,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]