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