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