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"
37 /* --- prototypes --- */
38 static void gtk_accel_group_class_init (GtkAccelGroupClass *class);
39 static void gtk_accel_group_init (GtkAccelGroup *accel_group);
40 static void gtk_accel_group_finalize (GObject *object);
43 /* --- variables --- */
44 static GObjectClass *parent_class = NULL;
45 static guint signal_accel_activate = 0;
46 static guint signal_accel_changed = 0;
47 static guint quark_acceleratable_groups = 0;
48 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
53 /* --- functions --- */
55 * gtk_accel_group_get_type:
56 * @returns: the type ID for accelerator groups.
59 gtk_accel_group_get_type (void)
61 static GType object_type = 0;
65 static const GTypeInfo object_info = {
66 sizeof (GtkAccelGroupClass),
68 (GBaseFinalizeFunc) NULL,
69 (GClassInitFunc) gtk_accel_group_class_init,
70 NULL, /* clas_finalize */
71 NULL, /* class_data */
72 sizeof (GtkAccelGroup),
74 (GInstanceInitFunc) gtk_accel_group_init,
77 object_type = g_type_register_static (G_TYPE_OBJECT,
86 accel_activate_accumulator (GSignalInvocationHint *ihint,
88 const GValue *handler_return,
91 gboolean continue_emission;
94 /* handler returns whether the accelerator was handled */
95 handler_val = g_value_get_boolean (handler_return);
97 /* record that as result for this emission */
98 g_value_set_boolean (return_accu, handler_val);
100 /* don't continue if accelerator was handled */
101 continue_emission = !handler_val;
103 return continue_emission;
107 gtk_accel_group_class_init (GtkAccelGroupClass *class)
109 GObjectClass *object_class = G_OBJECT_CLASS (class);
111 parent_class = g_type_class_peek_parent (class);
113 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
115 object_class->finalize = gtk_accel_group_finalize;
117 class->accel_changed = NULL;
118 signal_accel_activate = g_signal_new ("accel_activate",
119 G_OBJECT_CLASS_TYPE (class),
122 accel_activate_accumulator, NULL,
123 _gtk_marshal_BOOLEAN__OBJECT_UINT_UINT,
124 G_TYPE_BOOLEAN, 3, G_TYPE_OBJECT, G_TYPE_UINT, G_TYPE_UINT);
125 signal_accel_changed = g_signal_new ("accel_changed",
126 G_OBJECT_CLASS_TYPE (class),
127 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
128 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
130 _gtk_marshal_VOID__UINT_UINT_BOXED,
131 G_TYPE_NONE, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_CLOSURE);
135 gtk_accel_group_finalize (GObject *object)
137 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
140 for (i = 0; i < accel_group->n_accels; i++)
142 GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
144 if (entry->accel_path_quark)
146 const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
148 _gtk_accel_map_remove_group (accel_path, accel_group);
152 g_free (accel_group->priv_accels);
154 G_OBJECT_CLASS (parent_class)->finalize (object);
158 gtk_accel_group_init (GtkAccelGroup *accel_group)
160 accel_group->lock_count = 0;
161 accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
162 accel_group->acceleratables = NULL;
163 accel_group->n_accels = 0;
164 accel_group->priv_accels = NULL;
168 * gtk_accel_group_new:
169 * @returns: a new #GtkAccelGroup object
171 * Creates a new #GtkAccelGroup.
174 gtk_accel_group_new (void)
176 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
180 accel_group_weak_ref_detach (GSList *free_list,
181 GObject *stale_object)
185 for (slist = free_list; slist; slist = slist->next)
187 GtkAccelGroup *accel_group;
189 accel_group = slist->data;
190 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
191 g_object_unref (accel_group);
193 g_slist_free (free_list);
197 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
202 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
203 g_return_if_fail (G_IS_OBJECT (object));
204 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) == NULL);
206 g_object_ref (accel_group);
207 accel_group->acceleratables = g_slist_prepend (accel_group->acceleratables, object);
208 slist = g_object_get_qdata (object, quark_acceleratable_groups);
210 g_object_weak_unref (object,
211 (GWeakNotify) accel_group_weak_ref_detach,
213 slist = g_slist_prepend (slist, accel_group);
214 g_object_set_qdata (object, quark_acceleratable_groups, slist);
215 g_object_weak_ref (object,
216 (GWeakNotify) accel_group_weak_ref_detach,
221 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
226 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
227 g_return_if_fail (G_IS_OBJECT (object));
228 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) != NULL);
230 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, object);
231 slist = g_object_get_qdata (object, quark_acceleratable_groups);
232 g_object_weak_unref (object,
233 (GWeakNotify) accel_group_weak_ref_detach,
235 slist = g_slist_remove (slist, accel_group);
236 g_object_set_qdata (object, quark_acceleratable_groups, slist);
238 g_object_weak_ref (object,
239 (GWeakNotify) accel_group_weak_ref_detach,
241 g_object_unref (accel_group);
245 * gtk_accel_groups_from_object:
246 * @object: a #GObject, usually a #GtkWindow
247 * @returns: a list of all accel groups which are attached to @object
249 * Gets a list of all accel groups which are attached to @object.
252 gtk_accel_groups_from_object (GObject *object)
254 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
256 return g_object_get_qdata (object, quark_acceleratable_groups);
260 * gtk_accel_group_find:
261 * @accel_group: a #GtkAccelGroup
262 * @find_func: a function to filter the entries of @accel_group with
263 * @data: data to pass to @find_func
264 * @returns: the key of the first entry passing @find_func. The key is
265 * owned by GTK+ and must not be freed.
267 * Finds the first entry in an accelerator group for which
268 * @find_func returns %TRUE and returns its #GtkAccelKey.
272 gtk_accel_group_find (GtkAccelGroup *accel_group,
273 gboolean (*find_func) (GtkAccelKey *key,
278 GtkAccelKey *key = NULL;
281 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
282 g_return_val_if_fail (find_func != NULL, NULL);
284 g_object_ref (accel_group);
285 for (i = 0; i < accel_group->n_accels; i++)
286 if (find_func (&accel_group->priv_accels[i].key,
287 accel_group->priv_accels[i].closure,
290 key = &accel_group->priv_accels[i].key;
293 g_object_unref (accel_group);
299 * gtk_accel_group_lock:
300 * @accel_group: a #GtkAccelGroup
302 * Locks the given accelerator group.
304 * Locking an acelerator group prevents the accelerators contained
305 * within it to be changed during runtime. Refer to
306 * gtk_accel_map_change_entry() about runtime accelerator changes.
308 * If called more than once, @accel_group remains locked until
309 * gtk_accel_group_unlock() has been called an equivalent number
313 gtk_accel_group_lock (GtkAccelGroup *accel_group)
315 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
317 accel_group->lock_count += 1;
321 * gtk_accel_group_unlock:
322 * @accel_group: a #GtkAccelGroup
324 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
327 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
329 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
330 g_return_if_fail (accel_group->lock_count > 0);
332 accel_group->lock_count -= 1;
336 accel_closure_invalidate (gpointer data,
339 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
341 gtk_accel_group_disconnect (accel_group, closure);
345 bsearch_compare_accels (const void *d1,
348 const GtkAccelGroupEntry *entry1 = d1;
349 const GtkAccelGroupEntry *entry2 = d2;
351 if (entry1->key.accel_key == entry2->key.accel_key)
352 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
354 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
358 quick_accel_add (GtkAccelGroup *accel_group,
360 GdkModifierType accel_mods,
361 GtkAccelFlags accel_flags,
365 guint pos, i = accel_group->n_accels++;
366 GtkAccelGroupEntry key;
369 key.key.accel_key = accel_key;
370 key.key.accel_mods = accel_mods;
371 for (pos = 0; pos < i; pos++)
372 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
375 /* insert at position, ref closure */
376 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
377 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
378 (i - pos) * sizeof (accel_group->priv_accels[0]));
379 accel_group->priv_accels[pos].key.accel_key = accel_key;
380 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
381 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
382 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
383 accel_group->priv_accels[pos].accel_path_quark = path_quark;
384 g_closure_sink (closure);
386 /* handle closure invalidation and reverse lookups */
387 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
389 /* get accel path notification */
391 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
393 /* connect and notify changed */
396 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
397 GQuark accel_quark = g_quark_from_string (accel_name);
402 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
405 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
410 quick_accel_remove (GtkAccelGroup *accel_group,
411 GtkAccelGroupEntry *entry)
413 guint pos = entry - accel_group->priv_accels;
414 GQuark accel_quark = 0;
415 guint accel_key = entry->key.accel_key;
416 GdkModifierType accel_mods = entry->key.accel_mods;
417 GClosure *closure = entry->closure;
419 /* quark for notification */
422 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
424 accel_quark = g_quark_from_string (accel_name);
428 /* clean up closure invalidate notification and disconnect */
429 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
431 g_signal_handlers_disconnect_matched (accel_group,
432 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
433 signal_accel_activate, accel_quark,
434 closure, NULL, NULL);
435 /* clean up accel path notification */
436 if (entry->accel_path_quark)
437 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
439 /* physically remove */
440 accel_group->n_accels -= 1;
441 g_memmove (entry, entry + 1,
442 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
446 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
448 /* remove quick_accel_add() refcount */
449 g_closure_unref (closure);
452 static GtkAccelGroupEntry*
453 quick_accel_find (GtkAccelGroup *accel_group,
455 GdkModifierType accel_mods,
458 GtkAccelGroupEntry *entry;
459 GtkAccelGroupEntry key;
461 if (!accel_group->n_accels)
464 key.key.accel_key = accel_key;
465 key.key.accel_mods = accel_mods;
466 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
467 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
472 /* step back to the first member */
473 for (; entry > accel_group->priv_accels; entry--)
474 if (entry[-1].key.accel_key != accel_key ||
475 entry[-1].key.accel_mods != accel_mods)
477 /* count equal members */
478 for (*count_p = 0; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
479 if (entry[*count_p].key.accel_key != accel_key ||
480 entry[*count_p].key.accel_mods != accel_mods)
486 * gtk_accel_group_connect:
487 * @accel_group: the accelerator group to install an accelerator in
488 * @accel_key: key value of the accelerator
489 * @accel_mods: modifier combination of the accelerator
490 * @accel_flags: a flag mask to configure this accelerator
491 * @closure: closure to be executed upon accelerator activation
493 * Installs an accelerator in this group. When @accel_group is being activated
494 * in response to a call to gtk_accel_groups_activate(), @closure will be
495 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
496 * match those of this connection.
498 * The signature used for the @closure is that of #GtkAccelGroupActivate.
500 * Note that, due to implementation details, a single closure can only be
501 * connected to one accelerator group.
504 gtk_accel_group_connect (GtkAccelGroup *accel_group,
506 GdkModifierType accel_mods,
507 GtkAccelFlags accel_flags,
510 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
511 g_return_if_fail (closure != NULL);
512 g_return_if_fail (accel_key > 0);
513 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
515 g_object_ref (accel_group);
516 if (!closure->is_invalid)
517 quick_accel_add (accel_group, accel_key, accel_mods, accel_flags, closure, 0);
518 g_object_unref (accel_group);
522 * gtk_accel_group_connect_by_path:
523 * @accel_group: the accelerator group to install an accelerator in
524 * @accel_path: path used for determining key and modifiers.
525 * @closure: closure to be executed upon accelerator activation
527 * Installs an accelerator in this group, using an accelerator path to look
528 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
529 * When @accel_group is being activated in response to a call to
530 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
531 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
534 * The signature used for the @closure is that of #GtkAccelGroupActivate.
537 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
538 const gchar *accel_path,
542 GdkModifierType accel_mods = 0;
545 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
546 g_return_if_fail (closure != NULL);
547 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
549 if (closure->is_invalid)
552 g_object_ref (accel_group);
554 if (gtk_accel_map_lookup_entry (accel_path, &key))
556 accel_key = key.accel_key;
557 accel_mods = key.accel_mods;
560 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
561 g_quark_from_string (accel_path));
563 g_object_unref (accel_group);
567 * gtk_accel_group_disconnect:
568 * @accel_group: the accelerator group to remove an accelerator from
569 * @closure: the closure to remove from this accelerator group
570 * @returns: %TRUE if the closure was found and got disconnected
572 * Removes an accelerator previously installed through
573 * gtk_accel_group_connect().
576 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
581 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
583 for (i = 0; i < accel_group->n_accels; i++)
584 if (accel_group->priv_accels[i].closure == closure)
586 g_object_ref (accel_group);
587 quick_accel_remove (accel_group, accel_group->priv_accels + i);
588 g_object_unref (accel_group);
595 * gtk_accel_group_disconnect_key:
596 * @accel_group: the accelerator group to install an accelerator in
597 * @accel_key: key value of the accelerator
598 * @accel_mods: modifier combination of the accelerator
599 * @returns: %TRUE if there was an accelerator which could be
600 * removed, %FALSE otherwise
602 * Removes an accelerator previously installed through
603 * gtk_accel_group_connect().
606 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
608 GdkModifierType accel_mods)
610 GtkAccelGroupEntry *entries;
611 GSList *slist, *clist = NULL;
612 gboolean removed_one = FALSE;
615 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
617 g_object_ref (accel_group);
619 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
622 GClosure *closure = g_closure_ref (entries[n].closure);
624 clist = g_slist_prepend (clist, closure);
627 for (slist = clist; slist; slist = slist->next)
629 GClosure *closure = slist->data;
631 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
632 g_closure_unref (closure);
634 g_slist_free (clist);
636 g_object_unref (accel_group);
642 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
643 GQuark accel_path_quark)
645 GSList *slist, *clist = NULL;
648 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
650 g_object_ref (accel_group);
652 for (i = 0; i < accel_group->n_accels; i++)
653 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
655 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
657 clist = g_slist_prepend (clist, closure);
660 for (slist = clist; slist; slist = slist->next)
662 GClosure *closure = slist->data;
664 gtk_accel_group_disconnect (accel_group, closure);
665 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
666 g_closure_unref (closure);
668 g_slist_free (clist);
670 g_object_unref (accel_group);
674 * gtk_accel_group_query:
675 * @accel_group: the accelerator group to query
676 * @accel_key: key value of the accelerator
677 * @accel_mods: modifier combination of the accelerator
678 * @n_entries: location to return the number of entries found, or %NULL
679 * @returns: an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
681 * Queries an accelerator group for all entries matching @accel_key and
685 gtk_accel_group_query (GtkAccelGroup *accel_group,
687 GdkModifierType accel_mods,
690 GtkAccelGroupEntry *entries;
693 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
695 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
698 *n_entries = entries ? n : 0;
704 * gtk_accel_group_from_accel_closure:
705 * @closure: a #GClosure
706 * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
708 * Finds the #GtkAccelGroup to which @closure is connected;
709 * see gtk_accel_group_connect().
712 gtk_accel_group_from_accel_closure (GClosure *closure)
716 g_return_val_if_fail (closure != NULL, NULL);
718 /* a few remarks on wat we do here. in general, we need a way to reverse lookup
719 * accel_groups from closures that are being used in accel groups. this could
720 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
721 * use the invalidation notifier on the closure itself (which we need to install
722 * anyway), that contains the accel group as data which, besides needing to peek
723 * a bit at closure internals, works just as good.
725 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
726 if (closure->notifiers[i].notify == accel_closure_invalidate)
727 return closure->notifiers[i].data;
733 _gtk_accel_group_activate (GtkAccelGroup *accel_group,
735 GObject *acceleratable,
737 GdkModifierType accel_mods)
739 gboolean was_handled;
741 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
744 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
745 acceleratable, accel_key, accel_mods, &was_handled);
751 * gtk_accel_groups_activate:
752 * @object: the #GObject, usually a #GtkWindow, on which
753 * to activate the accelerator.
754 * @accel_key: accelerator keyval from a key event
755 * @accel_mods: keyboard state mask from a key event
756 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
758 * Finds the first accelerator in any #GtkAccelGroup attached
759 * to @object that matches @accel_key and @accel_mods, and
760 * activates that accelerator.
761 * If an accelerator was activated and handled this keypress, %TRUE
765 gtk_accel_groups_activate (GObject *object,
767 GdkModifierType accel_mods)
769 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
771 if (gtk_accelerator_valid (accel_key, accel_mods))
777 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
778 accel_quark = g_quark_from_string (accel_name);
781 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
782 if (_gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
790 * gtk_accelerator_valid:
791 * @keyval: a GDK keyval
792 * @modifiers: modifier mask
793 * @returns: %TRUE if the accelerator is valid
795 * Determines whether a given keyval and modifier mask constitute
796 * a valid keyboard accelerator. For example, the #GDK_a keyval
797 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
798 * But by default (see gtk_accelerator_set_default_mod_mask()) you
799 * cannot use the NumLock key as an accelerator modifier.
802 gtk_accelerator_valid (guint keyval,
803 GdkModifierType modifiers)
805 static const guint invalid_accelerator_vals[] = {
806 GDK_BackSpace, GDK_Delete, GDK_KP_Delete,
807 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
808 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
809 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
810 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
811 GDK_Scroll_Lock, GDK_Sys_Req,
812 GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
813 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
814 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
815 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
816 GDK_Terminate_Server, GDK_AudibleBell_Enable,
821 modifiers &= GDK_MODIFIER_MASK;
824 return keyval >= 0x20;
826 ac_val = invalid_accelerator_vals;
829 if (keyval == *ac_val++)
836 static inline gboolean
837 is_alt (const gchar *string)
839 return ((string[0] == '<') &&
840 (string[1] == 'a' || string[1] == 'A') &&
841 (string[2] == 'l' || string[2] == 'L') &&
842 (string[3] == 't' || string[3] == 'T') &&
846 static inline gboolean
847 is_ctl (const gchar *string)
849 return ((string[0] == '<') &&
850 (string[1] == 'c' || string[1] == 'C') &&
851 (string[2] == 't' || string[2] == 'T') &&
852 (string[3] == 'l' || string[3] == 'L') &&
856 static inline gboolean
857 is_modx (const gchar *string)
859 return ((string[0] == '<') &&
860 (string[1] == 'm' || string[1] == 'M') &&
861 (string[2] == 'o' || string[2] == 'O') &&
862 (string[3] == 'd' || string[3] == 'D') &&
863 (string[4] >= '1' && string[4] <= '5') &&
867 static inline gboolean
868 is_ctrl (const gchar *string)
870 return ((string[0] == '<') &&
871 (string[1] == 'c' || string[1] == 'C') &&
872 (string[2] == 't' || string[2] == 'T') &&
873 (string[3] == 'r' || string[3] == 'R') &&
874 (string[4] == 'l' || string[4] == 'L') &&
878 static inline gboolean
879 is_shft (const gchar *string)
881 return ((string[0] == '<') &&
882 (string[1] == 's' || string[1] == 'S') &&
883 (string[2] == 'h' || string[2] == 'H') &&
884 (string[3] == 'f' || string[3] == 'F') &&
885 (string[4] == 't' || string[4] == 'T') &&
889 static inline gboolean
890 is_shift (const gchar *string)
892 return ((string[0] == '<') &&
893 (string[1] == 's' || string[1] == 'S') &&
894 (string[2] == 'h' || string[2] == 'H') &&
895 (string[3] == 'i' || string[3] == 'I') &&
896 (string[4] == 'f' || string[4] == 'F') &&
897 (string[5] == 't' || string[5] == 'T') &&
901 static inline gboolean
902 is_control (const gchar *string)
904 return ((string[0] == '<') &&
905 (string[1] == 'c' || string[1] == 'C') &&
906 (string[2] == 'o' || string[2] == 'O') &&
907 (string[3] == 'n' || string[3] == 'N') &&
908 (string[4] == 't' || string[4] == 'T') &&
909 (string[5] == 'r' || string[5] == 'R') &&
910 (string[6] == 'o' || string[6] == 'O') &&
911 (string[7] == 'l' || string[7] == 'L') &&
915 static inline gboolean
916 is_release (const gchar *string)
918 return ((string[0] == '<') &&
919 (string[1] == 'r' || string[1] == 'R') &&
920 (string[2] == 'e' || string[2] == 'E') &&
921 (string[3] == 'l' || string[3] == 'L') &&
922 (string[4] == 'e' || string[4] == 'E') &&
923 (string[5] == 'a' || string[5] == 'A') &&
924 (string[6] == 's' || string[6] == 'S') &&
925 (string[7] == 'e' || string[7] == 'E') &&
930 * gtk_accelerator_parse:
931 * @accelerator: string representing an accelerator
932 * @accelerator_key: return location for accelerator keyval
933 * @accelerator_mods: return location for accelerator modifier mask
935 * Parses a string representing an accelerator. The
936 * format looks like "<Control>a" or "<Shift><Alt>F1" or
937 * "<Release>z" (the last one is for key release).
938 * The parser is fairly liberal and allows lower or upper case,
939 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
941 * If the parse fails, @accelerator_key and @accelerator_mods will
942 * be set to 0 (zero).
945 gtk_accelerator_parse (const gchar *accelerator,
946 guint *accelerator_key,
947 GdkModifierType *accelerator_mods)
950 GdkModifierType mods;
954 *accelerator_key = 0;
955 if (accelerator_mods)
956 *accelerator_mods = 0;
957 g_return_if_fail (accelerator != NULL);
961 len = strlen (accelerator);
964 if (*accelerator == '<')
966 if (len >= 9 && is_release (accelerator))
970 mods |= GDK_RELEASE_MASK;
972 else if (len >= 9 && is_control (accelerator))
976 mods |= GDK_CONTROL_MASK;
978 else if (len >= 7 && is_shift (accelerator))
982 mods |= GDK_SHIFT_MASK;
984 else if (len >= 6 && is_shft (accelerator))
988 mods |= GDK_SHIFT_MASK;
990 else if (len >= 6 && is_ctrl (accelerator))
994 mods |= GDK_CONTROL_MASK;
996 else if (len >= 6 && is_modx (accelerator))
998 static const guint mod_vals[] = {
999 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1000 GDK_MOD4_MASK, GDK_MOD5_MASK
1005 mods |= mod_vals[*accelerator - '1'];
1008 else if (len >= 5 && is_ctl (accelerator))
1012 mods |= GDK_CONTROL_MASK;
1014 else if (len >= 5 && is_alt (accelerator))
1018 mods |= GDK_MOD1_MASK;
1024 last_ch = *accelerator;
1025 while (last_ch && last_ch != '>')
1027 last_ch = *accelerator;
1035 keyval = gdk_keyval_from_name (accelerator);
1041 if (accelerator_key)
1042 *accelerator_key = gdk_keyval_to_lower (keyval);
1043 if (accelerator_mods)
1044 *accelerator_mods = mods;
1048 * gtk_accelerator_name:
1049 * @accelerator_key: accelerator keyval
1050 * @accelerator_mods: accelerator modifier mask
1051 * @returns: a newly-allocated accelerator name
1053 * Converts an accelerator keyval and modifier mask
1054 * into a string parseable by gtk_accelerator_parse().
1055 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1056 * this function returns "<Control>q".
1058 * The caller of this function must free the returned string.
1061 gtk_accelerator_name (guint accelerator_key,
1062 GdkModifierType accelerator_mods)
1064 static const gchar text_release[] = "<Release>";
1065 static const gchar text_shift[] = "<Shift>";
1066 static const gchar text_control[] = "<Control>";
1067 static const gchar text_mod1[] = "<Alt>";
1068 static const gchar text_mod2[] = "<Mod2>";
1069 static const gchar text_mod3[] = "<Mod3>";
1070 static const gchar text_mod4[] = "<Mod4>";
1071 static const gchar text_mod5[] = "<Mod5>";
1076 accelerator_mods &= GDK_MODIFIER_MASK;
1078 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1083 if (accelerator_mods & GDK_RELEASE_MASK)
1084 l += sizeof (text_release) - 1;
1085 if (accelerator_mods & GDK_SHIFT_MASK)
1086 l += sizeof (text_shift) - 1;
1087 if (accelerator_mods & GDK_CONTROL_MASK)
1088 l += sizeof (text_control) - 1;
1089 if (accelerator_mods & GDK_MOD1_MASK)
1090 l += sizeof (text_mod1) - 1;
1091 if (accelerator_mods & GDK_MOD2_MASK)
1092 l += sizeof (text_mod2) - 1;
1093 if (accelerator_mods & GDK_MOD3_MASK)
1094 l += sizeof (text_mod3) - 1;
1095 if (accelerator_mods & GDK_MOD4_MASK)
1096 l += sizeof (text_mod4) - 1;
1097 if (accelerator_mods & GDK_MOD5_MASK)
1098 l += sizeof (text_mod5) - 1;
1099 l += strlen (keyval_name);
1101 accelerator = g_new (gchar, l + 1);
1105 if (accelerator_mods & GDK_RELEASE_MASK)
1107 strcpy (accelerator + l, text_release);
1108 l += sizeof (text_release) - 1;
1110 if (accelerator_mods & GDK_SHIFT_MASK)
1112 strcpy (accelerator + l, text_shift);
1113 l += sizeof (text_shift) - 1;
1115 if (accelerator_mods & GDK_CONTROL_MASK)
1117 strcpy (accelerator + l, text_control);
1118 l += sizeof (text_control) - 1;
1120 if (accelerator_mods & GDK_MOD1_MASK)
1122 strcpy (accelerator + l, text_mod1);
1123 l += sizeof (text_mod1) - 1;
1125 if (accelerator_mods & GDK_MOD2_MASK)
1127 strcpy (accelerator + l, text_mod2);
1128 l += sizeof (text_mod2) - 1;
1130 if (accelerator_mods & GDK_MOD3_MASK)
1132 strcpy (accelerator + l, text_mod3);
1133 l += sizeof (text_mod3) - 1;
1135 if (accelerator_mods & GDK_MOD4_MASK)
1137 strcpy (accelerator + l, text_mod4);
1138 l += sizeof (text_mod4) - 1;
1140 if (accelerator_mods & GDK_MOD5_MASK)
1142 strcpy (accelerator + l, text_mod5);
1143 l += sizeof (text_mod5) - 1;
1145 strcpy (accelerator + l, keyval_name);
1151 * gtk_accelerator_set_default_mod_mask:
1152 * @default_mod_mask: accelerator modifier mask
1154 * Sets the modifiers that will be considered significant for keyboard
1155 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1156 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1157 * Other modifiers will by default be ignored by #GtkAccelGroup.
1159 * The default mod mask should be changed on application startup,
1160 * before using any accelerator groups.
1163 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1165 default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1169 * gtk_accelerator_get_default_mod_mask:
1170 * @returns: the default accelerator modifier mask
1172 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1175 gtk_accelerator_get_default_mod_mask (void)
1177 return default_accel_mod_mask;