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