]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccelgroup.c
d6cb323e98576b56fbd9b8946504ecc68cb07d0f
[~andy/gtk] / gtk / gtkaccelgroup.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1998, 2001 Tim Janik
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26 #include "gtkaccelgroup.h"
27 #include "gtkaccelmap.h"
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkmarshalers.h"
30 #include "gtksignal.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34
35
36 /* --- prototypes --- */
37 static void gtk_accel_group_class_init  (GtkAccelGroupClass     *class);
38 static void gtk_accel_group_init        (GtkAccelGroup          *accel_group);
39 static void gtk_accel_group_finalize    (GObject                *object);
40
41
42 /* --- variables --- */
43 static GObjectClass     *parent_class = NULL;
44 static guint             signal_accel_activate = 0;
45 static guint             signal_accel_changed = 0;
46 static guint             quark_acceleratable_groups = 0;
47 static guint             default_accel_mod_mask = (GDK_SHIFT_MASK |
48                                                    GDK_CONTROL_MASK |
49                                                    GDK_MOD1_MASK);
50
51
52 /* --- functions --- */
53 /**
54  * gtk_accel_group_get_type:
55  * @returns: the type ID for accelerator groups.
56  */
57 GType
58 gtk_accel_group_get_type (void)
59 {
60   static GType object_type = 0;
61
62   if (!object_type)
63     {
64       static const GTypeInfo object_info = {
65         sizeof (GtkAccelGroupClass),
66         (GBaseInitFunc) NULL,
67         (GBaseFinalizeFunc) NULL,
68         (GClassInitFunc) gtk_accel_group_class_init,
69         NULL,   /* clas_finalize */
70         NULL,   /* class_data */
71         sizeof (GtkAccelGroup),
72         0,      /* n_preallocs */
73         (GInstanceInitFunc) gtk_accel_group_init,
74       };
75
76       object_type = g_type_register_static (G_TYPE_OBJECT,
77                                             "GtkAccelGroup",
78                                             &object_info, 0);
79     }
80
81   return object_type;
82 }
83
84 static gboolean
85 accel_activate_accumulator (GSignalInvocationHint *ihint,
86                             GValue                *return_accu,
87                             const GValue          *handler_return,
88                             gpointer               data)
89 {
90   gboolean continue_emission;
91   gboolean handler_val;
92
93   /* handler returns whether the accelerator was handled */
94   handler_val = g_value_get_boolean (handler_return);
95
96   /* record that as result for this emission */
97   g_value_set_boolean (return_accu, handler_val);
98
99   /* don't continue if accelerator was handled */
100   continue_emission = !handler_val;
101
102   return continue_emission;
103 }
104
105 static void
106 gtk_accel_group_class_init (GtkAccelGroupClass *class)
107 {
108   GObjectClass *object_class = G_OBJECT_CLASS (class);
109
110   parent_class = g_type_class_peek_parent (class);
111
112   quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
113
114   object_class->finalize = gtk_accel_group_finalize;
115
116   class->accel_changed = NULL;
117   signal_accel_activate = g_signal_new ("accel_activate",
118                                         G_OBJECT_CLASS_TYPE (class),
119                                         G_SIGNAL_DETAILED,
120                                         0,
121                                         accel_activate_accumulator, NULL,
122                                         _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
123                                         G_TYPE_BOOLEAN, 3, G_TYPE_OBJECT, G_TYPE_UINT, GDK_TYPE_MODIFIER_TYPE);
124   signal_accel_changed = g_signal_new ("accel_changed",
125                                        G_OBJECT_CLASS_TYPE (class),
126                                        G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
127                                        G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
128                                        NULL, NULL,
129                                        _gtk_marshal_VOID__UINT_FLAGS_BOXED,
130                                        G_TYPE_NONE, 3, G_TYPE_UINT, GDK_TYPE_MODIFIER_TYPE, G_TYPE_CLOSURE);
131 }
132
133 static void
134 gtk_accel_group_finalize (GObject *object)
135 {
136   GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
137   guint i;
138   
139   for (i = 0; i < accel_group->n_accels; i++)
140     {
141       GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
142
143       if (entry->accel_path_quark)
144         {
145           const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
146
147           _gtk_accel_map_remove_group (accel_path, accel_group);
148         }
149     }
150
151   g_free (accel_group->priv_accels);
152
153   G_OBJECT_CLASS (parent_class)->finalize (object);
154 }
155
156 static void
157 gtk_accel_group_init (GtkAccelGroup *accel_group)
158 {
159   accel_group->lock_count = 0;
160   accel_group->modifier_mask = gtk_accelerator_get_default_mod_mask ();
161   accel_group->acceleratables = NULL;
162   accel_group->n_accels = 0;
163   accel_group->priv_accels = NULL;
164 }
165
166 /**
167  * gtk_accel_group_new:
168  * @returns: a new #GtkAccelGroup object
169  * 
170  * Creates a new #GtkAccelGroup. 
171  */
172 GtkAccelGroup*
173 gtk_accel_group_new (void)
174 {
175   return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
176 }
177
178 static void
179 accel_group_weak_ref_detach (GSList  *free_list,
180                              GObject *stale_object)
181 {
182   GSList *slist;
183   
184   for (slist = free_list; slist; slist = slist->next)
185     {
186       GtkAccelGroup *accel_group;
187       
188       accel_group = slist->data;
189       accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
190       g_object_unref (accel_group);
191     }
192   g_slist_free (free_list);
193   g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
194 }
195
196 void
197 _gtk_accel_group_attach (GtkAccelGroup *accel_group,
198                          GObject       *object)
199 {
200   GSList *slist;
201   
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);
205   
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);
209   if (slist)
210     g_object_weak_unref (object,
211                          (GWeakNotify) accel_group_weak_ref_detach,
212                          slist);
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,
217                      slist);
218 }
219
220 void
221 _gtk_accel_group_detach (GtkAccelGroup *accel_group,
222                          GObject       *object)
223 {
224   GSList *slist;
225   
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);
229   
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,
234                        slist);
235   slist = g_slist_remove (slist, accel_group);
236   g_object_set_qdata (object, quark_acceleratable_groups, slist);
237   if (slist)
238     g_object_weak_ref (object,
239                        (GWeakNotify) accel_group_weak_ref_detach,
240                        slist);
241   g_object_unref (accel_group);
242 }
243
244 /**
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
248  *
249  * Gets a list of all accel groups which are attached to @object.
250  */
251 GSList*
252 gtk_accel_groups_from_object (GObject *object)
253 {
254   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
255   
256   return g_object_get_qdata (object, quark_acceleratable_groups);
257 }
258
259 /**
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.
266  *
267  * Finds the first entry in an accelerator group for which 
268  * @find_func returns %TRUE and returns its #GtkAccelKey.
269  *
270  */
271 GtkAccelKey*
272 gtk_accel_group_find (GtkAccelGroup  *accel_group,
273                       gboolean (*find_func) (GtkAccelKey *key,
274                                              GClosure    *closure,
275                                              gpointer     data),
276                       gpointer        data)
277 {
278   GtkAccelKey *key = NULL;
279   guint i;
280
281   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
282   g_return_val_if_fail (find_func != NULL, NULL);
283
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,
288                    data))
289       {
290         key = &accel_group->priv_accels[i].key;
291         break;
292       }
293   g_object_unref (accel_group);
294
295   return key;
296 }
297
298 /**
299  * gtk_accel_group_lock:
300  * @accel_group: a #GtkAccelGroup
301  * 
302  * Locks the given accelerator group.
303  *
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.
307  *
308  * If called more than once, @accel_group remains locked until
309  * gtk_accel_group_unlock() has been called an equivalent number
310  * of times.
311  */
312 void
313 gtk_accel_group_lock (GtkAccelGroup *accel_group)
314 {
315   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
316   
317   accel_group->lock_count += 1;
318 }
319
320 /**
321  * gtk_accel_group_unlock:
322  * @accel_group: a #GtkAccelGroup
323  * 
324  * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
325  */
326 void
327 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
328 {
329   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
330   g_return_if_fail (accel_group->lock_count > 0);
331
332   accel_group->lock_count -= 1;
333 }
334
335 static void
336 accel_closure_invalidate (gpointer  data,
337                           GClosure *closure)
338 {
339   GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
340
341   gtk_accel_group_disconnect (accel_group, closure);
342 }
343
344 static int
345 bsearch_compare_accels (const void *d1,
346                         const void *d2)
347 {
348   const GtkAccelGroupEntry *entry1 = d1;
349   const GtkAccelGroupEntry *entry2 = d2;
350
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;
353   else
354     return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
355 }
356
357 static void
358 quick_accel_add (GtkAccelGroup  *accel_group,
359                  guint           accel_key,
360                  GdkModifierType accel_mods,
361                  GtkAccelFlags   accel_flags,
362                  GClosure       *closure,
363                  GQuark          path_quark)
364 {
365   guint pos, i = accel_group->n_accels++;
366   GtkAccelGroupEntry key;
367
368   /* find position */
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)
373       break;
374
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);
385   
386   /* handle closure invalidation and reverse lookups */
387   g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
388
389   /* get accel path notification */
390   if (path_quark)
391     _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
392
393   /* connect and notify changed */
394   if (accel_key)
395     {
396       gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
397       GQuark accel_quark = g_quark_from_string (accel_name);
398
399       g_free (accel_name);
400       
401       /* setup handler */
402       g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
403       
404       /* and notify */
405       g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
406     }
407 }
408
409 static void
410 quick_accel_remove (GtkAccelGroup      *accel_group,
411                     GtkAccelGroupEntry *entry)
412 {
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;
418
419   /* quark for notification */
420   if (accel_key)
421     {
422       gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
423
424       accel_quark = g_quark_from_string (accel_name);
425       g_free (accel_name);
426     }
427
428   /* clean up closure invalidate notification and disconnect */
429   g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
430   if (accel_quark)
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);
438
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]));
443
444   /* and notify */
445   if (accel_quark)
446     g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
447
448   /* remove quick_accel_add() refcount */
449   g_closure_unref (closure);
450 }
451
452 static GtkAccelGroupEntry*
453 quick_accel_find (GtkAccelGroup  *accel_group,
454                   guint           accel_key,
455                   GdkModifierType accel_mods,
456                   guint          *count_p)
457 {
458   GtkAccelGroupEntry *entry;
459   GtkAccelGroupEntry key;
460
461   if (!accel_group->n_accels)
462     return NULL;
463
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);
468   
469   if (!entry)
470     return NULL;
471
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)
476       break;
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)
481       break;
482   return entry;
483 }
484
485 /**
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
492  *
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.
497  *
498  * The signature used for the @closure is that of #GtkAccelGroupActivate.
499  * 
500  * Note that, due to implementation details, a single closure can only be
501  * connected to one accelerator group.
502  */
503 void
504 gtk_accel_group_connect (GtkAccelGroup  *accel_group,
505                          guint           accel_key,
506                          GdkModifierType accel_mods,
507                          GtkAccelFlags   accel_flags,
508                          GClosure       *closure)
509 {
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);
514
515   g_object_ref (accel_group);
516   if (!closure->is_invalid)
517     quick_accel_add (accel_group,
518                      gdk_keyval_to_lower (accel_key),
519                      accel_mods, accel_flags, closure, 0);
520   g_object_unref (accel_group);
521 }
522
523 /**
524  * gtk_accel_group_connect_by_path:
525  * @accel_group:      the accelerator group to install an accelerator in
526  * @accel_path:       path used for determining key and modifiers.
527  * @closure:          closure to be executed upon accelerator activation
528  *
529  * Installs an accelerator in this group, using an accelerator path to look
530  * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
531  * When @accel_group is being activated in response to a call to
532  * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
533  * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
534  * for the path.
535  *
536  * The signature used for the @closure is that of #GtkAccelGroupActivate.
537  */
538 void
539 gtk_accel_group_connect_by_path (GtkAccelGroup  *accel_group,
540                                  const gchar    *accel_path,
541                                  GClosure       *closure)
542 {
543   guint accel_key = 0;
544   GdkModifierType accel_mods = 0;
545   GtkAccelKey key;
546
547   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
548   g_return_if_fail (closure != NULL);
549   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
550
551   if (closure->is_invalid)
552     return;
553
554   g_object_ref (accel_group);
555
556   if (gtk_accel_map_lookup_entry (accel_path, &key))
557     {
558       accel_key = gdk_keyval_to_lower (key.accel_key);
559       accel_mods = key.accel_mods;
560     }
561
562   quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
563                    g_quark_from_string (accel_path));
564
565   g_object_unref (accel_group);
566 }
567
568 /**
569  * gtk_accel_group_disconnect:
570  * @accel_group: the accelerator group to remove an accelerator from
571  * @closure:     the closure to remove from this accelerator group
572  * @returns:     %TRUE if the closure was found and got disconnected
573  *
574  * Removes an accelerator previously installed through
575  * gtk_accel_group_connect().
576  */
577 gboolean
578 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
579                             GClosure      *closure)
580 {
581   guint i;
582
583   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
584
585   for (i = 0; i < accel_group->n_accels; i++)
586     if (accel_group->priv_accels[i].closure == closure)
587       {
588         g_object_ref (accel_group);
589         quick_accel_remove (accel_group, accel_group->priv_accels + i);
590         g_object_unref (accel_group);
591         return TRUE;
592       }
593   return FALSE;
594 }
595
596 /**
597  * gtk_accel_group_disconnect_key:
598  * @accel_group:      the accelerator group to install an accelerator in
599  * @accel_key:        key value of the accelerator
600  * @accel_mods:       modifier combination of the accelerator
601  * @returns:          %TRUE if there was an accelerator which could be 
602  *                    removed, %FALSE otherwise
603  *
604  * Removes an accelerator previously installed through
605  * gtk_accel_group_connect().
606  */
607 gboolean
608 gtk_accel_group_disconnect_key (GtkAccelGroup  *accel_group,
609                                 guint           accel_key,
610                                 GdkModifierType accel_mods)
611 {
612   GtkAccelGroupEntry *entries;
613   GSList *slist, *clist = NULL;
614   gboolean removed_one = FALSE;
615   guint n;
616
617   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
618
619   g_object_ref (accel_group);
620   
621   accel_key = gdk_keyval_to_lower (accel_key);
622   entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
623   while (n--)
624     {
625       GClosure *closure = g_closure_ref (entries[n].closure);
626
627       clist = g_slist_prepend (clist, closure);
628     }
629
630   for (slist = clist; slist; slist = slist->next)
631     {
632       GClosure *closure = slist->data;
633
634       removed_one |= gtk_accel_group_disconnect (accel_group, closure);
635       g_closure_unref (closure);
636     }
637   g_slist_free (clist);
638
639   g_object_unref (accel_group);
640
641   return removed_one;
642 }
643
644 void
645 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
646                             GQuark         accel_path_quark)
647 {
648   GSList *slist, *clist = NULL;
649   guint i;
650
651   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
652
653   g_object_ref (accel_group);
654
655   for (i = 0; i < accel_group->n_accels; i++)
656     if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
657       {
658         GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
659
660         clist = g_slist_prepend (clist, closure);
661       }
662
663   for (slist = clist; slist; slist = slist->next)
664     {
665       GClosure *closure = slist->data;
666
667       gtk_accel_group_disconnect (accel_group, closure);
668       gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
669       g_closure_unref (closure);
670     }
671   g_slist_free (clist);
672
673   g_object_unref (accel_group);
674 }
675
676 /**
677  * gtk_accel_group_query:
678  * @accel_group:      the accelerator group to query
679  * @accel_key:        key value of the accelerator
680  * @accel_mods:       modifier combination of the accelerator
681  * @n_entries:        location to return the number of entries found, or %NULL
682  * @returns:          an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed. 
683  *
684  * Queries an accelerator group for all entries matching @accel_key and 
685  * @accel_mods.
686  */
687 GtkAccelGroupEntry*
688 gtk_accel_group_query (GtkAccelGroup  *accel_group,
689                        guint           accel_key,
690                        GdkModifierType accel_mods,
691                        guint          *n_entries)
692 {
693   GtkAccelGroupEntry *entries;
694   guint n;
695
696   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
697
698   entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
699
700   if (n_entries)
701     *n_entries = entries ? n : 0;
702
703   return entries;
704 }
705
706 /**
707  * gtk_accel_group_from_accel_closure:
708  * @closure: a #GClosure
709  * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
710  *
711  * Finds the #GtkAccelGroup to which @closure is connected; 
712  * see gtk_accel_group_connect().
713  */
714 GtkAccelGroup*
715 gtk_accel_group_from_accel_closure (GClosure *closure)
716 {
717   guint i;
718
719   g_return_val_if_fail (closure != NULL, NULL);
720
721   /* a few remarks on what we do here. in general, we need a way to reverse lookup
722    * accel_groups from closures that are being used in accel groups. this could
723    * be done e.g via a hashtable. it is however cheaper (memory wise) to just
724    * use the invalidation notifier on the closure itself (which we need to install
725    * anyway), that contains the accel group as data which, besides needing to peek
726    * a bit at closure internals, works just as good.
727    */
728   for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
729     if (closure->notifiers[i].notify == accel_closure_invalidate)
730       return closure->notifiers[i].data;
731
732   return NULL;
733 }
734
735 gboolean
736 _gtk_accel_group_activate (GtkAccelGroup  *accel_group,
737                            GQuark          accel_quark,
738                            GObject        *acceleratable,
739                            guint           accel_key,
740                            GdkModifierType accel_mods)
741 {
742   gboolean was_handled;
743
744   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
745
746   was_handled = FALSE;
747   g_signal_emit (accel_group, signal_accel_activate, accel_quark,
748                  acceleratable, accel_key, accel_mods, &was_handled);
749
750   return was_handled;
751 }
752
753 /**
754  * gtk_accel_groups_activate:
755  * @object:        the #GObject, usually a #GtkWindow, on which
756  *                 to activate the accelerator.
757  * @accel_key:     accelerator keyval from a key event
758  * @accel_mods:    keyboard state mask from a key event
759  * @returns:       %TRUE if the accelerator was handled, %FALSE otherwise
760  * 
761  * Finds the first accelerator in any #GtkAccelGroup attached
762  * to @object that matches @accel_key and @accel_mods, and
763  * activates that accelerator.
764  * If an accelerator was activated and handled this keypress, %TRUE
765  * is returned.
766  */
767 gboolean
768 gtk_accel_groups_activate (GObject        *object,
769                            guint           accel_key,
770                            GdkModifierType accel_mods)
771 {
772   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
773   
774   if (gtk_accelerator_valid (accel_key, accel_mods))
775     {
776       gchar *accel_name;
777       GQuark accel_quark;
778       GSList *slist;
779
780       accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
781       accel_quark = g_quark_from_string (accel_name);
782       g_free (accel_name);
783       
784       for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
785         if (_gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
786           return TRUE;
787     }
788   
789   return FALSE;
790 }
791
792 /**
793  * gtk_accelerator_valid:
794  * @keyval:    a GDK keyval
795  * @modifiers: modifier mask
796  * @returns:   %TRUE if the accelerator is valid
797  * 
798  * Determines whether a given keyval and modifier mask constitute
799  * a valid keyboard accelerator. For example, the #GDK_a keyval
800  * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
801  * But, you can't, for instance, use the #GDK_Control_L keyval
802  * as an accelerator.
803  */
804 gboolean
805 gtk_accelerator_valid (guint              keyval,
806                        GdkModifierType    modifiers)
807 {
808   static const guint invalid_accelerator_vals[] = {
809     GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
810     GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
811     GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
812     GDK_ISO_Level3_Shift, GDK_ISO_Next_Group, GDK_ISO_Prev_Group,
813     GDK_ISO_First_Group, GDK_ISO_Last_Group,
814     GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
815     GDK_Scroll_Lock, GDK_Sys_Req, 
816     GDK_Tab, GDK_ISO_Left_Tab, GDK_KP_Tab,
817     GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
818     GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
819     GDK_Terminate_Server, GDK_AudibleBell_Enable,
820     0
821   };
822   static const guint invalid_unmodified_vals[] = {
823     GDK_Up, GDK_Down, GDK_Left, GDK_Right,
824     GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right,
825     0
826   };
827   const guint *ac_val;
828
829   modifiers &= GDK_MODIFIER_MASK;
830     
831   if (keyval <= 0xFF)
832     return keyval >= 0x20;
833
834   ac_val = invalid_accelerator_vals;
835   while (*ac_val)
836     {
837       if (keyval == *ac_val++)
838         return FALSE;
839     }
840
841   if (!modifiers)
842     {
843       ac_val = invalid_unmodified_vals;
844       while (*ac_val)
845         {
846           if (keyval == *ac_val++)
847             return FALSE;
848         }
849     }
850   
851   return TRUE;
852 }
853
854 static inline gboolean
855 is_alt (const gchar *string)
856 {
857   return ((string[0] == '<') &&
858           (string[1] == 'a' || string[1] == 'A') &&
859           (string[2] == 'l' || string[2] == 'L') &&
860           (string[3] == 't' || string[3] == 'T') &&
861           (string[4] == '>'));
862 }
863
864 static inline gboolean
865 is_ctl (const gchar *string)
866 {
867   return ((string[0] == '<') &&
868           (string[1] == 'c' || string[1] == 'C') &&
869           (string[2] == 't' || string[2] == 'T') &&
870           (string[3] == 'l' || string[3] == 'L') &&
871           (string[4] == '>'));
872 }
873
874 static inline gboolean
875 is_modx (const gchar *string)
876 {
877   return ((string[0] == '<') &&
878           (string[1] == 'm' || string[1] == 'M') &&
879           (string[2] == 'o' || string[2] == 'O') &&
880           (string[3] == 'd' || string[3] == 'D') &&
881           (string[4] >= '1' && string[4] <= '5') &&
882           (string[5] == '>'));
883 }
884
885 static inline gboolean
886 is_ctrl (const gchar *string)
887 {
888   return ((string[0] == '<') &&
889           (string[1] == 'c' || string[1] == 'C') &&
890           (string[2] == 't' || string[2] == 'T') &&
891           (string[3] == 'r' || string[3] == 'R') &&
892           (string[4] == 'l' || string[4] == 'L') &&
893           (string[5] == '>'));
894 }
895
896 static inline gboolean
897 is_shft (const gchar *string)
898 {
899   return ((string[0] == '<') &&
900           (string[1] == 's' || string[1] == 'S') &&
901           (string[2] == 'h' || string[2] == 'H') &&
902           (string[3] == 'f' || string[3] == 'F') &&
903           (string[4] == 't' || string[4] == 'T') &&
904           (string[5] == '>'));
905 }
906
907 static inline gboolean
908 is_shift (const gchar *string)
909 {
910   return ((string[0] == '<') &&
911           (string[1] == 's' || string[1] == 'S') &&
912           (string[2] == 'h' || string[2] == 'H') &&
913           (string[3] == 'i' || string[3] == 'I') &&
914           (string[4] == 'f' || string[4] == 'F') &&
915           (string[5] == 't' || string[5] == 'T') &&
916           (string[6] == '>'));
917 }
918
919 static inline gboolean
920 is_control (const gchar *string)
921 {
922   return ((string[0] == '<') &&
923           (string[1] == 'c' || string[1] == 'C') &&
924           (string[2] == 'o' || string[2] == 'O') &&
925           (string[3] == 'n' || string[3] == 'N') &&
926           (string[4] == 't' || string[4] == 'T') &&
927           (string[5] == 'r' || string[5] == 'R') &&
928           (string[6] == 'o' || string[6] == 'O') &&
929           (string[7] == 'l' || string[7] == 'L') &&
930           (string[8] == '>'));
931 }
932
933 static inline gboolean
934 is_release (const gchar *string)
935 {
936   return ((string[0] == '<') &&
937           (string[1] == 'r' || string[1] == 'R') &&
938           (string[2] == 'e' || string[2] == 'E') &&
939           (string[3] == 'l' || string[3] == 'L') &&
940           (string[4] == 'e' || string[4] == 'E') &&
941           (string[5] == 'a' || string[5] == 'A') &&
942           (string[6] == 's' || string[6] == 'S') &&
943           (string[7] == 'e' || string[7] == 'E') &&
944           (string[8] == '>'));
945 }
946
947 /**
948  * gtk_accelerator_parse:
949  * @accelerator:      string representing an accelerator
950  * @accelerator_key:  return location for accelerator keyval
951  * @accelerator_mods: return location for accelerator modifier mask
952  *
953  * Parses a string representing an accelerator. The
954  * format looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
955  * "&lt;Release&gt;z" (the last one is for key release).
956  * The parser is fairly liberal and allows lower or upper case,
957  * and also abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
958  *
959  * If the parse fails, @accelerator_key and @accelerator_mods will
960  * be set to 0 (zero).
961  */
962 void
963 gtk_accelerator_parse (const gchar     *accelerator,
964                        guint           *accelerator_key,
965                        GdkModifierType *accelerator_mods)
966 {
967   guint keyval;
968   GdkModifierType mods;
969   gint len;
970   
971   if (accelerator_key)
972     *accelerator_key = 0;
973   if (accelerator_mods)
974     *accelerator_mods = 0;
975   g_return_if_fail (accelerator != NULL);
976   
977   keyval = 0;
978   mods = 0;
979   len = strlen (accelerator);
980   while (len)
981     {
982       if (*accelerator == '<')
983         {
984           if (len >= 9 && is_release (accelerator))
985             {
986               accelerator += 9;
987               len -= 9;
988               mods |= GDK_RELEASE_MASK;
989             }
990           else if (len >= 9 && is_control (accelerator))
991             {
992               accelerator += 9;
993               len -= 9;
994               mods |= GDK_CONTROL_MASK;
995             }
996           else if (len >= 7 && is_shift (accelerator))
997             {
998               accelerator += 7;
999               len -= 7;
1000               mods |= GDK_SHIFT_MASK;
1001             }
1002           else if (len >= 6 && is_shft (accelerator))
1003             {
1004               accelerator += 6;
1005               len -= 6;
1006               mods |= GDK_SHIFT_MASK;
1007             }
1008           else if (len >= 6 && is_ctrl (accelerator))
1009             {
1010               accelerator += 6;
1011               len -= 6;
1012               mods |= GDK_CONTROL_MASK;
1013             }
1014           else if (len >= 6 && is_modx (accelerator))
1015             {
1016               static const guint mod_vals[] = {
1017                 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1018                 GDK_MOD4_MASK, GDK_MOD5_MASK
1019               };
1020
1021               len -= 6;
1022               accelerator += 4;
1023               mods |= mod_vals[*accelerator - '1'];
1024               accelerator += 2;
1025             }
1026           else if (len >= 5 && is_ctl (accelerator))
1027             {
1028               accelerator += 5;
1029               len -= 5;
1030               mods |= GDK_CONTROL_MASK;
1031             }
1032           else if (len >= 5 && is_alt (accelerator))
1033             {
1034               accelerator += 5;
1035               len -= 5;
1036               mods |= GDK_MOD1_MASK;
1037             }
1038           else
1039             {
1040               gchar last_ch;
1041               
1042               last_ch = *accelerator;
1043               while (last_ch && last_ch != '>')
1044                 {
1045                   last_ch = *accelerator;
1046                   accelerator += 1;
1047                   len -= 1;
1048                 }
1049             }
1050         }
1051       else
1052         {
1053           keyval = gdk_keyval_from_name (accelerator);
1054           accelerator += len;
1055           len -= len;
1056         }
1057     }
1058   
1059   if (accelerator_key)
1060     *accelerator_key = gdk_keyval_to_lower (keyval);
1061   if (accelerator_mods)
1062     *accelerator_mods = mods;
1063 }
1064
1065 /**
1066  * gtk_accelerator_name:
1067  * @accelerator_key:  accelerator keyval
1068  * @accelerator_mods: accelerator modifier mask
1069  * @returns:          a newly-allocated accelerator name
1070  * 
1071  * Converts an accelerator keyval and modifier mask
1072  * into a string parseable by gtk_accelerator_parse().
1073  * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1074  * this function returns "&lt;Control&gt;q". 
1075  *
1076  * The caller of this function must free the returned string.
1077  */
1078 gchar*
1079 gtk_accelerator_name (guint           accelerator_key,
1080                       GdkModifierType accelerator_mods)
1081 {
1082   static const gchar text_release[] = "<Release>";
1083   static const gchar text_shift[] = "<Shift>";
1084   static const gchar text_control[] = "<Control>";
1085   static const gchar text_mod1[] = "<Alt>";
1086   static const gchar text_mod2[] = "<Mod2>";
1087   static const gchar text_mod3[] = "<Mod3>";
1088   static const gchar text_mod4[] = "<Mod4>";
1089   static const gchar text_mod5[] = "<Mod5>";
1090   guint l;
1091   gchar *keyval_name;
1092   gchar *accelerator;
1093
1094   accelerator_mods &= GDK_MODIFIER_MASK;
1095
1096   keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1097   if (!keyval_name)
1098     keyval_name = "";
1099
1100   l = 0;
1101   if (accelerator_mods & GDK_RELEASE_MASK)
1102     l += sizeof (text_release) - 1;
1103   if (accelerator_mods & GDK_SHIFT_MASK)
1104     l += sizeof (text_shift) - 1;
1105   if (accelerator_mods & GDK_CONTROL_MASK)
1106     l += sizeof (text_control) - 1;
1107   if (accelerator_mods & GDK_MOD1_MASK)
1108     l += sizeof (text_mod1) - 1;
1109   if (accelerator_mods & GDK_MOD2_MASK)
1110     l += sizeof (text_mod2) - 1;
1111   if (accelerator_mods & GDK_MOD3_MASK)
1112     l += sizeof (text_mod3) - 1;
1113   if (accelerator_mods & GDK_MOD4_MASK)
1114     l += sizeof (text_mod4) - 1;
1115   if (accelerator_mods & GDK_MOD5_MASK)
1116     l += sizeof (text_mod5) - 1;
1117   l += strlen (keyval_name);
1118
1119   accelerator = g_new (gchar, l + 1);
1120
1121   l = 0;
1122   accelerator[l] = 0;
1123   if (accelerator_mods & GDK_RELEASE_MASK)
1124     {
1125       strcpy (accelerator + l, text_release);
1126       l += sizeof (text_release) - 1;
1127     }
1128   if (accelerator_mods & GDK_SHIFT_MASK)
1129     {
1130       strcpy (accelerator + l, text_shift);
1131       l += sizeof (text_shift) - 1;
1132     }
1133   if (accelerator_mods & GDK_CONTROL_MASK)
1134     {
1135       strcpy (accelerator + l, text_control);
1136       l += sizeof (text_control) - 1;
1137     }
1138   if (accelerator_mods & GDK_MOD1_MASK)
1139     {
1140       strcpy (accelerator + l, text_mod1);
1141       l += sizeof (text_mod1) - 1;
1142     }
1143   if (accelerator_mods & GDK_MOD2_MASK)
1144     {
1145       strcpy (accelerator + l, text_mod2);
1146       l += sizeof (text_mod2) - 1;
1147     }
1148   if (accelerator_mods & GDK_MOD3_MASK)
1149     {
1150       strcpy (accelerator + l, text_mod3);
1151       l += sizeof (text_mod3) - 1;
1152     }
1153   if (accelerator_mods & GDK_MOD4_MASK)
1154     {
1155       strcpy (accelerator + l, text_mod4);
1156       l += sizeof (text_mod4) - 1;
1157     }
1158   if (accelerator_mods & GDK_MOD5_MASK)
1159     {
1160       strcpy (accelerator + l, text_mod5);
1161       l += sizeof (text_mod5) - 1;
1162     }
1163   strcpy (accelerator + l, keyval_name);
1164
1165   return accelerator;
1166 }
1167
1168 /**
1169  * gtk_accelerator_set_default_mod_mask:
1170  * @default_mod_mask: accelerator modifier mask
1171  *
1172  * Sets the modifiers that will be considered significant for keyboard
1173  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1174  * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1175  * Other modifiers will by default be ignored by #GtkAccelGroup.
1176  * You must include at least the three default modifiers in any
1177  * value you pass to this function.
1178  *
1179  * The default mod mask should be changed on application startup,
1180  * before using any accelerator groups.
1181  */
1182 void
1183 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1184 {
1185   default_accel_mod_mask = (default_mod_mask & GDK_MODIFIER_MASK) |
1186     (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK);
1187 }
1188
1189 /**
1190  * gtk_accelerator_get_default_mod_mask:
1191  * @returns: the default accelerator modifier mask
1192  *
1193  * Gets the value set by gtk_accelerator_set_default_mod_mask().
1194  */
1195 guint
1196 gtk_accelerator_get_default_mod_mask (void)
1197 {
1198   return default_accel_mod_mask;
1199 }