]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccelgroup.c
& with the default mod mask so accels work when numlock is on.
[~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_BackSpace, GDK_Delete, GDK_KP_Delete,
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_Mode_switch, GDK_Num_Lock, GDK_Multi_key,
811     GDK_Scroll_Lock, GDK_Sys_Req, 
812     GDK_Up, GDK_Down, GDK_Left, GDK_Right, GDK_Tab, GDK_ISO_Left_Tab,
813     GDK_KP_Up, GDK_KP_Down, GDK_KP_Left, GDK_KP_Right, GDK_KP_Tab,
814     GDK_First_Virtual_Screen, GDK_Prev_Virtual_Screen,
815     GDK_Next_Virtual_Screen, GDK_Last_Virtual_Screen,
816     GDK_Terminate_Server, GDK_AudibleBell_Enable,
817     0
818   };
819   const guint *ac_val;
820
821   modifiers &= GDK_MODIFIER_MASK;
822     
823   if (keyval <= 0xFF)
824     return keyval >= 0x20;
825
826   ac_val = invalid_accelerator_vals;
827   while (*ac_val)
828     {
829       if (keyval == *ac_val++)
830         return FALSE;
831     }
832
833   return TRUE;
834 }
835
836 static inline gboolean
837 is_alt (const gchar *string)
838 {
839   return ((string[0] == '<') &&
840           (string[1] == 'a' || string[1] == 'A') &&
841           (string[2] == 'l' || string[2] == 'L') &&
842           (string[3] == 't' || string[3] == 'T') &&
843           (string[4] == '>'));
844 }
845
846 static inline gboolean
847 is_ctl (const gchar *string)
848 {
849   return ((string[0] == '<') &&
850           (string[1] == 'c' || string[1] == 'C') &&
851           (string[2] == 't' || string[2] == 'T') &&
852           (string[3] == 'l' || string[3] == 'L') &&
853           (string[4] == '>'));
854 }
855
856 static inline gboolean
857 is_modx (const gchar *string)
858 {
859   return ((string[0] == '<') &&
860           (string[1] == 'm' || string[1] == 'M') &&
861           (string[2] == 'o' || string[2] == 'O') &&
862           (string[3] == 'd' || string[3] == 'D') &&
863           (string[4] >= '1' && string[4] <= '5') &&
864           (string[5] == '>'));
865 }
866
867 static inline gboolean
868 is_ctrl (const gchar *string)
869 {
870   return ((string[0] == '<') &&
871           (string[1] == 'c' || string[1] == 'C') &&
872           (string[2] == 't' || string[2] == 'T') &&
873           (string[3] == 'r' || string[3] == 'R') &&
874           (string[4] == 'l' || string[4] == 'L') &&
875           (string[5] == '>'));
876 }
877
878 static inline gboolean
879 is_shft (const gchar *string)
880 {
881   return ((string[0] == '<') &&
882           (string[1] == 's' || string[1] == 'S') &&
883           (string[2] == 'h' || string[2] == 'H') &&
884           (string[3] == 'f' || string[3] == 'F') &&
885           (string[4] == 't' || string[4] == 'T') &&
886           (string[5] == '>'));
887 }
888
889 static inline gboolean
890 is_shift (const gchar *string)
891 {
892   return ((string[0] == '<') &&
893           (string[1] == 's' || string[1] == 'S') &&
894           (string[2] == 'h' || string[2] == 'H') &&
895           (string[3] == 'i' || string[3] == 'I') &&
896           (string[4] == 'f' || string[4] == 'F') &&
897           (string[5] == 't' || string[5] == 'T') &&
898           (string[6] == '>'));
899 }
900
901 static inline gboolean
902 is_control (const gchar *string)
903 {
904   return ((string[0] == '<') &&
905           (string[1] == 'c' || string[1] == 'C') &&
906           (string[2] == 'o' || string[2] == 'O') &&
907           (string[3] == 'n' || string[3] == 'N') &&
908           (string[4] == 't' || string[4] == 'T') &&
909           (string[5] == 'r' || string[5] == 'R') &&
910           (string[6] == 'o' || string[6] == 'O') &&
911           (string[7] == 'l' || string[7] == 'L') &&
912           (string[8] == '>'));
913 }
914
915 static inline gboolean
916 is_release (const gchar *string)
917 {
918   return ((string[0] == '<') &&
919           (string[1] == 'r' || string[1] == 'R') &&
920           (string[2] == 'e' || string[2] == 'E') &&
921           (string[3] == 'l' || string[3] == 'L') &&
922           (string[4] == 'e' || string[4] == 'E') &&
923           (string[5] == 'a' || string[5] == 'A') &&
924           (string[6] == 's' || string[6] == 'S') &&
925           (string[7] == 'e' || string[7] == 'E') &&
926           (string[8] == '>'));
927 }
928
929 /**
930  * gtk_accelerator_parse:
931  * @accelerator:      string representing an accelerator
932  * @accelerator_key:  return location for accelerator keyval
933  * @accelerator_mods: return location for accelerator modifier mask
934  *
935  * Parses a string representing an accelerator. The
936  * format looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
937  * "&lt;Release&gt;z" (the last one is for key release).
938  * The parser is fairly liberal and allows lower or upper case,
939  * and also abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
940  *
941  * If the parse fails, @accelerator_key and @accelerator_mods will
942  * be set to 0 (zero).
943  */
944 void
945 gtk_accelerator_parse (const gchar     *accelerator,
946                        guint           *accelerator_key,
947                        GdkModifierType *accelerator_mods)
948 {
949   guint keyval;
950   GdkModifierType mods;
951   gint len;
952   
953   if (accelerator_key)
954     *accelerator_key = 0;
955   if (accelerator_mods)
956     *accelerator_mods = 0;
957   g_return_if_fail (accelerator != NULL);
958   
959   keyval = 0;
960   mods = 0;
961   len = strlen (accelerator);
962   while (len)
963     {
964       if (*accelerator == '<')
965         {
966           if (len >= 9 && is_release (accelerator))
967             {
968               accelerator += 9;
969               len -= 9;
970               mods |= GDK_RELEASE_MASK;
971             }
972           else if (len >= 9 && is_control (accelerator))
973             {
974               accelerator += 9;
975               len -= 9;
976               mods |= GDK_CONTROL_MASK;
977             }
978           else if (len >= 7 && is_shift (accelerator))
979             {
980               accelerator += 7;
981               len -= 7;
982               mods |= GDK_SHIFT_MASK;
983             }
984           else if (len >= 6 && is_shft (accelerator))
985             {
986               accelerator += 6;
987               len -= 6;
988               mods |= GDK_SHIFT_MASK;
989             }
990           else if (len >= 6 && is_ctrl (accelerator))
991             {
992               accelerator += 6;
993               len -= 6;
994               mods |= GDK_CONTROL_MASK;
995             }
996           else if (len >= 6 && is_modx (accelerator))
997             {
998               static const guint mod_vals[] = {
999                 GDK_MOD1_MASK, GDK_MOD2_MASK, GDK_MOD3_MASK,
1000                 GDK_MOD4_MASK, GDK_MOD5_MASK
1001               };
1002
1003               len -= 6;
1004               accelerator += 4;
1005               mods |= mod_vals[*accelerator - '1'];
1006               accelerator += 2;
1007             }
1008           else if (len >= 5 && is_ctl (accelerator))
1009             {
1010               accelerator += 5;
1011               len -= 5;
1012               mods |= GDK_CONTROL_MASK;
1013             }
1014           else if (len >= 5 && is_alt (accelerator))
1015             {
1016               accelerator += 5;
1017               len -= 5;
1018               mods |= GDK_MOD1_MASK;
1019             }
1020           else
1021             {
1022               gchar last_ch;
1023               
1024               last_ch = *accelerator;
1025               while (last_ch && last_ch != '>')
1026                 {
1027                   last_ch = *accelerator;
1028                   accelerator += 1;
1029                   len -= 1;
1030                 }
1031             }
1032         }
1033       else
1034         {
1035           keyval = gdk_keyval_from_name (accelerator);
1036           accelerator += len;
1037           len -= len;
1038         }
1039     }
1040   
1041   if (accelerator_key)
1042     *accelerator_key = gdk_keyval_to_lower (keyval);
1043   if (accelerator_mods)
1044     *accelerator_mods = mods;
1045 }
1046
1047 /**
1048  * gtk_accelerator_name:
1049  * @accelerator_key:  accelerator keyval
1050  * @accelerator_mods: accelerator modifier mask
1051  * @returns:          a newly-allocated accelerator name
1052  * 
1053  * Converts an accelerator keyval and modifier mask
1054  * into a string parseable by gtk_accelerator_parse().
1055  * For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
1056  * this function returns "&lt;Control&gt;q". 
1057  *
1058  * The caller of this function must free the returned string.
1059  */
1060 gchar*
1061 gtk_accelerator_name (guint           accelerator_key,
1062                       GdkModifierType accelerator_mods)
1063 {
1064   static const gchar text_release[] = "<Release>";
1065   static const gchar text_shift[] = "<Shift>";
1066   static const gchar text_control[] = "<Control>";
1067   static const gchar text_mod1[] = "<Alt>";
1068   static const gchar text_mod2[] = "<Mod2>";
1069   static const gchar text_mod3[] = "<Mod3>";
1070   static const gchar text_mod4[] = "<Mod4>";
1071   static const gchar text_mod5[] = "<Mod5>";
1072   guint l;
1073   gchar *keyval_name;
1074   gchar *accelerator;
1075
1076   accelerator_mods &= GDK_MODIFIER_MASK;
1077
1078   keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
1079   if (!keyval_name)
1080     keyval_name = "";
1081
1082   l = 0;
1083   if (accelerator_mods & GDK_RELEASE_MASK)
1084     l += sizeof (text_release) - 1;
1085   if (accelerator_mods & GDK_SHIFT_MASK)
1086     l += sizeof (text_shift) - 1;
1087   if (accelerator_mods & GDK_CONTROL_MASK)
1088     l += sizeof (text_control) - 1;
1089   if (accelerator_mods & GDK_MOD1_MASK)
1090     l += sizeof (text_mod1) - 1;
1091   if (accelerator_mods & GDK_MOD2_MASK)
1092     l += sizeof (text_mod2) - 1;
1093   if (accelerator_mods & GDK_MOD3_MASK)
1094     l += sizeof (text_mod3) - 1;
1095   if (accelerator_mods & GDK_MOD4_MASK)
1096     l += sizeof (text_mod4) - 1;
1097   if (accelerator_mods & GDK_MOD5_MASK)
1098     l += sizeof (text_mod5) - 1;
1099   l += strlen (keyval_name);
1100
1101   accelerator = g_new (gchar, l + 1);
1102
1103   l = 0;
1104   accelerator[l] = 0;
1105   if (accelerator_mods & GDK_RELEASE_MASK)
1106     {
1107       strcpy (accelerator + l, text_release);
1108       l += sizeof (text_release) - 1;
1109     }
1110   if (accelerator_mods & GDK_SHIFT_MASK)
1111     {
1112       strcpy (accelerator + l, text_shift);
1113       l += sizeof (text_shift) - 1;
1114     }
1115   if (accelerator_mods & GDK_CONTROL_MASK)
1116     {
1117       strcpy (accelerator + l, text_control);
1118       l += sizeof (text_control) - 1;
1119     }
1120   if (accelerator_mods & GDK_MOD1_MASK)
1121     {
1122       strcpy (accelerator + l, text_mod1);
1123       l += sizeof (text_mod1) - 1;
1124     }
1125   if (accelerator_mods & GDK_MOD2_MASK)
1126     {
1127       strcpy (accelerator + l, text_mod2);
1128       l += sizeof (text_mod2) - 1;
1129     }
1130   if (accelerator_mods & GDK_MOD3_MASK)
1131     {
1132       strcpy (accelerator + l, text_mod3);
1133       l += sizeof (text_mod3) - 1;
1134     }
1135   if (accelerator_mods & GDK_MOD4_MASK)
1136     {
1137       strcpy (accelerator + l, text_mod4);
1138       l += sizeof (text_mod4) - 1;
1139     }
1140   if (accelerator_mods & GDK_MOD5_MASK)
1141     {
1142       strcpy (accelerator + l, text_mod5);
1143       l += sizeof (text_mod5) - 1;
1144     }
1145   strcpy (accelerator + l, keyval_name);
1146
1147   return accelerator;
1148 }
1149
1150 /**
1151  * gtk_accelerator_set_default_mod_mask:
1152  * @default_mod_mask: accelerator modifier mask
1153  *
1154  * Sets the modifiers that will be considered significant for keyboard
1155  * accelerators. The default mod mask is #GDK_CONTROL_MASK |
1156  * #GDK_SHIFT_MASK | #GDK_MOD1_MASK, that is, Control, Shift, and Alt.
1157  * Other modifiers will by default be ignored by #GtkAccelGroup.
1158  *
1159  * The default mod mask should be changed on application startup,
1160  * before using any accelerator groups.
1161  */
1162 void
1163 gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask)
1164 {
1165   default_accel_mod_mask = default_mod_mask & GDK_MODIFIER_MASK;
1166 }
1167
1168 /**
1169  * gtk_accelerator_get_default_mod_mask:
1170  * @returns: the default accelerator modifier mask
1171  *
1172  * Gets the value set by gtk_accelerator_set_default_mod_mask().
1173  */
1174 guint
1175 gtk_accelerator_get_default_mod_mask (void)
1176 {
1177   return default_accel_mod_mask;
1178 }