]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccelgroup.c
70db40aee51e5d9194022e1d35ab4f0a9b9f9bae
[~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 <ctype.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36
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);
41
42
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 |
49                                                    GDK_CONTROL_MASK |
50                                                    GDK_MOD1_MASK);
51
52
53 /* --- functions --- */
54 /**
55  * gtk_accel_group_get_type:
56  * @returns: the type ID for accelerator groups.
57  */
58 GType
59 gtk_accel_group_get_type (void)
60 {
61   static GType object_type = 0;
62
63   if (!object_type)
64     {
65       static const GTypeInfo object_info = {
66         sizeof (GtkAccelGroupClass),
67         (GBaseInitFunc) NULL,
68         (GBaseFinalizeFunc) NULL,
69         (GClassInitFunc) gtk_accel_group_class_init,
70         NULL,   /* clas_finalize */
71         NULL,   /* class_data */
72         sizeof (GtkAccelGroup),
73         0,      /* n_preallocs */
74         (GInstanceInitFunc) gtk_accel_group_init,
75       };
76
77       object_type = g_type_register_static (G_TYPE_OBJECT,
78                                             "GtkAccelGroup",
79                                             &object_info, 0);
80     }
81
82   return object_type;
83 }
84
85 static gboolean
86 accel_activate_accumulator (GSignalInvocationHint *ihint,
87                             GValue                *return_accu,
88                             const GValue          *handler_return,
89                             gpointer               data)
90 {
91   gboolean continue_emission;
92   gboolean handler_val;
93
94   /* handler returns whether the accelerator was handled */
95   handler_val = g_value_get_boolean (handler_return);
96
97   /* record that as result for this emission */
98   g_value_set_boolean (return_accu, handler_val);
99
100   /* don't continue if accelerator was handled */
101   continue_emission = !handler_val;
102
103   return continue_emission;
104 }
105
106 static void
107 gtk_accel_group_class_init (GtkAccelGroupClass *class)
108 {
109   GObjectClass *object_class = G_OBJECT_CLASS (class);
110
111   parent_class = g_type_class_peek_parent (class);
112
113   quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");
114
115   object_class->finalize = gtk_accel_group_finalize;
116
117   class->accel_changed = NULL;
118   signal_accel_activate = g_signal_new ("accel_activate",
119                                         G_OBJECT_CLASS_TYPE (class),
120                                         G_SIGNAL_DETAILED,
121                                         0,
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),
129                                        NULL, NULL,
130                                        _gtk_marshal_VOID__UINT_UINT_BOXED,
131                                        G_TYPE_NONE, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_CLOSURE);
132 }
133
134 static void
135 gtk_accel_group_finalize (GObject *object)
136 {
137   GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
138   guint i;
139   
140   for (i = 0; i < accel_group->n_accels; i++)
141     {
142       GtkAccelGroupEntry *entry = &accel_group->priv_accels[i];
143
144       if (entry->accel_path_quark)
145         {
146           const gchar *accel_path = g_quark_to_string (entry[i].accel_path_quark);
147
148           _gtk_accel_map_remove_group (accel_path, accel_group);
149         }
150     }
151
152   g_free (accel_group->priv_accels);
153
154   G_OBJECT_CLASS (parent_class)->finalize (object);
155 }
156
157 static void
158 gtk_accel_group_init (GtkAccelGroup *accel_group)
159 {
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;
165 }
166
167 /**
168  * gtk_accel_group_new:
169  * @returns: a new #GtkAccelGroup object
170  * 
171  * Creates a new #GtkAccelGroup. 
172  */
173 GtkAccelGroup*
174 gtk_accel_group_new (void)
175 {
176   return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
177 }
178
179 static void
180 accel_group_weak_ref_detach (GSList  *free_list,
181                              GObject *stale_object)
182 {
183   GSList *slist;
184   
185   for (slist = free_list; slist; slist = slist->next)
186     {
187       GtkAccelGroup *accel_group;
188       
189       accel_group = slist->data;
190       accel_group->acceleratables = g_slist_remove (accel_group->acceleratables, stale_object);
191       g_object_unref (accel_group);
192     }
193   g_slist_free (free_list);
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, accel_key, accel_mods, accel_flags, closure, 0);
518   g_object_unref (accel_group);
519 }
520
521 /**
522  * gtk_accel_group_connect_by_path:
523  * @accel_group:      the accelerator group to install an accelerator in
524  * @accel_path:       path used for determining key and modifiers.
525  * @closure:          closure to be executed upon accelerator activation
526  *
527  * Installs an accelerator in this group, using an accelerator path to look
528  * up the appropriate key and modifiers (see gtk_accel_map_add_entry()).
529  * When @accel_group is being activated in response to a call to
530  * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
531  * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
532  * for the path.
533  *
534  * The signature used for the @closure is that of #GtkAccelGroupActivate.
535  */
536 void
537 gtk_accel_group_connect_by_path (GtkAccelGroup  *accel_group,
538                                  const gchar    *accel_path,
539                                  GClosure       *closure)
540 {
541   guint accel_key = 0;
542   GdkModifierType accel_mods = 0;
543   GtkAccelKey key;
544
545   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
546   g_return_if_fail (closure != NULL);
547   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
548
549   if (closure->is_invalid)
550     return;
551
552   g_object_ref (accel_group);
553
554   if (gtk_accel_map_lookup_entry (accel_path, &key))
555     {
556       accel_key = key.accel_key;
557       accel_mods = key.accel_mods;
558     }
559
560   quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
561                    g_quark_from_string (accel_path));
562
563   g_object_unref (accel_group);
564 }
565
566 /**
567  * gtk_accel_group_disconnect:
568  * @accel_group: the accelerator group to remove an accelerator from
569  * @closure:     the closure to remove from this accelerator group
570  * @returns:     %TRUE if the closure was found and got disconnected
571  *
572  * Removes an accelerator previously installed through
573  * gtk_accel_group_connect().
574  */
575 gboolean
576 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
577                             GClosure      *closure)
578 {
579   guint i;
580
581   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
582
583   for (i = 0; i < accel_group->n_accels; i++)
584     if (accel_group->priv_accels[i].closure == closure)
585       {
586         g_object_ref (accel_group);
587         quick_accel_remove (accel_group, accel_group->priv_accels + i);
588         g_object_unref (accel_group);
589         return TRUE;
590       }
591   return FALSE;
592 }
593
594 /**
595  * gtk_accel_group_disconnect_key:
596  * @accel_group:      the accelerator group to install an accelerator in
597  * @accel_key:        key value of the accelerator
598  * @accel_mods:       modifier combination of the accelerator
599  * @returns:          %TRUE if there was an accelerator which could be 
600  *                    removed, %FALSE otherwise
601  *
602  * Removes an accelerator previously installed through
603  * gtk_accel_group_connect().
604  */
605 gboolean
606 gtk_accel_group_disconnect_key (GtkAccelGroup  *accel_group,
607                                 guint           accel_key,
608                                 GdkModifierType accel_mods)
609 {
610   GtkAccelGroupEntry *entries;
611   GSList *slist, *clist = NULL;
612   gboolean removed_one = FALSE;
613   guint n;
614
615   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
616
617   g_object_ref (accel_group);
618   
619   entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
620   while (n--)
621     {
622       GClosure *closure = g_closure_ref (entries[n].closure);
623
624       clist = g_slist_prepend (clist, closure);
625     }
626
627   for (slist = clist; slist; slist = slist->next)
628     {
629       GClosure *closure = slist->data;
630
631       removed_one |= gtk_accel_group_disconnect (accel_group, closure);
632       g_closure_unref (closure);
633     }
634   g_slist_free (clist);
635
636   g_object_unref (accel_group);
637
638   return removed_one;
639 }
640
641 void
642 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
643                             GQuark         accel_path_quark)
644 {
645   GSList *slist, *clist = NULL;
646   guint i;
647
648   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
649
650   g_object_ref (accel_group);
651
652   for (i = 0; i < accel_group->n_accels; i++)
653     if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
654       {
655         GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
656
657         clist = g_slist_prepend (clist, closure);
658       }
659
660   for (slist = clist; slist; slist = slist->next)
661     {
662       GClosure *closure = slist->data;
663
664       gtk_accel_group_disconnect (accel_group, closure);
665       gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
666       g_closure_unref (closure);
667     }
668   g_slist_free (clist);
669
670   g_object_unref (accel_group);
671 }
672
673 /**
674  * gtk_accel_group_query:
675  * @accel_group:      the accelerator group to query
676  * @accel_key:        key value of the accelerator
677  * @accel_mods:       modifier combination of the accelerator
678  * @n_entries:        location to return the number of entries found, or %NULL
679  * @returns:          an array of @n_entries #GtkAccelGroupEntry elements, or %NULL. The array is owned by GTK+ and must not be freed. 
680  *
681  * Queries an accelerator group for all entries matching @accel_key and 
682  * @accel_mods.
683  */
684 GtkAccelGroupEntry*
685 gtk_accel_group_query (GtkAccelGroup  *accel_group,
686                        guint           accel_key,
687                        GdkModifierType accel_mods,
688                        guint          *n_entries)
689 {
690   GtkAccelGroupEntry *entries;
691   guint n;
692
693   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
694
695   entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
696
697   if (n_entries)
698     *n_entries = entries ? n : 0;
699
700   return entries;
701 }
702
703 /**
704  * gtk_accel_group_from_accel_closure:
705  * @closure: a #GClosure
706  * @returns: the #GtkAccelGroup to which @closure is connected, or %NULL.
707  *
708  * Finds the #GtkAccelGroup to which @closure is connected; 
709  * see gtk_accel_group_connect().
710  */
711 GtkAccelGroup*
712 gtk_accel_group_from_accel_closure (GClosure *closure)
713 {
714   guint i;
715
716   g_return_val_if_fail (closure != NULL, NULL);
717
718   /* a few remarks on wat we do here. in general, we need a way to reverse lookup
719    * accel_groups from closures that are being used in accel groups. this could
720    * be done e.g via a hashtable. it is however cheaper (memory wise) to just
721    * use the invalidation notifier on the closure itself (which we need to install
722    * anyway), that contains the accel group as data which, besides needing to peek
723    * a bit at closure internals, works just as good.
724    */
725   for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
726     if (closure->notifiers[i].notify == accel_closure_invalidate)
727       return closure->notifiers[i].data;
728
729   return NULL;
730 }
731
732 gboolean
733 _gtk_accel_group_activate (GtkAccelGroup  *accel_group,
734                            GQuark          accel_quark,
735                            GObject        *acceleratable,
736                            guint           accel_key,
737                            GdkModifierType accel_mods)
738 {
739   gboolean was_handled;
740
741   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
742
743   was_handled = FALSE;
744   g_signal_emit (accel_group, signal_accel_activate, accel_quark,
745                  acceleratable, accel_key, accel_mods, &was_handled);
746
747   return was_handled;
748 }
749
750 /**
751  * gtk_accel_groups_activate:
752  * @object:        the #GObject, usually a #GtkWindow, on which
753  *                 to activate the accelerator.
754  * @accel_key:     accelerator keyval from a key event
755  * @accel_mods:    keyboard state mask from a key event
756  * @returns:       %TRUE if the accelerator was handled, %FALSE otherwise
757  * 
758  * Finds the first accelerator in any #GtkAccelGroup attached
759  * to @object that matches @accel_key and @accel_mods, and
760  * activates that accelerator.
761  * If an accelerator was activated and handled this keypress, %TRUE
762  * is returned.
763  */
764 gboolean
765 gtk_accel_groups_activate (GObject        *object,
766                            guint           accel_key,
767                            GdkModifierType accel_mods)
768 {
769   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
770   
771   if (gtk_accelerator_valid (accel_key, accel_mods))
772     {
773       gchar *accel_name;
774       GQuark accel_quark;
775       GSList *slist;
776
777       accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
778       accel_quark = g_quark_from_string (accel_name);
779       g_free (accel_name);
780       
781       for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
782         if (_gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
783           return TRUE;
784     }
785   
786   return FALSE;
787 }
788
789 /**
790  * gtk_accelerator_valid:
791  * @keyval:    a GDK keyval
792  * @modifiers: modifier mask
793  * @returns:   %TRUE if the accelerator is valid
794  * 
795  * Determines whether a given keyval and modifier mask constitute
796  * a valid keyboard accelerator. For example, the #GDK_a keyval
797  * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
798  * But by default (see gtk_accelerator_set_default_mod_mask()) you
799  * cannot use the NumLock key as an accelerator modifier.
800  */
801 gboolean
802 gtk_accelerator_valid (guint              keyval,
803                        GdkModifierType    modifiers)
804 {
805   static const guint invalid_accelerator_vals[] = {
806     GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
807     GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
808     GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
809     GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
810     GDK_Scroll_Lock, GDK_Sys_Req, 
811     GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
812     GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
813     GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
814     GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
815     GDK_Terminate_Server, GDK_AudibleBell_Enable,
816     0
817   };
818   const guint *ac_val;
819
820   modifiers &= GDK_MODIFIER_MASK;
821     
822   if (keyval <= 0xFF)
823     return keyval >= 0x20;
824
825   ac_val = invalid_accelerator_vals;
826   while (*ac_val)
827     {
828       if (keyval == *ac_val++)
829         return FALSE;
830     }
831
832   return TRUE;
833 }
834
835 static inline gboolean
836 is_alt (const gchar *string)
837 {
838   return ((string[0] == '<') &&
839           (string[1] == 'a' || string[1] == 'A') &&
840           (string[2] == 'l' || string[2] == 'L') &&
841           (string[3] == 't' || string[3] == 'T') &&
842           (string[4] == '>'));
843 }
844
845 static inline gboolean
846 is_ctl (const gchar *string)
847 {
848   return ((string[0] == '<') &&
849           (string[1] == 'c' || string[1] == 'C') &&
850           (string[2] == 't' || string[2] == 'T') &&
851           (string[3] == 'l' || string[3] == 'L') &&
852           (string[4] == '>'));
853 }
854
855 static inline gboolean
856 is_modx (const gchar *string)
857 {
858   return ((string[0] == '<') &&
859           (string[1] == 'm' || string[1] == 'M') &&
860           (string[2] == 'o' || string[2] == 'O') &&
861           (string[3] == 'd' || string[3] == 'D') &&
862           (string[4] >= '1' && string[4] <= '5') &&
863           (string[5] == '>'));
864 }
865
866 static inline gboolean
867 is_ctrl (const gchar *string)
868 {
869   return ((string[0] == '<') &&
870           (string[1] == 'c' || string[1] == 'C') &&
871           (string[2] == 't' || string[2] == 'T') &&
872           (string[3] == 'r' || string[3] == 'R') &&
873           (string[4] == 'l' || string[4] == 'L') &&
874           (string[5] == '>'));
875 }
876
877 static inline gboolean
878 is_shft (const gchar *string)
879 {
880   return ((string[0] == '<') &&
881           (string[1] == 's' || string[1] == 'S') &&
882           (string[2] == 'h' || string[2] == 'H') &&
883           (string[3] == 'f' || string[3] == 'F') &&
884           (string[4] == 't' || string[4] == 'T') &&
885           (string[5] == '>'));
886 }
887
888 static inline gboolean
889 is_shift (const gchar *string)
890 {
891   return ((string[0] == '<') &&
892           (string[1] == 's' || string[1] == 'S') &&
893           (string[2] == 'h' || string[2] == 'H') &&
894           (string[3] == 'i' || string[3] == 'I') &&
895           (string[4] == 'f' || string[4] == 'F') &&
896           (string[5] == 't' || string[5] == 'T') &&
897           (string[6] == '>'));
898 }
899
900 static inline gboolean
901 is_control (const gchar *string)
902 {
903   return ((string[0] == '<') &&
904           (string[1] == 'c' || string[1] == 'C') &&
905           (string[2] == 'o' || string[2] == 'O') &&
906           (string[3] == 'n' || string[3] == 'N') &&
907           (string[4] == 't' || string[4] == 'T') &&
908           (string[5] == 'r' || string[5] == 'R') &&
909           (string[6] == 'o' || string[6] == 'O') &&
910           (string[7] == 'l' || string[7] == 'L') &&
911           (string[8] == '>'));
912 }
913
914 static inline gboolean
915 is_release (const gchar *string)
916 {
917   return ((string[0] == '<') &&
918           (string[1] == 'r' || string[1] == 'R') &&
919           (string[2] == 'e' || string[2] == 'E') &&
920           (string[3] == 'l' || string[3] == 'L') &&
921           (string[4] == 'e' || string[4] == 'E') &&
922           (string[5] == 'a' || string[5] == 'A') &&
923           (string[6] == 's' || string[6] == 'S') &&
924           (string[7] == 'e' || string[7] == 'E') &&
925           (string[8] == '>'));
926 }
927
928 /**
929  * gtk_accelerator_parse:
930  * @accelerator:      string representing an accelerator
931  * @accelerator_key:  return location for accelerator keyval
932  * @accelerator_mods: return location for accelerator modifier mask
933  *
934  * Parses a string representing an accelerator. The
935  * format looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
936  * "&lt;Release&gt;z" (the last one is for key release).
937  * The parser is fairly liberal and allows lower or upper case,
938  * and also abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
939  *
940  * If the parse fails, @accelerator_key and @accelerator_mods will
941  * be set to 0 (zero).
942  */
943 void
944 gtk_accelerator_parse (const gchar     *accelerator,
945                        guint           *accelerator_key,
946                        GdkModifierType *accelerator_mods)
947 {
948   guint keyval;
949   GdkModifierType mods;
950   gint len;
951   
952   if (accelerator_key)
953     *accelerator_key = 0;
954   if (accelerator_mods)
955     *accelerator_mods = 0;
956   g_return_if_fail (accelerator != NULL);
957   
958   keyval = 0;
959   mods = 0;
960   len = strlen (accelerator);
961   while (len)
962     {
963       if (*accelerator == '<')
964         {
965           if (len >= 9 && is_release (accelerator))
966             {
967               accelerator += 9;
968               len -= 9;
969               mods |= GDK_RELEASE_MASK;
970             }
971           else if (len >= 9 && is_control (accelerator))
972             {
973               accelerator += 9;
974               len -= 9;
975               mods |= GDK_CONTROL_MASK;
976             }
977           else if (len >= 7 && is_shift (accelerator))
978             {
979               accelerator += 7;
980               len -= 7;
981               mods |= GDK_SHIFT_MASK;
982             }
983           else if (len >= 6 && is_shft (accelerator))
984             {
985               accelerator += 6;
986               len -= 6;
987               mods |= GDK_SHIFT_MASK;
988             }
989           else if (len >= 6 && is_ctrl (accelerator))
990             {
991               accelerator += 6;
992               len -= 6;
993               mods |= GDK_CONTROL_MASK;
994             }
995           else if (len >= 6 && is_modx (accelerator))
996             {
997               static const guint mod_vals[] = {
998                 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
999                 GDK_MOD4_MASK, GDK_MOD5_MASK
1000               };
1001
1002               len -= 6;
1003               accelerator += 4;
1004               mods |= mod_vals[*accelerator - '1'];
1005               accelerator += 2;
1006             }
1007           else if (len >= 5 && is_ctl (accelerator))
1008             {
1009               accelerator += 5;
1010               len -= 5;
1011               mods |= GDK_CONTROL_MASK;
1012             }
1013           else if (len >= 5 && is_alt (accelerator))
1014             {
1015               accelerator += 5;
1016               len -= 5;
1017               mods |= GDK_MOD1_MASK;
1018             }
1019           else
1020             {
1021               gchar last_ch;
1022               
1023               last_ch = *accelerator;
1024               while (last_ch && last_ch != '>')
1025                 {
1026                   last_ch = *accelerator;
1027                   accelerator += 1;
1028                   len -= 1;
1029                 }
1030             }
1031         }
1032       else
1033         {
1034           keyval = gdk_keyval_from_name (accelerator);
1035           accelerator += len;
1036           len -= len;
1037         }
1038     }
1039   
1040   if (accelerator_key)
1041     *accelerator_key = gdk_keyval_to_lower (keyval);
1042   if (accelerator_mods)
1043     *accelerator_mods = mods;
1044 }
1045
1046 /**
1047  * gtk_accelerator_name:
1048  * @accelerator_key:  accelerator keyval
1049  * @accelerator_mods: accelerator modifier mask
1050  * @returns:          a newly-allocated accelerator name
1051  * 
1052  * Converts an accelerator keyval and modifier mask
1053  * into a string parseable by gtk_accelerator_parse().
1054  * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1055  * this function returns "&lt;Control&gt;q". 
1056  *
1057  * The caller of this function must free the returned string.
1058  */
1059 gchar*
1060 gtk_accelerator_name (guint           accelerator_key,
1061                       GdkModifierType accelerator_mods)
1062 {
1063   static const gchar text_release[] = "<Release>";
1064   static const gchar text_shift[] = "<Shift>";
1065   static const gchar text_control[] = "<Control>";
1066   static const gchar text_mod1[] = "<Alt>";
1067   static const gchar text_mod2[] = "<Mod2>";
1068   static const gchar text_mod3[] = "<Mod3>";
1069   static const gchar text_mod4[] = "<Mod4>";
1070   static const gchar text_mod5[] = "<Mod5>";
1071   guint l;
1072   gchar *keyval_name;
1073   gchar *accelerator;
1074
1075   accelerator_mods &= GDK_MODIFIER_MASK;
1076
1077   keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1078   if (!keyval_name)
1079     keyval_name = "";
1080
1081   l = 0;
1082   if (accelerator_mods & GDK_RELEASE_MASK)
1083     l += sizeof (text_release) - 1;
1084   if (accelerator_mods & GDK_SHIFT_MASK)
1085     l += sizeof (text_shift) - 1;
1086   if (accelerator_mods & GDK_CONTROL_MASK)
1087     l += sizeof (text_control) - 1;
1088   if (accelerator_mods & GDK_MOD1_MASK)
1089     l += sizeof (text_mod1) - 1;
1090   if (accelerator_mods & GDK_MOD2_MASK)
1091     l += sizeof (text_mod2) - 1;
1092   if (accelerator_mods & GDK_MOD3_MASK)
1093     l += sizeof (text_mod3) - 1;
1094   if (accelerator_mods & GDK_MOD4_MASK)
1095     l += sizeof (text_mod4) - 1;
1096   if (accelerator_mods & GDK_MOD5_MASK)
1097     l += sizeof (text_mod5) - 1;
1098   l += strlen (keyval_name);
1099
1100   accelerator = g_new (gchar, l + 1);
1101
1102   l = 0;
1103   accelerator[l] = 0;
1104   if (accelerator_mods & GDK_RELEASE_MASK)
1105     {
1106       strcpy (accelerator + l, text_release);
1107       l += sizeof (text_release) - 1;
1108     }
1109   if (accelerator_mods & GDK_SHIFT_MASK)
1110     {
1111       strcpy (accelerator + l, text_shift);
1112       l += sizeof (text_shift) - 1;
1113     }
1114   if (accelerator_mods & GDK_CONTROL_MASK)
1115     {
1116       strcpy (accelerator + l, text_control);
1117       l += sizeof (text_control) - 1;
1118     }
1119   if (accelerator_mods & GDK_MOD1_MASK)
1120     {
1121       strcpy (accelerator + l, text_mod1);
1122       l += sizeof (text_mod1) - 1;
1123     }
1124   if (accelerator_mods & GDK_MOD2_MASK)
1125     {
1126       strcpy (accelerator + l, text_mod2);
1127       l += sizeof (text_mod2) - 1;
1128     }
1129   if (accelerator_mods & GDK_MOD3_MASK)
1130     {
1131       strcpy (accelerator + l, text_mod3);
1132       l += sizeof (text_mod3) - 1;
1133     }
1134   if (accelerator_mods & GDK_MOD4_MASK)
1135     {
1136       strcpy (accelerator + l, text_mod4);
1137       l += sizeof (text_mod4) - 1;
1138     }
1139   if (accelerator_mods & GDK_MOD5_MASK)
1140     {
1141       strcpy (accelerator + l, text_mod5);
1142       l += sizeof (text_mod5) - 1;
1143     }
1144   strcpy (accelerator + l, keyval_name);
1145
1146   return accelerator;
1147 }
1148
1149 /**
1150  * gtk_accelerator_set_default_mod_mask:
1151  * @default_mod_mask: accelerator modifier mask
1152  *
1153  * Sets the modifiers that will be considered significant for keyboard
1154  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1155  * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1156  * Other modifiers will by default be ignored by #GtkAccelGroup.
1157  *
1158  * The default mod mask should be changed on application startup,
1159  * before using any accelerator groups.
1160  */
1161 void
1162 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1163 {
1164   default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1165 }
1166
1167 /**
1168  * gtk_accelerator_get_default_mod_mask:
1169  * @returns: the default accelerator modifier mask
1170  *
1171  * Gets the value set by gtk_accelerator_set_default_mod_mask().
1172  */
1173 guint
1174 gtk_accelerator_get_default_mod_mask (void)
1175 {
1176   return default_accel_mod_mask;
1177 }