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 "gtkaccelgroupprivate.h"
33 #include "gtkaccellabel.h"
34 #include "gtkaccelmap.h"
36 #include "gtkmainprivate.h"
37 #include "gtkmarshalers.h"
41 * SECTION:gtkaccelgroup
42 * @Short_description: Groups of global keyboard accelerators for an
44 * @Title: Accelerator Groups
45 * @See_also:gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
46 * gtk_item_factory_new(), gtk_label_new_with_mnemonic()
48 * A #GtkAccelGroup represents a group of keyboard accelerators,
49 * typically attached to a toplevel #GtkWindow (with
50 * gtk_window_add_accel_group()). Usually you won't need to create a
51 * #GtkAccelGroup directly; instead, when using #GtkUIManager, GTK+
52 * automatically sets up the accelerators for your menus in the ui
53 * manager'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 /* --- prototypes --- */
66 static void gtk_accel_group_finalize (GObject *object);
67 static void gtk_accel_group_get_property (GObject *object,
71 static void accel_closure_invalidate (gpointer data,
75 /* --- variables --- */
76 static guint signal_accel_activate = 0;
77 static guint signal_accel_changed = 0;
78 static guint quark_acceleratable_groups = 0;
79 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
93 G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
95 /* --- functions --- */
97 gtk_accel_group_class_init (GtkAccelGroupClass *class)
99 GObjectClass *object_class = G_OBJECT_CLASS (class);
101 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
103 object_class->finalize = gtk_accel_group_finalize;
104 object_class->get_property = gtk_accel_group_get_property;
106 class->accel_changed = NULL;
108 g_object_class_install_property (object_class,
110 g_param_spec_boolean ("is-locked",
112 "Is the accel group locked",
116 g_object_class_install_property (object_class,
118 g_param_spec_flags ("modifier-mask",
121 GDK_TYPE_MODIFIER_TYPE,
122 default_accel_mod_mask,
126 * GtkAccelGroup::accel-activate:
127 * @accel_group: the #GtkAccelGroup which received the signal
128 * @acceleratable: the object on which the accelerator was activated
129 * @keyval: the accelerator keyval
130 * @modifier: the modifier combination of the accelerator
132 * The accel-activate signal is an implementation detail of
133 * #GtkAccelGroup and not meant to be used by applications.
135 * Returns: %TRUE if the accelerator was activated
137 signal_accel_activate =
138 g_signal_new (I_("accel-activate"),
139 G_OBJECT_CLASS_TYPE (class),
142 _gtk_boolean_handled_accumulator, NULL,
143 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
147 GDK_TYPE_MODIFIER_TYPE);
149 * GtkAccelGroup::accel-changed:
150 * @accel_group: the #GtkAccelGroup which received the signal
151 * @keyval: the accelerator keyval
152 * @modifier: the modifier combination of the accelerator
153 * @accel_closure: the #GClosure of the accelerator
155 * The accel-changed signal is emitted when a #GtkAccelGroupEntry
156 * is added to or removed from the accel group.
158 * Widgets like #GtkAccelLabel which display an associated
159 * accelerator should connect to this signal, and rebuild
160 * their visual representation if the @accel_closure is theirs.
162 signal_accel_changed =
163 g_signal_new (I_("accel-changed"),
164 G_OBJECT_CLASS_TYPE (class),
165 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
166 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
168 _gtk_marshal_VOID__UINT_FLAGS_BOXED,
171 GDK_TYPE_MODIFIER_TYPE,
174 g_type_class_add_private (object_class, sizeof (GtkAccelGroupPrivate));
178 gtk_accel_group_finalize (GObject *object)
180 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
183 for (i = 0; i < accel_group->priv->n_accels; i++)
185 GtkAccelGroupEntry *entry = &accel_group->priv->priv_accels[i];
187 if (entry->accel_path_quark)
189 const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
191 _gtk_accel_map_remove_group (accel_path, accel_group);
193 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
195 /* remove quick_accel_add() refcount */
196 g_closure_unref (entry->closure);
199 g_free (accel_group->priv->priv_accels);
201 G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
205 gtk_accel_group_get_property (GObject *object,
210 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
215 g_value_set_boolean (value, accel_group->priv->lock_count > 0);
217 case PROP_MODIFIER_MASK:
218 g_value_set_flags (value, accel_group->priv->modifier_mask);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
227 gtk_accel_group_init (GtkAccelGroup *accel_group)
229 GtkAccelGroupPrivate *priv;
231 accel_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (accel_group,
232 GTK_TYPE_ACCEL_GROUP,
233 GtkAccelGroupPrivate);
234 priv = accel_group->priv;
236 priv->lock_count = 0;
237 priv->modifier_mask = gtk_accelerator_get_default_mod_mask ();
238 priv->acceleratables = NULL;
240 priv->priv_accels = NULL;
244 * gtk_accel_group_new:
245 * @returns: a new #GtkAccelGroup object
247 * Creates a new #GtkAccelGroup.
250 gtk_accel_group_new (void)
252 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
256 * gtk_accel_group_get_is_locked:
257 * @accel_group: a #GtkAccelGroup
259 * Locks are added and removed using gtk_accel_group_lock() and
260 * gtk_accel_group_unlock().
262 * Returns: %TRUE if there are 1 or more locks on the @accel_group,
268 gtk_accel_group_get_is_locked (GtkAccelGroup *accel_group)
270 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
272 return accel_group->priv->lock_count > 0;
276 * gtk_accel_group_get_modifier_mask:
277 * @accel_group: a #GtkAccelGroup
279 * Gets a #GdkModifierType representing the mask for this
280 * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
282 * Returns: the modifier mask for this accel group.
287 gtk_accel_group_get_modifier_mask (GtkAccelGroup *accel_group)
289 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), 0);
291 return accel_group->priv->modifier_mask;
295 accel_group_weak_ref_detach (GSList *free_list,
296 GObject *stale_object)
300 for (slist = free_list; slist; slist = slist->next)
302 GtkAccelGroup *accel_group;
304 accel_group = slist->data;
305 accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, stale_object);
306 g_object_unref (accel_group);
308 g_slist_free (free_list);
309 g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
313 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
318 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
319 g_return_if_fail (G_IS_OBJECT (object));
320 g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) == NULL);
322 g_object_ref (accel_group);
323 accel_group->priv->acceleratables = g_slist_prepend (accel_group->priv->acceleratables, object);
324 slist = g_object_get_qdata (object, quark_acceleratable_groups);
326 g_object_weak_unref (object,
327 (GWeakNotify) accel_group_weak_ref_detach,
329 slist = g_slist_prepend (slist, accel_group);
330 g_object_set_qdata (object, quark_acceleratable_groups, slist);
331 g_object_weak_ref (object,
332 (GWeakNotify) accel_group_weak_ref_detach,
337 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
342 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
343 g_return_if_fail (G_IS_OBJECT (object));
344 g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) != NULL);
346 accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, object);
347 slist = g_object_get_qdata (object, quark_acceleratable_groups);
348 g_object_weak_unref (object,
349 (GWeakNotify) accel_group_weak_ref_detach,
351 slist = g_slist_remove (slist, accel_group);
352 g_object_set_qdata (object, quark_acceleratable_groups, slist);
354 g_object_weak_ref (object,
355 (GWeakNotify) accel_group_weak_ref_detach,
357 g_object_unref (accel_group);
361 * gtk_accel_groups_from_object:
362 * @object: a #GObject, usually a #GtkWindow
364 * Gets a list of all accel groups which are attached to @object.
366 * Returns: (element-type GtkAccelGroup) (transfer none): a list of
367 * all accel groups which are attached to @object
370 gtk_accel_groups_from_object (GObject *object)
372 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
374 return g_object_get_qdata (object, quark_acceleratable_groups);
378 * gtk_accel_group_find:
379 * @accel_group: a #GtkAccelGroup
380 * @find_func: (scope call): a function to filter the entries
381 * of @accel_group with
382 * @data: data to pass to @find_func
383 * @returns: (transfer none): the key of the first entry passing
384 * @find_func. The key is owned by GTK+ and must not be freed.
386 * Finds the first entry in an accelerator group for which
387 * @find_func returns %TRUE and returns its #GtkAccelKey.
390 gtk_accel_group_find (GtkAccelGroup *accel_group,
391 GtkAccelGroupFindFunc find_func,
394 GtkAccelKey *key = NULL;
397 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
398 g_return_val_if_fail (find_func != NULL, NULL);
400 g_object_ref (accel_group);
401 for (i = 0; i < accel_group->priv->n_accels; i++)
402 if (find_func (&accel_group->priv->priv_accels[i].key,
403 accel_group->priv->priv_accels[i].closure,
406 key = &accel_group->priv->priv_accels[i].key;
409 g_object_unref (accel_group);
415 * gtk_accel_group_lock:
416 * @accel_group: a #GtkAccelGroup
418 * Locks the given accelerator group.
420 * Locking an acelerator group prevents the accelerators contained
421 * within it to be changed during runtime. Refer to
422 * gtk_accel_map_change_entry() about runtime accelerator changes.
424 * If called more than once, @accel_group remains locked until
425 * gtk_accel_group_unlock() has been called an equivalent number
429 gtk_accel_group_lock (GtkAccelGroup *accel_group)
431 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
433 accel_group->priv->lock_count += 1;
435 if (accel_group->priv->lock_count == 1) {
436 /* State change from unlocked to locked */
437 g_object_notify (G_OBJECT (accel_group), "is-locked");
442 * gtk_accel_group_unlock:
443 * @accel_group: a #GtkAccelGroup
445 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
448 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
450 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
451 g_return_if_fail (accel_group->priv->lock_count > 0);
453 accel_group->priv->lock_count -= 1;
455 if (accel_group->priv->lock_count < 1) {
456 /* State change from locked to unlocked */
457 g_object_notify (G_OBJECT (accel_group), "is-locked");
462 accel_closure_invalidate (gpointer data,
465 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
467 gtk_accel_group_disconnect (accel_group, closure);
471 bsearch_compare_accels (const void *d1,
474 const GtkAccelGroupEntry *entry1 = d1;
475 const GtkAccelGroupEntry *entry2 = d2;
477 if (entry1->key.accel_key == entry2->key.accel_key)
478 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
480 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
484 quick_accel_add (GtkAccelGroup *accel_group,
486 GdkModifierType accel_mods,
487 GtkAccelFlags accel_flags,
491 guint pos, i = accel_group->priv->n_accels++;
492 GtkAccelGroupEntry key;
495 key.key.accel_key = accel_key;
496 key.key.accel_mods = accel_mods;
497 for (pos = 0; pos < i; pos++)
498 if (bsearch_compare_accels (&key, accel_group->priv->priv_accels + pos) < 0)
501 /* insert at position, ref closure */
502 accel_group->priv->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv->priv_accels, accel_group->priv->n_accels);
503 g_memmove (accel_group->priv->priv_accels + pos + 1, accel_group->priv->priv_accels + pos,
504 (i - pos) * sizeof (accel_group->priv->priv_accels[0]));
505 accel_group->priv->priv_accels[pos].key.accel_key = accel_key;
506 accel_group->priv->priv_accels[pos].key.accel_mods = accel_mods;
507 accel_group->priv->priv_accels[pos].key.accel_flags = accel_flags;
508 accel_group->priv->priv_accels[pos].closure = g_closure_ref (closure);
509 accel_group->priv->priv_accels[pos].accel_path_quark = path_quark;
510 g_closure_sink (closure);
512 /* handle closure invalidation and reverse lookups */
513 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
515 /* get accel path notification */
517 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
519 /* connect and notify changed */
522 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
523 GQuark accel_quark = g_quark_from_string (accel_name);
528 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
531 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
536 quick_accel_remove (GtkAccelGroup *accel_group,
539 GQuark accel_quark = 0;
540 GtkAccelGroupEntry *entry = accel_group->priv->priv_accels + pos;
541 guint accel_key = entry->key.accel_key;
542 GdkModifierType accel_mods = entry->key.accel_mods;
543 GClosure *closure = entry->closure;
545 /* quark for notification */
548 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
550 accel_quark = g_quark_from_string (accel_name);
554 /* clean up closure invalidate notification and disconnect */
555 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
557 g_signal_handlers_disconnect_matched (accel_group,
558 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
559 signal_accel_activate, accel_quark,
560 closure, NULL, NULL);
561 /* clean up accel path notification */
562 if (entry->accel_path_quark)
563 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
565 /* physically remove */
566 accel_group->priv->n_accels -= 1;
567 g_memmove (entry, entry + 1,
568 (accel_group->priv->n_accels - pos) * sizeof (accel_group->priv->priv_accels[0]));
572 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
574 /* remove quick_accel_add() refcount */
575 g_closure_unref (closure);
578 static GtkAccelGroupEntry*
579 quick_accel_find (GtkAccelGroup *accel_group,
581 GdkModifierType accel_mods,
584 GtkAccelGroupEntry *entry;
585 GtkAccelGroupEntry key;
589 if (!accel_group->priv->n_accels)
592 key.key.accel_key = accel_key;
593 key.key.accel_mods = accel_mods;
594 entry = bsearch (&key, accel_group->priv->priv_accels, accel_group->priv->n_accels,
595 sizeof (accel_group->priv->priv_accels[0]), bsearch_compare_accels);
600 /* step back to the first member */
601 for (; entry > accel_group->priv->priv_accels; entry--)
602 if (entry[-1].key.accel_key != accel_key ||
603 entry[-1].key.accel_mods != accel_mods)
605 /* count equal members */
606 for (; entry + *count_p < accel_group->priv->priv_accels + accel_group->priv->n_accels; (*count_p)++)
607 if (entry[*count_p].key.accel_key != accel_key ||
608 entry[*count_p].key.accel_mods != accel_mods)
614 * gtk_accel_group_connect:
615 * @accel_group: the accelerator group to install an accelerator in
616 * @accel_key: key value of the accelerator
617 * @accel_mods: modifier combination of the accelerator
618 * @accel_flags: a flag mask to configure this accelerator
619 * @closure: closure to be executed upon accelerator activation
621 * Installs an accelerator in this group. When @accel_group is being
622 * activated in response to a call to gtk_accel_groups_activate(),
623 * @closure will be invoked if the @accel_key and @accel_mods from
624 * gtk_accel_groups_activate() match those of this connection.
626 * The signature used for the @closure is that of #GtkAccelGroupActivate.
628 * Note that, due to implementation details, a single closure can
629 * only be connected to one accelerator group.
632 gtk_accel_group_connect (GtkAccelGroup *accel_group,
634 GdkModifierType accel_mods,
635 GtkAccelFlags accel_flags,
638 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
639 g_return_if_fail (closure != NULL);
640 g_return_if_fail (accel_key > 0);
641 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
643 g_object_ref (accel_group);
644 if (!closure->is_invalid)
645 quick_accel_add (accel_group,
646 gdk_keyval_to_lower (accel_key),
647 accel_mods, accel_flags, closure, 0);
648 g_object_unref (accel_group);
652 * gtk_accel_group_connect_by_path:
653 * @accel_group: the accelerator group to install an accelerator in
654 * @accel_path: path used for determining key and modifiers
655 * @closure: closure to be executed upon accelerator activation
657 * Installs an accelerator in this group, using an accelerator path
658 * to look up the appropriate key and modifiers (see
659 * gtk_accel_map_add_entry()). When @accel_group is being activated
660 * in response to a call to gtk_accel_groups_activate(), @closure will
661 * be invoked if the @accel_key and @accel_mods from
662 * gtk_accel_groups_activate() match the key and modifiers for the path.
664 * The signature used for the @closure is that of #GtkAccelGroupActivate.
666 * Note that @accel_path string will be stored in a #GQuark. Therefore,
667 * if you pass a static string, you can save some memory by interning it
668 * first with g_intern_static_string().
671 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
672 const gchar *accel_path,
676 GdkModifierType accel_mods = 0;
679 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
680 g_return_if_fail (closure != NULL);
681 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
683 if (closure->is_invalid)
686 g_object_ref (accel_group);
688 if (gtk_accel_map_lookup_entry (accel_path, &key))
690 accel_key = gdk_keyval_to_lower (key.accel_key);
691 accel_mods = key.accel_mods;
694 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
695 g_quark_from_string (accel_path));
697 g_object_unref (accel_group);
701 * gtk_accel_group_disconnect:
702 * @accel_group: the accelerator group to remove an accelerator from
703 * @closure: (allow-none): the closure to remove from this accelerator
704 * group, or %NULL to remove all closures
705 * @returns: %TRUE if the closure was found and got disconnected
707 * Removes an accelerator previously installed through
708 * gtk_accel_group_connect().
710 * Since 2.20 @closure can be %NULL.
713 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
718 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
720 for (i = 0; i < accel_group->priv->n_accels; i++)
721 if (accel_group->priv->priv_accels[i].closure == closure)
723 g_object_ref (accel_group);
724 quick_accel_remove (accel_group, i);
725 g_object_unref (accel_group);
732 * gtk_accel_group_disconnect_key:
733 * @accel_group: the accelerator group to install an accelerator in
734 * @accel_key: key value of the accelerator
735 * @accel_mods: modifier combination of the accelerator
736 * @returns: %TRUE if there was an accelerator which could be
737 * removed, %FALSE otherwise
739 * Removes an accelerator previously installed through
740 * gtk_accel_group_connect().
743 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
745 GdkModifierType accel_mods)
747 GtkAccelGroupEntry *entries;
748 GSList *slist, *clist = NULL;
749 gboolean removed_one = FALSE;
752 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
754 g_object_ref (accel_group);
756 accel_key = gdk_keyval_to_lower (accel_key);
757 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
760 GClosure *closure = g_closure_ref (entries[n].closure);
762 clist = g_slist_prepend (clist, closure);
765 for (slist = clist; slist; slist = slist->next)
767 GClosure *closure = slist->data;
769 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
770 g_closure_unref (closure);
772 g_slist_free (clist);
774 g_object_unref (accel_group);
780 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
781 GQuark accel_path_quark)
783 GSList *slist, *clist = NULL;
786 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
788 g_object_ref (accel_group);
790 for (i = 0; i < accel_group->priv->n_accels; i++)
791 if (accel_group->priv->priv_accels[i].accel_path_quark == accel_path_quark)
793 GClosure *closure = g_closure_ref (accel_group->priv->priv_accels[i].closure);
795 clist = g_slist_prepend (clist, closure);
798 for (slist = clist; slist; slist = slist->next)
800 GClosure *closure = slist->data;
802 gtk_accel_group_disconnect (accel_group, closure);
803 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
804 g_closure_unref (closure);
806 g_slist_free (clist);
808 g_object_unref (accel_group);
812 _gtk_accel_group_get_accelerables (GtkAccelGroup *accel_group)
814 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
816 return accel_group->priv->acceleratables;
820 * gtk_accel_group_query:
821 * @accel_group: the accelerator group to query
822 * @accel_key: key value of the accelerator
823 * @accel_mods: modifier combination of the accelerator
824 * @n_entries: (allow-none): location to return the number
825 * of entries found, or %NULL
826 * @returns: (transfer none) (array length=n_entries): an array of
827 * @n_entries #GtkAccelGroupEntry elements, or %NULL. The array
828 * is owned by GTK+ and must not be freed.
830 * Queries an accelerator group for all entries matching @accel_key
834 gtk_accel_group_query (GtkAccelGroup *accel_group,
836 GdkModifierType accel_mods,
839 GtkAccelGroupEntry *entries;
842 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
844 entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
847 *n_entries = entries ? n : 0;
853 * gtk_accel_group_from_accel_closure:
854 * @closure: a #GClosure
855 * @returns: (transfer none): the #GtkAccelGroup to which @closure
856 * is connected, or %NULL
858 * Finds the #GtkAccelGroup to which @closure is connected;
859 * see gtk_accel_group_connect().
862 gtk_accel_group_from_accel_closure (GClosure *closure)
866 g_return_val_if_fail (closure != NULL, NULL);
868 /* A few remarks on what we do here. in general, we need a way to
869 * reverse lookup accel_groups from closures that are being used in
870 * accel groups. this could be done e.g via a hashtable. it is however
871 * cheaper (memory wise) to just use the invalidation notifier on the
872 * closure itself (which we need to install anyway), that contains the
873 * accel group as data which, besides needing to peek a bit at closure
874 * internals, works just as good.
876 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
877 if (closure->notifiers[i].notify == accel_closure_invalidate)
878 return closure->notifiers[i].data;
884 * gtk_accel_group_activate:
885 * @accel_group: a #GtkAccelGroup
886 * @accel_quark: the quark for the accelerator name
887 * @acceleratable: the #GObject, usually a #GtkWindow, on which
888 * to activate the accelerator
889 * @accel_key: accelerator keyval from a key event
890 * @accel_mods: keyboard state mask from a key event
892 * Finds the first accelerator in @accel_group that matches
893 * @accel_key and @accel_mods, and activates it.
895 * Returns: %TRUE if an accelerator was activated and handled
899 gtk_accel_group_activate (GtkAccelGroup *accel_group,
901 GObject *acceleratable,
903 GdkModifierType accel_mods)
905 gboolean was_handled;
907 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
908 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
911 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
912 acceleratable, accel_key, accel_mods, &was_handled);
918 * gtk_accel_groups_activate:
919 * @object: the #GObject, usually a #GtkWindow, on which
920 * to activate the accelerator
921 * @accel_key: accelerator keyval from a key event
922 * @accel_mods: keyboard state mask from a key event
924 * Finds the first accelerator in any #GtkAccelGroup attached
925 * to @object that matches @accel_key and @accel_mods, and
926 * activates that accelerator.
928 * Returns: %TRUE if an accelerator was activated and handled
932 gtk_accel_groups_activate (GObject *object,
934 GdkModifierType accel_mods)
936 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
938 if (gtk_accelerator_valid (accel_key, accel_mods))
944 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
945 accel_quark = g_quark_from_string (accel_name);
948 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
949 if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
957 * gtk_accelerator_valid:
958 * @keyval: a GDK keyval
959 * @modifiers: modifier mask
960 * @returns: %TRUE if the accelerator is valid
962 * Determines whether a given keyval and modifier mask constitute
963 * a valid keyboard accelerator. For example, the #GDK_KEY_a keyval
964 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
965 * But, you can't, for instance, use the #GDK_KEY_Control_L keyval
969 gtk_accelerator_valid (guint keyval,
970 GdkModifierType modifiers)
972 static const guint invalid_accelerator_vals[] = {
973 GDK_KEY_Shift_L, GDK_KEY_Shift_R, GDK_KEY_Shift_Lock,
974 GDK_KEY_Caps_Lock, GDK_KEY_ISO_Lock, GDK_KEY_Control_L,
975 GDK_KEY_Control_R, GDK_KEY_Meta_L, GDK_KEY_Meta_R,
976 GDK_KEY_Alt_L, GDK_KEY_Alt_R, GDK_KEY_Super_L, GDK_KEY_Super_R,
977 GDK_KEY_Hyper_L, GDK_KEY_Hyper_R, GDK_KEY_ISO_Level3_Shift,
978 GDK_KEY_ISO_Next_Group, GDK_KEY_ISO_Prev_Group,
979 GDK_KEY_ISO_First_Group, GDK_KEY_ISO_Last_Group,
980 GDK_KEY_Mode_switch, GDK_KEY_Num_Lock, GDK_KEY_Multi_key,
981 GDK_KEY_Scroll_Lock, GDK_KEY_Sys_Req,
982 GDK_KEY_Tab, GDK_KEY_ISO_Left_Tab, GDK_KEY_KP_Tab,
983 GDK_KEY_First_Virtual_Screen, GDK_KEY_Prev_Virtual_Screen,
984 GDK_KEY_Next_Virtual_Screen, GDK_KEY_Last_Virtual_Screen,
985 GDK_KEY_Terminate_Server, GDK_KEY_AudibleBell_Enable,
988 static const guint invalid_unmodified_vals[] = {
989 GDK_KEY_Up, GDK_KEY_Down, GDK_KEY_Left, GDK_KEY_Right,
990 GDK_KEY_KP_Up, GDK_KEY_KP_Down, GDK_KEY_KP_Left, GDK_KEY_KP_Right,
995 modifiers &= GDK_MODIFIER_MASK;
998 return keyval >= 0x20;
1000 ac_val = invalid_accelerator_vals;
1003 if (keyval == *ac_val++)
1009 ac_val = invalid_unmodified_vals;
1012 if (keyval == *ac_val++)
1020 static inline gboolean
1021 is_alt (const gchar *string)
1023 return ((string[0] == '<') &&
1024 (string[1] == 'a' || string[1] == 'A') &&
1025 (string[2] == 'l' || string[2] == 'L') &&
1026 (string[3] == 't' || string[3] == 'T') &&
1027 (string[4] == '>'));
1030 static inline gboolean
1031 is_ctl (const gchar *string)
1033 return ((string[0] == '<') &&
1034 (string[1] == 'c' || string[1] == 'C') &&
1035 (string[2] == 't' || string[2] == 'T') &&
1036 (string[3] == 'l' || string[3] == 'L') &&
1037 (string[4] == '>'));
1040 static inline gboolean
1041 is_modx (const gchar *string)
1043 return ((string[0] == '<') &&
1044 (string[1] == 'm' || string[1] == 'M') &&
1045 (string[2] == 'o' || string[2] == 'O') &&
1046 (string[3] == 'd' || string[3] == 'D') &&
1047 (string[4] >= '1' && string[4] <= '5') &&
1048 (string[5] == '>'));
1051 static inline gboolean
1052 is_ctrl (const gchar *string)
1054 return ((string[0] == '<') &&
1055 (string[1] == 'c' || string[1] == 'C') &&
1056 (string[2] == 't' || string[2] == 'T') &&
1057 (string[3] == 'r' || string[3] == 'R') &&
1058 (string[4] == 'l' || string[4] == 'L') &&
1059 (string[5] == '>'));
1062 static inline gboolean
1063 is_shft (const gchar *string)
1065 return ((string[0] == '<') &&
1066 (string[1] == 's' || string[1] == 'S') &&
1067 (string[2] == 'h' || string[2] == 'H') &&
1068 (string[3] == 'f' || string[3] == 'F') &&
1069 (string[4] == 't' || string[4] == 'T') &&
1070 (string[5] == '>'));
1073 static inline gboolean
1074 is_shift (const gchar *string)
1076 return ((string[0] == '<') &&
1077 (string[1] == 's' || string[1] == 'S') &&
1078 (string[2] == 'h' || string[2] == 'H') &&
1079 (string[3] == 'i' || string[3] == 'I') &&
1080 (string[4] == 'f' || string[4] == 'F') &&
1081 (string[5] == 't' || string[5] == 'T') &&
1082 (string[6] == '>'));
1085 static inline gboolean
1086 is_control (const gchar *string)
1088 return ((string[0] == '<') &&
1089 (string[1] == 'c' || string[1] == 'C') &&
1090 (string[2] == 'o' || string[2] == 'O') &&
1091 (string[3] == 'n' || string[3] == 'N') &&
1092 (string[4] == 't' || string[4] == 'T') &&
1093 (string[5] == 'r' || string[5] == 'R') &&
1094 (string[6] == 'o' || string[6] == 'O') &&
1095 (string[7] == 'l' || string[7] == 'L') &&
1096 (string[8] == '>'));
1099 static inline gboolean
1100 is_release (const gchar *string)
1102 return ((string[0] == '<') &&
1103 (string[1] == 'r' || string[1] == 'R') &&
1104 (string[2] == 'e' || string[2] == 'E') &&
1105 (string[3] == 'l' || string[3] == 'L') &&
1106 (string[4] == 'e' || string[4] == 'E') &&
1107 (string[5] == 'a' || string[5] == 'A') &&
1108 (string[6] == 's' || string[6] == 'S') &&
1109 (string[7] == 'e' || string[7] == 'E') &&
1110 (string[8] == '>'));
1113 static inline gboolean
1114 is_meta (const gchar *string)
1116 return ((string[0] == '<') &&
1117 (string[1] == 'm' || string[1] == 'M') &&
1118 (string[2] == 'e' || string[2] == 'E') &&
1119 (string[3] == 't' || string[3] == 'T') &&
1120 (string[4] == 'a' || string[4] == 'A') &&
1121 (string[5] == '>'));
1124 static inline gboolean
1125 is_super (const gchar *string)
1127 return ((string[0] == '<') &&
1128 (string[1] == 's' || string[1] == 'S') &&
1129 (string[2] == 'u' || string[2] == 'U') &&
1130 (string[3] == 'p' || string[3] == 'P') &&
1131 (string[4] == 'e' || string[4] == 'E') &&
1132 (string[5] == 'r' || string[5] == 'R') &&
1133 (string[6] == '>'));
1136 static inline gboolean
1137 is_hyper (const gchar *string)
1139 return ((string[0] == '<') &&
1140 (string[1] == 'h' || string[1] == 'H') &&
1141 (string[2] == 'y' || string[2] == 'Y') &&
1142 (string[3] == 'p' || string[3] == 'P') &&
1143 (string[4] == 'e' || string[4] == 'E') &&
1144 (string[5] == 'r' || string[5] == 'R') &&
1145 (string[6] == '>'));
1149 * gtk_accelerator_parse:
1150 * @accelerator: string representing an accelerator
1151 * @accelerator_key: (out) (allow-none): return location for accelerator
1153 * @accelerator_mods: (out) (allow-none): return location for accelerator
1154 * modifier mask, %NULL
1156 * Parses a string representing an accelerator. The
1157 * format looks like "<Control>a" or "<Shift><Alt>F1"
1158 * or "<Release>z" (the last one is for key release).
1160 * The parser is fairly liberal and allows lower or upper case,
1161 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
1162 * Key names are parsed using gdk_keyval_from_name(). For character
1163 * keys the name is not the symbol, but the lowercase name, e.g. one
1164 * would use "<Ctrl>minus" instead of "<Ctrl>-".
1166 * If the parse fails, @accelerator_key and @accelerator_mods will
1167 * be set to 0 (zero).
1170 gtk_accelerator_parse (const gchar *accelerator,
1171 guint *accelerator_key,
1172 GdkModifierType *accelerator_mods)
1175 GdkModifierType mods;
1178 if (accelerator_key)
1179 *accelerator_key = 0;
1180 if (accelerator_mods)
1181 *accelerator_mods = 0;
1182 g_return_if_fail (accelerator != NULL);
1186 len = strlen (accelerator);
1189 if (*accelerator == '<')
1191 if (len >= 9 && is_release (accelerator))
1195 mods |= GDK_RELEASE_MASK;
1197 else if (len >= 9 && is_control (accelerator))
1201 mods |= GDK_CONTROL_MASK;
1203 else if (len >= 7 && is_shift (accelerator))
1207 mods |= GDK_SHIFT_MASK;
1209 else if (len >= 6 && is_shft (accelerator))
1213 mods |= GDK_SHIFT_MASK;
1215 else if (len >= 6 && is_ctrl (accelerator))
1219 mods |= GDK_CONTROL_MASK;
1221 else if (len >= 6 && is_modx (accelerator))
1223 static const guint mod_vals[] = {
1224 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1225 GDK_MOD4_MASK, GDK_MOD5_MASK
1230 mods |= mod_vals[*accelerator - '1'];
1233 else if (len >= 5 && is_ctl (accelerator))
1237 mods |= GDK_CONTROL_MASK;
1239 else if (len >= 5 && is_alt (accelerator))
1243 mods |= GDK_MOD1_MASK;
1245 else if (len >= 6 && is_meta (accelerator))
1249 mods |= GDK_META_MASK;
1251 else if (len >= 7 && is_hyper (accelerator))
1255 mods |= GDK_HYPER_MASK;
1257 else if (len >= 7 && is_super (accelerator))
1261 mods |= GDK_SUPER_MASK;
1267 last_ch = *accelerator;
1268 while (last_ch && last_ch != '>')
1270 last_ch = *accelerator;
1278 keyval = gdk_keyval_from_name (accelerator);
1284 if (accelerator_key)
1285 *accelerator_key = gdk_keyval_to_lower (keyval);
1286 if (accelerator_mods)
1287 *accelerator_mods = mods;
1291 * gtk_accelerator_name:
1292 * @accelerator_key: accelerator keyval
1293 * @accelerator_mods: accelerator modifier mask
1295 * Converts an accelerator keyval and modifier mask
1296 * into a string parseable by gtk_accelerator_parse().
1297 * For example, if you pass in #GDK_KEY_q and #GDK_CONTROL_MASK,
1298 * this function returns "<Control>q".
1300 * If you need to display accelerators in the user interface,
1301 * see gtk_accelerator_get_label().
1303 * Returns: a newly-allocated accelerator name
1306 gtk_accelerator_name (guint accelerator_key,
1307 GdkModifierType accelerator_mods)
1309 static const gchar text_release[] = "<Release>";
1310 static const gchar text_shift[] = "<Shift>";
1311 static const gchar text_control[] = "<Control>";
1312 static const gchar text_mod1[] = "<Alt>";
1313 static const gchar text_mod2[] = "<Mod2>";
1314 static const gchar text_mod3[] = "<Mod3>";
1315 static const gchar text_mod4[] = "<Mod4>";
1316 static const gchar text_mod5[] = "<Mod5>";
1317 static const gchar text_meta[] = "<Meta>";
1318 static const gchar text_super[] = "<Super>";
1319 static const gchar text_hyper[] = "<Hyper>";
1324 accelerator_mods &= GDK_MODIFIER_MASK;
1326 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1331 if (accelerator_mods & GDK_RELEASE_MASK)
1332 l += sizeof (text_release) - 1;
1333 if (accelerator_mods & GDK_SHIFT_MASK)
1334 l += sizeof (text_shift) - 1;
1335 if (accelerator_mods & GDK_CONTROL_MASK)
1336 l += sizeof (text_control) - 1;
1337 if (accelerator_mods & GDK_MOD1_MASK)
1338 l += sizeof (text_mod1) - 1;
1339 if (accelerator_mods & GDK_MOD2_MASK)
1340 l += sizeof (text_mod2) - 1;
1341 if (accelerator_mods & GDK_MOD3_MASK)
1342 l += sizeof (text_mod3) - 1;
1343 if (accelerator_mods & GDK_MOD4_MASK)
1344 l += sizeof (text_mod4) - 1;
1345 if (accelerator_mods & GDK_MOD5_MASK)
1346 l += sizeof (text_mod5) - 1;
1347 l += strlen (keyval_name);
1348 if (accelerator_mods & GDK_META_MASK)
1349 l += sizeof (text_meta) - 1;
1350 if (accelerator_mods & GDK_HYPER_MASK)
1351 l += sizeof (text_hyper) - 1;
1352 if (accelerator_mods & GDK_SUPER_MASK)
1353 l += sizeof (text_super) - 1;
1355 accelerator = g_new (gchar, l + 1);
1359 if (accelerator_mods & GDK_RELEASE_MASK)
1361 strcpy (accelerator + l, text_release);
1362 l += sizeof (text_release) - 1;
1364 if (accelerator_mods & GDK_SHIFT_MASK)
1366 strcpy (accelerator + l, text_shift);
1367 l += sizeof (text_shift) - 1;
1369 if (accelerator_mods & GDK_CONTROL_MASK)
1371 strcpy (accelerator + l, text_control);
1372 l += sizeof (text_control) - 1;
1374 if (accelerator_mods & GDK_MOD1_MASK)
1376 strcpy (accelerator + l, text_mod1);
1377 l += sizeof (text_mod1) - 1;
1379 if (accelerator_mods & GDK_MOD2_MASK)
1381 strcpy (accelerator + l, text_mod2);
1382 l += sizeof (text_mod2) - 1;
1384 if (accelerator_mods & GDK_MOD3_MASK)
1386 strcpy (accelerator + l, text_mod3);
1387 l += sizeof (text_mod3) - 1;
1389 if (accelerator_mods & GDK_MOD4_MASK)
1391 strcpy (accelerator + l, text_mod4);
1392 l += sizeof (text_mod4) - 1;
1394 if (accelerator_mods & GDK_MOD5_MASK)
1396 strcpy (accelerator + l, text_mod5);
1397 l += sizeof (text_mod5) - 1;
1399 if (accelerator_mods & GDK_META_MASK)
1401 strcpy (accelerator + l, text_meta);
1402 l += sizeof (text_meta) - 1;
1404 if (accelerator_mods & GDK_HYPER_MASK)
1406 strcpy (accelerator + l, text_hyper);
1407 l += sizeof (text_hyper) - 1;
1409 if (accelerator_mods & GDK_SUPER_MASK)
1411 strcpy (accelerator + l, text_super);
1412 l += sizeof (text_super) - 1;
1414 strcpy (accelerator + l, keyval_name);
1420 * gtk_accelerator_get_label:
1421 * @accelerator_key: accelerator keyval
1422 * @accelerator_mods: accelerator modifier mask
1424 * Converts an accelerator keyval and modifier mask into a string
1425 * which can be used to represent the accelerator to the user.
1427 * Returns: a newly-allocated string representing the accelerator.
1432 gtk_accelerator_get_label (guint accelerator_key,
1433 GdkModifierType accelerator_mods)
1435 GtkAccelLabelClass *klass;
1438 klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
1439 label = _gtk_accel_label_class_get_accelerator_label (klass,
1442 g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
1448 * gtk_accelerator_set_default_mod_mask:
1449 * @default_mod_mask: accelerator modifier mask
1451 * Sets the modifiers that will be considered significant for keyboard
1452 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1453 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK |
1454 * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt,
1455 * Super, Hyper and Meta. Other modifiers will by default be ignored
1456 * by #GtkAccelGroup.
1457 * You must include at least the three modifiers Control, Shift
1458 * and Alt in any value you pass to this function.
1460 * The default mod mask should be changed on application startup,
1461 * before using any accelerator groups.
1464 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1466 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1467 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1471 * gtk_accelerator_get_default_mod_mask:
1472 * @returns: the default accelerator modifier mask
1474 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1477 gtk_accelerator_get_default_mod_mask (void)
1479 return default_accel_mod_mask;