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_finalize (GObject *object);
45 /* --- variables --- */
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 |
57 G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
59 /* --- functions --- */
61 gtk_accel_group_class_init (GtkAccelGroupClass *class)
63 GObjectClass *object_class = G_OBJECT_CLASS (class);
65 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
67 object_class->finalize = gtk_accel_group_finalize;
69 class->accel_changed = NULL;
72 * GtkAccelGroup::accel-activate:
73 * @accel_group: the #GtkAccelGroup which received the signal
74 * @acceleratable: the object on which the accelerator was activated
75 * @keyval: the accelerator keyval
76 * @modifier: the modifier combination of the accelerator
78 * The accel-activate signal is an implementation detail of
79 * #GtkAccelGroup and not meant to be used by applications.
81 * Returns: %TRUE if the accelerator was activated
83 signal_accel_activate =
84 g_signal_new (I_("accel_activate"),
85 G_OBJECT_CLASS_TYPE (class),
88 _gtk_boolean_handled_accumulator, NULL,
89 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
93 GDK_TYPE_MODIFIER_TYPE);
95 * GtkAccelGroup::accel-changed:
96 * @accel_group: the #GtkAccelGroup which received the signal
97 * @keyval: the accelerator keyval
98 * @modifier: the modifier combination of the accelerator
99 * @accel_closure: the #GClosure of the accelerator
101 * The accel-changed signal is emitted when a #GtkAccelGroupEntry
102 * is added to or removed from the accel group.
104 * Widgets like #GtkAccelLabel which display an associated
105 * accelerator should connect to this signal, and rebuild
106 * their visual representation if the @accel_closure is theirs.
108 signal_accel_changed =
109 g_signal_new (I_("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->accel_path_quark);
135 _gtk_accel_map_remove_group (accel_path, accel_group);
139 g_free (accel_group->priv_accels);
141 G_OBJECT_CLASS (gtk_accel_group_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') &&
936 static inline gboolean
937 is_meta (const gchar *string)
939 return ((string[0] == '<') &&
940 (string[1] == 'm' || string[1] == 'M') &&
941 (string[2] == 'e' || string[2] == 'E') &&
942 (string[3] == 't' || string[3] == 'T') &&
943 (string[4] == 'a' || string[4] == 'A') &&
947 static inline gboolean
948 is_super (const gchar *string)
950 return ((string[0] == '<') &&
951 (string[1] == 's' || string[1] == 'S') &&
952 (string[2] == 'u' || string[2] == 'U') &&
953 (string[3] == 'p' || string[3] == 'P') &&
954 (string[4] == 'e' || string[4] == 'E') &&
955 (string[5] == 'r' || string[5] == 'R') &&
959 static inline gboolean
960 is_hyper (const gchar *string)
962 return ((string[0] == '<') &&
963 (string[1] == 'h' || string[1] == 'H') &&
964 (string[2] == 'y' || string[2] == 'Y') &&
965 (string[3] == 'p' || string[3] == 'P') &&
966 (string[4] == 'e' || string[4] == 'E') &&
967 (string[5] == 'r' || string[5] == 'R') &&
972 * gtk_accelerator_parse:
973 * @accelerator: string representing an accelerator
974 * @accelerator_key: return location for accelerator keyval
975 * @accelerator_mods: return location for accelerator modifier mask
977 * Parses a string representing an accelerator. The
978 * format looks like "<Control>a" or "<Shift><Alt>F1" or
979 * "<Release>z" (the last one is for key release).
980 * The parser is fairly liberal and allows lower or upper case,
981 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
983 * If the parse fails, @accelerator_key and @accelerator_mods will
984 * be set to 0 (zero).
987 gtk_accelerator_parse (const gchar *accelerator,
988 guint *accelerator_key,
989 GdkModifierType *accelerator_mods)
992 GdkModifierType mods;
996 *accelerator_key = 0;
997 if (accelerator_mods)
998 *accelerator_mods = 0;
999 g_return_if_fail (accelerator != NULL);
1003 len = strlen (accelerator);
1006 if (*accelerator == '<')
1008 if (len >= 9 && is_release (accelerator))
1012 mods |= GDK_RELEASE_MASK;
1014 else if (len >= 9 && is_control (accelerator))
1018 mods |= GDK_CONTROL_MASK;
1020 else if (len >= 7 && is_shift (accelerator))
1024 mods |= GDK_SHIFT_MASK;
1026 else if (len >= 6 && is_shft (accelerator))
1030 mods |= GDK_SHIFT_MASK;
1032 else if (len >= 6 && is_ctrl (accelerator))
1036 mods |= GDK_CONTROL_MASK;
1038 else if (len >= 6 && is_modx (accelerator))
1040 static const guint mod_vals[] = {
1041 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1042 GDK_MOD4_MASK, GDK_MOD5_MASK
1047 mods |= mod_vals[*accelerator - '1'];
1050 else if (len >= 5 && is_ctl (accelerator))
1054 mods |= GDK_CONTROL_MASK;
1056 else if (len >= 5 && is_alt (accelerator))
1060 mods |= GDK_MOD1_MASK;
1062 else if (len >= 6 && is_meta (accelerator))
1066 mods |= GDK_META_MASK;
1068 else if (len >= 7 && is_hyper (accelerator))
1072 mods |= GDK_HYPER_MASK;
1074 else if (len >= 7 && is_super (accelerator))
1078 mods |= GDK_SUPER_MASK;
1084 last_ch = *accelerator;
1085 while (last_ch && last_ch != '>')
1087 last_ch = *accelerator;
1095 keyval = gdk_keyval_from_name (accelerator);
1101 if (accelerator_key)
1102 *accelerator_key = gdk_keyval_to_lower (keyval);
1103 if (accelerator_mods)
1104 *accelerator_mods = mods;
1108 * gtk_accelerator_name:
1109 * @accelerator_key: accelerator keyval
1110 * @accelerator_mods: accelerator modifier mask
1112 * Converts an accelerator keyval and modifier mask
1113 * into a string parseable by gtk_accelerator_parse().
1114 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1115 * this function returns "<Control>q".
1117 * If you need to display accelerators in the user interface,
1118 * see gtk_accelerator_get_label().
1120 * Returns: a newly-allocated accelerator name
1123 gtk_accelerator_name (guint accelerator_key,
1124 GdkModifierType accelerator_mods)
1126 static const gchar text_release[] = "<Release>";
1127 static const gchar text_shift[] = "<Shift>";
1128 static const gchar text_control[] = "<Control>";
1129 static const gchar text_mod1[] = "<Alt>";
1130 static const gchar text_mod2[] = "<Mod2>";
1131 static const gchar text_mod3[] = "<Mod3>";
1132 static const gchar text_mod4[] = "<Mod4>";
1133 static const gchar text_mod5[] = "<Mod5>";
1134 static const gchar text_meta[] = "<Meta>";
1135 static const gchar text_super[] = "<Super>";
1136 static const gchar text_hyper[] = "<Hyper>";
1141 accelerator_mods &= GDK_MODIFIER_MASK;
1143 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1148 if (accelerator_mods & GDK_RELEASE_MASK)
1149 l += sizeof (text_release) - 1;
1150 if (accelerator_mods & GDK_SHIFT_MASK)
1151 l += sizeof (text_shift) - 1;
1152 if (accelerator_mods & GDK_CONTROL_MASK)
1153 l += sizeof (text_control) - 1;
1154 if (accelerator_mods & GDK_MOD1_MASK)
1155 l += sizeof (text_mod1) - 1;
1156 if (accelerator_mods & GDK_MOD2_MASK)
1157 l += sizeof (text_mod2) - 1;
1158 if (accelerator_mods & GDK_MOD3_MASK)
1159 l += sizeof (text_mod3) - 1;
1160 if (accelerator_mods & GDK_MOD4_MASK)
1161 l += sizeof (text_mod4) - 1;
1162 if (accelerator_mods & GDK_MOD5_MASK)
1163 l += sizeof (text_mod5) - 1;
1164 l += strlen (keyval_name);
1165 if (accelerator_mods & GDK_META_MASK)
1166 l += sizeof (text_meta) - 1;
1167 if (accelerator_mods & GDK_HYPER_MASK)
1168 l += sizeof (text_hyper) - 1;
1169 if (accelerator_mods & GDK_SUPER_MASK)
1170 l += sizeof (text_super) - 1;
1172 accelerator = g_new (gchar, l + 1);
1176 if (accelerator_mods & GDK_RELEASE_MASK)
1178 strcpy (accelerator + l, text_release);
1179 l += sizeof (text_release) - 1;
1181 if (accelerator_mods & GDK_SHIFT_MASK)
1183 strcpy (accelerator + l, text_shift);
1184 l += sizeof (text_shift) - 1;
1186 if (accelerator_mods & GDK_CONTROL_MASK)
1188 strcpy (accelerator + l, text_control);
1189 l += sizeof (text_control) - 1;
1191 if (accelerator_mods & GDK_MOD1_MASK)
1193 strcpy (accelerator + l, text_mod1);
1194 l += sizeof (text_mod1) - 1;
1196 if (accelerator_mods & GDK_MOD2_MASK)
1198 strcpy (accelerator + l, text_mod2);
1199 l += sizeof (text_mod2) - 1;
1201 if (accelerator_mods & GDK_MOD3_MASK)
1203 strcpy (accelerator + l, text_mod3);
1204 l += sizeof (text_mod3) - 1;
1206 if (accelerator_mods & GDK_MOD4_MASK)
1208 strcpy (accelerator + l, text_mod4);
1209 l += sizeof (text_mod4) - 1;
1211 if (accelerator_mods & GDK_MOD5_MASK)
1213 strcpy (accelerator + l, text_mod5);
1214 l += sizeof (text_mod5) - 1;
1216 if (accelerator_mods & GDK_META_MASK)
1218 strcpy (accelerator + l, text_meta);
1219 l += sizeof (text_meta) - 1;
1221 if (accelerator_mods & GDK_HYPER_MASK)
1223 strcpy (accelerator + l, text_hyper);
1224 l += sizeof (text_hyper) - 1;
1226 if (accelerator_mods & GDK_SUPER_MASK)
1228 strcpy (accelerator + l, text_super);
1229 l += sizeof (text_super) - 1;
1231 strcpy (accelerator + l, keyval_name);
1237 * gtk_accelerator_get_label:
1238 * @accelerator_key: accelerator keyval
1239 * @accelerator_mods: accelerator modifier mask
1241 * Converts an accelerator keyval and modifier mask into a string
1242 * which can be used to represent the accelerator to the user.
1244 * Returns: a newly-allocated string representing the accelerator.
1249 gtk_accelerator_get_label (guint accelerator_key,
1250 GdkModifierType accelerator_mods)
1252 GtkAccelLabelClass *klass;
1255 klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
1256 label = _gtk_accel_label_class_get_accelerator_label (klass,
1259 g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
1265 * gtk_accelerator_set_default_mod_mask:
1266 * @default_mod_mask: accelerator modifier mask
1268 * Sets the modifiers that will be considered significant for keyboard
1269 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1270 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK |
1271 * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt,
1272 * Super, Hyper and Meta. Other modifiers will by default be ignored
1273 * by #GtkAccelGroup.
1274 * You must include at least the three modifiers Control, Shift
1275 * and Alt in any value you pass to this function.
1277 * The default mod mask should be changed on application startup,
1278 * before using any accelerator groups.
1281 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1283 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1284 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1288 * gtk_accelerator_get_default_mod_mask:
1289 * @returns: the default accelerator modifier mask
1291 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1294 gtk_accelerator_get_default_mod_mask (void)
1296 return default_accel_mod_mask;
1299 #define __GTK_ACCEL_GROUP_C__
1300 #include "gtkaliasdef.c"