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);
43 static void accel_closure_invalidate (gpointer data,
47 /* --- variables --- */
48 static guint signal_accel_activate = 0;
49 static guint signal_accel_changed = 0;
50 static guint quark_acceleratable_groups = 0;
51 static guint default_accel_mod_mask = (GDK_SHIFT_MASK |
59 G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
61 /* --- functions --- */
63 gtk_accel_group_class_init (GtkAccelGroupClass *class)
65 GObjectClass *object_class = G_OBJECT_CLASS (class);
67 quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
69 object_class->finalize = gtk_accel_group_finalize;
71 class->accel_changed = NULL;
74 * GtkAccelGroup::accel-activate:
75 * @accel_group: the #GtkAccelGroup which received the signal
76 * @acceleratable: the object on which the accelerator was activated
77 * @keyval: the accelerator keyval
78 * @modifier: the modifier combination of the accelerator
80 * The accel-activate signal is an implementation detail of
81 * #GtkAccelGroup and not meant to be used by applications.
83 * Returns: %TRUE if the accelerator was activated
85 signal_accel_activate =
86 g_signal_new (I_("accel_activate"),
87 G_OBJECT_CLASS_TYPE (class),
90 _gtk_boolean_handled_accumulator, NULL,
91 _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
95 GDK_TYPE_MODIFIER_TYPE);
97 * GtkAccelGroup::accel-changed:
98 * @accel_group: the #GtkAccelGroup which received the signal
99 * @keyval: the accelerator keyval
100 * @modifier: the modifier combination of the accelerator
101 * @accel_closure: the #GClosure of the accelerator
103 * The accel-changed signal is emitted when a #GtkAccelGroupEntry
104 * is added to or removed from the accel group.
106 * Widgets like #GtkAccelLabel which display an associated
107 * accelerator should connect to this signal, and rebuild
108 * their visual representation if the @accel_closure is theirs.
110 signal_accel_changed =
111 g_signal_new (I_("accel_changed"),
112 G_OBJECT_CLASS_TYPE (class),
113 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
114 G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
116 _gtk_marshal_VOID__UINT_FLAGS_BOXED,
119 GDK_TYPE_MODIFIER_TYPE,
124 gtk_accel_group_finalize (GObject *object)
126 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
129 for (i = 0; i < accel_group->n_accels; i++)
131 GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
133 if (entry->accel_path_quark)
135 const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
137 _gtk_accel_map_remove_group (accel_path, accel_group);
139 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
142 g_free (accel_group->priv_accels);
144 G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
148 gtk_accel_group_init (GtkAccelGroup *accel_group)
150 accel_group->lock_count = 0;
151 accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
152 accel_group->acceleratables = NULL;
153 accel_group->n_accels = 0;
154 accel_group->priv_accels = NULL;
158 * gtk_accel_group_new:
159 * @returns: a new #GtkAccelGroup object
161 * Creates a new #GtkAccelGroup.
164 gtk_accel_group_new (void)
166 return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
170 accel_group_weak_ref_detach (GSList *free_list,
171 GObject *stale_object)
175 for (slist = free_list; slist; slist = slist->next)
177 GtkAccelGroup *accel_group;
179 accel_group = slist->data;
180 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
181 g_object_unref (accel_group);
183 g_slist_free (free_list);
184 g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
188 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
193 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
194 g_return_if_fail (G_IS_OBJECT (object));
195 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) == NULL);
197 g_object_ref (accel_group);
198 accel_group->acceleratables = g_slist_prepend (accel_group->acceleratables, object);
199 slist = g_object_get_qdata (object, quark_acceleratable_groups);
201 g_object_weak_unref (object,
202 (GWeakNotify) accel_group_weak_ref_detach,
204 slist = g_slist_prepend (slist, accel_group);
205 g_object_set_qdata (object, quark_acceleratable_groups, slist);
206 g_object_weak_ref (object,
207 (GWeakNotify) accel_group_weak_ref_detach,
212 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
217 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
218 g_return_if_fail (G_IS_OBJECT (object));
219 g_return_if_fail (g_slist_find (accel_group->acceleratables, object) != NULL);
221 accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, object);
222 slist = g_object_get_qdata (object, quark_acceleratable_groups);
223 g_object_weak_unref (object,
224 (GWeakNotify) accel_group_weak_ref_detach,
226 slist = g_slist_remove (slist, accel_group);
227 g_object_set_qdata (object, quark_acceleratable_groups, slist);
229 g_object_weak_ref (object,
230 (GWeakNotify) accel_group_weak_ref_detach,
232 g_object_unref (accel_group);
236 * gtk_accel_groups_from_object:
237 * @object: a #GObject, usually a #GtkWindow
238 * @returns: a list of all accel groups which are attached to @object
240 * Gets a list of all accel groups which are attached to @object.
243 gtk_accel_groups_from_object (GObject *object)
245 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
247 return g_object_get_qdata (object, quark_acceleratable_groups);
251 * gtk_accel_group_find:
252 * @accel_group: a #GtkAccelGroup
253 * @find_func: a function to filter the entries of @accel_group with
254 * @data: data to pass to @find_func
255 * @returns: the key of the first entry passing @find_func. The key is
256 * owned by GTK+ and must not be freed.
258 * Finds the first entry in an accelerator group for which
259 * @find_func returns %TRUE and returns its #GtkAccelKey.
263 gtk_accel_group_find (GtkAccelGroup *accel_group,
264 GtkAccelGroupFindFunc find_func,
267 GtkAccelKey *key = NULL;
270 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
271 g_return_val_if_fail (find_func != NULL, NULL);
273 g_object_ref (accel_group);
274 for (i = 0; i < accel_group->n_accels; i++)
275 if (find_func (&accel_group->priv_accels[i].key,
276 accel_group->priv_accels[i].closure,
279 key = &accel_group->priv_accels[i].key;
282 g_object_unref (accel_group);
288 * gtk_accel_group_lock:
289 * @accel_group: a #GtkAccelGroup
291 * Locks the given accelerator group.
293 * Locking an acelerator group prevents the accelerators contained
294 * within it to be changed during runtime. Refer to
295 * gtk_accel_map_change_entry() about runtime accelerator changes.
297 * If called more than once, @accel_group remains locked until
298 * gtk_accel_group_unlock() has been called an equivalent number
302 gtk_accel_group_lock (GtkAccelGroup *accel_group)
304 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
306 accel_group->lock_count += 1;
310 * gtk_accel_group_unlock:
311 * @accel_group: a #GtkAccelGroup
313 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
316 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
318 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
319 g_return_if_fail (accel_group->lock_count > 0);
321 accel_group->lock_count -= 1;
325 accel_closure_invalidate (gpointer data,
328 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
330 gtk_accel_group_disconnect (accel_group, closure);
334 bsearch_compare_accels (const void *d1,
337 const GtkAccelGroupEntry *entry1 = d1;
338 const GtkAccelGroupEntry *entry2 = d2;
340 if (entry1->key.accel_key == entry2->key.accel_key)
341 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
343 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
347 quick_accel_add (GtkAccelGroup *accel_group,
349 GdkModifierType accel_mods,
350 GtkAccelFlags accel_flags,
354 guint pos, i = accel_group->n_accels++;
355 GtkAccelGroupEntry key;
358 key.key.accel_key = accel_key;
359 key.key.accel_mods = accel_mods;
360 for (pos = 0; pos < i; pos++)
361 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
364 /* insert at position, ref closure */
365 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
366 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
367 (i - pos) * sizeof (accel_group->priv_accels[0]));
368 accel_group->priv_accels[pos].key.accel_key = accel_key;
369 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
370 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
371 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
372 accel_group->priv_accels[pos].accel_path_quark = path_quark;
373 g_closure_sink (closure);
375 /* handle closure invalidation and reverse lookups */
376 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
378 /* get accel path notification */
380 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
382 /* connect and notify changed */
385 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
386 GQuark accel_quark = g_quark_from_string (accel_name);
391 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
394 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
399 quick_accel_remove (GtkAccelGroup *accel_group,
402 GQuark accel_quark = 0;
403 GtkAccelGroupEntry *entry = accel_group->priv_accels + pos;
404 guint accel_key = entry->key.accel_key;
405 GdkModifierType accel_mods = entry->key.accel_mods;
406 GClosure *closure = entry->closure;
408 /* quark for notification */
411 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
413 accel_quark = g_quark_from_string (accel_name);
417 /* clean up closure invalidate notification and disconnect */
418 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
420 g_signal_handlers_disconnect_matched (accel_group,
421 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
422 signal_accel_activate, accel_quark,
423 closure, NULL, NULL);
424 /* clean up accel path notification */
425 if (entry->accel_path_quark)
426 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
428 /* physically remove */
429 accel_group->n_accels -= 1;
430 g_memmove (entry, entry + 1,
431 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
435 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
437 /* remove quick_accel_add() refcount */
438 g_closure_unref (closure);
441 static GtkAccelGroupEntry*
442 quick_accel_find (GtkAccelGroup *accel_group,
444 GdkModifierType accel_mods,
447 GtkAccelGroupEntry *entry;
448 GtkAccelGroupEntry key;
452 if (!accel_group->n_accels)
455 key.key.accel_key = accel_key;
456 key.key.accel_mods = accel_mods;
457 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
458 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
463 /* step back to the first member */
464 for (; entry > accel_group->priv_accels; entry--)
465 if (entry[-1].key.accel_key != accel_key ||
466 entry[-1].key.accel_mods != accel_mods)
468 /* count equal members */
469 for (; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
470 if (entry[*count_p].key.accel_key != accel_key ||
471 entry[*count_p].key.accel_mods != accel_mods)
477 * gtk_accel_group_connect:
478 * @accel_group: the accelerator group to install an accelerator in
479 * @accel_key: key value of the accelerator
480 * @accel_mods: modifier combination of the accelerator
481 * @accel_flags: a flag mask to configure this accelerator
482 * @closure: closure to be executed upon accelerator activation
484 * Installs an accelerator in this group. When @accel_group is being activated
485 * in response to a call to gtk_accel_groups_activate(), @closure will be
486 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
487 * match those of this connection.
489 * The signature used for the @closure is that of #GtkAccelGroupActivate.
491 * Note that, due to implementation details, a single closure can only be
492 * connected to one accelerator group.
495 gtk_accel_group_connect (GtkAccelGroup *accel_group,
497 GdkModifierType accel_mods,
498 GtkAccelFlags accel_flags,
501 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
502 g_return_if_fail (closure != NULL);
503 g_return_if_fail (accel_key > 0);
504 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
506 g_object_ref (accel_group);
507 if (!closure->is_invalid)
508 quick_accel_add (accel_group,
509 gdk_keyval_to_lower (accel_key),
510 accel_mods, accel_flags, closure, 0);
511 g_object_unref (accel_group);
515 * gtk_accel_group_connect_by_path:
516 * @accel_group: the accelerator group to install an accelerator in
517 * @accel_path: path used for determining key and modifiers.
518 * @closure: closure to be executed upon accelerator activation
520 * Installs an accelerator in this group, using an accelerator path to look
521 * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
522 * When @accel_group is being activated in response to a call to
523 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
524 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
527 * The signature used for the @closure is that of #GtkAccelGroupActivate.
529 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
530 * pass a static string, you can save some memory by interning it first with
531 * g_intern_static_string().
534 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
535 const gchar *accel_path,
539 GdkModifierType accel_mods = 0;
542 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
543 g_return_if_fail (closure != NULL);
544 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
546 if (closure->is_invalid)
549 g_object_ref (accel_group);
551 if (gtk_accel_map_lookup_entry (accel_path, &key))
553 accel_key = gdk_keyval_to_lower (key.accel_key);
554 accel_mods = key.accel_mods;
557 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
558 g_quark_from_string (accel_path));
560 g_object_unref (accel_group);
564 * gtk_accel_group_disconnect:
565 * @accel_group: the accelerator group to remove an accelerator from
566 * @closure: the closure to remove from this accelerator group
567 * @returns: %TRUE if the closure was found and got disconnected
569 * Removes an accelerator previously installed through
570 * gtk_accel_group_connect().
573 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
578 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
580 for (i = 0; i < accel_group->n_accels; i++)
581 if (accel_group->priv_accels[i].closure == closure)
583 g_object_ref (accel_group);
584 quick_accel_remove (accel_group, i);
585 g_object_unref (accel_group);
592 * gtk_accel_group_disconnect_key:
593 * @accel_group: the accelerator group to install an accelerator in
594 * @accel_key: key value of the accelerator
595 * @accel_mods: modifier combination of the accelerator
596 * @returns: %TRUE if there was an accelerator which could be
597 * removed, %FALSE otherwise
599 * Removes an accelerator previously installed through
600 * gtk_accel_group_connect().
603 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
605 GdkModifierType accel_mods)
607 GtkAccelGroupEntry *entries;
608 GSList *slist, *clist = NULL;
609 gboolean removed_one = FALSE;
612 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
614 g_object_ref (accel_group);
616 accel_key = gdk_keyval_to_lower (accel_key);
617 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
620 GClosure *closure = g_closure_ref (entries[n].closure);
622 clist = g_slist_prepend (clist, closure);
625 for (slist = clist; slist; slist = slist->next)
627 GClosure *closure = slist->data;
629 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
630 g_closure_unref (closure);
632 g_slist_free (clist);
634 g_object_unref (accel_group);
640 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
641 GQuark accel_path_quark)
643 GSList *slist, *clist = NULL;
646 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
648 g_object_ref (accel_group);
650 for (i = 0; i < accel_group->n_accels; i++)
651 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
653 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
655 clist = g_slist_prepend (clist, closure);
658 for (slist = clist; slist; slist = slist->next)
660 GClosure *closure = slist->data;
662 gtk_accel_group_disconnect (accel_group, closure);
663 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
664 g_closure_unref (closure);
666 g_slist_free (clist);
668 g_object_unref (accel_group);
672 * gtk_accel_group_query:
673 * @accel_group: the accelerator group to query
674 * @accel_key: key value of the accelerator
675 * @accel_mods: modifier combination of the accelerator
676 * @n_entries: location to return the number of entries found, or %NULL
677 * @returns: an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed.
679 * Queries an accelerator group for all entries matching @accel_key and
683 gtk_accel_group_query (GtkAccelGroup *accel_group,
685 GdkModifierType accel_mods,
688 GtkAccelGroupEntry *entries;
691 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
693 entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
696 *n_entries = entries ? n : 0;
702 * gtk_accel_group_from_accel_closure:
703 * @closure: a #GClosure
704 * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
706 * Finds the #GtkAccelGroup to which @closure is connected;
707 * see gtk_accel_group_connect().
710 gtk_accel_group_from_accel_closure (GClosure *closure)
714 g_return_val_if_fail (closure != NULL, NULL);
716 /* a few remarks on what we do here. in general, we need a way to reverse lookup
717 * accel_groups from closures that are being used in accel groups. this could
718 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
719 * use the invalidation notifier on the closure itself (which we need to install
720 * anyway), that contains the accel group as data which, besides needing to peek
721 * a bit at closure internals, works just as good.
723 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
724 if (closure->notifiers[i].notify == accel_closure_invalidate)
725 return closure->notifiers[i].data;
731 gtk_accel_group_activate (GtkAccelGroup *accel_group,
733 GObject *acceleratable,
735 GdkModifierType accel_mods)
737 gboolean was_handled;
739 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
740 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
743 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
744 acceleratable, accel_key, accel_mods, &was_handled);
750 * gtk_accel_groups_activate:
751 * @object: the #GObject, usually a #GtkWindow, on which
752 * to activate the accelerator.
753 * @accel_key: accelerator keyval from a key event
754 * @accel_mods: keyboard state mask from a key event
755 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
757 * Finds the first accelerator in any #GtkAccelGroup attached
758 * to @object that matches @accel_key and @accel_mods, and
759 * activates that accelerator.
760 * If an accelerator was activated and handled this keypress, %TRUE
764 gtk_accel_groups_activate (GObject *object,
766 GdkModifierType accel_mods)
768 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
770 if (gtk_accelerator_valid (accel_key, accel_mods))
776 accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
777 accel_quark = g_quark_from_string (accel_name);
780 for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
781 if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
789 * gtk_accelerator_valid:
790 * @keyval: a GDK keyval
791 * @modifiers: modifier mask
792 * @returns: %TRUE if the accelerator is valid
794 * Determines whether a given keyval and modifier mask constitute
795 * a valid keyboard accelerator. For example, the #GDK_a keyval
796 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
797 * But, you can't, for instance, use the #GDK_Control_L keyval
801 gtk_accelerator_valid (guint keyval,
802 GdkModifierType modifiers)
804 static const guint invalid_accelerator_vals[] = {
805 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
806 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
807 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
808 GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
809 GDK_ISO_First_Group, GDK_ISO_Last_Group,
810 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
811 GDK_Scroll_Lock, GDK_Sys_Req,
812 GDK_Tab, GDK_ISO_Left_Tab, GDK_KP_Tab,
813 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
814 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
815 GDK_Terminate_Server, GDK_AudibleBell_Enable,
818 static const guint invalid_unmodified_vals[] = {
819 GDK_Up, GDK_Down, GDK_Left, GDK_Right,
820 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right,
825 modifiers &= GDK_MODIFIER_MASK;
828 return keyval >= 0x20;
830 ac_val = invalid_accelerator_vals;
833 if (keyval == *ac_val++)
839 ac_val = invalid_unmodified_vals;
842 if (keyval == *ac_val++)
850 static inline gboolean
851 is_alt (const gchar *string)
853 return ((string[0] == '<') &&
854 (string[1] == 'a' || string[1] == 'A') &&
855 (string[2] == 'l' || string[2] == 'L') &&
856 (string[3] == 't' || string[3] == 'T') &&
860 static inline gboolean
861 is_ctl (const gchar *string)
863 return ((string[0] == '<') &&
864 (string[1] == 'c' || string[1] == 'C') &&
865 (string[2] == 't' || string[2] == 'T') &&
866 (string[3] == 'l' || string[3] == 'L') &&
870 static inline gboolean
871 is_modx (const gchar *string)
873 return ((string[0] == '<') &&
874 (string[1] == 'm' || string[1] == 'M') &&
875 (string[2] == 'o' || string[2] == 'O') &&
876 (string[3] == 'd' || string[3] == 'D') &&
877 (string[4] >= '1' && string[4] <= '5') &&
881 static inline gboolean
882 is_ctrl (const gchar *string)
884 return ((string[0] == '<') &&
885 (string[1] == 'c' || string[1] == 'C') &&
886 (string[2] == 't' || string[2] == 'T') &&
887 (string[3] == 'r' || string[3] == 'R') &&
888 (string[4] == 'l' || string[4] == 'L') &&
892 static inline gboolean
893 is_shft (const gchar *string)
895 return ((string[0] == '<') &&
896 (string[1] == 's' || string[1] == 'S') &&
897 (string[2] == 'h' || string[2] == 'H') &&
898 (string[3] == 'f' || string[3] == 'F') &&
899 (string[4] == 't' || string[4] == 'T') &&
903 static inline gboolean
904 is_shift (const gchar *string)
906 return ((string[0] == '<') &&
907 (string[1] == 's' || string[1] == 'S') &&
908 (string[2] == 'h' || string[2] == 'H') &&
909 (string[3] == 'i' || string[3] == 'I') &&
910 (string[4] == 'f' || string[4] == 'F') &&
911 (string[5] == 't' || string[5] == 'T') &&
915 static inline gboolean
916 is_control (const gchar *string)
918 return ((string[0] == '<') &&
919 (string[1] == 'c' || string[1] == 'C') &&
920 (string[2] == 'o' || string[2] == 'O') &&
921 (string[3] == 'n' || string[3] == 'N') &&
922 (string[4] == 't' || string[4] == 'T') &&
923 (string[5] == 'r' || string[5] == 'R') &&
924 (string[6] == 'o' || string[6] == 'O') &&
925 (string[7] == 'l' || string[7] == 'L') &&
929 static inline gboolean
930 is_release (const gchar *string)
932 return ((string[0] == '<') &&
933 (string[1] == 'r' || string[1] == 'R') &&
934 (string[2] == 'e' || string[2] == 'E') &&
935 (string[3] == 'l' || string[3] == 'L') &&
936 (string[4] == 'e' || string[4] == 'E') &&
937 (string[5] == 'a' || string[5] == 'A') &&
938 (string[6] == 's' || string[6] == 'S') &&
939 (string[7] == 'e' || string[7] == 'E') &&
943 static inline gboolean
944 is_meta (const gchar *string)
946 return ((string[0] == '<') &&
947 (string[1] == 'm' || string[1] == 'M') &&
948 (string[2] == 'e' || string[2] == 'E') &&
949 (string[3] == 't' || string[3] == 'T') &&
950 (string[4] == 'a' || string[4] == 'A') &&
954 static inline gboolean
955 is_super (const gchar *string)
957 return ((string[0] == '<') &&
958 (string[1] == 's' || string[1] == 'S') &&
959 (string[2] == 'u' || string[2] == 'U') &&
960 (string[3] == 'p' || string[3] == 'P') &&
961 (string[4] == 'e' || string[4] == 'E') &&
962 (string[5] == 'r' || string[5] == 'R') &&
966 static inline gboolean
967 is_hyper (const gchar *string)
969 return ((string[0] == '<') &&
970 (string[1] == 'h' || string[1] == 'H') &&
971 (string[2] == 'y' || string[2] == 'Y') &&
972 (string[3] == 'p' || string[3] == 'P') &&
973 (string[4] == 'e' || string[4] == 'E') &&
974 (string[5] == 'r' || string[5] == 'R') &&
979 * gtk_accelerator_parse:
980 * @accelerator: string representing an accelerator
981 * @accelerator_key: return location for accelerator keyval
982 * @accelerator_mods: return location for accelerator modifier mask
984 * Parses a string representing an accelerator. The
985 * format looks like "<Control>a" or "<Shift><Alt>F1" or
986 * "<Release>z" (the last one is for key release).
987 * The parser is fairly liberal and allows lower or upper case,
988 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
990 * If the parse fails, @accelerator_key and @accelerator_mods will
991 * be set to 0 (zero).
994 gtk_accelerator_parse (const gchar *accelerator,
995 guint *accelerator_key,
996 GdkModifierType *accelerator_mods)
999 GdkModifierType mods;
1002 if (accelerator_key)
1003 *accelerator_key = 0;
1004 if (accelerator_mods)
1005 *accelerator_mods = 0;
1006 g_return_if_fail (accelerator != NULL);
1010 len = strlen (accelerator);
1013 if (*accelerator == '<')
1015 if (len >= 9 && is_release (accelerator))
1019 mods |= GDK_RELEASE_MASK;
1021 else if (len >= 9 && is_control (accelerator))
1025 mods |= GDK_CONTROL_MASK;
1027 else if (len >= 7 && is_shift (accelerator))
1031 mods |= GDK_SHIFT_MASK;
1033 else if (len >= 6 && is_shft (accelerator))
1037 mods |= GDK_SHIFT_MASK;
1039 else if (len >= 6 && is_ctrl (accelerator))
1043 mods |= GDK_CONTROL_MASK;
1045 else if (len >= 6 && is_modx (accelerator))
1047 static const guint mod_vals[] = {
1048 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1049 GDK_MOD4_MASK, GDK_MOD5_MASK
1054 mods |= mod_vals[*accelerator - '1'];
1057 else if (len >= 5 && is_ctl (accelerator))
1061 mods |= GDK_CONTROL_MASK;
1063 else if (len >= 5 && is_alt (accelerator))
1067 mods |= GDK_MOD1_MASK;
1069 else if (len >= 6 && is_meta (accelerator))
1073 mods |= GDK_META_MASK;
1075 else if (len >= 7 && is_hyper (accelerator))
1079 mods |= GDK_HYPER_MASK;
1081 else if (len >= 7 && is_super (accelerator))
1085 mods |= GDK_SUPER_MASK;
1091 last_ch = *accelerator;
1092 while (last_ch && last_ch != '>')
1094 last_ch = *accelerator;
1102 keyval = gdk_keyval_from_name (accelerator);
1108 if (accelerator_key)
1109 *accelerator_key = gdk_keyval_to_lower (keyval);
1110 if (accelerator_mods)
1111 *accelerator_mods = mods;
1115 * gtk_accelerator_name:
1116 * @accelerator_key: accelerator keyval
1117 * @accelerator_mods: accelerator modifier mask
1119 * Converts an accelerator keyval and modifier mask
1120 * into a string parseable by gtk_accelerator_parse().
1121 * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1122 * this function returns "<Control>q".
1124 * If you need to display accelerators in the user interface,
1125 * see gtk_accelerator_get_label().
1127 * Returns: a newly-allocated accelerator name
1130 gtk_accelerator_name (guint accelerator_key,
1131 GdkModifierType accelerator_mods)
1133 static const gchar text_release[] = "<Release>";
1134 static const gchar text_shift[] = "<Shift>";
1135 static const gchar text_control[] = "<Control>";
1136 static const gchar text_mod1[] = "<Alt>";
1137 static const gchar text_mod2[] = "<Mod2>";
1138 static const gchar text_mod3[] = "<Mod3>";
1139 static const gchar text_mod4[] = "<Mod4>";
1140 static const gchar text_mod5[] = "<Mod5>";
1141 static const gchar text_meta[] = "<Meta>";
1142 static const gchar text_super[] = "<Super>";
1143 static const gchar text_hyper[] = "<Hyper>";
1148 accelerator_mods &= GDK_MODIFIER_MASK;
1150 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1155 if (accelerator_mods & GDK_RELEASE_MASK)
1156 l += sizeof (text_release) - 1;
1157 if (accelerator_mods & GDK_SHIFT_MASK)
1158 l += sizeof (text_shift) - 1;
1159 if (accelerator_mods & GDK_CONTROL_MASK)
1160 l += sizeof (text_control) - 1;
1161 if (accelerator_mods & GDK_MOD1_MASK)
1162 l += sizeof (text_mod1) - 1;
1163 if (accelerator_mods & GDK_MOD2_MASK)
1164 l += sizeof (text_mod2) - 1;
1165 if (accelerator_mods & GDK_MOD3_MASK)
1166 l += sizeof (text_mod3) - 1;
1167 if (accelerator_mods & GDK_MOD4_MASK)
1168 l += sizeof (text_mod4) - 1;
1169 if (accelerator_mods & GDK_MOD5_MASK)
1170 l += sizeof (text_mod5) - 1;
1171 l += strlen (keyval_name);
1172 if (accelerator_mods & GDK_META_MASK)
1173 l += sizeof (text_meta) - 1;
1174 if (accelerator_mods & GDK_HYPER_MASK)
1175 l += sizeof (text_hyper) - 1;
1176 if (accelerator_mods & GDK_SUPER_MASK)
1177 l += sizeof (text_super) - 1;
1179 accelerator = g_new (gchar, l + 1);
1183 if (accelerator_mods & GDK_RELEASE_MASK)
1185 strcpy (accelerator + l, text_release);
1186 l += sizeof (text_release) - 1;
1188 if (accelerator_mods & GDK_SHIFT_MASK)
1190 strcpy (accelerator + l, text_shift);
1191 l += sizeof (text_shift) - 1;
1193 if (accelerator_mods & GDK_CONTROL_MASK)
1195 strcpy (accelerator + l, text_control);
1196 l += sizeof (text_control) - 1;
1198 if (accelerator_mods & GDK_MOD1_MASK)
1200 strcpy (accelerator + l, text_mod1);
1201 l += sizeof (text_mod1) - 1;
1203 if (accelerator_mods & GDK_MOD2_MASK)
1205 strcpy (accelerator + l, text_mod2);
1206 l += sizeof (text_mod2) - 1;
1208 if (accelerator_mods & GDK_MOD3_MASK)
1210 strcpy (accelerator + l, text_mod3);
1211 l += sizeof (text_mod3) - 1;
1213 if (accelerator_mods & GDK_MOD4_MASK)
1215 strcpy (accelerator + l, text_mod4);
1216 l += sizeof (text_mod4) - 1;
1218 if (accelerator_mods & GDK_MOD5_MASK)
1220 strcpy (accelerator + l, text_mod5);
1221 l += sizeof (text_mod5) - 1;
1223 if (accelerator_mods & GDK_META_MASK)
1225 strcpy (accelerator + l, text_meta);
1226 l += sizeof (text_meta) - 1;
1228 if (accelerator_mods & GDK_HYPER_MASK)
1230 strcpy (accelerator + l, text_hyper);
1231 l += sizeof (text_hyper) - 1;
1233 if (accelerator_mods & GDK_SUPER_MASK)
1235 strcpy (accelerator + l, text_super);
1236 l += sizeof (text_super) - 1;
1238 strcpy (accelerator + l, keyval_name);
1244 * gtk_accelerator_get_label:
1245 * @accelerator_key: accelerator keyval
1246 * @accelerator_mods: accelerator modifier mask
1248 * Converts an accelerator keyval and modifier mask into a string
1249 * which can be used to represent the accelerator to the user.
1251 * Returns: a newly-allocated string representing the accelerator.
1256 gtk_accelerator_get_label (guint accelerator_key,
1257 GdkModifierType accelerator_mods)
1259 GtkAccelLabelClass *klass;
1262 klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
1263 label = _gtk_accel_label_class_get_accelerator_label (klass,
1266 g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
1272 * gtk_accelerator_set_default_mod_mask:
1273 * @default_mod_mask: accelerator modifier mask
1275 * Sets the modifiers that will be considered significant for keyboard
1276 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1277 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK | #GDK_SUPER_MASK |
1278 * #GDK_HYPER_MASK | #GDK_META_MASK, that is, Control, Shift, Alt,
1279 * Super, Hyper and Meta. Other modifiers will by default be ignored
1280 * by #GtkAccelGroup.
1281 * You must include at least the three modifiers Control, Shift
1282 * and Alt in any value you pass to this function.
1284 * The default mod mask should be changed on application startup,
1285 * before using any accelerator groups.
1288 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1290 default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1291 (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1295 * gtk_accelerator_get_default_mod_mask:
1296 * @returns: the default accelerator modifier mask
1298 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1301 gtk_accelerator_get_default_mod_mask (void)
1303 return default_accel_mod_mask;
1306 #define __GTK_ACCEL_GROUP_C__
1307 #include "gtkaliasdef.c"