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/.
26 #include "gtkaccelgroup.h"
27 #include "gtkaccelmap.h"
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkmarshalers.h"
30 #include "gtksignal.h"
36 /* --- prototypes --- */
37 static void gtk_accel_group_class_init (GtkAccelGroupClass *class);
38 static void gtk_accel_group_init (GtkAccelGroup *accel_group);
39 static void gtk_accel_group_finalize (GObject *object);
42 /* --- variables --- */
43 static GObjectClass *parent_class = NULL;
44 static guint signal_accel_activate = 0;
45 static guint signal_accel_changed = 0;
46 static guint quark_acceleratable_groups = 0;
47 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
52 /* --- functions --- */
54 * gtk_accel_group_get_type:
55 * @returns: the type ID for accelerator groups.
58 gtk_accel_group_get_type (void)
60 static GType object_type = 0;
64 static const GTypeInfo object_info = {
65 sizeof (GtkAccelGroupClass),
67 (GBaseFinalizeFunc) NULL,
68 (GClassInitFunc) gtk_accel_group_class_init,
69 NULL, /* clas_finalize */
70 NULL, /* class_data */
71 sizeof (GtkAccelGroup),
73 (GInstanceInitFunc) gtk_accel_group_init,
76 object_type = g_type_register_static (G_TYPE_OBJECT,
85 accel_activate_accumulator (GSignalInvocationHint *ihint,
87 const GValue *handler_return,
90 gboolean continue_emission;
93 /* handler returns whether the accelerator was handled */
94 handler_val = g_value_get_boolean (handler_return);
96 /* record that as result for this emission */
97 g_value_set_boolean (return_accu, handler_val);
99 /* don't continue if accelerator was handled */
100 continue_emission = !handler_val;
102 return continue_emission;
106 gtk_accel_group_class_init (GtkAccelGroupClass *class)
108 GObjectClass *object_class = G_OBJECT_CLASS (class);
110 parent_class = g_type_class_peek_parent (class);
112 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
114 object_class->finalize = gtk_accel_group_finalize;
116 class->accel_changed = NULL;
117 signal_accel_activate = g_signal_new ("accel_activate",
118 G_OBJECT_CLASS_TYPE (class),
121 accel_activate_accumulator, NULL,
122 _gtk_marshal_BOOLEAN__OBJECT_UINT_UINT,
123 G_TYPE_BOOLEAN, 3, G_TYPE_OBJECT, G_TYPE_UINT, G_TYPE_UINT);
124 signal_accel_changed = g_signal_new ("accel_changed",
125 G_OBJECT_CLASS_TYPE (class),
126 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
127 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
129 _gtk_marshal_VOID__UINT_UINT_BOXED,
130 G_TYPE_NONE, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_CLOSURE);
134 gtk_accel_group_finalize (GObject *object)
136 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
139 for (i = 0; i < accel_group->n_accels; i++)
141 GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
143 if (entry->accel_path_quark)
145 const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
147 _gtk_accel_map_remove_group (accel_path, accel_group);
151 g_free (accel_group->priv_accels);
153 G_OBJECT_CLASS (parent_class)->finalize (object);
157 gtk_accel_group_init (GtkAccelGroup *accel_group)
159 accel_group->lock_count = 0;
160 accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
161 accel_group->acceleratables = NULL;
162 accel_group->n_accels = 0;
163 accel_group->priv_accels = NULL;
167 * gtk_accel_group_new:
168 * @returns: a new #GtkAccelGroup object
170 * Creates a new #GtkAccelGroup.
173 gtk_accel_group_new (void)
175 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
179 accel_group_weak_ref_detach (GSList *free_list,
180 GObject *stale_object)
184 for (slist = free_list; slist; slist = slist->next)
186 GtkAccelGroup *accel_group;
188 accel_group = slist->data;
189 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
190 g_object_unref (accel_group);
192 g_slist_free (free_list);
196 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
201 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
202 g_return_if_fail (G_IS_OBJECT (object));
203 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) == NULL);
205 g_object_ref (accel_group);
206 accel_group->acceleratables = g_slist_prepend (accel_group->acceleratables, object);
207 slist = g_object_get_qdata (object, quark_acceleratable_groups);
209 g_object_weak_unref (object,
210 (GWeakNotify) accel_group_weak_ref_detach,
212 slist = g_slist_prepend (slist, accel_group);
213 g_object_set_qdata (object, quark_acceleratable_groups, slist);
214 g_object_weak_ref (object,
215 (GWeakNotify) accel_group_weak_ref_detach,
220 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
225 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
226 g_return_if_fail (G_IS_OBJECT (object));
227 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) != NULL);
229 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, object);
230 slist = g_object_get_qdata (object, quark_acceleratable_groups);
231 g_object_weak_unref (object,
232 (GWeakNotify) accel_group_weak_ref_detach,
234 slist = g_slist_remove (slist, accel_group);
235 g_object_set_qdata (object, quark_acceleratable_groups, slist);
237 g_object_weak_ref (object,
238 (GWeakNotify) accel_group_weak_ref_detach,
240 g_object_unref (accel_group);
244 * gtk_accel_groups_from_object:
245 * @object: a #GObject, usually a #GtkWindow
246 * @returns: a list of all accel groups which are attached to @object
248 * Gets a list of all accel groups which are attached to @object.
251 gtk_accel_groups_from_object (GObject *object)
253 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
255 return g_object_get_qdata (object, quark_acceleratable_groups);
259 * gtk_accel_group_find:
260 * @accel_group: a #GtkAccelGroup
261 * @find_func: a function to filter the entries of @accel_group with
262 * @data: data to pass to @find_func
263 * @returns: the key of the first entry passing @find_func. The key is
264 * owned by GTK+ and must not be freed.
266 * Finds the first entry in an accelerator group for which
267 * @find_func returns %TRUE and returns its #GtkAccelKey.
271 gtk_accel_group_find (GtkAccelGroup *accel_group,
272 gboolean (*find_func) (GtkAccelKey *key,
277 GtkAccelKey *key = NULL;
280 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
281 g_return_val_if_fail (find_func != NULL, NULL);
283 g_object_ref (accel_group);
284 for (i = 0; i < accel_group->n_accels; i++)
285 if (find_func (&accel_group->priv_accels[i].key,
286 accel_group->priv_accels[i].closure,
289 key = &accel_group->priv_accels[i].key;
292 g_object_unref (accel_group);
298 * gtk_accel_group_lock:
299 * @accel_group: a #GtkAccelGroup
301 * Locks the given accelerator group.
303 * Locking an acelerator group prevents the accelerators contained
304 * within it to be changed during runtime. Refer to
305 * gtk_accel_map_change_entry() about runtime accelerator changes.
307 * If called more than once, @accel_group remains locked until
308 * gtk_accel_group_unlock() has been called an equivalent number
312 gtk_accel_group_lock (GtkAccelGroup *accel_group)
314 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
316 accel_group->lock_count += 1;
320 * gtk_accel_group_unlock:
321 * @accel_group: a #GtkAccelGroup
323 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
326 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
328 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
329 g_return_if_fail (accel_group->lock_count > 0);
331 accel_group->lock_count -= 1;
335 accel_closure_invalidate (gpointer data,
338 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
340 gtk_accel_group_disconnect (accel_group, closure);
344 bsearch_compare_accels (const void *d1,
347 const GtkAccelGroupEntry *entry1 = d1;
348 const GtkAccelGroupEntry *entry2 = d2;
350 if (entry1->key.accel_key == entry2->key.accel_key)
351 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
353 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
357 quick_accel_add (GtkAccelGroup *accel_group,
359 GdkModifierType accel_mods,
360 GtkAccelFlags accel_flags,
364 guint pos, i = accel_group->n_accels++;
365 GtkAccelGroupEntry key;
368 key.key.accel_key = accel_key;
369 key.key.accel_mods = accel_mods;
370 for (pos = 0; pos < i; pos++)
371 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
374 /* insert at position, ref closure */
375 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
376 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
377 (i - pos) * sizeof (accel_group->priv_accels[0]));
378 accel_group->priv_accels[pos].key.accel_key = accel_key;
379 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
380 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
381 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
382 accel_group->priv_accels[pos].accel_path_quark = path_quark;
383 g_closure_sink (closure);
385 /* handle closure invalidation and reverse lookups */
386 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
388 /* get accel path notification */
390 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
392 /* connect and notify changed */
395 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
396 GQuark accel_quark = g_quark_from_string (accel_name);
401 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
404 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
409 quick_accel_remove (GtkAccelGroup *accel_group,
410 GtkAccelGroupEntry *entry)
412 guint pos = entry - accel_group->priv_accels;
413 GQuark accel_quark = 0;
414 guint accel_key = entry->key.accel_key;
415 GdkModifierType accel_mods = entry->key.accel_mods;
416 GClosure *closure = entry->closure;
418 /* quark for notification */
421 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
423 accel_quark = g_quark_from_string (accel_name);
427 /* clean up closure invalidate notification and disconnect */
428 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
430 g_signal_handlers_disconnect_matched (accel_group,
431 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
432 signal_accel_activate, accel_quark,
433 closure, NULL, NULL);
434 /* clean up accel path notification */
435 if (entry->accel_path_quark)
436 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
438 /* physically remove */
439 accel_group->n_accels -= 1;
440 g_memmove (entry, entry + 1,
441 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
445 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
447 /* remove quick_accel_add() refcount */
448 g_closure_unref (closure);
451 static GtkAccelGroupEntry*
452 quick_accel_find (GtkAccelGroup *accel_group,
454 GdkModifierType accel_mods,
457 GtkAccelGroupEntry *entry;
458 GtkAccelGroupEntry key;
460 if (!accel_group->n_accels)
463 key.key.accel_key = accel_key;
464 key.key.accel_mods = accel_mods;
465 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
466 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
471 /* step back to the first member */
472 for (; entry > accel_group->priv_accels; entry--)
473 if (entry[-1].key.accel_key != accel_key ||
474 entry[-1].key.accel_mods != accel_mods)
476 /* count equal members */
477 for (*count_p = 0; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
478 if (entry[*count_p].key.accel_key != accel_key ||
479 entry[*count_p].key.accel_mods != accel_mods)
485 * gtk_accel_group_connect:
486 * @accel_group: the accelerator group to install an accelerator in
487 * @accel_key: key value of the accelerator
488 * @accel_mods: modifier combination of the accelerator
489 * @accel_flags: a flag mask to configure this accelerator
490 * @closure: closure to be executed upon accelerator activation
492 * Installs an accelerator in this group. When @accel_group is being activated
493 * in response to a call to gtk_accel_groups_activate(), @closure will be
494 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
495 * match those of this connection.
497 * The signature used for the @closure is that of #GtkAccelGroupActivate.
499 * Note that, due to implementation details, a single closure can only be
500 * connected to one accelerator group.
503 gtk_accel_group_connect (GtkAccelGroup *accel_group,
505 GdkModifierType accel_mods,
506 GtkAccelFlags accel_flags,
509 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
510 g_return_if_fail (closure != NULL);
511 g_return_if_fail (accel_key > 0);
512 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
514 g_object_ref (accel_group);
515 if (!closure->is_invalid)
516 quick_accel_add (accel_group,
517 gdk_keyval_to_lower (accel_key),
518 accel_mods, accel_flags, closure, 0);
519 g_object_unref (accel_group);
523 * gtk_accel_group_connect_by_path:
524 * @accel_group: the accelerator group to install an accelerator in
525 * @accel_path: path used for determining key and modifiers.
526 * @closure: closure to be executed upon accelerator activation
528 * Installs an accelerator in this group, using an accelerator path to look
529 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
530 * When @accel_group is being activated in response to a call to
531 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
532 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
535 * The signature used for the @closure is that of #GtkAccelGroupActivate.
538 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
539 const gchar *accel_path,
543 GdkModifierType accel_mods = 0;
546 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
547 g_return_if_fail (closure != NULL);
548 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
550 if (closure->is_invalid)
553 g_object_ref (accel_group);
555 if (gtk_accel_map_lookup_entry (accel_path, &key))
557 accel_key = gdk_keyval_to_lower (key.accel_key),
558 accel_mods = key.accel_mods;
561 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
562 g_quark_from_string (accel_path));
564 g_object_unref (accel_group);
568 * gtk_accel_group_disconnect:
569 * @accel_group: the accelerator group to remove an accelerator from
570 * @closure: the closure to remove from this accelerator group
571 * @returns: %TRUE if the closure was found and got disconnected
573 * Removes an accelerator previously installed through
574 * gtk_accel_group_connect().
577 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
582 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
584 for (i = 0; i < accel_group->n_accels; i++)
585 if (accel_group->priv_accels[i].closure == closure)
587 g_object_ref (accel_group);
588 quick_accel_remove (accel_group, accel_group->priv_accels + i);
589 g_object_unref (accel_group);
596 * gtk_accel_group_disconnect_key:
597 * @accel_group: the accelerator group to install an accelerator in
598 * @accel_key: key value of the accelerator
599 * @accel_mods: modifier combination of the accelerator
600 * @returns: %TRUE if there was an accelerator which could be
601 * removed, %FALSE otherwise
603 * Removes an accelerator previously installed through
604 * gtk_accel_group_connect().
607 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
609 GdkModifierType accel_mods)
611 GtkAccelGroupEntry *entries;
612 GSList *slist, *clist = NULL;
613 gboolean removed_one = FALSE;
616 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
618 g_object_ref (accel_group);
620 accel_key = gdk_keyval_to_lower (accel_key);
621 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
624 GClosure *closure = g_closure_ref (entries[n].closure);
626 clist = g_slist_prepend (clist, closure);
629 for (slist = clist; slist; slist = slist->next)
631 GClosure *closure = slist->data;
633 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
634 g_closure_unref (closure);
636 g_slist_free (clist);
638 g_object_unref (accel_group);
644 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
645 GQuark accel_path_quark)
647 GSList *slist, *clist = NULL;
650 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
652 g_object_ref (accel_group);
654 for (i = 0; i < accel_group->n_accels; i++)
655 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
657 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
659 clist = g_slist_prepend (clist, closure);
662 for (slist = clist; slist; slist = slist->next)
664 GClosure *closure = slist->data;
666 gtk_accel_group_disconnect (accel_group, closure);
667 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
668 g_closure_unref (closure);
670 g_slist_free (clist);
672 g_object_unref (accel_group);
676 * gtk_accel_group_query:
677 * @accel_group: the accelerator group to query
678 * @accel_key: key value of the accelerator
679 * @accel_mods: modifier combination of the accelerator
680 * @n_entries: location to return the number of entries found, or %NULL
681 * @returns: an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
683 * Queries an accelerator group for all entries matching @accel_key and
687 gtk_accel_group_query (GtkAccelGroup *accel_group,
689 GdkModifierType accel_mods,
692 GtkAccelGroupEntry *entries;
695 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
697 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
700 *n_entries = entries ? n : 0;
706 * gtk_accel_group_from_accel_closure:
707 * @closure: a #GClosure
708 * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
710 * Finds the #GtkAccelGroup to which @closure is connected;
711 * see gtk_accel_group_connect().
714 gtk_accel_group_from_accel_closure (GClosure *closure)
718 g_return_val_if_fail (closure != NULL, NULL);
720 /* a few remarks on what we do here. in general, we need a way to reverse lookup
721 * accel_groups from closures that are being used in accel groups. this could
722 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
723 * use the invalidation notifier on the closure itself (which we need to install
724 * anyway), that contains the accel group as data which, besides needing to peek
725 * a bit at closure internals, works just as good.
727 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
728 if (closure->notifiers[i].notify == accel_closure_invalidate)
729 return closure->notifiers[i].data;
735 _gtk_accel_group_activate (GtkAccelGroup *accel_group,
737 GObject *acceleratable,
739 GdkModifierType accel_mods)
741 gboolean was_handled;
743 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
746 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
747 acceleratable, accel_key, accel_mods, &was_handled);
753 * gtk_accel_groups_activate:
754 * @object: the #GObject, usually a #GtkWindow, on which
755 * to activate the accelerator.
756 * @accel_key: accelerator keyval from a key event
757 * @accel_mods: keyboard state mask from a key event
758 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
760 * Finds the first accelerator in any #GtkAccelGroup attached
761 * to @object that matches @accel_key and @accel_mods, and
762 * activates that accelerator.
763 * If an accelerator was activated and handled this keypress, %TRUE
767 gtk_accel_groups_activate (GObject *object,
769 GdkModifierType accel_mods)
771 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
773 if (gtk_accelerator_valid (accel_key, accel_mods))
779 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
780 accel_quark = g_quark_from_string (accel_name);
783 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
784 if (_gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
792 * gtk_accelerator_valid:
793 * @keyval: a GDK keyval
794 * @modifiers: modifier mask
795 * @returns: %TRUE if the accelerator is valid
797 * Determines whether a given keyval and modifier mask constitute
798 * a valid keyboard accelerator. For example, the #GDK_a keyval
799 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
800 * But by default (see gtk_accelerator_set_default_mod_mask()) you
801 * cannot use the NumLock key as an accelerator modifier.
804 gtk_accelerator_valid (guint keyval,
805 GdkModifierType modifiers)
807 static const guint invalid_accelerator_vals[] = {
808 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
809 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
810 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
811 GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
812 GDK_ISO_First_Group, GDK_ISO_Last_Group,
813 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
814 GDK_Scroll_Lock, GDK_Sys_Req,
815 GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
816 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
817 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
818 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
819 GDK_Terminate_Server, GDK_AudibleBell_Enable,
824 modifiers &= GDK_MODIFIER_MASK;
827 return keyval >= 0x20;
829 ac_val = invalid_accelerator_vals;
832 if (keyval == *ac_val++)
839 static inline gboolean
840 is_alt (const gchar *string)
842 return ((string[0] == '<') &&
843 (string[1] == 'a' || string[1] == 'A') &&
844 (string[2] == 'l' || string[2] == 'L') &&
845 (string[3] == 't' || string[3] == 'T') &&
849 static inline gboolean
850 is_ctl (const gchar *string)
852 return ((string[0] == '<') &&
853 (string[1] == 'c' || string[1] == 'C') &&
854 (string[2] == 't' || string[2] == 'T') &&
855 (string[3] == 'l' || string[3] == 'L') &&
859 static inline gboolean
860 is_modx (const gchar *string)
862 return ((string[0] == '<') &&
863 (string[1] == 'm' || string[1] == 'M') &&
864 (string[2] == 'o' || string[2] == 'O') &&
865 (string[3] == 'd' || string[3] == 'D') &&
866 (string[4] >= '1' && string[4] <= '5') &&
870 static inline gboolean
871 is_ctrl (const gchar *string)
873 return ((string[0] == '<') &&
874 (string[1] == 'c' || string[1] == 'C') &&
875 (string[2] == 't' || string[2] == 'T') &&
876 (string[3] == 'r' || string[3] == 'R') &&
877 (string[4] == 'l' || string[4] == 'L') &&
881 static inline gboolean
882 is_shft (const gchar *string)
884 return ((string[0] == '<') &&
885 (string[1] == 's' || string[1] == 'S') &&
886 (string[2] == 'h' || string[2] == 'H') &&
887 (string[3] == 'f' || string[3] == 'F') &&
888 (string[4] == 't' || string[4] == 'T') &&
892 static inline gboolean
893 is_shift (const gchar *string)
895 return ((string[0] == '<') &&
896 (string[1] == 's' || string[1] == 'S') &&
897 (string[2] == 'h' || string[2] == 'H') &&
898 (string[3] == 'i' || string[3] == 'I') &&
899 (string[4] == 'f' || string[4] == 'F') &&
900 (string[5] == 't' || string[5] == 'T') &&
904 static inline gboolean
905 is_control (const gchar *string)
907 return ((string[0] == '<') &&
908 (string[1] == 'c' || string[1] == 'C') &&
909 (string[2] == 'o' || string[2] == 'O') &&
910 (string[3] == 'n' || string[3] == 'N') &&
911 (string[4] == 't' || string[4] == 'T') &&
912 (string[5] == 'r' || string[5] == 'R') &&
913 (string[6] == 'o' || string[6] == 'O') &&
914 (string[7] == 'l' || string[7] == 'L') &&
918 static inline gboolean
919 is_release (const gchar *string)
921 return ((string[0] == '<') &&
922 (string[1] == 'r' || string[1] == 'R') &&
923 (string[2] == 'e' || string[2] == 'E') &&
924 (string[3] == 'l' || string[3] == 'L') &&
925 (string[4] == 'e' || string[4] == 'E') &&
926 (string[5] == 'a' || string[5] == 'A') &&
927 (string[6] == 's' || string[6] == 'S') &&
928 (string[7] == 'e' || string[7] == 'E') &&
933 * gtk_accelerator_parse:
934 * @accelerator: string representing an accelerator
935 * @accelerator_key: return location for accelerator keyval
936 * @accelerator_mods: return location for accelerator modifier mask
938 * Parses a string representing an accelerator. The
939 * format looks like "<Control>a" or "<Shift><Alt>F1" or
940 * "<Release>z" (the last one is for key release).
941 * The parser is fairly liberal and allows lower or upper case,
942 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
944 * If the parse fails, @accelerator_key and @accelerator_mods will
945 * be set to 0 (zero).
948 gtk_accelerator_parse (const gchar *accelerator,
949 guint *accelerator_key,
950 GdkModifierType *accelerator_mods)
953 GdkModifierType mods;
957 *accelerator_key = 0;
958 if (accelerator_mods)
959 *accelerator_mods = 0;
960 g_return_if_fail (accelerator != NULL);
964 len = strlen (accelerator);
967 if (*accelerator == '<')
969 if (len >= 9 && is_release (accelerator))
973 mods |= GDK_RELEASE_MASK;
975 else if (len >= 9 && is_control (accelerator))
979 mods |= GDK_CONTROL_MASK;
981 else if (len >= 7 && is_shift (accelerator))
985 mods |= GDK_SHIFT_MASK;
987 else if (len >= 6 && is_shft (accelerator))
991 mods |= GDK_SHIFT_MASK;
993 else if (len >= 6 && is_ctrl (accelerator))
997 mods |= GDK_CONTROL_MASK;
999 else if (len >= 6 && is_modx (accelerator))
1001 static const guint mod_vals[] = {
1002 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1003 GDK_MOD4_MASK, GDK_MOD5_MASK
1008 mods |= mod_vals[*accelerator - '1'];
1011 else if (len >= 5 && is_ctl (accelerator))
1015 mods |= GDK_CONTROL_MASK;
1017 else if (len >= 5 && is_alt (accelerator))
1021 mods |= GDK_MOD1_MASK;
1027 last_ch = *accelerator;
1028 while (last_ch && last_ch != '>')
1030 last_ch = *accelerator;
1038 keyval = gdk_keyval_from_name (accelerator);
1044 if (accelerator_key)
1045 *accelerator_key = gdk_keyval_to_lower (keyval);
1046 if (accelerator_mods)
1047 *accelerator_mods = mods;
1051 * gtk_accelerator_name:
1052 * @accelerator_key: accelerator keyval
1053 * @accelerator_mods: accelerator modifier mask
1054 * @returns: a newly-allocated accelerator name
1056 * Converts an accelerator keyval and modifier mask
1057 * into a string parseable by gtk_accelerator_parse().
1058 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1059 * this function returns "<Control>q".
1061 * The caller of this function must free the returned string.
1064 gtk_accelerator_name (guint accelerator_key,
1065 GdkModifierType accelerator_mods)
1067 static const gchar text_release[] = "<Release>";
1068 static const gchar text_shift[] = "<Shift>";
1069 static const gchar text_control[] = "<Control>";
1070 static const gchar text_mod1[] = "<Alt>";
1071 static const gchar text_mod2[] = "<Mod2>";
1072 static const gchar text_mod3[] = "<Mod3>";
1073 static const gchar text_mod4[] = "<Mod4>";
1074 static const gchar text_mod5[] = "<Mod5>";
1079 accelerator_mods &= GDK_MODIFIER_MASK;
1081 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1086 if (accelerator_mods & GDK_RELEASE_MASK)
1087 l += sizeof (text_release) - 1;
1088 if (accelerator_mods & GDK_SHIFT_MASK)
1089 l += sizeof (text_shift) - 1;
1090 if (accelerator_mods & GDK_CONTROL_MASK)
1091 l += sizeof (text_control) - 1;
1092 if (accelerator_mods & GDK_MOD1_MASK)
1093 l += sizeof (text_mod1) - 1;
1094 if (accelerator_mods & GDK_MOD2_MASK)
1095 l += sizeof (text_mod2) - 1;
1096 if (accelerator_mods & GDK_MOD3_MASK)
1097 l += sizeof (text_mod3) - 1;
1098 if (accelerator_mods & GDK_MOD4_MASK)
1099 l += sizeof (text_mod4) - 1;
1100 if (accelerator_mods & GDK_MOD5_MASK)
1101 l += sizeof (text_mod5) - 1;
1102 l += strlen (keyval_name);
1104 accelerator = g_new (gchar, l + 1);
1108 if (accelerator_mods & GDK_RELEASE_MASK)
1110 strcpy (accelerator + l, text_release);
1111 l += sizeof (text_release) - 1;
1113 if (accelerator_mods & GDK_SHIFT_MASK)
1115 strcpy (accelerator + l, text_shift);
1116 l += sizeof (text_shift) - 1;
1118 if (accelerator_mods & GDK_CONTROL_MASK)
1120 strcpy (accelerator + l, text_control);
1121 l += sizeof (text_control) - 1;
1123 if (accelerator_mods & GDK_MOD1_MASK)
1125 strcpy (accelerator + l, text_mod1);
1126 l += sizeof (text_mod1) - 1;
1128 if (accelerator_mods & GDK_MOD2_MASK)
1130 strcpy (accelerator + l, text_mod2);
1131 l += sizeof (text_mod2) - 1;
1133 if (accelerator_mods & GDK_MOD3_MASK)
1135 strcpy (accelerator + l, text_mod3);
1136 l += sizeof (text_mod3) - 1;
1138 if (accelerator_mods & GDK_MOD4_MASK)
1140 strcpy (accelerator + l, text_mod4);
1141 l += sizeof (text_mod4) - 1;
1143 if (accelerator_mods & GDK_MOD5_MASK)
1145 strcpy (accelerator + l, text_mod5);
1146 l += sizeof (text_mod5) - 1;
1148 strcpy (accelerator + l, keyval_name);
1154 * gtk_accelerator_set_default_mod_mask:
1155 * @default_mod_mask: accelerator modifier mask
1157 * Sets the modifiers that will be considered significant for keyboard
1158 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1159 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1160 * Other modifiers will by default be ignored by #GtkAccelGroup.
1162 * The default mod mask should be changed on application startup,
1163 * before using any accelerator groups.
1166 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1168 default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1172 * gtk_accelerator_get_default_mod_mask:
1173 * @returns: the default accelerator modifier mask
1175 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1178 gtk_accelerator_get_default_mod_mask (void)
1180 return default_accel_mod_mask;