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_acceleratable (GObject *object)
247 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
249 return g_object_get_qdata (object, quark_acceleratable_groups);
253 gtk_accel_group_find (GtkAccelGroup *accel_group,
254 gboolean (*find_func) (GtkAccelKey *key,
259 GtkAccelKey *key = NULL;
262 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
263 g_return_val_if_fail (find_func != NULL, NULL);
265 g_object_ref (accel_group);
266 for (i = 0; i < accel_group->n_accels; i++)
267 if (find_func (&accel_group->priv_accels[i].key,
268 accel_group->priv_accels[i].closure,
271 key = &accel_group->priv_accels[i].key;
274 g_object_unref (accel_group);
280 * gtk_accel_group_lock
281 * @accel_group: a #GtkAccelGroup
283 * Locking an acelerator group prevents the accelerators contained
284 * within it to be changed during runtime. Refer to
285 * gtk_accel_map_change_entry() about runtime accelerator changes.
287 * If called more than once, @accel_group remains locked until
288 * gtk_accel_group_unlock() has been called an equivalent number
292 gtk_accel_group_lock (GtkAccelGroup *accel_group)
294 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
296 accel_group->lock_count += 1;
300 * gtk_accel_group_unlock
301 * @accel_group: a #GtkAccelGroup
303 * This function undoes the last call to gtk_accel_group_lock()
304 * on this @accel_group.
307 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
309 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
310 g_return_if_fail (accel_group->lock_count > 0);
312 accel_group->lock_count -= 1;
316 accel_closure_invalidate (gpointer data,
319 GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
321 gtk_accel_group_disconnect (accel_group, closure);
325 bsearch_compare_accels (const void *d1,
328 const GtkAccelGroupEntry *entry1 = d1;
329 const GtkAccelGroupEntry *entry2 = d2;
331 if (entry1->key.accel_key == entry2->key.accel_key)
332 return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
334 return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
338 quick_accel_add (GtkAccelGroup *accel_group,
340 GdkModifierType accel_mods,
341 GtkAccelFlags accel_flags,
345 guint pos, i = accel_group->n_accels++;
346 GtkAccelGroupEntry key;
349 key.key.accel_key = accel_key;
350 key.key.accel_mods = accel_mods;
351 for (pos = 0; pos < i; pos++)
352 if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
355 /* insert at position, ref closure */
356 accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
357 g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
358 (i - pos) * sizeof (accel_group->priv_accels[0]));
359 accel_group->priv_accels[pos].key.accel_key = accel_key;
360 accel_group->priv_accels[pos].key.accel_mods = accel_mods;
361 accel_group->priv_accels[pos].key.accel_flags = accel_flags;
362 accel_group->priv_accels[pos].closure = g_closure_ref (closure);
363 accel_group->priv_accels[pos].accel_path_quark = path_quark;
364 g_closure_sink (closure);
366 /* handle closure invalidation and reverse lookups */
367 g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
369 /* get accel path notification */
371 _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
373 /* connect and notify changed */
376 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
377 GQuark accel_quark = g_quark_from_string (accel_name);
382 g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
385 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
390 quick_accel_remove (GtkAccelGroup *accel_group,
391 GtkAccelGroupEntry *entry)
393 guint pos = entry - accel_group->priv_accels;
394 GQuark accel_quark = 0;
395 guint accel_key = entry->key.accel_key;
396 GdkModifierType accel_mods = entry->key.accel_mods;
397 GClosure *closure = entry->closure;
399 /* quark for notification */
402 gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
404 accel_quark = g_quark_from_string (accel_name);
408 /* clean up closure invalidate notification and disconnect */
409 g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
411 g_signal_handlers_disconnect_matched (accel_group,
412 G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
413 signal_accel_activate, accel_quark,
414 closure, NULL, NULL);
415 /* clean up accel path notification */
416 if (entry->accel_path_quark)
417 _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
419 /* physically remove */
420 accel_group->n_accels -= 1;
421 g_memmove (entry, entry + 1,
422 (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
426 g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
428 /* remove quick_accel_add() refcount */
429 g_closure_unref (closure);
432 static GtkAccelGroupEntry*
433 quick_accel_find (GtkAccelGroup *accel_group,
435 GdkModifierType accel_mods,
438 GtkAccelGroupEntry *entry;
439 GtkAccelGroupEntry key;
441 if (!accel_group->n_accels)
444 key.key.accel_key = accel_key;
445 key.key.accel_mods = accel_mods;
446 entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
447 sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
452 /* step back to the first member */
453 for (; entry > accel_group->priv_accels; entry--)
454 if (entry[-1].key.accel_key != accel_key ||
455 entry[-1].key.accel_mods != accel_mods)
457 /* count equal members */
458 for (*count_p = 0; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
459 if (entry[*count_p].key.accel_key != accel_key ||
460 entry[*count_p].key.accel_mods != accel_mods)
466 * gtk_accel_group_connect
467 * @accel_group: the ccelerator group to install an accelerator in
468 * @accel_key: key value of the accelerator
469 * @accel_mods: modifier combination of the accelerator
470 * @accel_flags: a flag mask to configure this accelerator
471 * @closure: closure to be executed upon accelerator activation
473 * Install an accelerator in this group. When @accel_group is being activated
474 * in response to a call to gtk_accel_groups_activate(), @closure will be
475 * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
476 * match those of this connection.
477 * The signature used for the @closure is that of #GtkAccelGroupActivate.
478 * Note that, due to implementation details, a single closure can only be
479 * connected to one accelerator group.
482 gtk_accel_group_connect (GtkAccelGroup *accel_group,
484 GdkModifierType accel_mods,
485 GtkAccelFlags accel_flags,
488 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
489 g_return_if_fail (closure != NULL);
490 g_return_if_fail (accel_key > 0);
491 g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
493 g_object_ref (accel_group);
494 if (!closure->is_invalid)
495 quick_accel_add (accel_group, accel_key, accel_mods, accel_flags, closure, 0);
496 g_object_unref (accel_group);
500 * gtk_accel_group_connect_by_path
501 * @accel_group: the ccelerator group to install an accelerator in
502 * @accel_path: path used for determining key and modifiers.
503 * @closure: closure to be executed upon accelerator activation
505 * Install an accelerator in this group, using a accelerator path to look
506 * up the appropriate key and modifiers. (See gtk_accel_map_add_entry())
507 * When @accel_group is being activated in response to a call to
508 * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
509 * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
511 * The signature used for the @closure is that of #GtkAccelGroupActivate.
514 gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
515 const gchar *accel_path,
519 GdkModifierType accel_mods = 0;
522 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
523 g_return_if_fail (closure != NULL);
524 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
526 if (closure->is_invalid)
529 g_object_ref (accel_group);
531 if (gtk_accel_map_lookup_entry (accel_path, &key))
533 accel_key = key.accel_key;
534 accel_mods = key.accel_mods;
537 quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
538 g_quark_from_string (accel_path));
540 g_object_unref (accel_group);
544 * gtk_accel_group_disconnect
545 * @accel_group: the accelerator group to remove an accelerator from
546 * @closure: the closure to remove from this accelerator group
547 * @returns: %TRUE if the closure was found and got disconnected
549 * Remove an accelerator previously installed through
550 * gtk_accel_group_connect().
553 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
558 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
560 for (i = 0; i < accel_group->n_accels; i++)
561 if (accel_group->priv_accels[i].closure == closure)
563 g_object_ref (accel_group);
564 quick_accel_remove (accel_group, accel_group->priv_accels + i);
565 g_object_unref (accel_group);
572 * gtk_accel_group_disconnect_key
573 * @accel_group: the ccelerator group to install an accelerator in
574 * @accel_key: key value of the accelerator
575 * @accel_mods: modifier combination of the accelerator
576 * @returns: %TRUE if there was an accelerator which could be removed, %FALSE otherwise
578 * Remove an accelerator previously installed through
579 * gtk_accel_group_connect().
582 gtk_accel_group_disconnect_key (GtkAccelGroup *accel_group,
584 GdkModifierType accel_mods)
586 GtkAccelGroupEntry *entries;
587 GSList *slist, *clist = NULL;
588 gboolean removed_one = FALSE;
591 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
593 g_object_ref (accel_group);
595 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
598 GClosure *closure = g_closure_ref (entries[n].closure);
600 clist = g_slist_prepend (clist, closure);
603 for (slist = clist; slist; slist = slist->next)
605 GClosure *closure = slist->data;
607 removed_one |= gtk_accel_group_disconnect (accel_group, closure);
608 g_closure_unref (closure);
610 g_slist_free (clist);
612 g_object_unref (accel_group);
618 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
619 GQuark accel_path_quark)
621 GSList *slist, *clist = NULL;
624 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
626 g_object_ref (accel_group);
628 for (i = 0; i < accel_group->n_accels; i++)
629 if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
631 GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
633 clist = g_slist_prepend (clist, closure);
636 for (slist = clist; slist; slist = slist->next)
638 GClosure *closure = slist->data;
640 gtk_accel_group_disconnect (accel_group, closure);
641 gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
642 g_closure_unref (closure);
644 g_slist_free (clist);
646 g_object_unref (accel_group);
650 gtk_accel_group_query (GtkAccelGroup *accel_group,
652 GdkModifierType accel_mods,
655 GtkAccelGroupEntry *entries;
658 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
660 entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
663 *n_entries = entries ? n : 0;
669 gtk_accel_group_from_accel_closure (GClosure *closure)
673 g_return_val_if_fail (closure != NULL, NULL);
675 /* a few remarks on wat we do here. in general, we need a way to reverse lookup
676 * accel_groups from closures that are being used in accel groups. this could
677 * be done e.g via a hashtable. it is however cheaper (memory wise) to just
678 * use the invalidation notifier on the closure itself (which we need to install
679 * anyway), that contains the accel group as data which, besides needing to peek
680 * a bit at closure internals, works just as good.
682 for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
683 if (closure->notifiers[i].notify == accel_closure_invalidate)
684 return closure->notifiers[i].data;
690 _gtk_accel_group_activate (GtkAccelGroup *accel_group,
692 GObject *acceleratable,
694 GdkModifierType accel_mods)
696 gboolean was_handled;
698 g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
701 g_signal_emit (accel_group, signal_accel_activate, accel_quark,
702 acceleratable, accel_key, accel_mods, &was_handled);
708 * gtk_accel_groups_activate:
709 * @acceleratable: usually a #GtkWindow
710 * @accel_key: accelerator keyval from a key event
711 * @accel_mods: keyboard state mask from a key event
712 * @returns: %TRUE if the accelerator was handled, %FALSE otherwise
714 * Finds the first accelerator in any #GtkAccelGroup attached
715 * to @acceleratable that matches @accel_key and @accel_mods, and
716 * activates that accelerator.
717 * If an accelerator was activated and handled this keypress, %TRUE
721 gtk_accel_groups_activate (GObject *acceleratable,
723 GdkModifierType accel_mods)
725 g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
727 if (gtk_accelerator_valid (accel_key, accel_mods))
733 accel_name = gtk_accelerator_name (accel_key, accel_mods);
734 accel_quark = g_quark_from_string (accel_name);
737 for (slist = gtk_accel_groups_from_acceleratable (acceleratable); slist; slist = slist->next)
738 if (_gtk_accel_group_activate (slist->data, accel_quark, acceleratable, accel_key, accel_mods))
746 * gtk_accelerator_valid
747 * @keyval: a GDK keyval
748 * @modifiers: modifier mask
749 * @returns: %TRUE if the accelerator is valid
751 * Determines whether a given keyval and modifier mask constitute
752 * a valid keyboard accelerator. For example, the GDK_a keyval
753 * plus GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
754 * But by default (see gtk_accelerator_set_default_mod_mask()) you
755 * cannot use the NumLock key as an accelerator modifier.
758 gtk_accelerator_valid (guint keyval,
759 GdkModifierType modifiers)
761 static const guint invalid_accelerator_vals[] = {
762 GDK_BackSpace, GDK_Delete, GDK_KP_Delete,
763 GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
764 GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
765 GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
766 GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
767 GDK_Scroll_Lock, GDK_Sys_Req,
768 GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
769 GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
770 GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
771 GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
772 GDK_Terminate_Server, GDK_AudibleBell_Enable,
777 modifiers &= GDK_MODIFIER_MASK;
780 return keyval >= 0x20;
782 ac_val = invalid_accelerator_vals;
785 if (keyval == *ac_val++)
792 static inline gboolean
793 is_alt (const gchar *string)
795 return ((string[0] == '<') &&
796 (string[1] == 'a' || string[1] == 'A') &&
797 (string[2] == 'l' || string[2] == 'L') &&
798 (string[3] == 't' || string[3] == 'T') &&
802 static inline gboolean
803 is_ctl (const gchar *string)
805 return ((string[0] == '<') &&
806 (string[1] == 'c' || string[1] == 'C') &&
807 (string[2] == 't' || string[2] == 'T') &&
808 (string[3] == 'l' || string[3] == 'L') &&
812 static inline gboolean
813 is_modx (const gchar *string)
815 return ((string[0] == '<') &&
816 (string[1] == 'm' || string[1] == 'M') &&
817 (string[2] == 'o' || string[2] == 'O') &&
818 (string[3] == 'd' || string[3] == 'D') &&
819 (string[4] >= '1' && string[4] <= '5') &&
823 static inline gboolean
824 is_ctrl (const gchar *string)
826 return ((string[0] == '<') &&
827 (string[1] == 'c' || string[1] == 'C') &&
828 (string[2] == 't' || string[2] == 'T') &&
829 (string[3] == 'r' || string[3] == 'R') &&
830 (string[4] == 'l' || string[4] == 'L') &&
834 static inline gboolean
835 is_shft (const gchar *string)
837 return ((string[0] == '<') &&
838 (string[1] == 's' || string[1] == 'S') &&
839 (string[2] == 'h' || string[2] == 'H') &&
840 (string[3] == 'f' || string[3] == 'F') &&
841 (string[4] == 't' || string[4] == 'T') &&
845 static inline gboolean
846 is_shift (const gchar *string)
848 return ((string[0] == '<') &&
849 (string[1] == 's' || string[1] == 'S') &&
850 (string[2] == 'h' || string[2] == 'H') &&
851 (string[3] == 'i' || string[3] == 'I') &&
852 (string[4] == 'f' || string[4] == 'F') &&
853 (string[5] == 't' || string[5] == 'T') &&
857 static inline gboolean
858 is_control (const gchar *string)
860 return ((string[0] == '<') &&
861 (string[1] == 'c' || string[1] == 'C') &&
862 (string[2] == 'o' || string[2] == 'O') &&
863 (string[3] == 'n' || string[3] == 'N') &&
864 (string[4] == 't' || string[4] == 'T') &&
865 (string[5] == 'r' || string[5] == 'R') &&
866 (string[6] == 'o' || string[6] == 'O') &&
867 (string[7] == 'l' || string[7] == 'L') &&
871 static inline gboolean
872 is_release (const gchar *string)
874 return ((string[0] == '<') &&
875 (string[1] == 'r' || string[1] == 'R') &&
876 (string[2] == 'e' || string[2] == 'E') &&
877 (string[3] == 'l' || string[3] == 'L') &&
878 (string[4] == 'e' || string[4] == 'E') &&
879 (string[5] == 'a' || string[5] == 'A') &&
880 (string[6] == 's' || string[6] == 'S') &&
881 (string[7] == 'e' || string[7] == 'E') &&
886 * gtk_accelerator_parse
887 * @accelerator: string representing an accelerator
888 * @accelerator_key: return location for accelerator keyval
889 * @accelerator_mods: return location for accelerator modifier mask
891 * Parses a string representing an accelerator. The
892 * format looks like "<Control>a" or "<Shift><Alt>F1" or
893 * "<Release>z" (the last one is for key release).
894 * The parser is fairly liberal and allows lower or upper case,
895 * and also abbreviations such as "<Ctl>" and "<Ctrl>".
897 * If the parse fails, @accelerator_key and @accelerator_mods will
898 * be set to 0 (zero).
901 gtk_accelerator_parse (const gchar *accelerator,
902 guint *accelerator_key,
903 GdkModifierType *accelerator_mods)
906 GdkModifierType mods;
910 *accelerator_key = 0;
911 if (accelerator_mods)
912 *accelerator_mods = 0;
913 g_return_if_fail (accelerator != NULL);
917 len = strlen (accelerator);
920 if (*accelerator == '<')
922 if (len >= 9 && is_release (accelerator))
926 mods |= GDK_RELEASE_MASK;
928 else if (len >= 9 && is_control (accelerator))
932 mods |= GDK_CONTROL_MASK;
934 else if (len >= 7 && is_shift (accelerator))
938 mods |= GDK_SHIFT_MASK;
940 else if (len >= 6 && is_shft (accelerator))
944 mods |= GDK_SHIFT_MASK;
946 else if (len >= 6 && is_ctrl (accelerator))
950 mods |= GDK_CONTROL_MASK;
952 else if (len >= 6 && is_modx (accelerator))
954 static const guint mod_vals[] = {
955 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
956 GDK_MOD4_MASK, GDK_MOD5_MASK
961 mods |= mod_vals[*accelerator - '1'];
964 else if (len >= 5 && is_ctl (accelerator))
968 mods |= GDK_CONTROL_MASK;
970 else if (len >= 5 && is_alt (accelerator))
974 mods |= GDK_MOD1_MASK;
980 last_ch = *accelerator;
981 while (last_ch && last_ch != '>')
983 last_ch = *accelerator;
991 keyval = gdk_keyval_from_name (accelerator);
998 *accelerator_key = gdk_keyval_to_lower (keyval);
999 if (accelerator_mods)
1000 *accelerator_mods = mods;
1004 * gtk_accelerator_name
1005 * @accelerator_key: accelerator keyval
1006 * @accelerator_mods: accelerator modifier mask
1007 * @returns: a newly allocated accelerator name
1009 * Converts an accelerator keyval and modifier mask
1010 * into a string parseable by gtk_accelerator_parse().
1011 * For example, if you pass in GDK_q and GDK_CONTROL_MASK,
1012 * this function returns "<Control>q".
1014 * The caller of this function must free the returned string.
1017 gtk_accelerator_name (guint accelerator_key,
1018 GdkModifierType accelerator_mods)
1020 static const gchar text_release[] = "<Release>";
1021 static const gchar text_shift[] = "<Shift>";
1022 static const gchar text_control[] = "<Control>";
1023 static const gchar text_mod1[] = "<Alt>";
1024 static const gchar text_mod2[] = "<Mod2>";
1025 static const gchar text_mod3[] = "<Mod3>";
1026 static const gchar text_mod4[] = "<Mod4>";
1027 static const gchar text_mod5[] = "<Mod5>";
1032 accelerator_mods &= GDK_MODIFIER_MASK;
1034 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1039 if (accelerator_mods & GDK_RELEASE_MASK)
1040 l += sizeof (text_release) - 1;
1041 if (accelerator_mods & GDK_SHIFT_MASK)
1042 l += sizeof (text_shift) - 1;
1043 if (accelerator_mods & GDK_CONTROL_MASK)
1044 l += sizeof (text_control) - 1;
1045 if (accelerator_mods & GDK_MOD1_MASK)
1046 l += sizeof (text_mod1) - 1;
1047 if (accelerator_mods & GDK_MOD2_MASK)
1048 l += sizeof (text_mod2) - 1;
1049 if (accelerator_mods & GDK_MOD3_MASK)
1050 l += sizeof (text_mod3) - 1;
1051 if (accelerator_mods & GDK_MOD4_MASK)
1052 l += sizeof (text_mod4) - 1;
1053 if (accelerator_mods & GDK_MOD5_MASK)
1054 l += sizeof (text_mod5) - 1;
1055 l += strlen (keyval_name);
1057 accelerator = g_new (gchar, l + 1);
1061 if (accelerator_mods & GDK_RELEASE_MASK)
1063 strcpy (accelerator + l, text_release);
1064 l += sizeof (text_release) - 1;
1066 if (accelerator_mods & GDK_SHIFT_MASK)
1068 strcpy (accelerator + l, text_shift);
1069 l += sizeof (text_shift) - 1;
1071 if (accelerator_mods & GDK_CONTROL_MASK)
1073 strcpy (accelerator + l, text_control);
1074 l += sizeof (text_control) - 1;
1076 if (accelerator_mods & GDK_MOD1_MASK)
1078 strcpy (accelerator + l, text_mod1);
1079 l += sizeof (text_mod1) - 1;
1081 if (accelerator_mods & GDK_MOD2_MASK)
1083 strcpy (accelerator + l, text_mod2);
1084 l += sizeof (text_mod2) - 1;
1086 if (accelerator_mods & GDK_MOD3_MASK)
1088 strcpy (accelerator + l, text_mod3);
1089 l += sizeof (text_mod3) - 1;
1091 if (accelerator_mods & GDK_MOD4_MASK)
1093 strcpy (accelerator + l, text_mod4);
1094 l += sizeof (text_mod4) - 1;
1096 if (accelerator_mods & GDK_MOD5_MASK)
1098 strcpy (accelerator + l, text_mod5);
1099 l += sizeof (text_mod5) - 1;
1101 strcpy (accelerator + l, keyval_name);
1107 * gtk_accelerator_set_default_mod_mask
1108 * @default_mod_mask: accelerator modifier mask
1110 * Sets the modifiers that will be considered significant for keyboard
1111 * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1112 * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1113 * Other modifiers will by default be ignored by #GtkAccelGroup.
1115 * The default mod mask should be changed on application startup,
1116 * before using any accelerator groups.
1119 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1121 default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1125 * gtk_accelerator_get_default_mod_mask
1126 * @returns: the default accelerator modifier mask
1128 * Gets the value set by gtk_accelerator_set_default_mod_mask().
1131 gtk_accelerator_get_default_mod_mask (void)
1133 return default_accel_mod_mask;