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