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 "gtkaccelmap.h"
33 #include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
34 #include "gdk/gdkkeysyms.h"
35 #include "gtkmarshalers.h"
38 /* --- prototypes --- */
39 static void gtk_accel_group_class_init (GtkAccelGroupClass *class);
40 static void gtk_accel_group_init (GtkAccelGroup *accel_group);
41 static void gtk_accel_group_finalize (GObject *object);
44 /* --- variables --- */
45 static GObjectClass *parent_class = NULL;
46 static guint signal_accel_activate = 0;
47 static guint signal_accel_changed = 0;
48 static guint quark_acceleratable_groups = 0;
49 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
54 /* --- functions --- */
56 * gtk_accel_group_get_type:
57 * @returns: the type ID for accelerator groups.
60 gtk_accel_group_get_type (void)
62 static GType object_type = 0;
66 static const GTypeInfo object_info = {
67 sizeof (GtkAccelGroupClass),
69 (GBaseFinalizeFunc) NULL,
70 (GClassInitFunc) gtk_accel_group_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
73 sizeof (GtkAccelGroup),
75 (GInstanceInitFunc) gtk_accel_group_init,
78 object_type = g_type_register_static (G_TYPE_OBJECT, "GtkAccelGroup",
86 gtk_accel_group_class_init (GtkAccelGroupClass *class)
88 GObjectClass *object_class = G_OBJECT_CLASS (class);
90 parent_class = g_type_class_peek_parent (class);
92 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
94 object_class->finalize = gtk_accel_group_finalize;
96 class->accel_changed = NULL;
97 signal_accel_activate =
98 g_signal_new ("accel_activate",
99 G_OBJECT_CLASS_TYPE (class),
102 _gtk_boolean_handled_accumulator, NULL,
103 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
107 GDK_TYPE_MODIFIER_TYPE);
108 signal_accel_changed =
109 g_signal_new ("accel_changed",
110 G_OBJECT_CLASS_TYPE (class),
111 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
112 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
114 _gtk_marshal_VOID__UINT_FLAGS_BOXED,
117 GDK_TYPE_MODIFIER_TYPE,
122 gtk_accel_group_finalize (GObject *object)
124 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
127 for (i = 0; i < accel_group->n_accels; i++)
129 GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
131 if (entry->accel_path_quark)
133 const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
135 _gtk_accel_map_remove_group (accel_path, accel_group);
139 g_free (accel_group->priv_accels);
141 G_OBJECT_CLASS (parent_class)->finalize (object);
145 gtk_accel_group_init (GtkAccelGroup *accel_group)
147 accel_group->lock_count = 0;
148 accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
149 accel_group->acceleratables = NULL;
150 accel_group->n_accels = 0;
151 accel_group->priv_accels = NULL;
155 * gtk_accel_group_new:
156 * @returns: a new #GtkAccelGroup object
158 * Creates a new #GtkAccelGroup.
161 gtk_accel_group_new (void)
163 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
167 accel_group_weak_ref_detach (GSList *free_list,
168 GObject *stale_object)
172 for (slist = free_list; slist; slist = slist->next)
174 GtkAccelGroup *accel_group;
176 accel_group = slist->data;
177 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
178 g_object_unref (accel_group);
180 g_slist_free (free_list);
181 g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
185 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
190 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
191 g_return_if_fail (G_IS_OBJECT (object));
192 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) == NULL);
194 g_object_ref (accel_group);
195 accel_group->acceleratables = g_slist_prepend (accel_group->acceleratables, object);
196 slist = g_object_get_qdata (object, quark_acceleratable_groups);
198 g_object_weak_unref (object,
199 (GWeakNotify) accel_group_weak_ref_detach,
201 slist = g_slist_prepend (slist, accel_group);
202 g_object_set_qdata (object, quark_acceleratable_groups, slist);
203 g_object_weak_ref (object,
204 (GWeakNotify) accel_group_weak_ref_detach,
209 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
214 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
215 g_return_if_fail (G_IS_OBJECT (object));
216 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) != NULL);
218 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, object);
219 slist = g_object_get_qdata (object, quark_acceleratable_groups);
220 g_object_weak_unref (object,
221 (GWeakNotify) accel_group_weak_ref_detach,
223 slist = g_slist_remove (slist, accel_group);
224 g_object_set_qdata (object, quark_acceleratable_groups, slist);
226 g_object_weak_ref (object,
227 (GWeakNotify) accel_group_weak_ref_detach,
229 g_object_unref (accel_group);
233 * gtk_accel_groups_from_object:
234 * @object: a #GObject, usually a #GtkWindow
235 * @returns: a list of all accel groups which are attached to @object
237 * Gets a list of all accel groups which are attached to @object.
240 gtk_accel_groups_from_object (GObject *object)
242 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
244 return g_object_get_qdata (object, quark_acceleratable_groups);
248 * gtk_accel_group_find:
249 * @accel_group: a #GtkAccelGroup
250 * @find_func: a function to filter the entries of @accel_group with
251 * @data: data to pass to @find_func
252 * @returns: the key of the first entry passing @find_func. The key is
253 * owned by GTK+ and must not be freed.
255 * Finds the first entry in an accelerator group for which
256 * @find_func returns %TRUE and returns its #GtkAccelKey.
260 gtk_accel_group_find (GtkAccelGroup *accel_group,
261 GtkAccelGroupFindFunc find_func,
264 GtkAccelKey *key = NULL;
267 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
268 g_return_val_if_fail (find_func != NULL, NULL);
270 g_object_ref (accel_group);
271 for (i = 0; i < accel_group->n_accels; i++)
272 if (find_func (&accel_group->priv_accels[i].key,
273 accel_group->priv_accels[i].closure,
276 key = &accel_group->priv_accels[i].key;
279 g_object_unref (accel_group);
285 * gtk_accel_group_lock:
286 * @accel_group: a #GtkAccelGroup
288 * Locks the given accelerator group.
290 * Locking an acelerator group prevents the accelerators contained
291 * within it to be changed during runtime. Refer to
292 * gtk_accel_map_change_entry() about runtime accelerator changes.
294 * If called more than once, @accel_group remains locked until
295 * gtk_accel_group_unlock() has been called an equivalent number
299 gtk_accel_group_lock (GtkAccelGroup *accel_group)
301 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
303 accel_group->lock_count += 1;
307 * gtk_accel_group_unlock:
308 * @accel_group: a #GtkAccelGroup
310 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
313 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
315 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
316 g_return_if_fail (accel_group->lock_count > 0);
318 accel_group->lock_count -= 1;
322 accel_closure_invalidate (gpointer data,
325 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
327 gtk_accel_group_disconnect (accel_group, closure);
331 bsearch_compare_accels (const void *d1,
334 const GtkAccelGroupEntry *entry1 = d1;
335 const GtkAccelGroupEntry *entry2 = d2;
337 if (entry1->key.accel_key == entry2->key.accel_key)
338 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
340 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
344 quick_accel_add (GtkAccelGroup *accel_group,
346 GdkModifierType accel_mods,
347 GtkAccelFlags accel_flags,
351 guint pos, i = accel_group->n_accels++;
352 GtkAccelGroupEntry key;
355 key.key.accel_key = accel_key;
356 key.key.accel_mods = accel_mods;
357 for (pos = 0; pos < i; pos++)
358 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
361 /* insert at position, ref closure */
362 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
363 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
364 (i - pos) * sizeof (accel_group->priv_accels[0]));
365 accel_group->priv_accels[pos].key.accel_key = accel_key;
366 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
367 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
368 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
369 accel_group->priv_accels[pos].accel_path_quark = path_quark;
370 g_closure_sink (closure);
372 /* handle closure invalidation and reverse lookups */
373 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
375 /* get accel path notification */
377 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
379 /* connect and notify changed */
382 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
383 GQuark accel_quark = g_quark_from_string (accel_name);
388 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
391 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
396 quick_accel_remove (GtkAccelGroup *accel_group,
397 GtkAccelGroupEntry *entry)
399 guint pos = entry - accel_group->priv_accels;
400 GQuark accel_quark = 0;
401 guint accel_key = entry->key.accel_key;
402 GdkModifierType accel_mods = entry->key.accel_mods;
403 GClosure *closure = entry->closure;
405 /* quark for notification */
408 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
410 accel_quark = g_quark_from_string (accel_name);
414 /* clean up closure invalidate notification and disconnect */
415 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
417 g_signal_handlers_disconnect_matched (accel_group,
418 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
419 signal_accel_activate, accel_quark,
420 closure, NULL, NULL);
421 /* clean up accel path notification */
422 if (entry->accel_path_quark)
423 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
425 /* physically remove */
426 accel_group->n_accels -= 1;
427 g_memmove (entry, entry + 1,
428 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
432 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
434 /* remove quick_accel_add() refcount */
435 g_closure_unref (closure);
438 static GtkAccelGroupEntry*
439 quick_accel_find (GtkAccelGroup *accel_group,
441 GdkModifierType accel_mods,
444 GtkAccelGroupEntry *entry;
445 GtkAccelGroupEntry key;
449 if (!accel_group->n_accels)
452 key.key.accel_key = accel_key;
453 key.key.accel_mods = accel_mods;
454 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
455 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
460 /* step back to the first member */
461 for (; entry > accel_group->priv_accels; entry--)
462 if (entry[-1].key.accel_key != accel_key ||
463 entry[-1].key.accel_mods != accel_mods)
465 /* count equal members */
466 for (; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
467 if (entry[*count_p].key.accel_key != accel_key ||
468 entry[*count_p].key.accel_mods != accel_mods)
474 * gtk_accel_group_connect:
475 * @accel_group: the accelerator group to install an accelerator in
476 * @accel_key: key value of the accelerator
477 * @accel_mods: modifier combination of the accelerator
478 * @accel_flags: a flag mask to configure this accelerator
479 * @closure: closure to be executed upon accelerator activation
481 * Installs an accelerator in this group. When @accel_group is being activated
482 * in response to a call to gtk_accel_groups_activate(), @closure will be
483 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
484 * match those of this connection.
486 * The signature used for the @closure is that of #GtkAccelGroupActivate.
488 * Note that, due to implementation details, a single closure can only be
489 * connected to one accelerator group.
492 gtk_accel_group_connect (GtkAccelGroup *accel_group,
494 GdkModifierType accel_mods,
495 GtkAccelFlags accel_flags,
498 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
499 g_return_if_fail (closure != NULL);
500 g_return_if_fail (accel_key > 0);
501 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
503 g_object_ref (accel_group);
504 if (!closure->is_invalid)
505 quick_accel_add (accel_group,
506 gdk_keyval_to_lower (accel_key),
507 accel_mods, accel_flags, closure, 0);
508 g_object_unref (accel_group);
512 * gtk_accel_group_connect_by_path:
513 * @accel_group: the accelerator group to install an accelerator in
514 * @accel_path: path used for determining key and modifiers.
515 * @closure: closure to be executed upon accelerator activation
517 * Installs an accelerator in this group, using an accelerator path to look
518 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
519 * When @accel_group is being activated in response to a call to
520 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
521 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
524 * The signature used for the @closure is that of #GtkAccelGroupActivate.
527 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
528 const gchar *accel_path,
532 GdkModifierType accel_mods = 0;
535 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
536 g_return_if_fail (closure != NULL);
537 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
539 if (closure->is_invalid)
542 g_object_ref (accel_group);
544 if (gtk_accel_map_lookup_entry (accel_path, &key))
546 accel_key = gdk_keyval_to_lower (key.accel_key);
547 accel_mods = key.accel_mods;
550 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
551 g_quark_from_string (accel_path));
553 g_object_unref (accel_group);
557 * gtk_accel_group_disconnect:
558 * @accel_group: the accelerator group to remove an accelerator from
559 * @closure: the closure to remove from this accelerator group
560 * @returns: %TRUE if the closure was found and got disconnected
562 * Removes an accelerator previously installed through
563 * gtk_accel_group_connect().
566 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
571 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
573 for (i = 0; i < accel_group->n_accels; i++)
574 if (accel_group->priv_accels[i].closure == closure)
576 g_object_ref (accel_group);
577 quick_accel_remove (accel_group, accel_group->priv_accels + i);
578 g_object_unref (accel_group);
585 * gtk_accel_group_disconnect_key:
586 * @accel_group: the accelerator group to install an accelerator in
587 * @accel_key: key value of the accelerator
588 * @accel_mods: modifier combination of the accelerator
589 * @returns: %TRUE if there was an accelerator which could be
590 * removed, %FALSE otherwise
592 * Removes an accelerator previously installed through
593 * gtk_accel_group_connect().
596 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
598 GdkModifierType accel_mods)
600 GtkAccelGroupEntry *entries;
601 GSList *slist, *clist = NULL;
602 gboolean removed_one = FALSE;
605 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
607 g_object_ref (accel_group);
609 accel_key = gdk_keyval_to_lower (accel_key);
610 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
613 GClosure *closure = g_closure_ref (entries[n].closure);
615 clist = g_slist_prepend (clist, closure);
618 for (slist = clist; slist; slist = slist->next)
620 GClosure *closure = slist->data;
622 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
623 g_closure_unref (closure);
625 g_slist_free (clist);
627 g_object_unref (accel_group);
633 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
634 GQuark accel_path_quark)
636 GSList *slist, *clist = NULL;
639 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
641 g_object_ref (accel_group);
643 for (i = 0; i < accel_group->n_accels; i++)
644 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
646 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
648 clist = g_slist_prepend (clist, closure);
651 for (slist = clist; slist; slist = slist->next)
653 GClosure *closure = slist->data;
655 gtk_accel_group_disconnect (accel_group, closure);
656 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
657 g_closure_unref (closure);
659 g_slist_free (clist);
661 g_object_unref (accel_group);
665 * gtk_accel_group_query:
666 * @accel_group: the accelerator group to query
667 * @accel_key: key value of the accelerator
668 * @accel_mods: modifier combination of the accelerator
669 * @n_entries: location to return the number of entries found, or %NULL
670 * @returns: an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
672 * Queries an accelerator group for all entries matching @accel_key and
676 gtk_accel_group_query (GtkAccelGroup *accel_group,
678 GdkModifierType accel_mods,
681 GtkAccelGroupEntry *entries;
684 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
686 entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
689 *n_entries = entries ? n : 0;
695 * gtk_accel_group_from_accel_closure:
696 * @closure: a #GClosure
697 * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
699 * Finds the #GtkAccelGroup to which @closure is connected;
700 * see gtk_accel_group_connect().
703 gtk_accel_group_from_accel_closure (GClosure *closure)
707 g_return_val_if_fail (closure != NULL, NULL);
709 /* a few remarks on what we do here. in general, we need a way to reverse lookup
710 * accel_groups from closures that are being used in accel groups. this could
711 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
712 * use the invalidation notifier on the closure itself (which we need to install
713 * anyway), that contains the accel group as data which, besides needing to peek
714 * a bit at closure internals, works just as good.
716 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
717 if (closure->notifiers[i].notify == accel_closure_invalidate)
718 return closure->notifiers[i].data;
724 gtk_accel_group_activate (GtkAccelGroup *accel_group,
726 GObject *acceleratable,
728 GdkModifierType accel_mods)
730 gboolean was_handled;
732 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
733 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
736 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
737 acceleratable, accel_key, accel_mods, &was_handled);
743 * gtk_accel_groups_activate:
744 * @object: the #GObject, usually a #GtkWindow, on which
745 * to activate the accelerator.
746 * @accel_key: accelerator keyval from a key event
747 * @accel_mods: keyboard state mask from a key event
748 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
750 * Finds the first accelerator in any #GtkAccelGroup attached
751 * to @object that matches @accel_key and @accel_mods, and
752 * activates that accelerator.
753 * If an accelerator was activated and handled this keypress, %TRUE
757 gtk_accel_groups_activate (GObject *object,
759 GdkModifierType accel_mods)
761 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
763 if (gtk_accelerator_valid (accel_key, accel_mods))
769 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
770 accel_quark = g_quark_from_string (accel_name);
773 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
774 if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
782 * gtk_accelerator_valid:
783 * @keyval: a GDK keyval
784 * @modifiers: modifier mask
785 * @returns: %TRUE if the accelerator is valid
787 * Determines whether a given keyval and modifier mask constitute
788 * a valid keyboard accelerator. For example, the #GDK_a keyval
789 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
790 * But, you can't, for instance, use the #GDK_Control_L keyval
794 gtk_accelerator_valid (guint keyval,
795 GdkModifierType modifiers)
797 static const guint invalid_accelerator_vals[] = {
798 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
799 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
800 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
801 GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
802 GDK_ISO_First_Group, GDK_ISO_Last_Group,
803 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
804 GDK_Scroll_Lock, GDK_Sys_Req,
805 GDK_Tab, GDK_ISO_Left_Tab, GDK_KP_Tab,
806 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
807 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
808 GDK_Terminate_Server, GDK_AudibleBell_Enable,
811 static const guint invalid_unmodified_vals[] = {
812 GDK_Up, GDK_Down, GDK_Left, GDK_Right,
813 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right,
818 modifiers &= GDK_MODIFIER_MASK;
821 return keyval >= 0x20;
823 ac_val = invalid_accelerator_vals;
826 if (keyval == *ac_val++)
832 ac_val = invalid_unmodified_vals;
835 if (keyval == *ac_val++)
843 static inline gboolean
844 is_alt (const gchar *string)
846 return ((string[0] == '<') &&
847 (string[1] == 'a' || string[1] == 'A') &&
848 (string[2] == 'l' || string[2] == 'L') &&
849 (string[3] == 't' || string[3] == 'T') &&
853 static inline gboolean
854 is_ctl (const gchar *string)
856 return ((string[0] == '<') &&
857 (string[1] == 'c' || string[1] == 'C') &&
858 (string[2] == 't' || string[2] == 'T') &&
859 (string[3] == 'l' || string[3] == 'L') &&
863 static inline gboolean
864 is_modx (const gchar *string)
866 return ((string[0] == '<') &&
867 (string[1] == 'm' || string[1] == 'M') &&
868 (string[2] == 'o' || string[2] == 'O') &&
869 (string[3] == 'd' || string[3] == 'D') &&
870 (string[4] >= '1' && string[4] <= '5') &&
874 static inline gboolean
875 is_ctrl (const gchar *string)
877 return ((string[0] == '<') &&
878 (string[1] == 'c' || string[1] == 'C') &&
879 (string[2] == 't' || string[2] == 'T') &&
880 (string[3] == 'r' || string[3] == 'R') &&
881 (string[4] == 'l' || string[4] == 'L') &&
885 static inline gboolean
886 is_shft (const gchar *string)
888 return ((string[0] == '<') &&
889 (string[1] == 's' || string[1] == 'S') &&
890 (string[2] == 'h' || string[2] == 'H') &&
891 (string[3] == 'f' || string[3] == 'F') &&
892 (string[4] == 't' || string[4] == 'T') &&
896 static inline gboolean
897 is_shift (const gchar *string)
899 return ((string[0] == '<') &&
900 (string[1] == 's' || string[1] == 'S') &&
901 (string[2] == 'h' || string[2] == 'H') &&
902 (string[3] == 'i' || string[3] == 'I') &&
903 (string[4] == 'f' || string[4] == 'F') &&
904 (string[5] == 't' || string[5] == 'T') &&
908 static inline gboolean
909 is_control (const gchar *string)
911 return ((string[0] == '<') &&
912 (string[1] == 'c' || string[1] == 'C') &&
913 (string[2] == 'o' || string[2] == 'O') &&
914 (string[3] == 'n' || string[3] == 'N') &&
915 (string[4] == 't' || string[4] == 'T') &&
916 (string[5] == 'r' || string[5] == 'R') &&
917 (string[6] == 'o' || string[6] == 'O') &&
918 (string[7] == 'l' || string[7] == 'L') &&
922 static inline gboolean
923 is_release (const gchar *string)
925 return ((string[0] == '<') &&
926 (string[1] == 'r' || string[1] == 'R') &&
927 (string[2] == 'e' || string[2] == 'E') &&
928 (string[3] == 'l' || string[3] == 'L') &&
929 (string[4] == 'e' || string[4] == 'E') &&
930 (string[5] == 'a' || string[5] == 'A') &&
931 (string[6] == 's' || string[6] == 'S') &&
932 (string[7] == 'e' || string[7] == 'E') &&
937 * gtk_accelerator_parse:
938 * @accelerator: string representing an accelerator
939 * @accelerator_key: return location for accelerator keyval
940 * @accelerator_mods: return location for accelerator modifier mask
942 * Parses a string representing an accelerator. The
943 * format looks like "<Control>a" or "<Shift><Alt>F1" or
944 * "<Release>z" (the last one is for key release).
945 * The parser is fairly liberal and allows lower or upper case,
946 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
948 * If the parse fails, @accelerator_key and @accelerator_mods will
949 * be set to 0 (zero).
952 gtk_accelerator_parse (const gchar *accelerator,
953 guint *accelerator_key,
954 GdkModifierType *accelerator_mods)
957 GdkModifierType mods;
961 *accelerator_key = 0;
962 if (accelerator_mods)
963 *accelerator_mods = 0;
964 g_return_if_fail (accelerator != NULL);
968 len = strlen (accelerator);
971 if (*accelerator == '<')
973 if (len >= 9 && is_release (accelerator))
977 mods |= GDK_RELEASE_MASK;
979 else if (len >= 9 && is_control (accelerator))
983 mods |= GDK_CONTROL_MASK;
985 else if (len >= 7 && is_shift (accelerator))
989 mods |= GDK_SHIFT_MASK;
991 else if (len >= 6 && is_shft (accelerator))
995 mods |= GDK_SHIFT_MASK;
997 else if (len >= 6 && is_ctrl (accelerator))
1001 mods |= GDK_CONTROL_MASK;
1003 else if (len >= 6 && is_modx (accelerator))
1005 static const guint mod_vals[] = {
1006 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1007 GDK_MOD4_MASK, GDK_MOD5_MASK
1012 mods |= mod_vals[*accelerator - '1'];
1015 else if (len >= 5 && is_ctl (accelerator))
1019 mods |= GDK_CONTROL_MASK;
1021 else if (len >= 5 && is_alt (accelerator))
1025 mods |= GDK_MOD1_MASK;
1031 last_ch = *accelerator;
1032 while (last_ch && last_ch != '>')
1034 last_ch = *accelerator;
1042 keyval = gdk_keyval_from_name (accelerator);
1048 if (accelerator_key)
1049 *accelerator_key = gdk_keyval_to_lower (keyval);
1050 if (accelerator_mods)
1051 *accelerator_mods = mods;
1055 * gtk_accelerator_name:
1056 * @accelerator_key: accelerator keyval
1057 * @accelerator_mods: accelerator modifier mask
1058 * @returns: a newly-allocated accelerator name
1060 * Converts an accelerator keyval and modifier mask
1061 * into a string parseable by gtk_accelerator_parse().
1062 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1063 * this function returns "<Control>q".
1065 * The caller of this function must free the returned string.
1068 gtk_accelerator_name (guint accelerator_key,
1069 GdkModifierType accelerator_mods)
1071 static const gchar text_release[] = "<Release>";
1072 static const gchar text_shift[] = "<Shift>";
1073 static const gchar text_control[] = "<Control>";
1074 static const gchar text_mod1[] = "<Alt>";
1075 static const gchar text_mod2[] = "<Mod2>";
1076 static const gchar text_mod3[] = "<Mod3>";
1077 static const gchar text_mod4[] = "<Mod4>";
1078 static const gchar text_mod5[] = "<Mod5>";
1083 accelerator_mods &= GDK_MODIFIER_MASK;
1085 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1090 if (accelerator_mods & GDK_RELEASE_MASK)
1091 l += sizeof (text_release) - 1;
1092 if (accelerator_mods & GDK_SHIFT_MASK)
1093 l += sizeof (text_shift) - 1;
1094 if (accelerator_mods & GDK_CONTROL_MASK)
1095 l += sizeof (text_control) - 1;
1096 if (accelerator_mods & GDK_MOD1_MASK)
1097 l += sizeof (text_mod1) - 1;
1098 if (accelerator_mods & GDK_MOD2_MASK)
1099 l += sizeof (text_mod2) - 1;
1100 if (accelerator_mods & GDK_MOD3_MASK)
1101 l += sizeof (text_mod3) - 1;
1102 if (accelerator_mods & GDK_MOD4_MASK)
1103 l += sizeof (text_mod4) - 1;
1104 if (accelerator_mods & GDK_MOD5_MASK)
1105 l += sizeof (text_mod5) - 1;
1106 l += strlen (keyval_name);
1108 accelerator = g_new (gchar, l + 1);
1112 if (accelerator_mods & GDK_RELEASE_MASK)
1114 strcpy (accelerator + l, text_release);
1115 l += sizeof (text_release) - 1;
1117 if (accelerator_mods & GDK_SHIFT_MASK)
1119 strcpy (accelerator + l, text_shift);
1120 l += sizeof (text_shift) - 1;
1122 if (accelerator_mods & GDK_CONTROL_MASK)
1124 strcpy (accelerator + l, text_control);
1125 l += sizeof (text_control) - 1;
1127 if (accelerator_mods & GDK_MOD1_MASK)
1129 strcpy (accelerator + l, text_mod1);
1130 l += sizeof (text_mod1) - 1;
1132 if (accelerator_mods & GDK_MOD2_MASK)
1134 strcpy (accelerator + l, text_mod2);
1135 l += sizeof (text_mod2) - 1;
1137 if (accelerator_mods & GDK_MOD3_MASK)
1139 strcpy (accelerator + l, text_mod3);
1140 l += sizeof (text_mod3) - 1;
1142 if (accelerator_mods & GDK_MOD4_MASK)
1144 strcpy (accelerator + l, text_mod4);
1145 l += sizeof (text_mod4) - 1;
1147 if (accelerator_mods & GDK_MOD5_MASK)
1149 strcpy (accelerator + l, text_mod5);
1150 l += sizeof (text_mod5) - 1;
1152 strcpy (accelerator + l, keyval_name);
1158 * gtk_accelerator_set_default_mod_mask:
1159 * @default_mod_mask: accelerator modifier mask
1161 * Sets the modifiers that will be considered significant for keyboard
1162 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1163 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1164 * Other modifiers will by default be ignored by #GtkAccelGroup.
1165 * You must include at least the three default modifiers in any
1166 * value you pass to this function.
1168 * The default mod mask should be changed on application startup,
1169 * before using any accelerator groups.
1172 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1174 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1175 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1179 * gtk_accelerator_get_default_mod_mask:
1180 * @returns: the default accelerator modifier mask
1182 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1185 gtk_accelerator_get_default_mod_mask (void)
1187 return default_accel_mod_mask;