]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccelgroup.c
a5029432232bebae99b7f1a082e5a8f4033fecb3
[~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 GSList*
245 gtk_accel_groups_from_object (GObject *object)
246 {
247   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
248   
249   return g_object_get_qdata (object, quark_acceleratable_groups);
250 }
251
252 GtkAccelKey*
253 gtk_accel_group_find (GtkAccelGroup  *accel_group,
254                       gboolean (*find_func) (GtkAccelKey *key,
255                                              GClosure    *closure,
256                                              gpointer     data),
257                       gpointer        data)
258 {
259   GtkAccelKey *key = NULL;
260   guint i;
261
262   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
263   g_return_val_if_fail (find_func != NULL, NULL);
264
265   g_object_ref (accel_group);
266   for (i = 0; i < accel_group->n_accels; i++)
267     if (find_func (&accel_group->priv_accels[i].key,
268                    accel_group->priv_accels[i].closure,
269                    data))
270       {
271         key = &accel_group->priv_accels[i].key;
272         break;
273       }
274   g_object_unref (accel_group);
275
276   return key;
277 }
278
279 /**
280  * gtk_accel_group_lock
281  * @accel_group: a #GtkAccelGroup
282  * 
283  * Locking an acelerator group prevents the accelerators contained
284  * within it to be changed during runtime. Refer to
285  * gtk_accel_map_change_entry() about runtime accelerator changes.
286  *
287  * If called more than once, @accel_group remains locked until
288  * gtk_accel_group_unlock() has been called an equivalent number
289  * of times.
290  */
291 void
292 gtk_accel_group_lock (GtkAccelGroup *accel_group)
293 {
294   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
295   
296   accel_group->lock_count += 1;
297 }
298
299 /**
300  * gtk_accel_group_unlock
301  * @accel_group: a #GtkAccelGroup
302  * 
303  * This function undoes the last call to gtk_accel_group_lock()
304  * on this @accel_group.
305  */
306 void
307 gtk_accel_group_unlock (GtkAccelGroup *accel_group)
308 {
309   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
310   g_return_if_fail (accel_group->lock_count > 0);
311
312   accel_group->lock_count -= 1;
313 }
314
315 static void
316 accel_closure_invalidate (gpointer  data,
317                           GClosure *closure)
318 {
319   GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);
320
321   gtk_accel_group_disconnect (accel_group, closure);
322 }
323
324 static int
325 bsearch_compare_accels (const void *d1,
326                         const void *d2)
327 {
328   const GtkAccelGroupEntry *entry1 = d1;
329   const GtkAccelGroupEntry *entry2 = d2;
330
331   if (entry1->key.accel_key == entry2->key.accel_key)
332     return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
333   else
334     return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
335 }
336
337 static void
338 quick_accel_add (GtkAccelGroup  *accel_group,
339                  guint           accel_key,
340                  GdkModifierType accel_mods,
341                  GtkAccelFlags   accel_flags,
342                  GClosure       *closure,
343                  GQuark          path_quark)
344 {
345   guint pos, i = accel_group->n_accels++;
346   GtkAccelGroupEntry key;
347
348   /* find position */
349   key.key.accel_key = accel_key;
350   key.key.accel_mods = accel_mods;
351   for (pos = 0; pos < i; pos++)
352     if (bsearch_compare_accels (&key, accel_group->priv_accels + pos) < 0)
353       break;
354
355   /* insert at position, ref closure */
356   accel_group->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv_accels, accel_group->n_accels);
357   g_memmove (accel_group->priv_accels + pos + 1, accel_group->priv_accels + pos,
358              (i - pos) * sizeof (accel_group->priv_accels[0]));
359   accel_group->priv_accels[pos].key.accel_key = accel_key;
360   accel_group->priv_accels[pos].key.accel_mods = accel_mods;
361   accel_group->priv_accels[pos].key.accel_flags = accel_flags;
362   accel_group->priv_accels[pos].closure = g_closure_ref (closure);
363   accel_group->priv_accels[pos].accel_path_quark = path_quark;
364   g_closure_sink (closure);
365   
366   /* handle closure invalidation and reverse lookups */
367   g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);
368
369   /* get accel path notification */
370   if (path_quark)
371     _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);
372
373   /* connect and notify changed */
374   if (accel_key)
375     {
376       gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
377       GQuark accel_quark = g_quark_from_string (accel_name);
378
379       g_free (accel_name);
380       
381       /* setup handler */
382       g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
383       
384       /* and notify */
385       g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
386     }
387 }
388
389 static void
390 quick_accel_remove (GtkAccelGroup      *accel_group,
391                     GtkAccelGroupEntry *entry)
392 {
393   guint pos = entry - accel_group->priv_accels;
394   GQuark accel_quark = 0;
395   guint accel_key = entry->key.accel_key;
396   GdkModifierType accel_mods = entry->key.accel_mods;
397   GClosure *closure = entry->closure;
398
399   /* quark for notification */
400   if (accel_key)
401     {
402       gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
403
404       accel_quark = g_quark_from_string (accel_name);
405       g_free (accel_name);
406     }
407
408   /* clean up closure invalidate notification and disconnect */
409   g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
410   if (accel_quark)
411     g_signal_handlers_disconnect_matched (accel_group,
412                                           G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
413                                           signal_accel_activate, accel_quark,
414                                           closure, NULL, NULL);
415   /* clean up accel path notification */
416   if (entry->accel_path_quark)
417     _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);
418
419   /* physically remove */
420   accel_group->n_accels -= 1;
421   g_memmove (entry, entry + 1,
422              (accel_group->n_accels - pos) * sizeof (accel_group->priv_accels[0]));
423
424   /* and notify */
425   if (accel_quark)
426     g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
427
428   /* remove quick_accel_add() refcount */
429   g_closure_unref (closure);
430 }
431
432 static GtkAccelGroupEntry*
433 quick_accel_find (GtkAccelGroup  *accel_group,
434                   guint           accel_key,
435                   GdkModifierType accel_mods,
436                   guint          *count_p)
437 {
438   GtkAccelGroupEntry *entry;
439   GtkAccelGroupEntry key;
440
441   if (!accel_group->n_accels)
442     return NULL;
443
444   key.key.accel_key = accel_key;
445   key.key.accel_mods = accel_mods;
446   entry = bsearch (&key, accel_group->priv_accels, accel_group->n_accels,
447                    sizeof (accel_group->priv_accels[0]), bsearch_compare_accels);
448   
449   if (!entry)
450     return NULL;
451
452   /* step back to the first member */
453   for (; entry > accel_group->priv_accels; entry--)
454     if (entry[-1].key.accel_key != accel_key ||
455         entry[-1].key.accel_mods != accel_mods)
456       break;
457   /* count equal members */
458   for (*count_p = 0; entry + *count_p < accel_group->priv_accels + accel_group->n_accels; (*count_p)++)
459     if (entry[*count_p].key.accel_key != accel_key ||
460         entry[*count_p].key.accel_mods != accel_mods)
461       break;
462   return entry;
463 }
464
465 /**
466  * gtk_accel_group_connect
467  * @accel_group:      the ccelerator group to install an accelerator in
468  * @accel_key:        key value of the accelerator
469  * @accel_mods:       modifier combination of the accelerator
470  * @accel_flags:      a flag mask to configure this accelerator
471  * @closure:          closure to be executed upon accelerator activation
472  *
473  * Install an accelerator in this group. When @accel_group is being activated
474  * in response to a call to gtk_accel_groups_activate(), @closure will be
475  * invoked if the @accel_key and @accel_mods from gtk_accel_groups_activate()
476  * match those of this connection.
477  * The signature used for the @closure is that of #GtkAccelGroupActivate.
478  * Note that, due to implementation details, a single closure can only be
479  * connected to one accelerator group.
480  */
481 void
482 gtk_accel_group_connect (GtkAccelGroup  *accel_group,
483                          guint           accel_key,
484                          GdkModifierType accel_mods,
485                          GtkAccelFlags   accel_flags,
486                          GClosure       *closure)
487 {
488   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
489   g_return_if_fail (closure != NULL);
490   g_return_if_fail (accel_key > 0);
491   g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
492
493   g_object_ref (accel_group);
494   if (!closure->is_invalid)
495     quick_accel_add (accel_group, accel_key, accel_mods, accel_flags, closure, 0);
496   g_object_unref (accel_group);
497 }
498
499 /**
500  * gtk_accel_group_connect_by_path
501  * @accel_group:      the ccelerator group to install an accelerator in
502  * @accel_path:       path used for determining key and modifiers.
503  * @closure:          closure to be executed upon accelerator activation
504  *
505  * Install an accelerator in this group, using a accelerator path to look
506  * up the appropriate key and modifiers. (See gtk_accel_map_add_entry())
507  * When @accel_group is being activated in response to a call to
508  * gtk_accel_groups_activate(), @closure will be invoked if the @accel_key and
509  * @accel_mods from gtk_accel_groups_activate() match the key and modifiers
510  * for the path.
511  * The signature used for the @closure is that of #GtkAccelGroupActivate.
512  */
513 void
514 gtk_accel_group_connect_by_path (GtkAccelGroup  *accel_group,
515                                  const gchar    *accel_path,
516                                  GClosure       *closure)
517 {
518   guint accel_key = 0;
519   GdkModifierType accel_mods = 0;
520   GtkAccelKey key;
521
522   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
523   g_return_if_fail (closure != NULL);
524   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
525
526   if (closure->is_invalid)
527     return;
528
529   g_object_ref (accel_group);
530
531   if (gtk_accel_map_lookup_entry (accel_path, &key))
532     {
533       accel_key = key.accel_key;
534       accel_mods = key.accel_mods;
535     }
536
537   quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
538                    g_quark_from_string (accel_path));
539
540   g_object_unref (accel_group);
541 }
542
543 /**
544  * gtk_accel_group_disconnect
545  * @accel_group: the accelerator group to remove an accelerator from
546  * @closure:     the closure to remove from this accelerator group
547  * @returns:     %TRUE if the closure was found and got disconnected
548  *
549  * Remove an accelerator previously installed through
550  * gtk_accel_group_connect().
551  */
552 gboolean
553 gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
554                             GClosure      *closure)
555 {
556   guint i;
557
558   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
559
560   for (i = 0; i < accel_group->n_accels; i++)
561     if (accel_group->priv_accels[i].closure == closure)
562       {
563         g_object_ref (accel_group);
564         quick_accel_remove (accel_group, accel_group->priv_accels + i);
565         g_object_unref (accel_group);
566         return TRUE;
567       }
568   return FALSE;
569 }
570
571 /**
572  * gtk_accel_group_disconnect_key
573  * @accel_group:      the ccelerator group to install an accelerator in
574  * @accel_key:        key value of the accelerator
575  * @accel_mods:       modifier combination of the accelerator
576  * @returns:          %TRUE if there was an accelerator which could be removed, %FALSE otherwise
577  *
578  * Remove an accelerator previously installed through
579  * gtk_accel_group_connect().
580  */
581 gboolean
582 gtk_accel_group_disconnect_key (GtkAccelGroup  *accel_group,
583                                 guint           accel_key,
584                                 GdkModifierType accel_mods)
585 {
586   GtkAccelGroupEntry *entries;
587   GSList *slist, *clist = NULL;
588   gboolean removed_one = FALSE;
589   guint n;
590
591   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
592
593   g_object_ref (accel_group);
594   
595   entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
596   while (n--)
597     {
598       GClosure *closure = g_closure_ref (entries[n].closure);
599
600       clist = g_slist_prepend (clist, closure);
601     }
602
603   for (slist = clist; slist; slist = slist->next)
604     {
605       GClosure *closure = slist->data;
606
607       removed_one |= gtk_accel_group_disconnect (accel_group, closure);
608       g_closure_unref (closure);
609     }
610   g_slist_free (clist);
611
612   g_object_unref (accel_group);
613
614   return removed_one;
615 }
616
617 void
618 _gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
619                             GQuark         accel_path_quark)
620 {
621   GSList *slist, *clist = NULL;
622   guint i;
623
624   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
625
626   g_object_ref (accel_group);
627
628   for (i = 0; i < accel_group->n_accels; i++)
629     if (accel_group->priv_accels[i].accel_path_quark == accel_path_quark)
630       {
631         GClosure *closure = g_closure_ref (accel_group->priv_accels[i].closure);
632
633         clist = g_slist_prepend (clist, closure);
634       }
635
636   for (slist = clist; slist; slist = slist->next)
637     {
638       GClosure *closure = slist->data;
639
640       gtk_accel_group_disconnect (accel_group, closure);
641       gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
642       g_closure_unref (closure);
643     }
644   g_slist_free (clist);
645
646   g_object_unref (accel_group);
647 }
648
649 GtkAccelGroupEntry*
650 gtk_accel_group_query (GtkAccelGroup  *accel_group,
651                        guint           accel_key,
652                        GdkModifierType accel_mods,
653                        guint          *n_entries)
654 {
655   GtkAccelGroupEntry *entries;
656   guint n;
657
658   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
659
660   entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
661
662   if (n_entries)
663     *n_entries = entries ? n : 0;
664
665   return entries;
666 }
667
668 GtkAccelGroup*
669 gtk_accel_group_from_accel_closure (GClosure *closure)
670 {
671   guint i;
672
673   g_return_val_if_fail (closure != NULL, NULL);
674
675   /* a few remarks on wat we do here. in general, we need a way to reverse lookup
676    * accel_groups from closures that are being used in accel groups. this could
677    * be done e.g via a hashtable. it is however cheaper (memory wise) to just
678    * use the invalidation notifier on the closure itself (which we need to install
679    * anyway), that contains the accel group as data which, besides needing to peek
680    * a bit at closure internals, works just as good.
681    */
682   for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
683     if (closure->notifiers[i].notify == accel_closure_invalidate)
684       return closure->notifiers[i].data;
685
686   return NULL;
687 }
688
689 gboolean
690 _gtk_accel_group_activate (GtkAccelGroup  *accel_group,
691                            GQuark          accel_quark,
692                            GObject        *acceleratable,
693                            guint           accel_key,
694                            GdkModifierType accel_mods)
695 {
696   gboolean was_handled;
697
698   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
699
700   was_handled = FALSE;
701   g_signal_emit (accel_group, signal_accel_activate, accel_quark,
702                  acceleratable, accel_key, accel_mods, &was_handled);
703
704   return was_handled;
705 }
706
707 /**
708  * gtk_accel_groups_activate:
709  * @object:        the #Gobject, usually a #GtkWindow, on which
710  *                 to activate the accelerator.
711  * @accel_key:     accelerator keyval from a key event
712  * @accel_mods:    keyboard state mask from a key event
713  * @returns:       %TRUE if the accelerator was handled, %FALSE otherwise
714  * 
715  * Finds the first accelerator in any #GtkAccelGroup attached
716  * to @object that matches @accel_key and @accel_mods, and
717  * activates that accelerator.
718  * If an accelerator was activated and handled this keypress, %TRUE
719  * is returned.
720  */
721 gboolean
722 gtk_accel_groups_activate (GObject        *object,
723                            guint           accel_key,
724                            GdkModifierType accel_mods)
725 {
726   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
727   
728   if (gtk_accelerator_valid (accel_key, accel_mods))
729     {
730       gchar *accel_name;
731       GQuark accel_quark;
732       GSList *slist;
733
734       accel_name = gtk_accelerator_name (accel_key, accel_mods);
735       accel_quark = g_quark_from_string (accel_name);
736       g_free (accel_name);
737       
738       for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
739         if (_gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
740           return TRUE;
741     }
742   
743   return FALSE;
744 }
745
746 /**
747  * gtk_accelerator_valid
748  * @keyval:    a GDK keyval
749  * @modifiers: modifier mask
750  * @returns:   %TRUE if the accelerator is valid
751  * 
752  * Determines whether a given keyval and modifier mask constitute
753  * a valid keyboard accelerator. For example, the GDK_a keyval
754  * plus GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
755  * But by default (see gtk_accelerator_set_default_mod_mask()) you
756  * cannot use the NumLock key as an accelerator modifier.
757  */
758 gboolean
759 gtk_accelerator_valid (guint              keyval,
760                        GdkModifierType    modifiers)
761 {
762   static const guint invalid_accelerator_vals[] = {
763     GDK_BackSpace, GDK_Delete, GDK_KP_Delete,
764     GDK_Shift_L, GDK_Shift_R, GDK_Shift_Lock, GDK_Caps_Lock, GDK_ISO_Lock,
765     GDK_Control_L, GDK_Control_R, GDK_Meta_L, GDK_Meta_R,
766     GDK_Alt_L, GDK_Alt_R, GDK_Super_L, GDK_Super_R, GDK_Hyper_L, GDK_Hyper_R,
767     GDK_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
768     GDK_Scroll_Lock, GDK_Sys_Req, 
769     GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
770     GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
771     GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
772     GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
773     GDK_Terminate_Server, GDK_AudibleBell_Enable,
774     0
775   };
776   const guint *ac_val;
777
778   modifiers &= GDK_MODIFIER_MASK;
779     
780   if (keyval <= 0xFF)
781     return keyval >= 0x20;
782
783   ac_val = invalid_accelerator_vals;
784   while (*ac_val)
785     {
786       if (keyval == *ac_val++)
787         return FALSE;
788     }
789
790   return TRUE;
791 }
792
793 static inline gboolean
794 is_alt (const gchar *string)
795 {
796   return ((string[0] == '<') &&
797           (string[1] == 'a' || string[1] == 'A') &&
798           (string[2] == 'l' || string[2] == 'L') &&
799           (string[3] == 't' || string[3] == 'T') &&
800           (string[4] == '>'));
801 }
802
803 static inline gboolean
804 is_ctl (const gchar *string)
805 {
806   return ((string[0] == '<') &&
807           (string[1] == 'c' || string[1] == 'C') &&
808           (string[2] == 't' || string[2] == 'T') &&
809           (string[3] == 'l' || string[3] == 'L') &&
810           (string[4] == '>'));
811 }
812
813 static inline gboolean
814 is_modx (const gchar *string)
815 {
816   return ((string[0] == '<') &&
817           (string[1] == 'm' || string[1] == 'M') &&
818           (string[2] == 'o' || string[2] == 'O') &&
819           (string[3] == 'd' || string[3] == 'D') &&
820           (string[4] >= '1' && string[4] <= '5') &&
821           (string[5] == '>'));
822 }
823
824 static inline gboolean
825 is_ctrl (const gchar *string)
826 {
827   return ((string[0] == '<') &&
828           (string[1] == 'c' || string[1] == 'C') &&
829           (string[2] == 't' || string[2] == 'T') &&
830           (string[3] == 'r' || string[3] == 'R') &&
831           (string[4] == 'l' || string[4] == 'L') &&
832           (string[5] == '>'));
833 }
834
835 static inline gboolean
836 is_shft (const gchar *string)
837 {
838   return ((string[0] == '<') &&
839           (string[1] == 's' || string[1] == 'S') &&
840           (string[2] == 'h' || string[2] == 'H') &&
841           (string[3] == 'f' || string[3] == 'F') &&
842           (string[4] == 't' || string[4] == 'T') &&
843           (string[5] == '>'));
844 }
845
846 static inline gboolean
847 is_shift (const gchar *string)
848 {
849   return ((string[0] == '<') &&
850           (string[1] == 's' || string[1] == 'S') &&
851           (string[2] == 'h' || string[2] == 'H') &&
852           (string[3] == 'i' || string[3] == 'I') &&
853           (string[4] == 'f' || string[4] == 'F') &&
854           (string[5] == 't' || string[5] == 'T') &&
855           (string[6] == '>'));
856 }
857
858 static inline gboolean
859 is_control (const gchar *string)
860 {
861   return ((string[0] == '<') &&
862           (string[1] == 'c' || string[1] == 'C') &&
863           (string[2] == 'o' || string[2] == 'O') &&
864           (string[3] == 'n' || string[3] == 'N') &&
865           (string[4] == 't' || string[4] == 'T') &&
866           (string[5] == 'r' || string[5] == 'R') &&
867           (string[6] == 'o' || string[6] == 'O') &&
868           (string[7] == 'l' || string[7] == 'L') &&
869           (string[8] == '>'));
870 }
871
872 static inline gboolean
873 is_release (const gchar *string)
874 {
875   return ((string[0] == '<') &&
876           (string[1] == 'r' || string[1] == 'R') &&
877           (string[2] == 'e' || string[2] == 'E') &&
878           (string[3] == 'l' || string[3] == 'L') &&
879           (string[4] == 'e' || string[4] == 'E') &&
880           (string[5] == 'a' || string[5] == 'A') &&
881           (string[6] == 's' || string[6] == 'S') &&
882           (string[7] == 'e' || string[7] == 'E') &&
883           (string[8] == '>'));
884 }
885
886 /**
887  * gtk_accelerator_parse
888  * @accelerator:      string representing an accelerator
889  * @accelerator_key:  return location for accelerator keyval
890  * @accelerator_mods: return location for accelerator modifier mask
891  *
892  * Parses a string representing an accelerator. The
893  * format looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
894  * "&lt;Release&gt;z" (the last one is for key release).
895  * The parser is fairly liberal and allows lower or upper case,
896  * and also abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
897  *
898  * If the parse fails, @accelerator_key and @accelerator_mods will
899  * be set to 0 (zero).
900  */
901 void
902 gtk_accelerator_parse (const gchar     *accelerator,
903                        guint           *accelerator_key,
904                        GdkModifierType *accelerator_mods)
905 {
906   guint keyval;
907   GdkModifierType mods;
908   gint len;
909   
910   if (accelerator_key)
911     *accelerator_key = 0;
912   if (accelerator_mods)
913     *accelerator_mods = 0;
914   g_return_if_fail (accelerator != NULL);
915   
916   keyval = 0;
917   mods = 0;
918   len = strlen (accelerator);
919   while (len)
920     {
921       if (*accelerator == '<')
922         {
923           if (len >= 9 && is_release (accelerator))
924             {
925               accelerator += 9;
926               len -= 9;
927               mods |= GDK_RELEASE_MASK;
928             }
929           else if (len >= 9 && is_control (accelerator))
930             {
931               accelerator += 9;
932               len -= 9;
933               mods |= GDK_CONTROL_MASK;
934             }
935           else if (len >= 7 && is_shift (accelerator))
936             {
937               accelerator += 7;
938               len -= 7;
939               mods |= GDK_SHIFT_MASK;
940             }
941           else if (len >= 6 && is_shft (accelerator))
942             {
943               accelerator += 6;
944               len -= 6;
945               mods |= GDK_SHIFT_MASK;
946             }
947           else if (len >= 6 && is_ctrl (accelerator))
948             {
949               accelerator += 6;
950               len -= 6;
951               mods |= GDK_CONTROL_MASK;
952             }
953           else if (len >= 6 && is_modx (accelerator))
954             {
955               static const guint mod_vals[] = {
956                 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
957                 GDK_MOD4_MASK, GDK_MOD5_MASK
958               };
959
960               len -= 6;
961               accelerator += 4;
962               mods |= mod_vals[*accelerator - '1'];
963               accelerator += 2;
964             }
965           else if (len >= 5 && is_ctl (accelerator))
966             {
967               accelerator += 5;
968               len -= 5;
969               mods |= GDK_CONTROL_MASK;
970             }
971           else if (len >= 5 && is_alt (accelerator))
972             {
973               accelerator += 5;
974               len -= 5;
975               mods |= GDK_MOD1_MASK;
976             }
977           else
978             {
979               gchar last_ch;
980               
981               last_ch = *accelerator;
982               while (last_ch && last_ch != '>')
983                 {
984                   last_ch = *accelerator;
985                   accelerator += 1;
986                   len -= 1;
987                 }
988             }
989         }
990       else
991         {
992           keyval = gdk_keyval_from_name (accelerator);
993           accelerator += len;
994           len -= len;
995         }
996     }
997   
998   if (accelerator_key)
999     *accelerator_key = gdk_keyval_to_lower (keyval);
1000   if (accelerator_mods)
1001     *accelerator_mods = mods;
1002 }
1003
1004 /**
1005  * gtk_accelerator_name
1006  * @accelerator_key:  accelerator keyval
1007  * @accelerator_mods: accelerator modifier mask
1008  * @returns:          a newly allocated accelerator name
1009  * 
1010  * Converts an accelerator keyval and modifier mask
1011  * into a string parseable by gtk_accelerator_parse().
1012  * For example, if you pass in GDK_q and GDK_CONTROL_MASK,
1013  * this function returns "&lt;Control&gt;q". 
1014  *
1015  * The caller of this function must free the returned string.
1016  */
1017 gchar*
1018 gtk_accelerator_name (guint           accelerator_key,
1019                       GdkModifierType accelerator_mods)
1020 {
1021   static const gchar text_release[] = "<Release>";
1022   static const gchar text_shift[] = "<Shift>";
1023   static const gchar text_control[] = "<Control>";
1024   static const gchar text_mod1[] = "<Alt>";
1025   static const gchar text_mod2[] = "<Mod2>";
1026   static const gchar text_mod3[] = "<Mod3>";
1027   static const gchar text_mod4[] = "<Mod4>";
1028   static const gchar text_mod5[] = "<Mod5>";
1029   guint l;
1030   gchar *keyval_name;
1031   gchar *accelerator;
1032
1033   accelerator_mods &= GDK_MODIFIER_MASK;
1034
1035   keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1036   if (!keyval_name)
1037     keyval_name = "";
1038
1039   l = 0;
1040   if (accelerator_mods & GDK_RELEASE_MASK)
1041     l += sizeof (text_release) - 1;
1042   if (accelerator_mods & GDK_SHIFT_MASK)
1043     l += sizeof (text_shift) - 1;
1044   if (accelerator_mods & GDK_CONTROL_MASK)
1045     l += sizeof (text_control) - 1;
1046   if (accelerator_mods & GDK_MOD1_MASK)
1047     l += sizeof (text_mod1) - 1;
1048   if (accelerator_mods & GDK_MOD2_MASK)
1049     l += sizeof (text_mod2) - 1;
1050   if (accelerator_mods & GDK_MOD3_MASK)
1051     l += sizeof (text_mod3) - 1;
1052   if (accelerator_mods & GDK_MOD4_MASK)
1053     l += sizeof (text_mod4) - 1;
1054   if (accelerator_mods & GDK_MOD5_MASK)
1055     l += sizeof (text_mod5) - 1;
1056   l += strlen (keyval_name);
1057
1058   accelerator = g_new (gchar, l + 1);
1059
1060   l = 0;
1061   accelerator[l] = 0;
1062   if (accelerator_mods & GDK_RELEASE_MASK)
1063     {
1064       strcpy (accelerator + l, text_release);
1065       l += sizeof (text_release) - 1;
1066     }
1067   if (accelerator_mods & GDK_SHIFT_MASK)
1068     {
1069       strcpy (accelerator + l, text_shift);
1070       l += sizeof (text_shift) - 1;
1071     }
1072   if (accelerator_mods & GDK_CONTROL_MASK)
1073     {
1074       strcpy (accelerator + l, text_control);
1075       l += sizeof (text_control) - 1;
1076     }
1077   if (accelerator_mods & GDK_MOD1_MASK)
1078     {
1079       strcpy (accelerator + l, text_mod1);
1080       l += sizeof (text_mod1) - 1;
1081     }
1082   if (accelerator_mods & GDK_MOD2_MASK)
1083     {
1084       strcpy (accelerator + l, text_mod2);
1085       l += sizeof (text_mod2) - 1;
1086     }
1087   if (accelerator_mods & GDK_MOD3_MASK)
1088     {
1089       strcpy (accelerator + l, text_mod3);
1090       l += sizeof (text_mod3) - 1;
1091     }
1092   if (accelerator_mods & GDK_MOD4_MASK)
1093     {
1094       strcpy (accelerator + l, text_mod4);
1095       l += sizeof (text_mod4) - 1;
1096     }
1097   if (accelerator_mods & GDK_MOD5_MASK)
1098     {
1099       strcpy (accelerator + l, text_mod5);
1100       l += sizeof (text_mod5) - 1;
1101     }
1102   strcpy (accelerator + l, keyval_name);
1103
1104   return accelerator;
1105 }
1106
1107 /**
1108  * gtk_accelerator_set_default_mod_mask
1109  * @default_mod_mask: accelerator modifier mask
1110  *
1111  * Sets the modifiers that will be considered significant for keyboard
1112  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1113  * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1114  * Other modifiers will by default be ignored by #GtkAccelGroup.
1115  *
1116  * The default mod mask should be changed on application startup,
1117  * before using any accelerator groups.
1118  */
1119 void
1120 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1121 {
1122   default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1123 }
1124
1125 /**
1126  * gtk_accelerator_get_default_mod_mask
1127  * @returns: the default accelerator modifier mask
1128  *
1129  * Gets the value set by gtk_accelerator_set_default_mod_mask().
1130  */
1131 guint
1132 gtk_accelerator_get_default_mod_mask (void)
1133 {
1134   return default_accel_mod_mask;
1135 }