]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkaccelgroup.c
Change FSF Address
[~andy/gtk] / gtk / gtkaccelgroup.c
index 0464ecd7ce0370c54285044c250a9a61a723cd92..b76e2268d1d6becbb37be18bae57d473906eec0c 100644 (file)
@@ -1,8 +1,5 @@
 /* GTK - The GIMP Toolkit
- * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
- *
- * GtkAccelGroup: Accelerator manager for GtkObjects.
- * Copyright (C) 1998 Tim Janik
+ * Copyright (C) 1998, 2001 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GTK+ Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#include <ctype.h>
+#include "config.h"
 #include <string.h>
+#include <stdlib.h>
+
 #include "gtkaccelgroup.h"
-#include "gdk/gdkkeysyms.h"
-#include "gtksignal.h"
-#include "gtkwidget.h"
-
-
-/* --- signals --- */
-typedef void (*GtkSignalAddAccelerator)           (GObject         *object,
-                                           guint            accel_signal_id,
-                                           GtkAccelGroup   *accel_group,
-                                           guint            accel_key,
-                                           GdkModifierType  accel_mods,
-                                           GtkAccelFlags    accel_flags,
-                                           gpointer         func_data);
-typedef void (*GtkSignalRemoveAccelerator) (GObject        *object,
-                                           GtkAccelGroup   *accel_group,
-                                           guint            accel_key,
-                                           GdkModifierType  accel_mods,
-                                           gpointer         func_data);
+#include "gtkaccelgroupprivate.h"
+#include "gtkaccellabel.h"
+#include "gtkaccelmapprivate.h"
+#include "gtkintl.h"
+#include "gtkmarshalers.h"
+#include "gtkprivate.h"
+
+/**
+ * SECTION:gtkaccelgroup
+ * @Short_description: Groups of global keyboard accelerators for an
+ *     entire GtkWindow
+ * @Title: Accelerator Groups
+ * @See_also:gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
+ * gtk_item_factory_new(), gtk_label_new_with_mnemonic()
+ *
+ * A #GtkAccelGroup represents a group of keyboard accelerators,
+ * typically attached to a toplevel #GtkWindow (with
+ * gtk_window_add_accel_group()). Usually you won't need to create a
+ * #GtkAccelGroup directly; instead, when using #GtkUIManager, GTK+
+ * automatically sets up the accelerators for your menus in the ui
+ * manager's #GtkAccelGroup.
+ *
+ * Note that <firstterm>accelerators</firstterm> are different from
+ * <firstterm>mnemonics</firstterm>. Accelerators are shortcuts for
+ * activating a menu item; they appear alongside the menu item they're a
+ * shortcut for. For example "Ctrl+Q" might appear alongside the "Quit"
+ * menu item. Mnemonics are shortcuts for GUI elements such as text
+ * entries or buttons; they appear as underlined characters. See
+ * gtk_label_new_with_mnemonic(). Menu items can have both accelerators
+ * and mnemonics, of course.
+ */
+
+/* --- prototypes --- */
+static void gtk_accel_group_finalize     (GObject    *object);
+static void gtk_accel_group_get_property (GObject    *object,
+                                          guint       param_id,
+                                          GValue     *value,
+                                          GParamSpec *pspec);
+static void accel_closure_invalidate     (gpointer    data,
+                                          GClosure   *closure);
+
 
 /* --- variables --- */
-static GtkAccelGroup   *default_accel_group = NULL;
-static guint            default_accel_mod_mask = (GDK_SHIFT_MASK |
-                                                  GDK_CONTROL_MASK |
-                                                  GDK_MOD1_MASK);
-static const gchar     *accel_groups_key = "gtk-accel-groups";
-static guint            accel_groups_key_id = 0;
-static const gchar     *accel_entries_key = "gtk-accel-entries";
-static guint            accel_entries_key_id = 0;
-static GHashTable      *accel_entry_hash_table = NULL;
-static GMemChunk       *accel_entries_mem_chunk = NULL;
+static guint  signal_accel_activate      = 0;
+static guint  signal_accel_changed       = 0;
+static guint  quark_acceleratable_groups = 0;
+static guint  default_accel_mod_mask     = (GDK_SHIFT_MASK   |
+                                            GDK_CONTROL_MASK |
+                                            GDK_MOD1_MASK    |
+                                            GDK_SUPER_MASK   |
+                                            GDK_HYPER_MASK   |
+                                            GDK_META_MASK);
 
-static GObjectClass     *parent_class = NULL;
 
+enum {
+  PROP_0,
+  PROP_IS_LOCKED,
+  PROP_MODIFIER_MASK,
+};
 
-/* --- functions --- */
-static gboolean
-gtk_accel_entries_equal (gconstpointer a,
-                        gconstpointer b)
-{
-  const GtkAccelEntry *e1;
-  const GtkAccelEntry *e2;
-  
-  e1 = a;
-  e2 = b;
-  
-  return ((e1->accel_group == e2->accel_group) &&
-         (e1->accelerator_key == e2->accelerator_key) &&
-         (e1->accelerator_mods == e2->accelerator_mods));
-}
+G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
 
-static guint
-gtk_accel_entries_hash (gconstpointer a)
+/* --- functions --- */
+static void
+gtk_accel_group_class_init (GtkAccelGroupClass *class)
 {
-  const GtkAccelEntry *e;
-  guint h;
-  
-  e = a;
-  
-  h = (gulong) e->accel_group;
-  h ^= e->accelerator_key << 16;
-  h ^= e->accelerator_key >> 16;
-  h ^= e->accelerator_mods;
-  
-  return h;
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
+
+  object_class->finalize = gtk_accel_group_finalize;
+  object_class->get_property = gtk_accel_group_get_property;
+
+  class->accel_changed = NULL;
+
+  g_object_class_install_property (object_class,
+                                   PROP_IS_LOCKED,
+                                   g_param_spec_boolean ("is-locked",
+                                                         "Is locked",
+                                                         "Is the accel group locked",
+                                                         FALSE,
+                                                         G_PARAM_READABLE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_MODIFIER_MASK,
+                                   g_param_spec_flags ("modifier-mask",
+                                                       "Modifier Mask",
+                                                       "Modifier Mask",
+                                                       GDK_TYPE_MODIFIER_TYPE,
+                                                       default_accel_mod_mask,
+                                                       G_PARAM_READABLE));
+
+  /**
+   * GtkAccelGroup::accel-activate:
+   * @accel_group: the #GtkAccelGroup which received the signal
+   * @acceleratable: the object on which the accelerator was activated
+   * @keyval: the accelerator keyval
+   * @modifier: the modifier combination of the accelerator
+   *
+   * The accel-activate signal is an implementation detail of
+   * #GtkAccelGroup and not meant to be used by applications.
+   *
+   * Returns: %TRUE if the accelerator was activated
+   */
+  signal_accel_activate =
+    g_signal_new (I_("accel-activate"),
+                  G_OBJECT_CLASS_TYPE (class),
+                  G_SIGNAL_DETAILED,
+                  0,
+                  _gtk_boolean_handled_accumulator, NULL,
+                  _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
+                  G_TYPE_BOOLEAN, 3,
+                  G_TYPE_OBJECT,
+                  G_TYPE_UINT,
+                  GDK_TYPE_MODIFIER_TYPE);
+  /**
+   * GtkAccelGroup::accel-changed:
+   * @accel_group: the #GtkAccelGroup which received the signal
+   * @keyval: the accelerator keyval
+   * @modifier: the modifier combination of the accelerator
+   * @accel_closure: the #GClosure of the accelerator
+   *
+   * The accel-changed signal is emitted when a #GtkAccelGroupEntry
+   * is added to or removed from the accel group.
+   *
+   * Widgets like #GtkAccelLabel which display an associated
+   * accelerator should connect to this signal, and rebuild
+   * their visual representation if the @accel_closure is theirs.
+   */
+  signal_accel_changed =
+    g_signal_new (I_("accel-changed"),
+                  G_OBJECT_CLASS_TYPE (class),
+                  G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
+                  G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
+                  NULL, NULL,
+                  _gtk_marshal_VOID__UINT_FLAGS_BOXED,
+                  G_TYPE_NONE, 3,
+                  G_TYPE_UINT,
+                  GDK_TYPE_MODIFIER_TYPE,
+                  G_TYPE_CLOSURE);
+
+  g_type_class_add_private (object_class, sizeof (GtkAccelGroupPrivate));
 }
 
-static void gtk_accel_group_class_init (GObjectClass *class);
-static void gtk_accel_group_init (GtkAccelGroup *accel_group);
-
-GType
-gtk_accel_group_get_type (void)
+static void
+gtk_accel_group_finalize (GObject *object)
 {
-  static GType object_type = 0;
+  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
+  guint i;
 
-  if (!object_type)
+  for (i = 0; i < accel_group->priv->n_accels; i++)
     {
-      static const GTypeInfo object_info =
-      {
-       sizeof (GtkAccelGroupClass),
-       (GBaseInitFunc) NULL,
-       (GBaseFinalizeFunc) NULL,
-       (GClassInitFunc) gtk_accel_group_class_init,
-       NULL,   /* clas_finalize */
-       NULL,   /* class_data */
-       sizeof(GtkAccelGroup),
-       0,      /* n_preallocs */
-       (GInstanceInitFunc) gtk_accel_group_init,
-      };
-
-      object_type = g_type_register_static (G_TYPE_OBJECT,
-                                           "GtkAccelGroup",
-                                           &object_info, 0);
-    }
+      GtkAccelGroupEntry *entry = &accel_group->priv->priv_accels[i];
 
-  return object_type;
-}
+      if (entry->accel_path_quark)
+        {
+          const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
 
-static void
-gtk_accel_group_finalize (GObject *object)
-{
-  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP(object);
+          _gtk_accel_map_remove_group (accel_path, accel_group);
+        }
+      g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
 
-  if (accel_group == default_accel_group)
-    g_warning (G_STRLOC "default accel group should not be finalized");
+      /* remove quick_accel_add() refcount */
+      g_closure_unref (entry->closure);
+    }
+
+  g_free (accel_group->priv->priv_accels);
 
-  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
+  G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
 }
 
 static void
-gtk_accel_group_class_init (GObjectClass *class)
+gtk_accel_group_get_property (GObject    *object,
+                              guint       param_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
 {
-  parent_class = g_type_class_ref (G_TYPE_OBJECT);
+  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
 
-  class->finalize = gtk_accel_group_finalize;
+  switch (param_id)
+    {
+    case PROP_IS_LOCKED:
+      g_value_set_boolean (value, accel_group->priv->lock_count > 0);
+      break;
+    case PROP_MODIFIER_MASK:
+      g_value_set_flags (value, accel_group->priv->modifier_mask);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+      break;
+    }
 }
 
 static void
 gtk_accel_group_init (GtkAccelGroup *accel_group)
 {
-  if (!accel_groups_key_id)
-    {
-      accel_groups_key_id = g_quark_from_static_string (accel_groups_key);
-      accel_entries_key_id = g_quark_from_static_string (accel_entries_key);
-      
-      accel_entry_hash_table = g_hash_table_new (gtk_accel_entries_hash,
-                                                gtk_accel_entries_equal);
-      
-      accel_entries_mem_chunk = g_mem_chunk_create (GtkAccelEntry, 64, G_ALLOC_AND_FREE);
-    }
-
-  accel_group->lock_count = 0;
-  accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
-  accel_group->attach_objects = NULL;
+  GtkAccelGroupPrivate *priv;
+
+  accel_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (accel_group,
+                                                   GTK_TYPE_ACCEL_GROUP,
+                                                   GtkAccelGroupPrivate);
+  priv = accel_group->priv;
+
+  priv->lock_count = 0;
+  priv->modifier_mask = gtk_accelerator_get_default_mod_mask ();
+  priv->acceleratables = NULL;
+  priv->n_accels = 0;
+  priv->priv_accels = NULL;
 }
 
 /**
  * gtk_accel_group_new:
- * 
- * Creates a new #GtkAccelGroup. 
- * 
- * Return value: a new #GtkAccelGroup
- **/
+ *
+ * Creates a new #GtkAccelGroup.
+ *
+ * Returns: a new #GtkAccelGroup object
+ */
 GtkAccelGroup*
 gtk_accel_group_new (void)
 {
-  GtkAccelGroup *accel_group;
-
-  accel_group = (GtkAccelGroup *)g_object_new(GTK_TYPE_ACCEL_GROUP, NULL);
-
-  return accel_group;
-}
-
-/**
- * gtk_accel_group_get_default:
- * 
- * Gets the global default accelerator group; this is a fallback
- * used for all objects when gtk_accel_groups_activate() is called
- * on them. As such it's probably not appropriate for most uses.
- * (Accelerators are normally specific to a document window or the
- * like, rather than global to an application.)
- *
- * The returned value does not have its reference count incremented,
- * and should not be unreferenced.
- * 
- * Return value: the default accelerator group
- **/
-GtkAccelGroup*
-gtk_accel_group_get_default (void)
-{
-  if (!default_accel_group)
-    default_accel_group = gtk_accel_group_new ();
-  
-  return default_accel_group;
+  return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
 }
 
 /**
- * gtk_accel_group_ref:
+ * gtk_accel_group_get_is_locked:
  * @accel_group: a #GtkAccelGroup
- * 
- * This is simply equivalent to g_object_ref (G_OBJECT (@accel_group)),
- * and exists for historical reasons only.
- * 
- * Return value: @accel_group
- **/
-GtkAccelGroup*
-gtk_accel_group_ref (GtkAccelGroup     *accel_group)
+ *
+ * Locks are added and removed using gtk_accel_group_lock() and
+ * gtk_accel_group_unlock().
+ *
+ * Returns: %TRUE if there are 1 or more locks on the @accel_group,
+ *     %FALSE otherwise.
+ *
+ * Since: 2.14
+ */
+gboolean
+gtk_accel_group_get_is_locked (GtkAccelGroup *accel_group)
 {
-  g_return_val_if_fail (GTK_IS_ACCEL_GROUP(accel_group), NULL);
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
 
-  return (GtkAccelGroup *)g_object_ref(accel_group);
+  return accel_group->priv->lock_count > 0;
 }
 
 /**
- * gtk_accel_group_unref:
+ * gtk_accel_group_get_modifier_mask:
  * @accel_group: a #GtkAccelGroup
- * 
- * This is simply equivalent to g_object_unref (G_OBJECT (@accel_group)),
- * and exists for historical reasons only.
- * 
- **/
-void
-gtk_accel_group_unref (GtkAccelGroup  *accel_group)
+ *
+ * Gets a #GdkModifierType representing the mask for this
+ * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
+ *
+ * Returns: the modifier mask for this accel group.
+ *
+ * Since: 2.14
+ */
+GdkModifierType
+gtk_accel_group_get_modifier_mask (GtkAccelGroup *accel_group)
 {
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  g_object_unref(accel_group);
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), 0);
+
+  return accel_group->priv->modifier_mask;
 }
 
 static void
-gtk_accel_group_object_destroy (GSList *free_list,
-                               GObject *where_the_object_was)
+accel_group_weak_ref_detach (GSList  *free_list,
+                             GObject *stale_object)
 {
   GSList *slist;
-  
+
   for (slist = free_list; slist; slist = slist->next)
     {
       GtkAccelGroup *accel_group;
-      
+
       accel_group = slist->data;
-      accel_group->attach_objects = g_slist_remove (accel_group->attach_objects, where_the_object_was);
+      accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, stale_object);
       g_object_unref (accel_group);
     }
   g_slist_free (free_list);
+  g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
 }
 
-/**
- * gtk_accel_group_attach:
- * @accel_group: a #GtkAccelGroup
- * @object: object to attach accelerators to
- *
- * Associate @accel_group with @object, such that calling
- * gtk_accel_groups_activate() on @object will activate accelerators
- * in @accel_group.
- *
- * After calling this function, you still own a reference to both
- * @accel_group and @object; gtk_accel_group_attach() will not
- * "adopt" a reference to either one.
- * 
- **/
 void
-gtk_accel_group_attach (GtkAccelGroup  *accel_group,
-                       GObject         *object)
+_gtk_accel_group_attach (GtkAccelGroup *accel_group,
+                         GObject       *object)
 {
   GSList *slist;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
+
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
   g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (g_slist_find (accel_group->attach_objects, object) == NULL);
-  
-  accel_group->attach_objects = g_slist_prepend (accel_group->attach_objects, object);
+  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) == NULL);
+
   g_object_ref (accel_group);
-  slist = g_object_get_qdata (object, accel_groups_key_id);
+  accel_group->priv->acceleratables = g_slist_prepend (accel_group->priv->acceleratables, object);
+  slist = g_object_get_qdata (object, quark_acceleratable_groups);
   if (slist)
-    g_object_weak_unref(object,
-                       (GWeakNotify)gtk_accel_group_object_destroy,
-                       slist);
+    g_object_weak_unref (object,
+                         (GWeakNotify) accel_group_weak_ref_detach,
+                         slist);
   slist = g_slist_prepend (slist, accel_group);
-  g_object_set_qdata (object, accel_groups_key_id, slist);
-  g_object_weak_ref(object,
-                   (GWeakNotify)gtk_accel_group_object_destroy,
-                   slist);
+  g_object_set_qdata (object, quark_acceleratable_groups, slist);
+  g_object_weak_ref (object,
+                     (GWeakNotify) accel_group_weak_ref_detach,
+                     slist);
 }
 
-/**
- * gtk_accel_group_detach:
- * @accel_group: a #GtkAccelGroup
- * @object: a #GObject
- *
- * Reverses the effects of gtk_accel_group_attach().
- * 
- **/
 void
-gtk_accel_group_detach (GtkAccelGroup  *accel_group,
-                       GObject         *object)
+_gtk_accel_group_detach (GtkAccelGroup *accel_group,
+                         GObject       *object)
 {
   GSList *slist;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
+
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
   g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (g_slist_find (accel_group->attach_objects, object) != NULL);
-  
-  accel_group->attach_objects = g_slist_remove (accel_group->attach_objects, object);
-  g_object_unref (accel_group);
-  slist = g_object_get_qdata (object, accel_groups_key_id);
-  g_object_weak_unref(object,
-                     (GWeakNotify)gtk_accel_group_object_destroy,
-                     slist);
+  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) != NULL);
+
+  accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, object);
+  slist = g_object_get_qdata (object, quark_acceleratable_groups);
+  g_object_weak_unref (object,
+                       (GWeakNotify) accel_group_weak_ref_detach,
+                       slist);
   slist = g_slist_remove (slist, accel_group);
-  g_object_set_qdata (object, accel_groups_key_id, slist);
+  g_object_set_qdata (object, quark_acceleratable_groups, slist);
   if (slist)
-    g_object_weak_ref(object,
-                     (GWeakNotify)gtk_accel_group_object_destroy,
-                     slist);
+    g_object_weak_ref (object,
+                       (GWeakNotify) accel_group_weak_ref_detach,
+                       slist);
+  g_object_unref (accel_group);
+}
+
+/**
+ * gtk_accel_groups_from_object:
+ * @object: a #GObject, usually a #GtkWindow
+ *
+ * Gets a list of all accel groups which are attached to @object.
+ *
+ * Returns: (element-type GtkAccelGroup) (transfer none): a list of
+ *     all accel groups which are attached to @object
+ */
+GSList*
+gtk_accel_groups_from_object (GObject *object)
+{
+  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
+
+  return g_object_get_qdata (object, quark_acceleratable_groups);
+}
+
+/**
+ * gtk_accel_group_find:
+ * @accel_group: a #GtkAccelGroup
+ * @find_func: (scope call): a function to filter the entries
+ *    of @accel_group with
+ * @data: data to pass to @find_func
+ *
+ * Finds the first entry in an accelerator group for which
+ * @find_func returns %TRUE and returns its #GtkAccelKey.
+ *
+ * Returns: (transfer none): the key of the first entry passing
+ *    @find_func. The key is owned by GTK+ and must not be freed.
+ */
+GtkAccelKey*
+gtk_accel_group_find (GtkAccelGroup         *accel_group,
+                      GtkAccelGroupFindFunc  find_func,
+                      gpointer               data)
+{
+  GtkAccelKey *key = NULL;
+  guint i;
+
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
+  g_return_val_if_fail (find_func != NULL, NULL);
+
+  g_object_ref (accel_group);
+  for (i = 0; i < accel_group->priv->n_accels; i++)
+    if (find_func (&accel_group->priv->priv_accels[i].key,
+                   accel_group->priv->priv_accels[i].closure,
+                   data))
+      {
+        key = &accel_group->priv->priv_accels[i].key;
+        break;
+      }
+  g_object_unref (accel_group);
+
+  return key;
 }
 
 /**
  * gtk_accel_group_lock:
  * @accel_group: a #GtkAccelGroup
- * 
- * Prevents the addition of new accelerators to @accel_group.
- * Primarily used to avoid the "dynamic accelerator editing" feature
- * of #GtkMenu.
+ *
+ * Locks the given accelerator group.
+ *
+ * Locking an acelerator group prevents the accelerators contained
+ * within it to be changed during runtime. Refer to
+ * gtk_accel_map_change_entry() about runtime accelerator changes.
  *
  * If called more than once, @accel_group remains locked until
  * gtk_accel_group_unlock() has been called an equivalent number
  * of times.
- * 
- **/
+ */
 void
-gtk_accel_group_lock (GtkAccelGroup     *accel_group)
+gtk_accel_group_lock (GtkAccelGroup *accel_group)
 {
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  accel_group->lock_count += 1;
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
+
+  accel_group->priv->lock_count += 1;
+
+  if (accel_group->priv->lock_count == 1) {
+    /* State change from unlocked to locked */
+    g_object_notify (G_OBJECT (accel_group), "is-locked");
+  }
 }
 
 /**
  * gtk_accel_group_unlock:
  * @accel_group: a #GtkAccelGroup
- * 
- * Allows the addition of new accelerators to @accel_group.
- * Primarily used to enable the "dynamic accelerator editing" feature
- * of #GtkMenu.
- * 
- **/
+ *
+ * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
+ */
 void
-gtk_accel_group_unlock (GtkAccelGroup  *accel_group)
+gtk_accel_group_unlock (GtkAccelGroup *accel_group)
 {
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  if (accel_group->lock_count)
-    accel_group->lock_count -= 1;
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
+  g_return_if_fail (accel_group->priv->lock_count > 0);
+
+  accel_group->priv->lock_count -= 1;
+
+  if (accel_group->priv->lock_count < 1) {
+    /* State change from locked to unlocked */
+    g_object_notify (G_OBJECT (accel_group), "is-locked");
+  }
 }
 
-static GtkAccelEntry*
-gtk_accel_group_lookup (GtkAccelGroup  *accel_group,
-                       guint            accel_key,
-                       GdkModifierType  accel_mods)
+static void
+accel_closure_invalidate (gpointer  data,
+                          GClosure *closure)
 {
-  GtkAccelEntry key_entry = { 0 };
-  
-  key_entry.accel_group = accel_group;
-  key_entry.accelerator_key = gdk_keyval_to_lower (accel_key);
-  key_entry.accelerator_mods = accel_mods & accel_group->modifier_mask;
-  
-  return g_hash_table_lookup (accel_entry_hash_table, &key_entry);
+  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
+
+  gtk_accel_group_disconnect (accel_group, closure);
 }
 
-/**
- * gtk_accel_group_activate:
- * @accel_group: a #GtkAccelGroup
- * @accel_key: keyval from a key event
- * @accel_mods: modifier mask from a key event
- *
- * Checks whether a key event matches an accelerator in @accel_group;
- * if so, activates the accelerator, and returns %TRUE. Returns
- * %FALSE if no match.
- *
- * gtk_accel_groups_activate() should normally be used instead of
- * this function.
- * 
- * Return value: %TRUE if an accelerator was activated
- **/
-gboolean
-gtk_accel_group_activate (GtkAccelGroup         *accel_group,
-                         guint           accel_key,
-                         GdkModifierType accel_mods)
+static int
+bsearch_compare_accels (const void *d1,
+                        const void *d2)
 {
-  GtkAccelEntry *entry;
-  
-  g_return_val_if_fail (GTK_IS_ACCEL_GROUP(accel_group), FALSE);
-  
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry && entry->signal_id &&
-      (!GTK_IS_WIDGET (entry->object) || GTK_WIDGET_IS_SENSITIVE (entry->object)))
+  const GtkAccelGroupEntry *entry1 = d1;
+  const GtkAccelGroupEntry *entry2 = d2;
+
+  if (entry1->key.accel_key == entry2->key.accel_key)
+    return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
+  else
+    return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
+}
+
+static void
+quick_accel_add (GtkAccelGroup   *accel_group,
+                 guint            accel_key,
+                 GdkModifierType  accel_mods,
+                 GtkAccelFlags    accel_flags,
+                 GClosure        *closure,
+                 GQuark           path_quark)
+{
+  guint pos, i = accel_group->priv->n_accels++;
+  GtkAccelGroupEntry key;
+
+  /* find position */
+  key.key.accel_key = accel_key;
+  key.key.accel_mods = accel_mods;
+  for (pos = 0; pos < i; pos++)
+    if (bsearch_compare_accels (&key, accel_group->priv->priv_accels + pos) < 0)
+      break;
+
+  /* insert at position, ref closure */
+  accel_group->priv->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv->priv_accels, accel_group->priv->n_accels);
+  g_memmove (accel_group->priv->priv_accels + pos + 1, accel_group->priv->priv_accels + pos,
+             (i - pos) * sizeof (accel_group->priv->priv_accels[0]));
+  accel_group->priv->priv_accels[pos].key.accel_key = accel_key;
+  accel_group->priv->priv_accels[pos].key.accel_mods = accel_mods;
+  accel_group->priv->priv_accels[pos].key.accel_flags = accel_flags;
+  accel_group->priv->priv_accels[pos].closure = g_closure_ref (closure);
+  accel_group->priv->priv_accels[pos].accel_path_quark = path_quark;
+  g_closure_sink (closure);
+
+  /* handle closure invalidation and reverse lookups */
+  g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
+
+  /* get accel path notification */
+  if (path_quark)
+    _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
+
+  /* connect and notify changed */
+  if (accel_key)
     {
-      g_signal_emit (entry->object, entry->signal_id, 0);
-      return TRUE;
+      gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
+      GQuark accel_quark = g_quark_from_string (accel_name);
+
+      g_free (accel_name);
+
+      /* setup handler */
+      g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
+
+      /* and notify */
+      g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
     }
-  return FALSE;
 }
 
-/**
- * gtk_accel_groups_activate:
- * @object: a #GObject
- * @accel_key: accelerator keyval from a key event
- * @accel_mods: keyboard state mask from a key event
- * 
- * Finds the first accelerator in any #GtkAccelGroup attached
- * to @object that matches @accel_key and @accel_mods, and
- * activates that accelerator. If no accelerators are found
- * in groups attached to @object, this function also tries
- * the default #GtkAccelGroup (see gtk_accel_group_get_default()).
- * If an accelerator is activated, returns %TRUE, otherwise
- * %FALSE.
- * 
- * Return value: %TRUE if an accelerator was activated
- **/
-gboolean
-gtk_accel_groups_activate (GObject         *object,
-                          guint             accel_key,
-                          GdkModifierType   accel_mods)
+static void
+quick_accel_remove (GtkAccelGroup *accel_group,
+                    guint          pos)
 {
-  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
-  
-  if (gtk_accelerator_valid (accel_key, accel_mods))
+  GQuark accel_quark = 0;
+  GtkAccelGroupEntry *entry = accel_group->priv->priv_accels + pos;
+  guint accel_key = entry->key.accel_key;
+  GdkModifierType accel_mods = entry->key.accel_mods;
+  GClosure *closure = entry->closure;
+
+  /* quark for notification */
+  if (accel_key)
     {
-      GSList *slist;
-      
-      for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
-       if (gtk_accel_group_activate (slist->data, accel_key, accel_mods))
-         return TRUE;
-      return gtk_accel_group_activate (gtk_accel_group_get_default (), accel_key, accel_mods);
+      gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
+
+      accel_quark = g_quark_from_string (accel_name);
+      g_free (accel_name);
     }
-  
-  return FALSE;
+
+  /* clean up closure invalidate notification and disconnect */
+  g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
+  if (accel_quark)
+    g_signal_handlers_disconnect_matched (accel_group,
+                                          G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
+                                          signal_accel_activate, accel_quark,
+                                          closure, NULL, NULL);
+  /* clean up accel path notification */
+  if (entry->accel_path_quark)
+    _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
+
+  /* physically remove */
+  accel_group->priv->n_accels -= 1;
+  g_memmove (entry, entry + 1,
+             (accel_group->priv->n_accels - pos) * sizeof (accel_group->priv->priv_accels[0]));
+
+  /* and notify */
+  if (accel_quark)
+    g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
+
+  /* remove quick_accel_add() refcount */
+  g_closure_unref (closure);
 }
 
-void
-gtk_accel_group_lock_entry (GtkAccelGroup       *accel_group,
-                           guint                 accel_key,
-                           GdkModifierType       accel_mods)
+static GtkAccelGroupEntry*
+quick_accel_find (GtkAccelGroup   *accel_group,
+                  guint            accel_key,
+                  GdkModifierType  accel_mods,
+                  guint           *count_p)
 {
-  GtkAccelEntry *entry;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry)
-    entry->accel_flags |= GTK_ACCEL_LOCKED;
+  GtkAccelGroupEntry *entry;
+  GtkAccelGroupEntry key;
+
+  *count_p = 0;
+
+  if (!accel_group->priv->n_accels)
+    return NULL;
+
+  key.key.accel_key = accel_key;
+  key.key.accel_mods = accel_mods;
+  entry = bsearch (&key, accel_group->priv->priv_accels, accel_group->priv->n_accels,
+                   sizeof (accel_group->priv->priv_accels[0]), bsearch_compare_accels);
+
+  if (!entry)
+    return NULL;
+
+  /* step back to the first member */
+  for (; entry > accel_group->priv->priv_accels; entry--)
+    if (entry[-1].key.accel_key != accel_key ||
+        entry[-1].key.accel_mods != accel_mods)
+      break;
+  /* count equal members */
+  for (; entry + *count_p < accel_group->priv->priv_accels + accel_group->priv->n_accels; (*count_p)++)
+    if (entry[*count_p].key.accel_key != accel_key ||
+        entry[*count_p].key.accel_mods != accel_mods)
+      break;
+  return entry;
 }
 
+/**
+ * gtk_accel_group_connect:
+ * @accel_group: the accelerator group to install an accelerator in
+ * @accel_key: key value of the accelerator
+ * @accel_mods: modifier combination of the accelerator
+ * @accel_flags: a flag mask to configure this accelerator
+ * @closure: closure to be executed upon accelerator activation
+ *
+ * Installs an accelerator in this group. When @accel_group is being
+ * activated in response to a call to gtk_accel_groups_activate(),
+ * @closure will be invoked if the @accel_key and @accel_mods from
+ * gtk_accel_groups_activate() match those of this connection.
+ *
+ * The signature used for the @closure is that of #GtkAccelGroupActivate.
+ *
+ * Note that, due to implementation details, a single closure can
+ * only be connected to one accelerator group.
+ */
 void
-gtk_accel_group_unlock_entry (GtkAccelGroup    *accel_group,
-                             guint              accel_key,
-                             GdkModifierType    accel_mods)
+gtk_accel_group_connect (GtkAccelGroup   *accel_group,
+                         guint            accel_key,
+                         GdkModifierType  accel_mods,
+                         GtkAccelFlags    accel_flags,
+                         GClosure        *closure)
 {
-  GtkAccelEntry *entry;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry)
-    entry->accel_flags &= ~GTK_ACCEL_LOCKED;
-}
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
+  g_return_if_fail (closure != NULL);
+  g_return_if_fail (accel_key > 0);
+  g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
 
-GtkAccelEntry*
-gtk_accel_group_get_entry (GtkAccelGroup    *accel_group,
-                          guint             accel_key,
-                          GdkModifierType   accel_mods)
-{
-  g_return_val_if_fail (GTK_IS_ACCEL_GROUP(accel_group), 0);
-  
-  return gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
+  g_object_ref (accel_group);
+  if (!closure->is_invalid)
+    quick_accel_add (accel_group,
+                     gdk_keyval_to_lower (accel_key),
+                     accel_mods, accel_flags, closure, 0);
+  g_object_unref (accel_group);
 }
 
 /**
- * gtk_accel_group_add:
- * @accel_group: a #GtkAccelGroup
- * @accel_key: accelerator keyval
- * @accel_mods: accelerator modifiers
- * @accel_flags: accelerator flags
- * @object: object that @accel_signal will be emitted on
- * @accel_signal: name of a #G_SIGNAL_ACTION signal to emit
- *
- * Adds an accelerator to @accel_group. When the accelerator is
- * activated, the @accel_signal signal will be emitted on @object.
+ * gtk_accel_group_connect_by_path:
+ * @accel_group: the accelerator group to install an accelerator in
+ * @accel_path: path used for determining key and modifiers
+ * @closure: closure to be executed upon accelerator activation
  *
- * So for example, to click a button when Ctrl+a is pressed, you would
- * write: gtk_accel_group_add (accel_group, GDK_a, GDK_CONTROL_MASK,
- * 0, G_OBJECT (button), "clicked").
+ * Installs an accelerator in this group, using an accelerator path
+ * to look up the appropriate key and modifiers (see
+ * gtk_accel_map_add_entry()). When @accel_group is being activated
+ * in response to a call to gtk_accel_groups_activate(), @closure will
+ * be invoked if the @accel_key and @accel_mods from
+ * gtk_accel_groups_activate() match the key and modifiers for the path.
  *
- * @accel_flags is not particularly useful, always pass 0 for
- * normal applications.
+ * The signature used for the @closure is that of #GtkAccelGroupActivate.
  *
- * @object must be an object that specifically supports accelerators,
- * such as #GtkWidget.
- **/
+ * Note that @accel_path string will be stored in a #GQuark. Therefore,
+ * if you pass a static string, you can save some memory by interning it
+ * first with g_intern_static_string().
+ */
 void
-gtk_accel_group_add (GtkAccelGroup     *accel_group,
-                    guint               accel_key,
-                    GdkModifierType     accel_mods,
-                    GtkAccelFlags       accel_flags,
-                    GObject            *object,
-                    const gchar        *accel_signal)
+gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
+                                 const gchar   *accel_path,
+                                 GClosure      *closure)
 {
-  guint accel_signal_id = 0;
-  guint add_accelerator_signal_id = 0;
-  guint remove_accelerator_signal_id = 0;
-  gchar *signal;
-  GSignalQuery query;
-  GSList *slist;
-  GSList *groups;
-  GSList *attach_objects;
-  GtkAccelEntry *entry;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (accel_signal != NULL);
-  
-  /* check for required signals in the objects branch
-   */
-  signal = (gchar*) accel_signal;
-  accel_signal_id = g_signal_lookup (signal, G_OBJECT_TYPE (object));
-  if (accel_signal_id)
-    {
-      signal = "add-accelerator";
-      add_accelerator_signal_id = g_signal_lookup (signal, G_OBJECT_TYPE (object));
-    }
-  if (add_accelerator_signal_id)
-    {
-      signal = "remove-accelerator";
-      remove_accelerator_signal_id = g_signal_lookup (signal, G_OBJECT_TYPE (object));
-    }
-  if (!accel_signal_id ||
-      !add_accelerator_signal_id ||
-      !remove_accelerator_signal_id)
-    {
-      g_warning ("gtk_accel_group_add(): could not find signal \"%s\""
-                "in the `%s' class ancestry",
-                signal,
-                g_type_name (G_OBJECT_TYPE (object)));
-      return;
-    }
-  g_signal_query (accel_signal_id, &query);
-  if (!query.signal_id || query.n_params > 0)
-    {
-      g_warning ("gtk_accel_group_add(): signal \"%s\" in the `%s' class ancestry"
-                "cannot be used as accelerator signal %s",
-                accel_signal,
-                g_type_name (G_OBJECT_TYPE (object)),
-                query.n_params > 0 ? "(extraneous parameters are not supported)" : "");
-      return;
-    }
+  guint accel_key = 0;
+  GdkModifierType accel_mods = 0;
+  GtkAccelKey key;
 
-  /* prematurely abort if the group/entry is already locked
-   */
-  if (accel_group->lock_count > 0)
-    return;
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry && entry->accel_flags & GTK_ACCEL_LOCKED)
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
+  g_return_if_fail (closure != NULL);
+  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
+
+  if (closure->is_invalid)
     return;
-  
-  /* make sure our structures stay alive
-   */
+
   g_object_ref (accel_group);
-  g_object_ref (object);
-  
-  /* remove an existing entry
-   */
-  if (entry)
-    g_signal_emit (entry->object, remove_accelerator_signal_id, 0,
-                  accel_group,
-                  gdk_keyval_to_lower (accel_key),
-                  accel_mods & accel_group->modifier_mask);
-  
-  /* abort if the entry still exists
-   */
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry)
-    {
-      g_object_unref (accel_group);
-      g_object_unref (object);
-      
-      return;
-    }
-  
-  /* collect accel groups and remove existing entries
-   */
-  attach_objects = accel_group->attach_objects;
-  groups = NULL;
-  for (attach_objects = accel_group->attach_objects; attach_objects; attach_objects = attach_objects->next)
-    {
-      GSList *tmp_groups;
-      
-      tmp_groups = g_object_get_qdata (attach_objects->data, accel_groups_key_id);
-      while (tmp_groups)
-       {
-         groups = g_slist_prepend (groups, tmp_groups->data);
-         g_object_ref (tmp_groups->data);
-         tmp_groups = tmp_groups->next;
-       }
-    }
-  for (slist = groups; slist; slist = slist->next)
+
+  if (gtk_accel_map_lookup_entry (accel_path, &key))
     {
-      GtkAccelGroup *tmp_group;
-      
-      tmp_group = slist->data;
-      
-      /* we only remove the accelerator if neccessary
-       */
-      if (tmp_group->lock_count == 0)
-       {
-         entry = gtk_accel_group_lookup (tmp_group, accel_key, accel_mods);
-         if (entry && !(entry->accel_flags & GTK_ACCEL_LOCKED))
-           g_signal_emit (entry->object, remove_accelerator_signal_id, 0,
-                          tmp_group,
-                          gdk_keyval_to_lower (accel_key),
-                          accel_mods & tmp_group->modifier_mask);
-       }
-      g_object_unref (tmp_group);
+      accel_key = gdk_keyval_to_lower (key.accel_key);
+      accel_mods = key.accel_mods;
     }
-  g_slist_free (groups);
-  
-  /* now install the new accelerator
-   */
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (!entry)
-    g_signal_emit (object, add_accelerator_signal_id, 0,
-                  accel_signal_id,
-                  accel_group,
-                  gdk_keyval_to_lower (accel_key),
-                  accel_mods & accel_group->modifier_mask,
-                  accel_flags & GTK_ACCEL_MASK);
-  
-  /* and release the structures again
-   */
+
+  quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
+                   g_quark_from_string (accel_path));
+
   g_object_unref (accel_group);
-  g_object_unref (object);
 }
 
-static void
-gtk_accel_group_delete_entries (GSList *entries)
+/**
+ * gtk_accel_group_disconnect:
+ * @accel_group: the accelerator group to remove an accelerator from
+ * @closure: (allow-none): the closure to remove from this accelerator
+ *     group, or %NULL to remove all closures
+ *
+ * Removes an accelerator previously installed through
+ * gtk_accel_group_connect().
+ *
+ * Since 2.20 @closure can be %NULL.
+ *
+ * Returns: %TRUE if the closure was found and got disconnected
+ */
+gboolean
+gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
+                            GClosure      *closure)
 {
-  GSList *slist;
-  
-  /* we remove all entries of this object the hard
-   * way (i.e. without signal emission).
-   */
-  for (slist = entries; slist; slist = slist->next)
-    {
-      GtkAccelEntry *entry;
-      
-      entry = slist->data;
-      
-      g_hash_table_remove (accel_entry_hash_table, entry);
-      g_object_unref (entry->accel_group);
-      g_chunk_free (entry, accel_entries_mem_chunk);
-    }
-  g_slist_free (entries);
-}
+  guint i;
 
-void
-gtk_accel_group_handle_add (GObject          *object,
-                           guint              accel_signal_id,
-                           GtkAccelGroup     *accel_group,
-                           guint              accel_key,
-                           GdkModifierType    accel_mods,
-                           GtkAccelFlags      accel_flags)
-{
-  GtkAccelEntry *entry;
-  
-  g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  g_return_if_fail (accel_signal_id > 0);
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
 
-  if (!gtk_accelerator_valid (accel_key, accel_mods))
-    return;
-  
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (!entry)
-    {
-      GSList *slist;
-      
-      g_object_ref (accel_group);
-      
-      entry = g_chunk_new (GtkAccelEntry, accel_entries_mem_chunk);
-      entry->accel_group = accel_group;
-      entry->accelerator_key = gdk_keyval_to_lower (accel_key);
-      entry->accelerator_mods = accel_mods & accel_group->modifier_mask;
-      entry->accel_flags = accel_flags & GTK_ACCEL_MASK;
-      entry->object = object;
-      entry->signal_id = accel_signal_id;
-      
-      g_hash_table_insert (accel_entry_hash_table, entry, entry);
-      
-      slist = g_object_steal_qdata (object, accel_entries_key_id);
-      slist = g_slist_prepend (slist, entry);
-      g_object_set_qdata_full (object, accel_entries_key_id, slist,
-                              (GDestroyNotify) gtk_accel_group_delete_entries);
-    }
+  for (i = 0; i < accel_group->priv->n_accels; i++)
+    if (accel_group->priv->priv_accels[i].closure == closure)
+      {
+        g_object_ref (accel_group);
+        quick_accel_remove (accel_group, i);
+        g_object_unref (accel_group);
+        return TRUE;
+      }
+  return FALSE;
 }
 
 /**
- * gtk_accel_group_remove:
- * @accel_group: a #GtkAccelGroup
- * @accel_key: accelerator keyval
- * @accel_mods: accelerator modifiers
- * @object: object the accelerator activates
- *
- * Removes an accelerator. The @accel_key, @accel_mods, and @object
- * arguments are the same ones used to add the accelerator
- * with gtk_accel_group_add().
- * 
- **/
-void
-gtk_accel_group_remove (GtkAccelGroup    *accel_group,
-                       guint              accel_key,
-                       GdkModifierType    accel_mods,
-                       GObject           *object)
+ * gtk_accel_group_disconnect_key:
+ * @accel_group: the accelerator group to install an accelerator in
+ * @accel_key: key value of the accelerator
+ * @accel_mods: modifier combination of the accelerator
+ *
+ * Removes an accelerator previously installed through
+ * gtk_accel_group_connect().
+ *
+ * Returns: %TRUE if there was an accelerator which could be
+ *     removed, %FALSE otherwise
+ */
+gboolean
+gtk_accel_group_disconnect_key (GtkAccelGroup   *accel_group,
+                                guint            accel_key,
+                                GdkModifierType  accel_mods)
 {
-  GtkAccelEntry *entry;
-  guint remove_accelerator_signal_id = 0;
-  
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  g_return_if_fail (G_IS_OBJECT (object));
-  
-  /* check for required signals in the objects branch
-   */
-  remove_accelerator_signal_id = g_signal_lookup ("remove-accelerator", G_OBJECT_TYPE (object));
-  if (!remove_accelerator_signal_id)
+  GtkAccelGroupEntry *entries;
+  GSList *slist, *clist = NULL;
+  gboolean removed_one = FALSE;
+  guint n;
+
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
+
+  g_object_ref (accel_group);
+
+  accel_key = gdk_keyval_to_lower (accel_key);
+  entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
+  while (n--)
     {
-      g_warning ("gtk_accel_group_remove(): could not find signal \"%s\""
-                "in the `%s' class ancestry",
-                "remove-accelerator",
-                g_type_name (G_OBJECT_TYPE (object)));
-      return;
+      GClosure *closure = g_closure_ref (entries[n].closure);
+
+      clist = g_slist_prepend (clist, closure);
     }
-  
-  /* prematurely abort if the entry is locked
-   */
-  if (accel_group->lock_count > 0)
-    return;
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (!entry ||
-      entry->accel_flags & GTK_ACCEL_LOCKED)
-    return;
-  if (entry->object != object)
+
+  for (slist = clist; slist; slist = slist->next)
     {
-      g_warning ("gtk_accel_group_remove(): invalid object reference for accel-group entry");
-      return;
+      GClosure *closure = slist->data;
+
+      removed_one |= gtk_accel_group_disconnect (accel_group, closure);
+      g_closure_unref (closure);
     }
-  
-  /* make sure our structures stay alive
-   */
-  g_object_ref (accel_group);
-  g_object_ref (object);
-  
-  /* remove the entry
-   */
-  g_signal_emit (entry->object, remove_accelerator_signal_id, 0,
-                accel_group,
-                gdk_keyval_to_lower (accel_key),
-                accel_mods & accel_group->modifier_mask);
-  
-  /* and release the structures again
-   */
+  g_slist_free (clist);
+
   g_object_unref (accel_group);
-  g_object_unref (object);
+
+  return removed_one;
 }
 
 void
-gtk_accel_group_handle_remove (GObject          *object,
-                              GtkAccelGroup     *accel_group,
-                              guint              accel_key,
-                              GdkModifierType    accel_mods)
+_gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
+                            GQuark         accel_path_quark)
 {
-  GtkAccelEntry *entry;
-  
-  g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (GTK_IS_ACCEL_GROUP(accel_group));
-  
-  entry = gtk_accel_group_lookup (accel_group, accel_key, accel_mods);
-  if (entry)
+  GSList *slist, *clist = NULL;
+  guint i;
+
+  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
+
+  g_object_ref (accel_group);
+
+  for (i = 0; i < accel_group->priv->n_accels; i++)
+    if (accel_group->priv->priv_accels[i].accel_path_quark == accel_path_quark)
+      {
+        GClosure *closure = g_closure_ref (accel_group->priv->priv_accels[i].closure);
+
+        clist = g_slist_prepend (clist, closure);
+      }
+
+  for (slist = clist; slist; slist = slist->next)
     {
-      if (entry->object == object)
-       {
-         GSList *slist;
-         
-         g_hash_table_remove (accel_entry_hash_table, entry);
-         
-         slist = g_object_steal_qdata (object, accel_entries_key_id);
-         if (slist)
-           {
-             slist = g_slist_remove (slist, entry);
-             if (slist)
-               g_object_set_qdata_full (object, accel_entries_key_id, slist,
-                                        (GDestroyNotify) gtk_accel_group_delete_entries);
-             
-             g_object_unref (accel_group);
-             
-             g_chunk_free (entry, accel_entries_mem_chunk);
-           }
-       }
-      else
-       g_warning ("gtk_accel_group_handle_remove(): invalid object reference for accel-group entry");
+      GClosure *closure = slist->data;
+
+      gtk_accel_group_disconnect (accel_group, closure);
+      gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
+      g_closure_unref (closure);
     }
-  else
-    g_warning ("gtk_accel_group_handle_remove(): attempt to remove unexisting accel-group entry");
+  g_slist_free (clist);
+
+  g_object_unref (accel_group);
 }
 
-guint
-gtk_accel_group_create_add (GType        class_type,
-                           GSignalFlags signal_flags,
-                           guint        handler_offset)
+GSList*
+_gtk_accel_group_get_accelerables (GtkAccelGroup *accel_group)
 {
-  g_return_val_if_fail (G_TYPE_IS_OBJECT (class_type), 0);
-
-  return g_signal_new ("add-accelerator",
-                      class_type,
-                      signal_flags,
-                      handler_offset,
-                      (GSignalAccumulator) NULL, NULL,
-                      gtk_marshal_VOID__UINT_OBJECT_UINT_FLAGS_FLAGS,
-                      G_TYPE_NONE, 5,
-                      G_TYPE_UINT,
-                      GTK_TYPE_ACCEL_GROUP,
-                      G_TYPE_UINT,
-                      GDK_TYPE_MODIFIER_TYPE,
-                      GTK_TYPE_ACCEL_FLAGS);
+    g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
+
+    return accel_group->priv->acceleratables;
 }
 
-guint
-gtk_accel_group_create_remove (GType        class_type,
-                              GSignalFlags signal_flags,
-                              guint        handler_offset)
+/**
+ * gtk_accel_group_query:
+ * @accel_group: the accelerator group to query
+ * @accel_key: key value of the accelerator
+ * @accel_mods: modifier combination of the accelerator
+ * @n_entries: (allow-none): location to return the number
+ *     of entries found, or %NULL
+ *
+ * Queries an accelerator group for all entries matching @accel_key
+ * and @accel_mods.
+ *
+ * Returns: (transfer none) (array length=n_entries): an array of
+ *     @n_entries #GtkAccelGroupEntry elements, or %NULL. The array
+ *     is owned by GTK+ and must not be freed.
+ */
+GtkAccelGroupEntry*
+gtk_accel_group_query (GtkAccelGroup   *accel_group,
+                       guint            accel_key,
+                       GdkModifierType  accel_mods,
+                       guint           *n_entries)
 {
-  g_return_val_if_fail (G_TYPE_IS_OBJECT (class_type), 0);
-
-  return g_signal_new ("remove-accelerator",
-                      class_type,
-                      signal_flags,
-                      handler_offset,
-                      (GSignalAccumulator) NULL, NULL,
-                      gtk_marshal_VOID__OBJECT_UINT_FLAGS,
-                      G_TYPE_NONE, 3,
-                      GTK_TYPE_ACCEL_GROUP,
-                      G_TYPE_UINT,
-                      GDK_TYPE_MODIFIER_TYPE);
+  GtkAccelGroupEntry *entries;
+  guint n;
+
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
+
+  entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
+
+  if (n_entries)
+    *n_entries = entries ? n : 0;
+
+  return entries;
 }
 
-GSList*
-gtk_accel_groups_from_object (GObject       *object)
+/**
+ * gtk_accel_group_from_accel_closure:
+ * @closure: a #GClosure
+ *
+ * Finds the #GtkAccelGroup to which @closure is connected;
+ * see gtk_accel_group_connect().
+ *
+ * Returns: (transfer none): the #GtkAccelGroup to which @closure
+ *     is connected, or %NULL
+ */
+GtkAccelGroup*
+gtk_accel_group_from_accel_closure (GClosure *closure)
 {
-  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
-  
-  return g_object_get_qdata (object, accel_groups_key_id);
+  guint i;
+
+  g_return_val_if_fail (closure != NULL, NULL);
+
+  /* A few remarks on what we do here. in general, we need a way to
+   * reverse lookup accel_groups from closures that are being used in
+   * accel groups. this could be done e.g via a hashtable. it is however
+   * cheaper (memory wise) to just use the invalidation notifier on the
+   * closure itself (which we need to install anyway), that contains the
+   * accel group as data which, besides needing to peek a bit at closure
+   * internals, works just as good.
+   */
+  for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
+    if (closure->notifiers[i].notify == accel_closure_invalidate)
+      return closure->notifiers[i].data;
+
+  return NULL;
 }
 
-GSList*
-gtk_accel_group_entries_from_object (GObject        *object)
+/**
+ * gtk_accel_group_activate:
+ * @accel_group: a #GtkAccelGroup
+ * @accel_quark: the quark for the accelerator name
+ * @acceleratable: the #GObject, usually a #GtkWindow, on which
+ *    to activate the accelerator
+ * @accel_key: accelerator keyval from a key event
+ * @accel_mods: keyboard state mask from a key event
+ *
+ * Finds the first accelerator in @accel_group that matches
+ * @accel_key and @accel_mods, and activates it.
+ *
+ * Returns: %TRUE if an accelerator was activated and handled
+ *     this keypress
+ */
+gboolean
+gtk_accel_group_activate (GtkAccelGroup   *accel_group,
+                          GQuark           accel_quark,
+                          GObject         *acceleratable,
+                          guint            accel_key,
+                          GdkModifierType  accel_mods)
 {
-  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
-  
-  return g_object_get_qdata (object, accel_entries_key_id);
+  gboolean was_handled;
+
+  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
+  g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
+
+  was_handled = FALSE;
+  g_signal_emit (accel_group, signal_accel_activate, accel_quark,
+                 acceleratable, accel_key, accel_mods, &was_handled);
+
+  return was_handled;
+}
+
+/**
+ * gtk_accel_groups_activate:
+ * @object: the #GObject, usually a #GtkWindow, on which
+ *     to activate the accelerator
+ * @accel_key: accelerator keyval from a key event
+ * @accel_mods: keyboard state mask from a key event
+ *
+ * Finds the first accelerator in any #GtkAccelGroup attached
+ * to @object that matches @accel_key and @accel_mods, and
+ * activates that accelerator.
+ *
+ * Returns: %TRUE if an accelerator was activated and handled
+ *     this keypress
+ */
+gboolean
+gtk_accel_groups_activate (GObject         *object,
+                           guint            accel_key,
+                           GdkModifierType  accel_mods)
+{
+  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
+
+  if (gtk_accelerator_valid (accel_key, accel_mods))
+    {
+      gchar *accel_name;
+      GQuark accel_quark;
+      GSList *slist;
+
+      accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
+      accel_quark = g_quark_from_string (accel_name);
+      g_free (accel_name);
+
+      for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
+        if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
+          return TRUE;
+    }
+
+  return FALSE;
 }
 
 /**
  * gtk_accelerator_valid:
  * @keyval: a GDK keyval
  * @modifiers: modifier mask
- * 
+ *
  * Determines whether a given keyval and modifier mask constitute
- * a valid keyboard accelerator. For example, the GDK_a keyval
- * plus GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
- * But you can't use the NumLock key as an accelerator.
- * 
- * Return value: %TRUE if the accelerator is valid
- **/
+ * a valid keyboard accelerator. For example, the #GDK_KEY_a keyval
+ * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
+ * But, you can't, for instance, use the #GDK_KEY_Control_L keyval
+ * as an accelerator.
+ *
+ * Returns: %TRUE if the accelerator is valid
+ */
 gboolean
-gtk_accelerator_valid (guint             keyval,
-                      GdkModifierType    modifiers)
+gtk_accelerator_valid (guint           keyval,
+                       GdkModifierType modifiers)
 {
   static const guint invalid_accelerator_vals[] = {
-    GDK_BackSpace, GDK_Delete, GDK_KP_Delete,
-    GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
-    GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
-    GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
-    GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
-    GDK_Scroll_Lock, GDK_Sys_Req, 
-    GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
-    GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
-    GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
-    GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
-    GDK_Terminate_Server, GDK_AudibleBell_Enable,
+    GDK_KEY_Shift_L, GDK_KEY_Shift_R, GDK_KEY_Shift_Lock,
+    GDK_KEY_Caps_Lock, GDK_KEY_ISO_Lock, GDK_KEY_Control_L,
+    GDK_KEY_Control_R, GDK_KEY_Meta_L, GDK_KEY_Meta_R,
+    GDK_KEY_Alt_L, GDK_KEY_Alt_R, GDK_KEY_Super_L, GDK_KEY_Super_R,
+    GDK_KEY_Hyper_L, GDK_KEY_Hyper_R, GDK_KEY_ISO_Level3_Shift,
+    GDK_KEY_ISO_Next_Group, GDK_KEY_ISO_Prev_Group,
+    GDK_KEY_ISO_First_Group, GDK_KEY_ISO_Last_Group,
+    GDK_KEY_Mode_switch, GDK_KEY_Num_Lock, GDK_KEY_Multi_key,
+    GDK_KEY_Scroll_Lock, GDK_KEY_Sys_Req,
+    GDK_KEY_Tab, GDK_KEY_ISO_Left_Tab, GDK_KEY_KP_Tab,
+    GDK_KEY_First_Virtual_Screen, GDK_KEY_Prev_Virtual_Screen,
+    GDK_KEY_Next_Virtual_Screen, GDK_KEY_Last_Virtual_Screen,
+    GDK_KEY_Terminate_Server, GDK_KEY_AudibleBell_Enable,
+    0
+  };
+  static const guint invalid_unmodified_vals[] = {
+    GDK_KEY_Up, GDK_KEY_Down, GDK_KEY_Left, GDK_KEY_Right,
+    GDK_KEY_KP_Up, GDK_KEY_KP_Down, GDK_KEY_KP_Left, GDK_KEY_KP_Right,
     0
   };
   const guint *ac_val;
 
   modifiers &= GDK_MODIFIER_MASK;
-    
+
   if (keyval <= 0xFF)
     return keyval >= 0x20;
 
@@ -918,7 +1005,17 @@ gtk_accelerator_valid (guint                keyval,
   while (*ac_val)
     {
       if (keyval == *ac_val++)
-       return FALSE;
+        return FALSE;
+    }
+
+  if (!modifiers)
+    {
+      ac_val = invalid_unmodified_vals;
+      while (*ac_val)
+        {
+          if (keyval == *ac_val++)
+            return FALSE;
+        }
     }
 
   return TRUE;
@@ -928,232 +1025,491 @@ static inline gboolean
 is_alt (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'a' || string[1] == 'A') &&
-         (string[2] == 'l' || string[2] == 'L') &&
-         (string[3] == 't' || string[3] == 'T') &&
-         (string[4] == '>'));
+          (string[1] == 'a' || string[1] == 'A') &&
+          (string[2] == 'l' || string[2] == 'L') &&
+          (string[3] == 't' || string[3] == 'T') &&
+          (string[4] == '>'));
 }
 
 static inline gboolean
 is_ctl (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'c' || string[1] == 'C') &&
-         (string[2] == 't' || string[2] == 'T') &&
-         (string[3] == 'l' || string[3] == 'L') &&
-         (string[4] == '>'));
+          (string[1] == 'c' || string[1] == 'C') &&
+          (string[2] == 't' || string[2] == 'T') &&
+          (string[3] == 'l' || string[3] == 'L') &&
+          (string[4] == '>'));
 }
 
 static inline gboolean
 is_modx (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'm' || string[1] == 'M') &&
-         (string[2] == 'o' || string[2] == 'O') &&
-         (string[3] == 'd' || string[3] == 'D') &&
-         (string[4] >= '1' && string[4] <= '5') &&
-         (string[5] == '>'));
+          (string[1] == 'm' || string[1] == 'M') &&
+          (string[2] == 'o' || string[2] == 'O') &&
+          (string[3] == 'd' || string[3] == 'D') &&
+          (string[4] >= '1' && string[4] <= '5') &&
+          (string[5] == '>'));
 }
 
 static inline gboolean
 is_ctrl (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'c' || string[1] == 'C') &&
-         (string[2] == 't' || string[2] == 'T') &&
-         (string[3] == 'r' || string[3] == 'R') &&
-         (string[4] == 'l' || string[4] == 'L') &&
-         (string[5] == '>'));
+          (string[1] == 'c' || string[1] == 'C') &&
+          (string[2] == 't' || string[2] == 'T') &&
+          (string[3] == 'r' || string[3] == 'R') &&
+          (string[4] == 'l' || string[4] == 'L') &&
+          (string[5] == '>'));
 }
 
 static inline gboolean
 is_shft (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 's' || string[1] == 'S') &&
-         (string[2] == 'h' || string[2] == 'H') &&
-         (string[3] == 'f' || string[3] == 'F') &&
-         (string[4] == 't' || string[4] == 'T') &&
-         (string[5] == '>'));
+          (string[1] == 's' || string[1] == 'S') &&
+          (string[2] == 'h' || string[2] == 'H') &&
+          (string[3] == 'f' || string[3] == 'F') &&
+          (string[4] == 't' || string[4] == 'T') &&
+          (string[5] == '>'));
 }
 
 static inline gboolean
 is_shift (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 's' || string[1] == 'S') &&
-         (string[2] == 'h' || string[2] == 'H') &&
-         (string[3] == 'i' || string[3] == 'I') &&
-         (string[4] == 'f' || string[4] == 'F') &&
-         (string[5] == 't' || string[5] == 'T') &&
-         (string[6] == '>'));
+          (string[1] == 's' || string[1] == 'S') &&
+          (string[2] == 'h' || string[2] == 'H') &&
+          (string[3] == 'i' || string[3] == 'I') &&
+          (string[4] == 'f' || string[4] == 'F') &&
+          (string[5] == 't' || string[5] == 'T') &&
+          (string[6] == '>'));
 }
 
 static inline gboolean
 is_control (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'c' || string[1] == 'C') &&
-         (string[2] == 'o' || string[2] == 'O') &&
-         (string[3] == 'n' || string[3] == 'N') &&
-         (string[4] == 't' || string[4] == 'T') &&
-         (string[5] == 'r' || string[5] == 'R') &&
-         (string[6] == 'o' || string[6] == 'O') &&
-         (string[7] == 'l' || string[7] == 'L') &&
-         (string[8] == '>'));
+          (string[1] == 'c' || string[1] == 'C') &&
+          (string[2] == 'o' || string[2] == 'O') &&
+          (string[3] == 'n' || string[3] == 'N') &&
+          (string[4] == 't' || string[4] == 'T') &&
+          (string[5] == 'r' || string[5] == 'R') &&
+          (string[6] == 'o' || string[6] == 'O') &&
+          (string[7] == 'l' || string[7] == 'L') &&
+          (string[8] == '>'));
 }
 
 static inline gboolean
 is_release (const gchar *string)
 {
   return ((string[0] == '<') &&
-         (string[1] == 'r' || string[1] == 'R') &&
-         (string[2] == 'e' || string[2] == 'E') &&
-         (string[3] == 'l' || string[3] == 'L') &&
-         (string[4] == 'e' || string[4] == 'E') &&
+          (string[1] == 'r' || string[1] == 'R') &&
+          (string[2] == 'e' || string[2] == 'E') &&
+          (string[3] == 'l' || string[3] == 'L') &&
+          (string[4] == 'e' || string[4] == 'E') &&
+          (string[5] == 'a' || string[5] == 'A') &&
+          (string[6] == 's' || string[6] == 'S') &&
+          (string[7] == 'e' || string[7] == 'E') &&
+          (string[8] == '>'));
+}
+
+static inline gboolean
+is_meta (const gchar *string)
+{
+  return ((string[0] == '<') &&
+          (string[1] == 'm' || string[1] == 'M') &&
+          (string[2] == 'e' || string[2] == 'E') &&
+          (string[3] == 't' || string[3] == 'T') &&
+          (string[4] == 'a' || string[4] == 'A') &&
+          (string[5] == '>'));
+}
+
+static inline gboolean
+is_super (const gchar *string)
+{
+  return ((string[0] == '<') &&
+          (string[1] == 's' || string[1] == 'S') &&
+          (string[2] == 'u' || string[2] == 'U') &&
+          (string[3] == 'p' || string[3] == 'P') &&
+          (string[4] == 'e' || string[4] == 'E') &&
+          (string[5] == 'r' || string[5] == 'R') &&
+          (string[6] == '>'));
+}
+
+static inline gboolean
+is_hyper (const gchar *string)
+{
+  return ((string[0] == '<') &&
+          (string[1] == 'h' || string[1] == 'H') &&
+          (string[2] == 'y' || string[2] == 'Y') &&
+          (string[3] == 'p' || string[3] == 'P') &&
+          (string[4] == 'e' || string[4] == 'E') &&
+          (string[5] == 'r' || string[5] == 'R') &&
+          (string[6] == '>'));
+}
+
+static inline gboolean
+is_primary (const gchar *string)
+{
+  return ((string[0] == '<') &&
+         (string[1] == 'p' || string[1] == 'P') &&
+         (string[2] == 'r' || string[2] == 'R') &&
+         (string[3] == 'i' || string[3] == 'I') &&
+         (string[4] == 'm' || string[4] == 'M') &&
          (string[5] == 'a' || string[5] == 'A') &&
-         (string[6] == 's' || string[6] == 'S') &&
-         (string[7] == 'e' || string[7] == 'E') &&
+         (string[6] == 'r' || string[6] == 'R') &&
+         (string[7] == 'y' || string[7] == 'Y') &&
          (string[8] == '>'));
 }
 
+static inline gboolean
+is_keycode (const gchar *string)
+{
+  return (string[0] == '0' &&
+          string[1] == 'x' &&
+          g_ascii_isxdigit (string[2]) &&
+          g_ascii_isxdigit (string[3]));
+}
+
 /**
- * gtk_accelerator_parse:
+ * gtk_accelerator_parse_with_keycode:
  * @accelerator: string representing an accelerator
- * @accelerator_key: return location for accelerator keyval
- * @accelerator_mods: return location for accelerator mod mask
+ * @accelerator_key: (out) (allow-none): return location for accelerator
+ *     keyval, or %NULL
+ * @accelerator_codes: (out) (allow-none): return location for accelerator
+ *     keycodes, or %NULL
+ * @accelerator_mods: (out) (allow-none): return location for accelerator
+ *     modifier mask, %NULL
  *
- * Parses a string representing an accelerator. The
- * format looks like "<Control>a" or "<Shift><Alt>F1" or
- * "<Release>z" (the last one is for key release).
- * The parser is fairly liberal and allows lower or upper case,
- * and also abbreviations such as "<Ctl>" and "<Ctrl>".
+ * Parses a string representing an accelerator, similarly to
+ * gtk_accelerator_parse() but handles keycodes as well. This is only
+ * useful for system-level components, applications should use
+ * gtk_accelerator_parse() instead.
  *
- * If the parse fails, @accelerator_key and @accelerator_mods will
- * be set to 0 (zero).
- **/
+ * If a keycode is present in the accelerator and no @accelerator_codes
+ * is given, the parse will fail.
+ *
+ * If the parse fails, @accelerator_key, @accelerator_mods and
+ * @accelerator_codes will be set to 0 (zero).
+ *
+ * Since: 3.4
+ */
 void
-gtk_accelerator_parse (const gchar    *accelerator,
-                      guint          *accelerator_key,
-                      GdkModifierType*accelerator_mods)
+gtk_accelerator_parse_with_keycode (const gchar     *accelerator,
+                                    guint           *accelerator_key,
+                                    guint          **accelerator_codes,
+                                    GdkModifierType *accelerator_mods)
 {
   guint keyval;
   GdkModifierType mods;
   gint len;
-  
+  gboolean error;
+
   if (accelerator_key)
     *accelerator_key = 0;
   if (accelerator_mods)
     *accelerator_mods = 0;
+  if (accelerator_codes)
+    *accelerator_codes = NULL;
   g_return_if_fail (accelerator != NULL);
-  
+
+  error = FALSE;
   keyval = 0;
   mods = 0;
   len = strlen (accelerator);
   while (len)
     {
       if (*accelerator == '<')
-       {
-         if (len >= 9 && is_release (accelerator))
-           {
-             accelerator += 9;
-             len -= 9;
-             mods |= GDK_RELEASE_MASK;
-           }
-         else if (len >= 9 && is_control (accelerator))
-           {
-             accelerator += 9;
-             len -= 9;
-             mods |= GDK_CONTROL_MASK;
-           }
-         else if (len >= 7 && is_shift (accelerator))
-           {
-             accelerator += 7;
-             len -= 7;
-             mods |= GDK_SHIFT_MASK;
-           }
-         else if (len >= 6 && is_shft (accelerator))
-           {
-             accelerator += 6;
-             len -= 6;
-             mods |= GDK_SHIFT_MASK;
-           }
-         else if (len >= 6 && is_ctrl (accelerator))
-           {
-             accelerator += 6;
-             len -= 6;
-             mods |= GDK_CONTROL_MASK;
-           }
-         else if (len >= 6 && is_modx (accelerator))
-           {
-             static const guint mod_vals[] = {
-               GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
-               GDK_MOD4_MASK, GDK_MOD5_MASK
-             };
-
-             len -= 6;
-             accelerator += 4;
-             mods |= mod_vals[*accelerator - '1'];
-             accelerator += 2;
-           }
-         else if (len >= 5 && is_ctl (accelerator))
-           {
-             accelerator += 5;
-             len -= 5;
-             mods |= GDK_CONTROL_MASK;
-           }
-         else if (len >= 5 && is_alt (accelerator))
-           {
-             accelerator += 5;
-             len -= 5;
-             mods |= GDK_MOD1_MASK;
-           }
+        {
+          if (len >= 9 && is_release (accelerator))
+            {
+              accelerator += 9;
+              len -= 9;
+              mods |= GDK_RELEASE_MASK;
+            }
+          else if (len >= 9 && is_primary (accelerator))
+            {
+              accelerator += 9;
+              len -= 9;
+              mods |= _gtk_get_primary_accel_mod ();
+            }
+          else if (len >= 9 && is_control (accelerator))
+            {
+              accelerator += 9;
+              len -= 9;
+              mods |= GDK_CONTROL_MASK;
+            }
+          else if (len >= 7 && is_shift (accelerator))
+            {
+              accelerator += 7;
+              len -= 7;
+              mods |= GDK_SHIFT_MASK;
+            }
+          else if (len >= 6 && is_shft (accelerator))
+            {
+              accelerator += 6;
+              len -= 6;
+              mods |= GDK_SHIFT_MASK;
+            }
+          else if (len >= 6 && is_ctrl (accelerator))
+            {
+              accelerator += 6;
+              len -= 6;
+              mods |= GDK_CONTROL_MASK;
+            }
+          else if (len >= 6 && is_modx (accelerator))
+            {
+              static const guint mod_vals[] = {
+                GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
+                GDK_MOD4_MASK, GDK_MOD5_MASK
+              };
+
+              len -= 6;
+              accelerator += 4;
+              mods |= mod_vals[*accelerator - '1'];
+              accelerator += 2;
+            }
+          else if (len >= 5 && is_ctl (accelerator))
+            {
+              accelerator += 5;
+              len -= 5;
+              mods |= GDK_CONTROL_MASK;
+            }
+          else if (len >= 5 && is_alt (accelerator))
+            {
+              accelerator += 5;
+              len -= 5;
+              mods |= GDK_MOD1_MASK;
+            }
+          else if (len >= 6 && is_meta (accelerator))
+            {
+              accelerator += 6;
+              len -= 6;
+              mods |= GDK_META_MASK;
+            }
+          else if (len >= 7 && is_hyper (accelerator))
+            {
+              accelerator += 7;
+              len -= 7;
+              mods |= GDK_HYPER_MASK;
+            }
+          else if (len >= 7 && is_super (accelerator))
+            {
+              accelerator += 7;
+              len -= 7;
+              mods |= GDK_SUPER_MASK;
+            }
+          else
+            {
+              gchar last_ch;
+
+              last_ch = *accelerator;
+              while (last_ch && last_ch != '>')
+                {
+                  last_ch = *accelerator;
+                  accelerator += 1;
+                  len -= 1;
+                }
+            }
+        }
+      else
+        {
+          if (len >= 4 && is_keycode (accelerator))
+            {
+               char keystring[5];
+               gchar *endptr;
+               gint tmp_keycode;
+
+               memcpy (keystring, accelerator, 4);
+               keystring [4] = '\000';
+
+               tmp_keycode = strtol (keystring, &endptr, 16);
+
+               if (endptr == NULL || *endptr != '\000')
+                 {
+                   error = TRUE;
+                   goto out;
+                 }
+               else if (accelerator_codes != NULL)
+                 {
+                   /* 0x00 is an invalid keycode too. */
+                   if (tmp_keycode == 0)
+                     {
+                       error = TRUE;
+                       goto out;
+                     }
+                   else
+                     {
+                       *accelerator_codes = g_new0 (guint, 2);
+                       (*accelerator_codes)[0] = tmp_keycode;
+                     }
+                 }
+               else
+                 {
+                   /* There was a keycode in the string, but
+                    * we cannot store it, so we have an error */
+                   error = TRUE;
+                   goto out;
+                 }
+            }
          else
            {
-             gchar last_ch;
-             
-             last_ch = *accelerator;
-             while (last_ch && last_ch != '>')
-               {
-                 last_ch = *accelerator;
-                 accelerator += 1;
-                 len -= 1;
+             keyval = gdk_keyval_from_name (accelerator);
+             if (keyval == GDK_KEY_VoidSymbol)
+               {
+                 error = TRUE;
+                 goto out;
                }
            }
-       }
-      else
-       {
-         keyval = gdk_keyval_from_name (accelerator);
-         accelerator += len;
-         len -= len;
-       }
+
+          if (keyval && accelerator_codes != NULL)
+            {
+              GdkKeymapKey *keys;
+              gint n_keys, i, j;
+
+              if (!gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (), keyval, &keys, &n_keys))
+                {
+                  /* Not in keymap */
+                  error = TRUE;
+                  goto out;
+                }
+              else
+                {
+                  *accelerator_codes = g_new0 (guint, n_keys + 1);
+
+                  /* Prefer level-0 keys to modified keys */
+                  for (i = 0, j = 0; i < n_keys; ++i)
+                    {
+                      if (keys[i].level == 0)
+                        (*accelerator_codes)[j++] = keys[i].keycode;
+                    }
+
+                  /* No level-0 keys? Find in the whole keymap */
+                  if (j == 0)
+                    {
+                      for (i = 0, j = 0; i < n_keys; ++i)
+                        (*accelerator_codes)[j++] = keys[i].keycode;
+                    }
+
+                  if (j == 0)
+                    {
+                      g_free (*accelerator_codes);
+                      *accelerator_codes = NULL;
+                      /* Not in keymap */
+                      error = TRUE;
+                      goto out;
+                    }
+                  g_free (keys);
+                }
+            }
+
+          accelerator += len;
+          len -= len;
+        }
     }
-  
+
+out:
+  if (error)
+    keyval = mods = 0;
+
   if (accelerator_key)
     *accelerator_key = gdk_keyval_to_lower (keyval);
   if (accelerator_mods)
     *accelerator_mods = mods;
 }
 
+/**
+ * gtk_accelerator_parse:
+ * @accelerator: string representing an accelerator
+ * @accelerator_key: (out) (allow-none): return location for accelerator
+ *     keyval, or %NULL
+ * @accelerator_mods: (out) (allow-none): return location for accelerator
+ *     modifier mask, %NULL
+ *
+ * Parses a string representing an accelerator. The
+ * format looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1"
+ * or "&lt;Release&gt;z" (the last one is for key release).
+ *
+ * The parser is fairly liberal and allows lower or upper case,
+ * and also abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
+ * Key names are parsed using gdk_keyval_from_name(). For character
+ * keys the name is not the symbol, but the lowercase name, e.g. one
+ * would use "&lt;Ctrl&gt;minus" instead of "&lt;Ctrl&gt;-".
+ *
+ * If the parse fails, @accelerator_key and @accelerator_mods will
+ * be set to 0 (zero).
+ */
+void
+gtk_accelerator_parse (const gchar     *accelerator,
+                       guint           *accelerator_key,
+                       GdkModifierType *accelerator_mods)
+{
+  gtk_accelerator_parse_with_keycode (accelerator, accelerator_key, NULL, accelerator_mods);
+}
+
+/**
+ * gtk_accelerator_name_with_keycode:
+ * @display: (allow-none): a #GdkDisplay or %NULL to use the default display
+ * @accelerator_key: accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ *
+ * Converts an accelerator keyval and modifier mask
+ * into a string parseable by gtk_accelerator_parse_full(),
+ * similarly to gtk_accelerator_name() but handling keycodes.
+ * This is only useful for system-level components, applications
+ * should use gtk_accelerator_parse() instead.
+ *
+ * Returns: a newly allocated accelerator name.
+ *
+ * Since: 3.4
+ */
+gchar *
+gtk_accelerator_name_with_keycode (GdkDisplay      *display,
+                                   guint            accelerator_key,
+                                   guint            keycode,
+                                   GdkModifierType  accelerator_mods)
+{
+  gchar *gtk_name;
+
+  if (display == NULL)
+    display = gdk_display_manager_get_default_display (gdk_display_manager_get ());
+
+  gdk_keymap_add_virtual_modifiers (gdk_keymap_get_for_display (display), &accelerator_mods);
+  gtk_name = gtk_accelerator_name (accelerator_key, accelerator_mods);
+
+  if (!accelerator_key)
+    {
+      gchar *name;
+      name = g_strdup_printf ("%s0x%02x", gtk_name, keycode);
+      g_free (gtk_name);
+      return name;
+    }
+
+  return gtk_name;
+}
+
 /**
  * gtk_accelerator_name:
- * @accelerator_key: an accelerator keyval
- * @accelerator_mods: modifier mask
- * 
+ * @accelerator_key: accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ *
  * Converts an accelerator keyval and modifier mask
  * into a string parseable by gtk_accelerator_parse().
- * For example, if you pass in GDK_q and GDK_CONTROL_MASK,
- * this function returns "<Control>q". 
+ * For example, if you pass in #GDK_KEY_q and #GDK_CONTROL_MASK,
+ * this function returns "&lt;Control&gt;q".
+ *
+ * If you need to display accelerators in the user interface,
+ * see gtk_accelerator_get_label().
  *
- * The caller of this function must free the return value.
- * 
- * Return value: the new accelerator name
- **/
+ * Returns: a newly-allocated accelerator name
+ */
 gchar*
 gtk_accelerator_name (guint           accelerator_key,
-                     GdkModifierType accelerator_mods)
+                      GdkModifierType accelerator_mods)
 {
   static const gchar text_release[] = "<Release>";
+  static const gchar text_primary[] = "<Primary>";
   static const gchar text_shift[] = "<Shift>";
   static const gchar text_control[] = "<Control>";
   static const gchar text_mod1[] = "<Alt>";
@@ -1161,6 +1517,10 @@ gtk_accelerator_name (guint           accelerator_key,
   static const gchar text_mod3[] = "<Mod3>";
   static const gchar text_mod4[] = "<Mod4>";
   static const gchar text_mod5[] = "<Mod5>";
+  static const gchar text_meta[] = "<Meta>";
+  static const gchar text_super[] = "<Super>";
+  static const gchar text_hyper[] = "<Hyper>";
+  GdkModifierType saved_mods;
   guint l;
   gchar *keyval_name;
   gchar *accelerator;
@@ -1171,9 +1531,15 @@ gtk_accelerator_name (guint           accelerator_key,
   if (!keyval_name)
     keyval_name = "";
 
+  saved_mods = accelerator_mods;
   l = 0;
   if (accelerator_mods & GDK_RELEASE_MASK)
     l += sizeof (text_release) - 1;
+  if (accelerator_mods & _gtk_get_primary_accel_mod ())
+    {
+      l += sizeof (text_primary) - 1;
+      accelerator_mods &= ~_gtk_get_primary_accel_mod (); /* consume the default accel */
+    }
   if (accelerator_mods & GDK_SHIFT_MASK)
     l += sizeof (text_shift) - 1;
   if (accelerator_mods & GDK_CONTROL_MASK)
@@ -1189,9 +1555,16 @@ gtk_accelerator_name (guint           accelerator_key,
   if (accelerator_mods & GDK_MOD5_MASK)
     l += sizeof (text_mod5) - 1;
   l += strlen (keyval_name);
+  if (accelerator_mods & GDK_META_MASK)
+    l += sizeof (text_meta) - 1;
+  if (accelerator_mods & GDK_HYPER_MASK)
+    l += sizeof (text_hyper) - 1;
+  if (accelerator_mods & GDK_SUPER_MASK)
+    l += sizeof (text_super) - 1;
 
   accelerator = g_new (gchar, l + 1);
 
+  accelerator_mods = saved_mods;
   l = 0;
   accelerator[l] = 0;
   if (accelerator_mods & GDK_RELEASE_MASK)
@@ -1199,6 +1572,12 @@ gtk_accelerator_name (guint           accelerator_key,
       strcpy (accelerator + l, text_release);
       l += sizeof (text_release) - 1;
     }
+  if (accelerator_mods & _gtk_get_primary_accel_mod ())
+    {
+      strcpy (accelerator + l, text_primary);
+      l += sizeof (text_primary) - 1;
+      accelerator_mods &= ~_gtk_get_primary_accel_mod (); /* consume the default accel */
+    }
   if (accelerator_mods & GDK_SHIFT_MASK)
     {
       strcpy (accelerator + l, text_shift);
@@ -1234,38 +1613,128 @@ gtk_accelerator_name (guint           accelerator_key,
       strcpy (accelerator + l, text_mod5);
       l += sizeof (text_mod5) - 1;
     }
+  if (accelerator_mods & GDK_META_MASK)
+    {
+      strcpy (accelerator + l, text_meta);
+      l += sizeof (text_meta) - 1;
+    }
+  if (accelerator_mods & GDK_HYPER_MASK)
+    {
+      strcpy (accelerator + l, text_hyper);
+      l += sizeof (text_hyper) - 1;
+    }
+  if (accelerator_mods & GDK_SUPER_MASK)
+    {
+      strcpy (accelerator + l, text_super);
+      l += sizeof (text_super) - 1;
+    }
   strcpy (accelerator + l, keyval_name);
 
   return accelerator;
 }
 
+/**
+ * gtk_accelerator_get_label_with_keycode:
+ * @display: (allow-none): a #GdkDisplay or %NULL to use the default display
+ * @accelerator_key: accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ *
+ * Converts an accelerator keyval and modifier mask
+ * into a (possibly translated) string that can be displayed to
+ * a user, similarly to gtk_accelerator_get_label(), but handling
+ * keycodes.
+ *
+ * This is only useful for system-level components, applications
+ * should use gtk_accelerator_parse() instead.
+ *
+ * Returns: a newly-allocated string representing the accelerator.
+ *
+ * Since: 3.4
+ */
+gchar *
+gtk_accelerator_get_label_with_keycode (GdkDisplay      *display,
+                                        guint            accelerator_key,
+                                        guint            keycode,
+                                        GdkModifierType  accelerator_mods)
+{
+  gchar *gtk_label;
+
+  if (display == NULL)
+    display = gdk_display_manager_get_default_display (gdk_display_manager_get ());
+
+  gdk_keymap_add_virtual_modifiers (gdk_keymap_get_for_display (display), &accelerator_mods);
+  gtk_label = gtk_accelerator_get_label (accelerator_key, accelerator_mods);
+
+  if (!accelerator_key)
+    {
+      gchar *label;
+      label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
+      g_free (gtk_label);
+      return label;
+    }
+
+  return gtk_label;
+}
+
+/**
+ * gtk_accelerator_get_label:
+ * @accelerator_key: accelerator keyval
+ * @accelerator_mods: accelerator modifier mask
+ *
+ * Converts an accelerator keyval and modifier mask into a string
+ * which can be used to represent the accelerator to the user.
+ *
+ * Returns: a newly-allocated string representing the accelerator.
+ *
+ * Since: 2.6
+ */
+gchar*
+gtk_accelerator_get_label (guint           accelerator_key,
+                           GdkModifierType accelerator_mods)
+{
+  GtkAccelLabelClass *klass;
+  gchar *label;
+
+  klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
+  label = _gtk_accel_label_class_get_accelerator_label (klass,
+                                                        accelerator_key,
+                                                        accelerator_mods);
+  g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
+
+  return label;
+}
+
 /**
  * gtk_accelerator_set_default_mod_mask:
- * @default_mod_mask: a modifier mask
+ * @default_mod_mask: accelerator modifier mask
  *
  * Sets the modifiers that will be considered significant for keyboard
  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
- * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
- * Other modifiers will be ignored by #GtkAccelGroup.
+ * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK |
+ * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt,
+ * Super, Hyper and Meta. Other modifiers will by default be ignored
+ * by #GtkAccelGroup.
+ * You must include at least the three modifiers Control, Shift
+ * and Alt in any value you pass to this function.
  *
  * The default mod mask should be changed on application startup,
- * before creating any accelerator groups.
- * 
- **/
+ * before using any accelerator groups.
+ */
 void
 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
 {
-  default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
+  default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
+    (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
 }
 
 /**
  * gtk_accelerator_get_default_mod_mask:
  *
  * Gets the value set by gtk_accelerator_set_default_mod_mask().
- * 
- * Return value: the default modifier mask.
- **/
-guint
+ *
+ * Returns: the default accelerator modifier mask
+ */
+GdkModifierType
 gtk_accelerator_get_default_mod_mask (void)
 {
   return default_accel_mod_mask;