1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1998, 2001 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkaccelgroup.h"
32 #include "gtkaccellabel.h" /* For _gtk_accel_label_class_get_accelerator_label */
33 #include "gtkaccelmap.h"
35 #include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkmarshalers.h"
41 * SECTION:gtkaccelgroup
42 * @Short_description: Groups of global keyboard accelerators for an entire GtkWindow
43 * @Title: Accelerator Groups
44 * @See_also:gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
45 * gtk_item_factory_new(), gtk_label_new_with_mnemonic()
47 * A #GtkAccelGroup represents a group of keyboard accelerators,
48 * typically attached to a toplevel #GtkWindow (with
49 * gtk_window_add_accel_group()). Usually you won't need to create a
50 * #GtkAccelGroup directly; instead, when using #GtkItemFactory, GTK+
51 * automatically sets up the accelerators for your menus in the item
52 * factory's #GtkAccelGroup.
55 * Note that <firstterm>accelerators</firstterm> are different from
56 * <firstterm>mnemonics</firstterm>. Accelerators are shortcuts for
57 * activating a menu item; they appear alongside the menu item they're a
58 * shortcut for. For example "Ctrl+Q" might appear alongside the "Quit"
59 * menu item. Mnemonics are shortcuts for GUI elements such as text
60 * entries or buttons; they appear as underlined characters. See
61 * gtk_label_new_with_mnemonic(). Menu items can have both accelerators
62 * and mnemonics, of course.
65 #define GTK_ACCEL_GROUP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ACCEL_GROUP, GtkAccelGroupPrivate))
67 struct _GtkAccelGroupPrivate
70 GdkModifierType modifier_mask;
71 GSList *acceleratables;
73 GtkAccelGroupEntry *priv_accels;
76 /* --- prototypes --- */
77 static void gtk_accel_group_finalize (GObject *object);
78 static void gtk_accel_group_get_property (GObject *object,
82 static void accel_closure_invalidate (gpointer data,
86 /* --- variables --- */
87 static guint signal_accel_activate = 0;
88 static guint signal_accel_changed = 0;
89 static guint quark_acceleratable_groups = 0;
90 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
104 G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
106 /* --- functions --- */
108 gtk_accel_group_class_init (GtkAccelGroupClass *class)
110 GObjectClass *object_class = G_OBJECT_CLASS (class);
112 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
114 object_class->finalize = gtk_accel_group_finalize;
115 object_class->get_property = gtk_accel_group_get_property;
117 class->accel_changed = NULL;
119 g_object_class_install_property (object_class,
121 g_param_spec_boolean ("is-locked",
123 "Is the accel group locked",
127 g_object_class_install_property (object_class,
129 g_param_spec_flags ("modifier-mask",
132 GDK_TYPE_MODIFIER_TYPE,
133 default_accel_mod_mask,
137 * GtkAccelGroup::accel-activate:
138 * @accel_group: the #GtkAccelGroup which received the signal
139 * @acceleratable: the object on which the accelerator was activated
140 * @keyval: the accelerator keyval
141 * @modifier: the modifier combination of the accelerator
143 * The accel-activate signal is an implementation detail of
144 * #GtkAccelGroup and not meant to be used by applications.
146 * Returns: %TRUE if the accelerator was activated
148 signal_accel_activate =
149 g_signal_new (I_("accel-activate"),
150 G_OBJECT_CLASS_TYPE (class),
153 _gtk_boolean_handled_accumulator, NULL,
154 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
158 GDK_TYPE_MODIFIER_TYPE);
160 * GtkAccelGroup::accel-changed:
161 * @accel_group: the #GtkAccelGroup which received the signal
162 * @keyval: the accelerator keyval
163 * @modifier: the modifier combination of the accelerator
164 * @accel_closure: the #GClosure of the accelerator
166 * The accel-changed signal is emitted when a #GtkAccelGroupEntry
167 * is added to or removed from the accel group.
169 * Widgets like #GtkAccelLabel which display an associated
170 * accelerator should connect to this signal, and rebuild
171 * their visual representation if the @accel_closure is theirs.
173 signal_accel_changed =
174 g_signal_new (I_("accel-changed"),
175 G_OBJECT_CLASS_TYPE (class),
176 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
177 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
179 _gtk_marshal_VOID__UINT_FLAGS_BOXED,
182 GDK_TYPE_MODIFIER_TYPE,
185 g_type_class_add_private (object_class, sizeof (GtkAccelGroupPrivate));
189 gtk_accel_group_finalize (GObject *object)
191 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
194 for (i = 0; i < accel_group->priv->n_accels; i++)
196 GtkAccelGroupEntry *entry = &accel_group->priv->priv_accels[i];
198 if (entry->accel_path_quark)
200 const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
202 _gtk_accel_map_remove_group (accel_path, accel_group);
204 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
206 /* remove quick_accel_add() refcount */
207 g_closure_unref (entry->closure);
210 g_free (accel_group->priv->priv_accels);
212 G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
216 gtk_accel_group_get_property (GObject *object,
221 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
226 g_value_set_boolean (value, accel_group->priv->lock_count > 0);
228 case PROP_MODIFIER_MASK:
229 g_value_set_flags (value, accel_group->priv->modifier_mask);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
238 gtk_accel_group_init (GtkAccelGroup *accel_group)
240 GtkAccelGroupPrivate *priv = GTK_ACCEL_GROUP_GET_PRIVATE (accel_group);
242 priv->lock_count = 0;
243 priv->modifier_mask = gtk_accelerator_get_default_mod_mask ();
244 priv->acceleratables = NULL;
246 priv->priv_accels = NULL;
248 accel_group->priv = priv;
252 * gtk_accel_group_new:
253 * @returns: a new #GtkAccelGroup object
255 * Creates a new #GtkAccelGroup.
258 gtk_accel_group_new (void)
260 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
264 * gtk_accel_group_get_is_locked:
265 * @accel_group: a #GtkAccelGroup
267 * Locks are added and removed using gtk_accel_group_lock() and
268 * gtk_accel_group_unlock().
270 * Returns: %TRUE if there are 1 or more locks on the @accel_group,
276 gtk_accel_group_get_is_locked (GtkAccelGroup *accel_group)
278 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
280 return accel_group->priv->lock_count > 0;
284 * gtk_accel_group_get_modifier_mask:
285 * @accel_group: a #GtkAccelGroup
287 * Gets a #GdkModifierType representing the mask for this
288 * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
290 * Returns: the modifier mask for this accel group.
295 gtk_accel_group_get_modifier_mask (GtkAccelGroup *accel_group)
297 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), 0);
299 return accel_group->priv->modifier_mask;
303 accel_group_weak_ref_detach (GSList *free_list,
304 GObject *stale_object)
308 for (slist = free_list; slist; slist = slist->next)
310 GtkAccelGroup *accel_group;
312 accel_group = slist->data;
313 accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, stale_object);
314 g_object_unref (accel_group);
316 g_slist_free (free_list);
317 g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
321 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
326 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
327 g_return_if_fail (G_IS_OBJECT (object));
328 g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) == NULL);
330 g_object_ref (accel_group);
331 accel_group->priv->acceleratables = g_slist_prepend (accel_group->priv->acceleratables, object);
332 slist = g_object_get_qdata (object, quark_acceleratable_groups);
334 g_object_weak_unref (object,
335 (GWeakNotify) accel_group_weak_ref_detach,
337 slist = g_slist_prepend (slist, accel_group);
338 g_object_set_qdata (object, quark_acceleratable_groups, slist);
339 g_object_weak_ref (object,
340 (GWeakNotify) accel_group_weak_ref_detach,
345 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
350 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
351 g_return_if_fail (G_IS_OBJECT (object));
352 g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) != NULL);
354 accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, object);
355 slist = g_object_get_qdata (object, quark_acceleratable_groups);
356 g_object_weak_unref (object,
357 (GWeakNotify) accel_group_weak_ref_detach,
359 slist = g_slist_remove (slist, accel_group);
360 g_object_set_qdata (object, quark_acceleratable_groups, slist);
362 g_object_weak_ref (object,
363 (GWeakNotify) accel_group_weak_ref_detach,
365 g_object_unref (accel_group);
369 * gtk_accel_groups_from_object:
370 * @object: a #GObject, usually a #GtkWindow
372 * Gets a list of all accel groups which are attached to @object.
374 * Returns: (element-type GtkAccelGroup) (transfer none): a list of all accel groups which are attached to @object
377 gtk_accel_groups_from_object (GObject *object)
379 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
381 return g_object_get_qdata (object, quark_acceleratable_groups);
385 * gtk_accel_group_find:
386 * @accel_group: a #GtkAccelGroup
387 * @find_func: a function to filter the entries of @accel_group with
388 * @data: data to pass to @find_func
389 * @returns: the key of the first entry passing @find_func. The key is
390 * owned by GTK+ and must not be freed.
392 * Finds the first entry in an accelerator group for which
393 * @find_func returns %TRUE and returns its #GtkAccelKey.
397 gtk_accel_group_find (GtkAccelGroup *accel_group,
398 GtkAccelGroupFindFunc find_func,
401 GtkAccelKey *key = NULL;
404 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
405 g_return_val_if_fail (find_func != NULL, NULL);
407 g_object_ref (accel_group);
408 for (i = 0; i < accel_group->priv->n_accels; i++)
409 if (find_func (&accel_group->priv->priv_accels[i].key,
410 accel_group->priv->priv_accels[i].closure,
413 key = &accel_group->priv->priv_accels[i].key;
416 g_object_unref (accel_group);
422 * gtk_accel_group_lock:
423 * @accel_group: a #GtkAccelGroup
425 * Locks the given accelerator group.
427 * Locking an acelerator group prevents the accelerators contained
428 * within it to be changed during runtime. Refer to
429 * gtk_accel_map_change_entry() about runtime accelerator changes.
431 * If called more than once, @accel_group remains locked until
432 * gtk_accel_group_unlock() has been called an equivalent number
436 gtk_accel_group_lock (GtkAccelGroup *accel_group)
438 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
440 accel_group->priv->lock_count += 1;
442 if (accel_group->priv->lock_count == 1) {
443 /* State change from unlocked to locked */
444 g_object_notify (G_OBJECT (accel_group), "is-locked");
449 * gtk_accel_group_unlock:
450 * @accel_group: a #GtkAccelGroup
452 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
455 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
457 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
458 g_return_if_fail (accel_group->priv->lock_count > 0);
460 accel_group->priv->lock_count -= 1;
462 if (accel_group->priv->lock_count < 1) {
463 /* State change from locked to unlocked */
464 g_object_notify (G_OBJECT (accel_group), "is-locked");
469 accel_closure_invalidate (gpointer data,
472 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
474 gtk_accel_group_disconnect (accel_group, closure);
478 bsearch_compare_accels (const void *d1,
481 const GtkAccelGroupEntry *entry1 = d1;
482 const GtkAccelGroupEntry *entry2 = d2;
484 if (entry1->key.accel_key == entry2->key.accel_key)
485 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
487 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
491 quick_accel_add (GtkAccelGroup *accel_group,
493 GdkModifierType accel_mods,
494 GtkAccelFlags accel_flags,
498 guint pos, i = accel_group->priv->n_accels++;
499 GtkAccelGroupEntry key;
502 key.key.accel_key = accel_key;
503 key.key.accel_mods = accel_mods;
504 for (pos = 0; pos < i; pos++)
505 if (bsearch_compare_accels (&key, accel_group->priv->priv_accels + pos) < 0)
508 /* insert at position, ref closure */
509 accel_group->priv->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv->priv_accels, accel_group->priv->n_accels);
510 g_memmove (accel_group->priv->priv_accels + pos + 1, accel_group->priv->priv_accels + pos,
511 (i - pos) * sizeof (accel_group->priv->priv_accels[0]));
512 accel_group->priv->priv_accels[pos].key.accel_key = accel_key;
513 accel_group->priv->priv_accels[pos].key.accel_mods = accel_mods;
514 accel_group->priv->priv_accels[pos].key.accel_flags = accel_flags;
515 accel_group->priv->priv_accels[pos].closure = g_closure_ref (closure);
516 accel_group->priv->priv_accels[pos].accel_path_quark = path_quark;
517 g_closure_sink (closure);
519 /* handle closure invalidation and reverse lookups */
520 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
522 /* get accel path notification */
524 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
526 /* connect and notify changed */
529 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
530 GQuark accel_quark = g_quark_from_string (accel_name);
535 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
538 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
543 quick_accel_remove (GtkAccelGroup *accel_group,
546 GQuark accel_quark = 0;
547 GtkAccelGroupEntry *entry = accel_group->priv->priv_accels + pos;
548 guint accel_key = entry->key.accel_key;
549 GdkModifierType accel_mods = entry->key.accel_mods;
550 GClosure *closure = entry->closure;
552 /* quark for notification */
555 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
557 accel_quark = g_quark_from_string (accel_name);
561 /* clean up closure invalidate notification and disconnect */
562 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
564 g_signal_handlers_disconnect_matched (accel_group,
565 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
566 signal_accel_activate, accel_quark,
567 closure, NULL, NULL);
568 /* clean up accel path notification */
569 if (entry->accel_path_quark)
570 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
572 /* physically remove */
573 accel_group->priv->n_accels -= 1;
574 g_memmove (entry, entry + 1,
575 (accel_group->priv->n_accels - pos) * sizeof (accel_group->priv->priv_accels[0]));
579 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
581 /* remove quick_accel_add() refcount */
582 g_closure_unref (closure);
585 static GtkAccelGroupEntry*
586 quick_accel_find (GtkAccelGroup *accel_group,
588 GdkModifierType accel_mods,
591 GtkAccelGroupEntry *entry;
592 GtkAccelGroupEntry key;
596 if (!accel_group->priv->n_accels)
599 key.key.accel_key = accel_key;
600 key.key.accel_mods = accel_mods;
601 entry = bsearch (&key, accel_group->priv->priv_accels, accel_group->priv->n_accels,
602 sizeof (accel_group->priv->priv_accels[0]), bsearch_compare_accels);
607 /* step back to the first member */
608 for (; entry > accel_group->priv->priv_accels; entry--)
609 if (entry[-1].key.accel_key != accel_key ||
610 entry[-1].key.accel_mods != accel_mods)
612 /* count equal members */
613 for (; entry + *count_p < accel_group->priv->priv_accels + accel_group->priv->n_accels; (*count_p)++)
614 if (entry[*count_p].key.accel_key != accel_key ||
615 entry[*count_p].key.accel_mods != accel_mods)
621 * gtk_accel_group_connect:
622 * @accel_group: the accelerator group to install an accelerator in
623 * @accel_key: key value of the accelerator
624 * @accel_mods: modifier combination of the accelerator
625 * @accel_flags: a flag mask to configure this accelerator
626 * @closure: closure to be executed upon accelerator activation
628 * Installs an accelerator in this group. When @accel_group is being activated
629 * in response to a call to gtk_accel_groups_activate(), @closure will be
630 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
631 * match those of this connection.
633 * The signature used for the @closure is that of #GtkAccelGroupActivate.
635 * Note that, due to implementation details, a single closure can only be
636 * connected to one accelerator group.
639 gtk_accel_group_connect (GtkAccelGroup *accel_group,
641 GdkModifierType accel_mods,
642 GtkAccelFlags accel_flags,
645 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
646 g_return_if_fail (closure != NULL);
647 g_return_if_fail (accel_key > 0);
648 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
650 g_object_ref (accel_group);
651 if (!closure->is_invalid)
652 quick_accel_add (accel_group,
653 gdk_keyval_to_lower (accel_key),
654 accel_mods, accel_flags, closure, 0);
655 g_object_unref (accel_group);
659 * gtk_accel_group_connect_by_path:
660 * @accel_group: the accelerator group to install an accelerator in
661 * @accel_path: path used for determining key and modifiers.
662 * @closure: closure to be executed upon accelerator activation
664 * Installs an accelerator in this group, using an accelerator path to look
665 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
666 * When @accel_group is being activated in response to a call to
667 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
668 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
671 * The signature used for the @closure is that of #GtkAccelGroupActivate.
673 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
674 * pass a static string, you can save some memory by interning it first with
675 * g_intern_static_string().
678 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
679 const gchar *accel_path,
683 GdkModifierType accel_mods = 0;
686 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
687 g_return_if_fail (closure != NULL);
688 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
690 if (closure->is_invalid)
693 g_object_ref (accel_group);
695 if (gtk_accel_map_lookup_entry (accel_path, &key))
697 accel_key = gdk_keyval_to_lower (key.accel_key);
698 accel_mods = key.accel_mods;
701 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
702 g_quark_from_string (accel_path));
704 g_object_unref (accel_group);
708 * gtk_accel_group_disconnect:
709 * @accel_group: the accelerator group to remove an accelerator from
710 * @closure: (allow-none): the closure to remove from this accelerator group, or %NULL
711 * to remove all closures
712 * @returns: %TRUE if the closure was found and got disconnected
714 * Removes an accelerator previously installed through
715 * gtk_accel_group_connect().
717 * Since 2.20 @closure can be %NULL.
720 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
725 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
727 for (i = 0; i < accel_group->priv->n_accels; i++)
728 if (accel_group->priv->priv_accels[i].closure == closure)
730 g_object_ref (accel_group);
731 quick_accel_remove (accel_group, i);
732 g_object_unref (accel_group);
739 * gtk_accel_group_disconnect_key:
740 * @accel_group: the accelerator group to install an accelerator in
741 * @accel_key: key value of the accelerator
742 * @accel_mods: modifier combination of the accelerator
743 * @returns: %TRUE if there was an accelerator which could be
744 * removed, %FALSE otherwise
746 * Removes an accelerator previously installed through
747 * gtk_accel_group_connect().
750 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
752 GdkModifierType accel_mods)
754 GtkAccelGroupEntry *entries;
755 GSList *slist, *clist = NULL;
756 gboolean removed_one = FALSE;
759 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
761 g_object_ref (accel_group);
763 accel_key = gdk_keyval_to_lower (accel_key);
764 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
767 GClosure *closure = g_closure_ref (entries[n].closure);
769 clist = g_slist_prepend (clist, closure);
772 for (slist = clist; slist; slist = slist->next)
774 GClosure *closure = slist->data;
776 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
777 g_closure_unref (closure);
779 g_slist_free (clist);
781 g_object_unref (accel_group);
787 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
788 GQuark accel_path_quark)
790 GSList *slist, *clist = NULL;
793 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
795 g_object_ref (accel_group);
797 for (i = 0; i < accel_group->priv->n_accels; i++)
798 if (accel_group->priv->priv_accels[i].accel_path_quark == accel_path_quark)
800 GClosure *closure = g_closure_ref (accel_group->priv->priv_accels[i].closure);
802 clist = g_slist_prepend (clist, closure);
805 for (slist = clist; slist; slist = slist->next)
807 GClosure *closure = slist->data;
809 gtk_accel_group_disconnect (accel_group, closure);
810 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
811 g_closure_unref (closure);
813 g_slist_free (clist);
815 g_object_unref (accel_group);
819 _gtk_accel_group_get_accelerables (GtkAccelGroup *accel_group)
821 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
823 return accel_group->priv->acceleratables;
827 * gtk_accel_group_query:
828 * @accel_group: the accelerator group to query
829 * @accel_key: key value of the accelerator
830 * @accel_mods: modifier combination of the accelerator
831 * @n_entries: (allow-none): location to return the number of entries found, or %NULL
832 * @returns: (allow-none): an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
834 * Queries an accelerator group for all entries matching @accel_key and
838 gtk_accel_group_query (GtkAccelGroup *accel_group,
840 GdkModifierType accel_mods,
843 GtkAccelGroupEntry *entries;
846 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
848 entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
851 *n_entries = entries ? n : 0;
857 * gtk_accel_group_from_accel_closure:
858 * @closure: a #GClosure
859 * @returns: (allow-none): the #GtkAccelGroup to which @closure is connected, or %NULL.
861 * Finds the #GtkAccelGroup to which @closure is connected;
862 * see gtk_accel_group_connect().
865 gtk_accel_group_from_accel_closure (GClosure *closure)
869 g_return_val_if_fail (closure != NULL, NULL);
871 /* a few remarks on what we do here. in general, we need a way to reverse lookup
872 * accel_groups from closures that are being used in accel groups. this could
873 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
874 * use the invalidation notifier on the closure itself (which we need to install
875 * anyway), that contains the accel group as data which, besides needing to peek
876 * a bit at closure internals, works just as good.
878 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
879 if (closure->notifiers[i].notify == accel_closure_invalidate)
880 return closure->notifiers[i].data;
886 * gtk_accel_group_activate:
887 * @accel_group: a #GtkAccelGroup
888 * @accel_quark: the quark for the accelerator name
889 * @acceleratable: the #GObject, usually a #GtkWindow, on which
890 * to activate the accelerator.
891 * @accel_key: accelerator keyval from a key event
892 * @accel_mods: keyboard state mask from a key event
894 * Finds the first accelerator in @accel_group
895 * that matches @accel_key and @accel_mods, and
898 * Returns: %TRUE if an accelerator was activated and handled this keypress
901 gtk_accel_group_activate (GtkAccelGroup *accel_group,
903 GObject *acceleratable,
905 GdkModifierType accel_mods)
907 gboolean was_handled;
909 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
910 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
913 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
914 acceleratable, accel_key, accel_mods, &was_handled);
920 * gtk_accel_groups_activate:
921 * @object: the #GObject, usually a #GtkWindow, on which
922 * to activate the accelerator.
923 * @accel_key: accelerator keyval from a key event
924 * @accel_mods: keyboard state mask from a key event
926 * Finds the first accelerator in any #GtkAccelGroup attached
927 * to @object that matches @accel_key and @accel_mods, and
928 * activates that accelerator.
930 * Returns: %TRUE if an accelerator was activated and handled this keypress
933 gtk_accel_groups_activate (GObject *object,
935 GdkModifierType accel_mods)
937 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
939 if (gtk_accelerator_valid (accel_key, accel_mods))
945 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
946 accel_quark = g_quark_from_string (accel_name);
949 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
950 if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
958 * gtk_accelerator_valid:
959 * @keyval: a GDK keyval
960 * @modifiers: modifier mask
961 * @returns: %TRUE if the accelerator is valid
963 * Determines whether a given keyval and modifier mask constitute
964 * a valid keyboard accelerator. For example, the #GDK_a keyval
965 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
966 * But, you can't, for instance, use the #GDK_Control_L keyval
970 gtk_accelerator_valid (guint keyval,
971 GdkModifierType modifiers)
973 static const guint invalid_accelerator_vals[] = {
974 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
975 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
976 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
977 GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
978 GDK_ISO_First_Group, GDK_ISO_Last_Group,
979 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
980 GDK_Scroll_Lock, GDK_Sys_Req,
981 GDK_Tab, GDK_ISO_Left_Tab, GDK_KP_Tab,
982 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
983 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
984 GDK_Terminate_Server, GDK_AudibleBell_Enable,
987 static const guint invalid_unmodified_vals[] = {
988 GDK_Up, GDK_Down, GDK_Left, GDK_Right,
989 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right,
994 modifiers &= GDK_MODIFIER_MASK;
997 return keyval >= 0x20;
999 ac_val = invalid_accelerator_vals;
1002 if (keyval == *ac_val++)
1008 ac_val = invalid_unmodified_vals;
1011 if (keyval == *ac_val++)
1019 static inline gboolean
1020 is_alt (const gchar *string)
1022 return ((string[0] == '<') &&
1023 (string[1] == 'a' || string[1] == 'A') &&
1024 (string[2] == 'l' || string[2] == 'L') &&
1025 (string[3] == 't' || string[3] == 'T') &&
1026 (string[4] == '>'));
1029 static inline gboolean
1030 is_ctl (const gchar *string)
1032 return ((string[0] == '<') &&
1033 (string[1] == 'c' || string[1] == 'C') &&
1034 (string[2] == 't' || string[2] == 'T') &&
1035 (string[3] == 'l' || string[3] == 'L') &&
1036 (string[4] == '>'));
1039 static inline gboolean
1040 is_modx (const gchar *string)
1042 return ((string[0] == '<') &&
1043 (string[1] == 'm' || string[1] == 'M') &&
1044 (string[2] == 'o' || string[2] == 'O') &&
1045 (string[3] == 'd' || string[3] == 'D') &&
1046 (string[4] >= '1' && string[4] <= '5') &&
1047 (string[5] == '>'));
1050 static inline gboolean
1051 is_ctrl (const gchar *string)
1053 return ((string[0] == '<') &&
1054 (string[1] == 'c' || string[1] == 'C') &&
1055 (string[2] == 't' || string[2] == 'T') &&
1056 (string[3] == 'r' || string[3] == 'R') &&
1057 (string[4] == 'l' || string[4] == 'L') &&
1058 (string[5] == '>'));
1061 static inline gboolean
1062 is_shft (const gchar *string)
1064 return ((string[0] == '<') &&
1065 (string[1] == 's' || string[1] == 'S') &&
1066 (string[2] == 'h' || string[2] == 'H') &&
1067 (string[3] == 'f' || string[3] == 'F') &&
1068 (string[4] == 't' || string[4] == 'T') &&
1069 (string[5] == '>'));
1072 static inline gboolean
1073 is_shift (const gchar *string)
1075 return ((string[0] == '<') &&
1076 (string[1] == 's' || string[1] == 'S') &&
1077 (string[2] == 'h' || string[2] == 'H') &&
1078 (string[3] == 'i' || string[3] == 'I') &&
1079 (string[4] == 'f' || string[4] == 'F') &&
1080 (string[5] == 't' || string[5] == 'T') &&
1081 (string[6] == '>'));
1084 static inline gboolean
1085 is_control (const gchar *string)
1087 return ((string[0] == '<') &&
1088 (string[1] == 'c' || string[1] == 'C') &&
1089 (string[2] == 'o' || string[2] == 'O') &&
1090 (string[3] == 'n' || string[3] == 'N') &&
1091 (string[4] == 't' || string[4] == 'T') &&
1092 (string[5] == 'r' || string[5] == 'R') &&
1093 (string[6] == 'o' || string[6] == 'O') &&
1094 (string[7] == 'l' || string[7] == 'L') &&
1095 (string[8] == '>'));
1098 static inline gboolean
1099 is_release (const gchar *string)
1101 return ((string[0] == '<') &&
1102 (string[1] == 'r' || string[1] == 'R') &&
1103 (string[2] == 'e' || string[2] == 'E') &&
1104 (string[3] == 'l' || string[3] == 'L') &&
1105 (string[4] == 'e' || string[4] == 'E') &&
1106 (string[5] == 'a' || string[5] == 'A') &&
1107 (string[6] == 's' || string[6] == 'S') &&
1108 (string[7] == 'e' || string[7] == 'E') &&
1109 (string[8] == '>'));
1112 static inline gboolean
1113 is_meta (const gchar *string)
1115 return ((string[0] == '<') &&
1116 (string[1] == 'm' || string[1] == 'M') &&
1117 (string[2] == 'e' || string[2] == 'E') &&
1118 (string[3] == 't' || string[3] == 'T') &&
1119 (string[4] == 'a' || string[4] == 'A') &&
1120 (string[5] == '>'));
1123 static inline gboolean
1124 is_super (const gchar *string)
1126 return ((string[0] == '<') &&
1127 (string[1] == 's' || string[1] == 'S') &&
1128 (string[2] == 'u' || string[2] == 'U') &&
1129 (string[3] == 'p' || string[3] == 'P') &&
1130 (string[4] == 'e' || string[4] == 'E') &&
1131 (string[5] == 'r' || string[5] == 'R') &&
1132 (string[6] == '>'));
1135 static inline gboolean
1136 is_hyper (const gchar *string)
1138 return ((string[0] == '<') &&
1139 (string[1] == 'h' || string[1] == 'H') &&
1140 (string[2] == 'y' || string[2] == 'Y') &&
1141 (string[3] == 'p' || string[3] == 'P') &&
1142 (string[4] == 'e' || string[4] == 'E') &&
1143 (string[5] == 'r' || string[5] == 'R') &&
1144 (string[6] == '>'));
1148 * gtk_accelerator_parse:
1149 * @accelerator: string representing an accelerator
1150 * @accelerator_key: return location for accelerator keyval
1151 * @accelerator_mods: return location for accelerator modifier mask
1153 * Parses a string representing an accelerator. The
1154 * format looks like "<Control>a" or "<Shift><Alt>F1" or
1155 * "<Release>z" (the last one is for key release).
1156 * The parser is fairly liberal and allows lower or upper case,
1157 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
1158 * Key names are parsed using gdk_keyval_from_name(). For character keys the
1159 * name is not the symbol, but the lowercase name, e.g. one would use
1160 * "<Ctrl>minus" instead of "<Ctrl>-".
1162 * If the parse fails, @accelerator_key and @accelerator_mods will
1163 * be set to 0 (zero).
1166 gtk_accelerator_parse (const gchar *accelerator,
1167 guint *accelerator_key,
1168 GdkModifierType *accelerator_mods)
1171 GdkModifierType mods;
1174 if (accelerator_key)
1175 *accelerator_key = 0;
1176 if (accelerator_mods)
1177 *accelerator_mods = 0;
1178 g_return_if_fail (accelerator != NULL);
1182 len = strlen (accelerator);
1185 if (*accelerator == '<')
1187 if (len >= 9 && is_release (accelerator))
1191 mods |= GDK_RELEASE_MASK;
1193 else if (len >= 9 && is_control (accelerator))
1197 mods |= GDK_CONTROL_MASK;
1199 else if (len >= 7 && is_shift (accelerator))
1203 mods |= GDK_SHIFT_MASK;
1205 else if (len >= 6 && is_shft (accelerator))
1209 mods |= GDK_SHIFT_MASK;
1211 else if (len >= 6 && is_ctrl (accelerator))
1215 mods |= GDK_CONTROL_MASK;
1217 else if (len >= 6 && is_modx (accelerator))
1219 static const guint mod_vals[] = {
1220 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1221 GDK_MOD4_MASK, GDK_MOD5_MASK
1226 mods |= mod_vals[*accelerator - '1'];
1229 else if (len >= 5 && is_ctl (accelerator))
1233 mods |= GDK_CONTROL_MASK;
1235 else if (len >= 5 && is_alt (accelerator))
1239 mods |= GDK_MOD1_MASK;
1241 else if (len >= 6 && is_meta (accelerator))
1245 mods |= GDK_META_MASK;
1247 else if (len >= 7 && is_hyper (accelerator))
1251 mods |= GDK_HYPER_MASK;
1253 else if (len >= 7 && is_super (accelerator))
1257 mods |= GDK_SUPER_MASK;
1263 last_ch = *accelerator;
1264 while (last_ch && last_ch != '>')
1266 last_ch = *accelerator;
1274 keyval = gdk_keyval_from_name (accelerator);
1280 if (accelerator_key)
1281 *accelerator_key = gdk_keyval_to_lower (keyval);
1282 if (accelerator_mods)
1283 *accelerator_mods = mods;
1287 * gtk_accelerator_name:
1288 * @accelerator_key: accelerator keyval
1289 * @accelerator_mods: accelerator modifier mask
1291 * Converts an accelerator keyval and modifier mask
1292 * into a string parseable by gtk_accelerator_parse().
1293 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1294 * this function returns "<Control>q".
1296 * If you need to display accelerators in the user interface,
1297 * see gtk_accelerator_get_label().
1299 * Returns: a newly-allocated accelerator name
1302 gtk_accelerator_name (guint accelerator_key,
1303 GdkModifierType accelerator_mods)
1305 static const gchar text_release[] = "<Release>";
1306 static const gchar text_shift[] = "<Shift>";
1307 static const gchar text_control[] = "<Control>";
1308 static const gchar text_mod1[] = "<Alt>";
1309 static const gchar text_mod2[] = "<Mod2>";
1310 static const gchar text_mod3[] = "<Mod3>";
1311 static const gchar text_mod4[] = "<Mod4>";
1312 static const gchar text_mod5[] = "<Mod5>";
1313 static const gchar text_meta[] = "<Meta>";
1314 static const gchar text_super[] = "<Super>";
1315 static const gchar text_hyper[] = "<Hyper>";
1320 accelerator_mods &= GDK_MODIFIER_MASK;
1322 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1327 if (accelerator_mods & GDK_RELEASE_MASK)
1328 l += sizeof (text_release) - 1;
1329 if (accelerator_mods & GDK_SHIFT_MASK)
1330 l += sizeof (text_shift) - 1;
1331 if (accelerator_mods & GDK_CONTROL_MASK)
1332 l += sizeof (text_control) - 1;
1333 if (accelerator_mods & GDK_MOD1_MASK)
1334 l += sizeof (text_mod1) - 1;
1335 if (accelerator_mods & GDK_MOD2_MASK)
1336 l += sizeof (text_mod2) - 1;
1337 if (accelerator_mods & GDK_MOD3_MASK)
1338 l += sizeof (text_mod3) - 1;
1339 if (accelerator_mods & GDK_MOD4_MASK)
1340 l += sizeof (text_mod4) - 1;
1341 if (accelerator_mods & GDK_MOD5_MASK)
1342 l += sizeof (text_mod5) - 1;
1343 l += strlen (keyval_name);
1344 if (accelerator_mods & GDK_META_MASK)
1345 l += sizeof (text_meta) - 1;
1346 if (accelerator_mods & GDK_HYPER_MASK)
1347 l += sizeof (text_hyper) - 1;
1348 if (accelerator_mods & GDK_SUPER_MASK)
1349 l += sizeof (text_super) - 1;
1351 accelerator = g_new (gchar, l + 1);
1355 if (accelerator_mods & GDK_RELEASE_MASK)
1357 strcpy (accelerator + l, text_release);
1358 l += sizeof (text_release) - 1;
1360 if (accelerator_mods & GDK_SHIFT_MASK)
1362 strcpy (accelerator + l, text_shift);
1363 l += sizeof (text_shift) - 1;
1365 if (accelerator_mods & GDK_CONTROL_MASK)
1367 strcpy (accelerator + l, text_control);
1368 l += sizeof (text_control) - 1;
1370 if (accelerator_mods & GDK_MOD1_MASK)
1372 strcpy (accelerator + l, text_mod1);
1373 l += sizeof (text_mod1) - 1;
1375 if (accelerator_mods & GDK_MOD2_MASK)
1377 strcpy (accelerator + l, text_mod2);
1378 l += sizeof (text_mod2) - 1;
1380 if (accelerator_mods & GDK_MOD3_MASK)
1382 strcpy (accelerator + l, text_mod3);
1383 l += sizeof (text_mod3) - 1;
1385 if (accelerator_mods & GDK_MOD4_MASK)
1387 strcpy (accelerator + l, text_mod4);
1388 l += sizeof (text_mod4) - 1;
1390 if (accelerator_mods & GDK_MOD5_MASK)
1392 strcpy (accelerator + l, text_mod5);
1393 l += sizeof (text_mod5) - 1;
1395 if (accelerator_mods & GDK_META_MASK)
1397 strcpy (accelerator + l, text_meta);
1398 l += sizeof (text_meta) - 1;
1400 if (accelerator_mods & GDK_HYPER_MASK)
1402 strcpy (accelerator + l, text_hyper);
1403 l += sizeof (text_hyper) - 1;
1405 if (accelerator_mods & GDK_SUPER_MASK)
1407 strcpy (accelerator + l, text_super);
1408 l += sizeof (text_super) - 1;
1410 strcpy (accelerator + l, keyval_name);
1416 * gtk_accelerator_get_label:
1417 * @accelerator_key: accelerator keyval
1418 * @accelerator_mods: accelerator modifier mask
1420 * Converts an accelerator keyval and modifier mask into a string
1421 * which can be used to represent the accelerator to the user.
1423 * Returns: a newly-allocated string representing the accelerator.
1428 gtk_accelerator_get_label (guint accelerator_key,
1429 GdkModifierType accelerator_mods)
1431 GtkAccelLabelClass *klass;
1434 klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
1435 label = _gtk_accel_label_class_get_accelerator_label (klass,
1438 g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
1444 * gtk_accelerator_set_default_mod_mask:
1445 * @default_mod_mask: accelerator modifier mask
1447 * Sets the modifiers that will be considered significant for keyboard
1448 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1449 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK |
1450 * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt,
1451 * Super, Hyper and Meta. Other modifiers will by default be ignored
1452 * by #GtkAccelGroup.
1453 * You must include at least the three modifiers Control, Shift
1454 * and Alt in any value you pass to this function.
1456 * The default mod mask should be changed on application startup,
1457 * before using any accelerator groups.
1460 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1462 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1463 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1467 * gtk_accelerator_get_default_mod_mask:
1468 * @returns: the default accelerator modifier mask
1470 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1473 gtk_accelerator_get_default_mod_mask (void)
1475 return default_accel_mod_mask;
1478 #define __GTK_ACCEL_GROUP_C__
1479 #include "gtkaliasdef.c"