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