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