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