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