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 /* --- prototypes --- */
42 static void gtk_accel_group_class_init (GtkAccelGroupClass *class);
43 static void gtk_accel_group_init (GtkAccelGroup *accel_group);
44 static void gtk_accel_group_finalize (GObject *object);
47 /* --- variables --- */
48 static GObjectClass *parent_class = NULL;
49 static guint signal_accel_activate = 0;
50 static guint signal_accel_changed = 0;
51 static guint quark_acceleratable_groups = 0;
52 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
57 /* --- functions --- */
59 * gtk_accel_group_get_type:
60 * @returns: the type ID for accelerator groups.
63 gtk_accel_group_get_type (void)
65 static GType object_type = 0;
69 static const GTypeInfo object_info = {
70 sizeof (GtkAccelGroupClass),
72 (GBaseFinalizeFunc) NULL,
73 (GClassInitFunc) gtk_accel_group_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
76 sizeof (GtkAccelGroup),
78 (GInstanceInitFunc) gtk_accel_group_init,
81 object_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkAccelGroup"),
89 gtk_accel_group_class_init (GtkAccelGroupClass *class)
91 GObjectClass *object_class = G_OBJECT_CLASS (class);
93 parent_class = g_type_class_peek_parent (class);
95 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
97 object_class->finalize = gtk_accel_group_finalize;
99 class->accel_changed = NULL;
102 * GtkAccelGroup::accel-activate:
103 * @accel_group: the #GtkAccelGroup which received the signal
104 * @acceleratable: the object on which the accelerator was activated
105 * @keyval: the accelerator keyval
106 * @modifier: the modifier combination of the accelerator
108 * The accel-activate signal is an implementation detail of
109 * #GtkAccelGroup and not meant to be used by applications.
111 * Returns: %TRUE if the accelerator was activated
113 signal_accel_activate =
114 g_signal_new (I_("accel_activate"),
115 G_OBJECT_CLASS_TYPE (class),
118 _gtk_boolean_handled_accumulator, NULL,
119 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
123 GDK_TYPE_MODIFIER_TYPE);
125 * GtkAccelGroup::accel-changed:
126 * @accel_group: the #GtkAccelGroup which received the signal
127 * @keyval: the accelerator keyval
128 * @modifier: the modifier combination of the accelerator
129 * @accel_closure: the #GClosure of the accelerator
131 * The accel-changed signal is emitted when a #GtkAccelGroupEntry
132 * is added to or removed from the accel group.
134 * Widgets like #GtkAccelLabel which display an associated
135 * accelerator should connect to this signal, and rebuild
136 * their visual representation if the @accel_closure is theirs.
138 signal_accel_changed =
139 g_signal_new (I_("accel_changed"),
140 G_OBJECT_CLASS_TYPE (class),
141 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
142 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
144 _gtk_marshal_VOID__UINT_FLAGS_BOXED,
147 GDK_TYPE_MODIFIER_TYPE,
152 gtk_accel_group_finalize (GObject *object)
154 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
157 for (i = 0; i < accel_group->n_accels; i++)
159 GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
161 if (entry->accel_path_quark)
163 const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
165 _gtk_accel_map_remove_group (accel_path, accel_group);
169 g_free (accel_group->priv_accels);
171 G_OBJECT_CLASS (parent_class)->finalize (object);
175 gtk_accel_group_init (GtkAccelGroup *accel_group)
177 accel_group->lock_count = 0;
178 accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
179 accel_group->acceleratables = NULL;
180 accel_group->n_accels = 0;
181 accel_group->priv_accels = NULL;
185 * gtk_accel_group_new:
186 * @returns: a new #GtkAccelGroup object
188 * Creates a new #GtkAccelGroup.
191 gtk_accel_group_new (void)
193 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
197 accel_group_weak_ref_detach (GSList *free_list,
198 GObject *stale_object)
202 for (slist = free_list; slist; slist = slist->next)
204 GtkAccelGroup *accel_group;
206 accel_group = slist->data;
207 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
208 g_object_unref (accel_group);
210 g_slist_free (free_list);
211 g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
215 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
220 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
221 g_return_if_fail (G_IS_OBJECT (object));
222 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) == NULL);
224 g_object_ref (accel_group);
225 accel_group->acceleratables = g_slist_prepend (accel_group->acceleratables, object);
226 slist = g_object_get_qdata (object, quark_acceleratable_groups);
228 g_object_weak_unref (object,
229 (GWeakNotify) accel_group_weak_ref_detach,
231 slist = g_slist_prepend (slist, accel_group);
232 g_object_set_qdata (object, quark_acceleratable_groups, slist);
233 g_object_weak_ref (object,
234 (GWeakNotify) accel_group_weak_ref_detach,
239 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
244 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
245 g_return_if_fail (G_IS_OBJECT (object));
246 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) != NULL);
248 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, object);
249 slist = g_object_get_qdata (object, quark_acceleratable_groups);
250 g_object_weak_unref (object,
251 (GWeakNotify) accel_group_weak_ref_detach,
253 slist = g_slist_remove (slist, accel_group);
254 g_object_set_qdata (object, quark_acceleratable_groups, slist);
256 g_object_weak_ref (object,
257 (GWeakNotify) accel_group_weak_ref_detach,
259 g_object_unref (accel_group);
263 * gtk_accel_groups_from_object:
264 * @object: a #GObject, usually a #GtkWindow
265 * @returns: a list of all accel groups which are attached to @object
267 * Gets a list of all accel groups which are attached to @object.
270 gtk_accel_groups_from_object (GObject *object)
272 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
274 return g_object_get_qdata (object, quark_acceleratable_groups);
278 * gtk_accel_group_find:
279 * @accel_group: a #GtkAccelGroup
280 * @find_func: a function to filter the entries of @accel_group with
281 * @data: data to pass to @find_func
282 * @returns: the key of the first entry passing @find_func. The key is
283 * owned by GTK+ and must not be freed.
285 * Finds the first entry in an accelerator group for which
286 * @find_func returns %TRUE and returns its #GtkAccelKey.
290 gtk_accel_group_find (GtkAccelGroup *accel_group,
291 GtkAccelGroupFindFunc find_func,
294 GtkAccelKey *key = NULL;
297 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
298 g_return_val_if_fail (find_func != NULL, NULL);
300 g_object_ref (accel_group);
301 for (i = 0; i < accel_group->n_accels; i++)
302 if (find_func (&accel_group->priv_accels[i].key,
303 accel_group->priv_accels[i].closure,
306 key = &accel_group->priv_accels[i].key;
309 g_object_unref (accel_group);
315 * gtk_accel_group_lock:
316 * @accel_group: a #GtkAccelGroup
318 * Locks the given accelerator group.
320 * Locking an acelerator group prevents the accelerators contained
321 * within it to be changed during runtime. Refer to
322 * gtk_accel_map_change_entry() about runtime accelerator changes.
324 * If called more than once, @accel_group remains locked until
325 * gtk_accel_group_unlock() has been called an equivalent number
329 gtk_accel_group_lock (GtkAccelGroup *accel_group)
331 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
333 accel_group->lock_count += 1;
337 * gtk_accel_group_unlock:
338 * @accel_group: a #GtkAccelGroup
340 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
343 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
345 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
346 g_return_if_fail (accel_group->lock_count > 0);
348 accel_group->lock_count -= 1;
352 accel_closure_invalidate (gpointer data,
355 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
357 gtk_accel_group_disconnect (accel_group, closure);
361 bsearch_compare_accels (const void *d1,
364 const GtkAccelGroupEntry *entry1 = d1;
365 const GtkAccelGroupEntry *entry2 = d2;
367 if (entry1->key.accel_key == entry2->key.accel_key)
368 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
370 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
374 quick_accel_add (GtkAccelGroup *accel_group,
376 GdkModifierType accel_mods,
377 GtkAccelFlags accel_flags,
381 guint pos, i = accel_group->n_accels++;
382 GtkAccelGroupEntry key;
385 key.key.accel_key = accel_key;
386 key.key.accel_mods = accel_mods;
387 for (pos = 0; pos < i; pos++)
388 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
391 /* insert at position, ref closure */
392 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
393 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
394 (i - pos) * sizeof (accel_group->priv_accels[0]));
395 accel_group->priv_accels[pos].key.accel_key = accel_key;
396 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
397 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
398 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
399 accel_group->priv_accels[pos].accel_path_quark = path_quark;
400 g_closure_sink (closure);
402 /* handle closure invalidation and reverse lookups */
403 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
405 /* get accel path notification */
407 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
409 /* connect and notify changed */
412 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
413 GQuark accel_quark = g_quark_from_string (accel_name);
418 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
421 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
426 quick_accel_remove (GtkAccelGroup *accel_group,
427 GtkAccelGroupEntry *entry)
429 guint pos = entry - accel_group->priv_accels;
430 GQuark accel_quark = 0;
431 guint accel_key = entry->key.accel_key;
432 GdkModifierType accel_mods = entry->key.accel_mods;
433 GClosure *closure = entry->closure;
435 /* quark for notification */
438 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
440 accel_quark = g_quark_from_string (accel_name);
444 /* clean up closure invalidate notification and disconnect */
445 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
447 g_signal_handlers_disconnect_matched (accel_group,
448 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
449 signal_accel_activate, accel_quark,
450 closure, NULL, NULL);
451 /* clean up accel path notification */
452 if (entry->accel_path_quark)
453 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
455 /* physically remove */
456 accel_group->n_accels -= 1;
457 g_memmove (entry, entry + 1,
458 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
462 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
464 /* remove quick_accel_add() refcount */
465 g_closure_unref (closure);
468 static GtkAccelGroupEntry*
469 quick_accel_find (GtkAccelGroup *accel_group,
471 GdkModifierType accel_mods,
474 GtkAccelGroupEntry *entry;
475 GtkAccelGroupEntry key;
479 if (!accel_group->n_accels)
482 key.key.accel_key = accel_key;
483 key.key.accel_mods = accel_mods;
484 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
485 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
490 /* step back to the first member */
491 for (; entry > accel_group->priv_accels; entry--)
492 if (entry[-1].key.accel_key != accel_key ||
493 entry[-1].key.accel_mods != accel_mods)
495 /* count equal members */
496 for (; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
497 if (entry[*count_p].key.accel_key != accel_key ||
498 entry[*count_p].key.accel_mods != accel_mods)
504 * gtk_accel_group_connect:
505 * @accel_group: the accelerator group to install an accelerator in
506 * @accel_key: key value of the accelerator
507 * @accel_mods: modifier combination of the accelerator
508 * @accel_flags: a flag mask to configure this accelerator
509 * @closure: closure to be executed upon accelerator activation
511 * Installs an accelerator in this group. When @accel_group is being activated
512 * in response to a call to gtk_accel_groups_activate(), @closure will be
513 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
514 * match those of this connection.
516 * The signature used for the @closure is that of #GtkAccelGroupActivate.
518 * Note that, due to implementation details, a single closure can only be
519 * connected to one accelerator group.
522 gtk_accel_group_connect (GtkAccelGroup *accel_group,
524 GdkModifierType accel_mods,
525 GtkAccelFlags accel_flags,
528 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
529 g_return_if_fail (closure != NULL);
530 g_return_if_fail (accel_key > 0);
531 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
533 g_object_ref (accel_group);
534 if (!closure->is_invalid)
535 quick_accel_add (accel_group,
536 gdk_keyval_to_lower (accel_key),
537 accel_mods, accel_flags, closure, 0);
538 g_object_unref (accel_group);
542 * gtk_accel_group_connect_by_path:
543 * @accel_group: the accelerator group to install an accelerator in
544 * @accel_path: path used for determining key and modifiers.
545 * @closure: closure to be executed upon accelerator activation
547 * Installs an accelerator in this group, using an accelerator path to look
548 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
549 * When @accel_group is being activated in response to a call to
550 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
551 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
554 * The signature used for the @closure is that of #GtkAccelGroupActivate.
557 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
558 const gchar *accel_path,
562 GdkModifierType accel_mods = 0;
565 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
566 g_return_if_fail (closure != NULL);
567 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
569 if (closure->is_invalid)
572 g_object_ref (accel_group);
574 if (gtk_accel_map_lookup_entry (accel_path, &key))
576 accel_key = gdk_keyval_to_lower (key.accel_key);
577 accel_mods = key.accel_mods;
580 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
581 g_quark_from_string (accel_path));
583 g_object_unref (accel_group);
587 * gtk_accel_group_disconnect:
588 * @accel_group: the accelerator group to remove an accelerator from
589 * @closure: the closure to remove from this accelerator group
590 * @returns: %TRUE if the closure was found and got disconnected
592 * Removes an accelerator previously installed through
593 * gtk_accel_group_connect().
596 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
601 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
603 for (i = 0; i < accel_group->n_accels; i++)
604 if (accel_group->priv_accels[i].closure == closure)
606 g_object_ref (accel_group);
607 quick_accel_remove (accel_group, accel_group->priv_accels + i);
608 g_object_unref (accel_group);
615 * gtk_accel_group_disconnect_key:
616 * @accel_group: the accelerator group to install an accelerator in
617 * @accel_key: key value of the accelerator
618 * @accel_mods: modifier combination of the accelerator
619 * @returns: %TRUE if there was an accelerator which could be
620 * removed, %FALSE otherwise
622 * Removes an accelerator previously installed through
623 * gtk_accel_group_connect().
626 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
628 GdkModifierType accel_mods)
630 GtkAccelGroupEntry *entries;
631 GSList *slist, *clist = NULL;
632 gboolean removed_one = FALSE;
635 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
637 g_object_ref (accel_group);
639 accel_key = gdk_keyval_to_lower (accel_key);
640 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
643 GClosure *closure = g_closure_ref (entries[n].closure);
645 clist = g_slist_prepend (clist, closure);
648 for (slist = clist; slist; slist = slist->next)
650 GClosure *closure = slist->data;
652 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
653 g_closure_unref (closure);
655 g_slist_free (clist);
657 g_object_unref (accel_group);
663 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
664 GQuark accel_path_quark)
666 GSList *slist, *clist = NULL;
669 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
671 g_object_ref (accel_group);
673 for (i = 0; i < accel_group->n_accels; i++)
674 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
676 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
678 clist = g_slist_prepend (clist, closure);
681 for (slist = clist; slist; slist = slist->next)
683 GClosure *closure = slist->data;
685 gtk_accel_group_disconnect (accel_group, closure);
686 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
687 g_closure_unref (closure);
689 g_slist_free (clist);
691 g_object_unref (accel_group);
695 * gtk_accel_group_query:
696 * @accel_group: the accelerator group to query
697 * @accel_key: key value of the accelerator
698 * @accel_mods: modifier combination of the accelerator
699 * @n_entries: location to return the number of entries found, or %NULL
700 * @returns: an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
702 * Queries an accelerator group for all entries matching @accel_key and
706 gtk_accel_group_query (GtkAccelGroup *accel_group,
708 GdkModifierType accel_mods,
711 GtkAccelGroupEntry *entries;
714 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
716 entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
719 *n_entries = entries ? n : 0;
725 * gtk_accel_group_from_accel_closure:
726 * @closure: a #GClosure
727 * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
729 * Finds the #GtkAccelGroup to which @closure is connected;
730 * see gtk_accel_group_connect().
733 gtk_accel_group_from_accel_closure (GClosure *closure)
737 g_return_val_if_fail (closure != NULL, NULL);
739 /* a few remarks on what we do here. in general, we need a way to reverse lookup
740 * accel_groups from closures that are being used in accel groups. this could
741 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
742 * use the invalidation notifier on the closure itself (which we need to install
743 * anyway), that contains the accel group as data which, besides needing to peek
744 * a bit at closure internals, works just as good.
746 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
747 if (closure->notifiers[i].notify == accel_closure_invalidate)
748 return closure->notifiers[i].data;
754 gtk_accel_group_activate (GtkAccelGroup *accel_group,
756 GObject *acceleratable,
758 GdkModifierType accel_mods)
760 gboolean was_handled;
762 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
763 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
766 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
767 acceleratable, accel_key, accel_mods, &was_handled);
773 * gtk_accel_groups_activate:
774 * @object: the #GObject, usually a #GtkWindow, on which
775 * to activate the accelerator.
776 * @accel_key: accelerator keyval from a key event
777 * @accel_mods: keyboard state mask from a key event
778 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
780 * Finds the first accelerator in any #GtkAccelGroup attached
781 * to @object that matches @accel_key and @accel_mods, and
782 * activates that accelerator.
783 * If an accelerator was activated and handled this keypress, %TRUE
787 gtk_accel_groups_activate (GObject *object,
789 GdkModifierType accel_mods)
791 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
793 if (gtk_accelerator_valid (accel_key, accel_mods))
799 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
800 accel_quark = g_quark_from_string (accel_name);
803 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
804 if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
812 * gtk_accelerator_valid:
813 * @keyval: a GDK keyval
814 * @modifiers: modifier mask
815 * @returns: %TRUE if the accelerator is valid
817 * Determines whether a given keyval and modifier mask constitute
818 * a valid keyboard accelerator. For example, the #GDK_a keyval
819 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
820 * But, you can't, for instance, use the #GDK_Control_L keyval
824 gtk_accelerator_valid (guint keyval,
825 GdkModifierType modifiers)
827 static const guint invalid_accelerator_vals[] = {
828 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
829 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
830 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
831 GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
832 GDK_ISO_First_Group, GDK_ISO_Last_Group,
833 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
834 GDK_Scroll_Lock, GDK_Sys_Req,
835 GDK_Tab, GDK_ISO_Left_Tab, GDK_KP_Tab,
836 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
837 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
838 GDK_Terminate_Server, GDK_AudibleBell_Enable,
841 static const guint invalid_unmodified_vals[] = {
842 GDK_Up, GDK_Down, GDK_Left, GDK_Right,
843 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right,
848 modifiers &= GDK_MODIFIER_MASK;
851 return keyval >= 0x20;
853 ac_val = invalid_accelerator_vals;
856 if (keyval == *ac_val++)
862 ac_val = invalid_unmodified_vals;
865 if (keyval == *ac_val++)
873 static inline gboolean
874 is_alt (const gchar *string)
876 return ((string[0] == '<') &&
877 (string[1] == 'a' || string[1] == 'A') &&
878 (string[2] == 'l' || string[2] == 'L') &&
879 (string[3] == 't' || string[3] == 'T') &&
883 static inline gboolean
884 is_ctl (const gchar *string)
886 return ((string[0] == '<') &&
887 (string[1] == 'c' || string[1] == 'C') &&
888 (string[2] == 't' || string[2] == 'T') &&
889 (string[3] == 'l' || string[3] == 'L') &&
893 static inline gboolean
894 is_modx (const gchar *string)
896 return ((string[0] == '<') &&
897 (string[1] == 'm' || string[1] == 'M') &&
898 (string[2] == 'o' || string[2] == 'O') &&
899 (string[3] == 'd' || string[3] == 'D') &&
900 (string[4] >= '1' && string[4] <= '5') &&
904 static inline gboolean
905 is_ctrl (const gchar *string)
907 return ((string[0] == '<') &&
908 (string[1] == 'c' || string[1] == 'C') &&
909 (string[2] == 't' || string[2] == 'T') &&
910 (string[3] == 'r' || string[3] == 'R') &&
911 (string[4] == 'l' || string[4] == 'L') &&
915 static inline gboolean
916 is_shft (const gchar *string)
918 return ((string[0] == '<') &&
919 (string[1] == 's' || string[1] == 'S') &&
920 (string[2] == 'h' || string[2] == 'H') &&
921 (string[3] == 'f' || string[3] == 'F') &&
922 (string[4] == 't' || string[4] == 'T') &&
926 static inline gboolean
927 is_shift (const gchar *string)
929 return ((string[0] == '<') &&
930 (string[1] == 's' || string[1] == 'S') &&
931 (string[2] == 'h' || string[2] == 'H') &&
932 (string[3] == 'i' || string[3] == 'I') &&
933 (string[4] == 'f' || string[4] == 'F') &&
934 (string[5] == 't' || string[5] == 'T') &&
938 static inline gboolean
939 is_control (const gchar *string)
941 return ((string[0] == '<') &&
942 (string[1] == 'c' || string[1] == 'C') &&
943 (string[2] == 'o' || string[2] == 'O') &&
944 (string[3] == 'n' || string[3] == 'N') &&
945 (string[4] == 't' || string[4] == 'T') &&
946 (string[5] == 'r' || string[5] == 'R') &&
947 (string[6] == 'o' || string[6] == 'O') &&
948 (string[7] == 'l' || string[7] == 'L') &&
952 static inline gboolean
953 is_release (const gchar *string)
955 return ((string[0] == '<') &&
956 (string[1] == 'r' || string[1] == 'R') &&
957 (string[2] == 'e' || string[2] == 'E') &&
958 (string[3] == 'l' || string[3] == 'L') &&
959 (string[4] == 'e' || string[4] == 'E') &&
960 (string[5] == 'a' || string[5] == 'A') &&
961 (string[6] == 's' || string[6] == 'S') &&
962 (string[7] == 'e' || string[7] == 'E') &&
966 static inline gboolean
967 is_meta (const gchar *string)
969 return ((string[0] == '<') &&
970 (string[1] == 'm' || string[1] == 'M') &&
971 (string[2] == 'e' || string[2] == 'E') &&
972 (string[3] == 't' || string[3] == 'T') &&
973 (string[4] == 'a' || string[4] == 'A') &&
977 static inline gboolean
978 is_super (const gchar *string)
980 return ((string[0] == '<') &&
981 (string[1] == 's' || string[1] == 'S') &&
982 (string[2] == 'u' || string[2] == 'U') &&
983 (string[3] == 'p' || string[3] == 'P') &&
984 (string[4] == 'e' || string[4] == 'E') &&
985 (string[5] == 'r' || string[5] == 'R') &&
989 static inline gboolean
990 is_hyper (const gchar *string)
992 return ((string[0] == '<') &&
993 (string[1] == 'h' || string[1] == 'H') &&
994 (string[2] == 'y' || string[2] == 'Y') &&
995 (string[3] == 'p' || string[3] == 'P') &&
996 (string[4] == 'e' || string[4] == 'E') &&
997 (string[5] == 'r' || string[5] == 'R') &&
1002 * gtk_accelerator_parse:
1003 * @accelerator: string representing an accelerator
1004 * @accelerator_key: return location for accelerator keyval
1005 * @accelerator_mods: return location for accelerator modifier mask
1007 * Parses a string representing an accelerator. The
1008 * format looks like "<Control>a" or "<Shift><Alt>F1" or
1009 * "<Release>z" (the last one is for key release).
1010 * The parser is fairly liberal and allows lower or upper case,
1011 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
1013 * If the parse fails, @accelerator_key and @accelerator_mods will
1014 * be set to 0 (zero).
1017 gtk_accelerator_parse (const gchar *accelerator,
1018 guint *accelerator_key,
1019 GdkModifierType *accelerator_mods)
1022 GdkModifierType mods;
1025 if (accelerator_key)
1026 *accelerator_key = 0;
1027 if (accelerator_mods)
1028 *accelerator_mods = 0;
1029 g_return_if_fail (accelerator != NULL);
1033 len = strlen (accelerator);
1036 if (*accelerator == '<')
1038 if (len >= 9 && is_release (accelerator))
1042 mods |= GDK_RELEASE_MASK;
1044 else if (len >= 9 && is_control (accelerator))
1048 mods |= GDK_CONTROL_MASK;
1050 else if (len >= 7 && is_shift (accelerator))
1054 mods |= GDK_SHIFT_MASK;
1056 else if (len >= 6 && is_shft (accelerator))
1060 mods |= GDK_SHIFT_MASK;
1062 else if (len >= 6 && is_ctrl (accelerator))
1066 mods |= GDK_CONTROL_MASK;
1068 else if (len >= 6 && is_modx (accelerator))
1070 static const guint mod_vals[] = {
1071 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1072 GDK_MOD4_MASK, GDK_MOD5_MASK
1077 mods |= mod_vals[*accelerator - '1'];
1080 else if (len >= 5 && is_ctl (accelerator))
1084 mods |= GDK_CONTROL_MASK;
1086 else if (len >= 5 && is_alt (accelerator))
1090 mods |= GDK_MOD1_MASK;
1092 else if (len >= 6 && is_meta (accelerator))
1096 mods |= GDK_META_MASK;
1098 else if (len >= 7 && is_hyper (accelerator))
1102 mods |= GDK_HYPER_MASK;
1104 else if (len >= 7 && is_super (accelerator))
1108 mods |= GDK_SUPER_MASK;
1114 last_ch = *accelerator;
1115 while (last_ch && last_ch != '>')
1117 last_ch = *accelerator;
1125 keyval = gdk_keyval_from_name (accelerator);
1131 if (accelerator_key)
1132 *accelerator_key = gdk_keyval_to_lower (keyval);
1133 if (accelerator_mods)
1134 *accelerator_mods = mods;
1138 * gtk_accelerator_name:
1139 * @accelerator_key: accelerator keyval
1140 * @accelerator_mods: accelerator modifier mask
1142 * Converts an accelerator keyval and modifier mask
1143 * into a string parseable by gtk_accelerator_parse().
1144 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1145 * this function returns "<Control>q".
1147 * If you need to display accelerators in the user interface,
1148 * see gtk_accelerator_get_label().
1150 * Returns: a newly-allocated accelerator name
1153 gtk_accelerator_name (guint accelerator_key,
1154 GdkModifierType accelerator_mods)
1156 static const gchar text_release[] = "<Release>";
1157 static const gchar text_shift[] = "<Shift>";
1158 static const gchar text_control[] = "<Control>";
1159 static const gchar text_mod1[] = "<Alt>";
1160 static const gchar text_mod2[] = "<Mod2>";
1161 static const gchar text_mod3[] = "<Mod3>";
1162 static const gchar text_mod4[] = "<Mod4>";
1163 static const gchar text_mod5[] = "<Mod5>";
1164 static const gchar text_meta[] = "<Meta>";
1165 static const gchar text_super[] = "<Super>";
1166 static const gchar text_hyper[] = "<Hyper>";
1171 accelerator_mods &= GDK_MODIFIER_MASK;
1173 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1178 if (accelerator_mods & GDK_RELEASE_MASK)
1179 l += sizeof (text_release) - 1;
1180 if (accelerator_mods & GDK_SHIFT_MASK)
1181 l += sizeof (text_shift) - 1;
1182 if (accelerator_mods & GDK_CONTROL_MASK)
1183 l += sizeof (text_control) - 1;
1184 if (accelerator_mods & GDK_MOD1_MASK)
1185 l += sizeof (text_mod1) - 1;
1186 if (accelerator_mods & GDK_MOD2_MASK)
1187 l += sizeof (text_mod2) - 1;
1188 if (accelerator_mods & GDK_MOD3_MASK)
1189 l += sizeof (text_mod3) - 1;
1190 if (accelerator_mods & GDK_MOD4_MASK)
1191 l += sizeof (text_mod4) - 1;
1192 if (accelerator_mods & GDK_MOD5_MASK)
1193 l += sizeof (text_mod5) - 1;
1194 l += strlen (keyval_name);
1195 if (accelerator_mods & GDK_META_MASK)
1196 l += sizeof (text_meta) - 1;
1197 if (accelerator_mods & GDK_HYPER_MASK)
1198 l += sizeof (text_hyper) - 1;
1199 if (accelerator_mods & GDK_SUPER_MASK)
1200 l += sizeof (text_super) - 1;
1202 accelerator = g_new (gchar, l + 1);
1206 if (accelerator_mods & GDK_RELEASE_MASK)
1208 strcpy (accelerator + l, text_release);
1209 l += sizeof (text_release) - 1;
1211 if (accelerator_mods & GDK_SHIFT_MASK)
1213 strcpy (accelerator + l, text_shift);
1214 l += sizeof (text_shift) - 1;
1216 if (accelerator_mods & GDK_CONTROL_MASK)
1218 strcpy (accelerator + l, text_control);
1219 l += sizeof (text_control) - 1;
1221 if (accelerator_mods & GDK_MOD1_MASK)
1223 strcpy (accelerator + l, text_mod1);
1224 l += sizeof (text_mod1) - 1;
1226 if (accelerator_mods & GDK_MOD2_MASK)
1228 strcpy (accelerator + l, text_mod2);
1229 l += sizeof (text_mod2) - 1;
1231 if (accelerator_mods & GDK_MOD3_MASK)
1233 strcpy (accelerator + l, text_mod3);
1234 l += sizeof (text_mod3) - 1;
1236 if (accelerator_mods & GDK_MOD4_MASK)
1238 strcpy (accelerator + l, text_mod4);
1239 l += sizeof (text_mod4) - 1;
1241 if (accelerator_mods & GDK_MOD5_MASK)
1243 strcpy (accelerator + l, text_mod5);
1244 l += sizeof (text_mod5) - 1;
1246 if (accelerator_mods & GDK_META_MASK)
1248 strcpy (accelerator + l, text_meta);
1249 l += sizeof (text_meta) - 1;
1251 if (accelerator_mods & GDK_HYPER_MASK)
1253 strcpy (accelerator + l, text_hyper);
1254 l += sizeof (text_hyper) - 1;
1256 if (accelerator_mods & GDK_SUPER_MASK)
1258 strcpy (accelerator + l, text_super);
1259 l += sizeof (text_super) - 1;
1261 strcpy (accelerator + l, keyval_name);
1267 * gtk_accelerator_get_label:
1268 * @accelerator_key: accelerator keyval
1269 * @accelerator_mods: accelerator modifier mask
1271 * Converts an accelerator keyval and modifier mask into a string
1272 * which can be used to represent the accelerator to the user.
1274 * Returns: a newly-allocated string representing the accelerator.
1279 gtk_accelerator_get_label (guint accelerator_key,
1280 GdkModifierType accelerator_mods)
1282 GtkAccelLabelClass *klass;
1285 klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
1286 label = _gtk_accel_label_class_get_accelerator_label (klass,
1289 g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
1295 * gtk_accelerator_set_default_mod_mask:
1296 * @default_mod_mask: accelerator modifier mask
1298 * Sets the modifiers that will be considered significant for keyboard
1299 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1300 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1301 * Other modifiers will by default be ignored by #GtkAccelGroup.
1302 * You must include at least the three default modifiers in any
1303 * value you pass to this function.
1305 * The default mod mask should be changed on application startup,
1306 * before using any accelerator groups.
1309 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1311 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1312 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1316 * gtk_accelerator_get_default_mod_mask:
1317 * @returns: the default accelerator modifier mask
1319 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1322 gtk_accelerator_get_default_mod_mask (void)
1324 return default_accel_mod_mask;
1327 #define __GTK_ACCEL_GROUP_C__
1328 #include "gtkaliasdef.c"