]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Require XInput2.h in X11 backend
[~andy/gtk] / gtk / gtkaccellabel.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GtkAccelLabel: GtkLabel with accelerator monitoring facilities.
5  * Copyright (C) 1998 Tim Janik
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29 #include <string.h>
30
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkmain.h"
34 #include "gtksizerequest.h"
35 #include "gtkprivate.h"
36 #include "gtkintl.h"
37
38 /**
39  * SECTION:gtkaccellabel
40  * @Short_description: A label which displays an accelerator key on the right of the text
41  * @Title: GtkAccelLabel
42  * @See_also: #GtkAccelGroup
43  *
44  * The #GtkAccelLabel widget is a subclass of #GtkLabel that also displays an
45  * accelerator key on the right of the label text, e.g. 'Ctl+S'.
46  * It is commonly used in menus to show the keyboard short-cuts for commands.
47  *
48  * The accelerator key to display is not set explicitly.
49  * Instead, the #GtkAccelLabel displays the accelerators which have been added to
50  * a particular widget. This widget is set by calling
51  * gtk_accel_label_set_accel_widget().
52  *
53  * For example, a #GtkMenuItem widget may have an accelerator added to emit the
54  * "activate" signal when the 'Ctl+S' key combination is pressed.
55  * A #GtkAccelLabel is created and added to the #GtkMenuItem, and
56  * gtk_accel_label_set_accel_widget() is called with the #GtkMenuItem as the
57  * second argument. The #GtkAccelLabel will now display 'Ctl+S' after its label.
58  *
59  * Note that creating a #GtkMenuItem with gtk_menu_item_new_with_label() (or
60  * one of the similar functions for #GtkCheckMenuItem and #GtkRadioMenuItem)
61  * automatically adds a #GtkAccelLabel to the #GtkMenuItem and calls
62  * gtk_accel_label_set_accel_widget() to set it up for you.
63  *
64  * A #GtkAccelLabel will only display accelerators which have %GTK_ACCEL_VISIBLE
65  * set (see #GtkAccelFlags).
66  * A #GtkAccelLabel can display multiple accelerators and even signal names,
67  * though it is almost always used to display just one accelerator key.
68  * <example>
69  * <title>Creating a simple menu item with an accelerator key.</title>
70  * <programlisting>
71  *   GtkWidget *save_item;
72  *   GtkAccelGroup *accel_group;
73  *
74  *   /<!---->* Create a GtkAccelGroup and add it to the window. *<!---->/
75  *   accel_group = gtk_accel_group_new (<!-- -->);
76  *   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
77  *
78  *   /<!---->* Create the menu item using the convenience function. *<!---->/
79  *   save_item = gtk_menu_item_new_with_label ("Save");
80  *   gtk_widget_show (save_item);
81  *   gtk_container_add (GTK_CONTAINER (menu), save_item);
82  *
83  *   /<!---->* Now add the accelerator to the GtkMenuItem. Note that since we called
84  *      gtk_menu_item_new_with_label(<!-- -->) to create the GtkMenuItem the
85  *      GtkAccelLabel is automatically set up to display the GtkMenuItem
86  *      accelerators. We just need to make sure we use GTK_ACCEL_VISIBLE here. *<!---->/
87  *   gtk_widget_add_accelerator (save_item, "activate", accel_group,
88  *                               GDK_KEY_s, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
89  * </programlisting>
90  * </example>
91  */
92
93 enum {
94   PROP_0,
95   PROP_ACCEL_CLOSURE,
96   PROP_ACCEL_WIDGET
97 };
98
99 struct _GtkAccelLabelPrivate
100 {
101   GtkWidget     *accel_widget;       /* done */
102   GClosure      *accel_closure;      /* has set function */
103   GtkAccelGroup *accel_group;        /* set by set_accel_closure() */
104   gchar         *accel_string;       /* has set function */
105   guint          accel_padding;      /* should be style property? */
106   guint16        accel_string_width; /* seems to be private */
107 };
108
109 static void         gtk_accel_label_set_property (GObject            *object,
110                                                   guint               prop_id,
111                                                   const GValue       *value,
112                                                   GParamSpec         *pspec);
113 static void         gtk_accel_label_get_property (GObject            *object,
114                                                   guint               prop_id,
115                                                   GValue             *value,
116                                                   GParamSpec         *pspec);
117 static void         gtk_accel_label_destroy      (GtkWidget          *widget);
118 static void         gtk_accel_label_finalize     (GObject            *object);
119 static gboolean     gtk_accel_label_draw         (GtkWidget          *widget,
120                                                   cairo_t            *cr);
121 static const gchar *gtk_accel_label_get_string   (GtkAccelLabel      *accel_label);
122
123
124 static void         gtk_accel_label_get_preferred_width (GtkWidget           *widget,
125                                                          gint                *min_width,
126                                                          gint                *nat_width);
127
128
129 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
130
131 static void
132 gtk_accel_label_class_init (GtkAccelLabelClass *class)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
135   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
136   
137   gobject_class->finalize = gtk_accel_label_finalize;
138   gobject_class->set_property = gtk_accel_label_set_property;
139   gobject_class->get_property = gtk_accel_label_get_property;
140
141   widget_class->draw = gtk_accel_label_draw;
142   widget_class->get_preferred_width = gtk_accel_label_get_preferred_width;
143   widget_class->destroy = gtk_accel_label_destroy;
144
145   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_ACCEL_LABEL);
146
147   class->signal_quote1 = g_strdup ("<:");
148   class->signal_quote2 = g_strdup (":>");
149
150 #ifndef GDK_WINDOWING_QUARTZ
151   /* This is the text that should appear next to menu accelerators
152    * that use the shift key. If the text on this key isn't typically
153    * translated on keyboards used for your language, don't translate
154    * this.
155    */
156   class->mod_name_shift = g_strdup (C_("keyboard label", "Shift"));
157   /* This is the text that should appear next to menu accelerators
158    * that use the control key. If the text on this key isn't typically
159    * translated on keyboards used for your language, don't translate
160    * this.
161    */
162   class->mod_name_control = g_strdup (C_("keyboard label", "Ctrl"));
163   /* This is the text that should appear next to menu accelerators
164    * that use the alt key. If the text on this key isn't typically
165    * translated on keyboards used for your language, don't translate
166    * this.
167    */
168   class->mod_name_alt = g_strdup (C_("keyboard label", "Alt"));
169   class->mod_separator = g_strdup ("+");
170 #else /* GDK_WINDOWING_QUARTZ */
171
172   /* U+21E7 UPWARDS WHITE ARROW */
173   class->mod_name_shift = g_strdup ("\xe2\x87\xa7");
174   /* U+2303 UP ARROWHEAD */
175   class->mod_name_control = g_strdup ("\xe2\x8c\x83");
176   /* U+2325 OPTION KEY */
177   class->mod_name_alt = g_strdup ("\xe2\x8c\xa5");
178   class->mod_separator = g_strdup ("");
179
180 #endif /* GDK_WINDOWING_QUARTZ */
181
182   g_object_class_install_property (gobject_class,
183                                    PROP_ACCEL_CLOSURE,
184                                    g_param_spec_boxed ("accel-closure",
185                                                        P_("Accelerator Closure"),
186                                                        P_("The closure to be monitored for accelerator changes"),
187                                                        G_TYPE_CLOSURE,
188                                                        GTK_PARAM_READWRITE));
189   g_object_class_install_property (gobject_class,
190                                    PROP_ACCEL_WIDGET,
191                                    g_param_spec_object ("accel-widget",
192                                                         P_("Accelerator Widget"),
193                                                         P_("The widget to be monitored for accelerator changes"),
194                                                         GTK_TYPE_WIDGET,
195                                                         GTK_PARAM_READWRITE));
196
197   g_type_class_add_private (gobject_class, sizeof (GtkAccelLabelPrivate));
198 }
199
200 static void
201 gtk_accel_label_set_property (GObject      *object,
202                               guint         prop_id,
203                               const GValue *value,
204                               GParamSpec   *pspec)
205 {
206   GtkAccelLabel  *accel_label;
207
208   accel_label = GTK_ACCEL_LABEL (object);
209
210   switch (prop_id)
211     {
212     case PROP_ACCEL_CLOSURE:
213       gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
214       break;
215     case PROP_ACCEL_WIDGET:
216       gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221     }
222 }
223
224 static void
225 gtk_accel_label_get_property (GObject    *object,
226                               guint       prop_id,
227                               GValue     *value,
228                               GParamSpec *pspec)
229 {
230   GtkAccelLabel  *accel_label;
231
232   accel_label = GTK_ACCEL_LABEL (object);
233
234   switch (prop_id)
235     {
236     case PROP_ACCEL_CLOSURE:
237       g_value_set_boxed (value, accel_label->priv->accel_closure);
238       break;
239     case PROP_ACCEL_WIDGET:
240       g_value_set_object (value, accel_label->priv->accel_widget);
241       break;
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245     }
246 }
247
248 static void
249 gtk_accel_label_init (GtkAccelLabel *accel_label)
250 {
251   GtkAccelLabelPrivate *priv;
252
253   accel_label->priv = G_TYPE_INSTANCE_GET_PRIVATE (accel_label,
254                                                    GTK_TYPE_ACCEL_LABEL,
255                                                    GtkAccelLabelPrivate);
256   priv = accel_label->priv;
257
258   priv->accel_padding = 3;
259   priv->accel_widget = NULL;
260   priv->accel_closure = NULL;
261   priv->accel_group = NULL;
262   priv->accel_string = NULL;
263 }
264
265 /**
266  * gtk_accel_label_new:
267  * @string: the label string. Must be non-%NULL.
268  *
269  * Creates a new #GtkAccelLabel.
270  *
271  * Returns: a new #GtkAccelLabel.
272  */
273 GtkWidget*
274 gtk_accel_label_new (const gchar *string)
275 {
276   GtkAccelLabel *accel_label;
277   
278   g_return_val_if_fail (string != NULL, NULL);
279   
280   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
281   
282   gtk_label_set_text (GTK_LABEL (accel_label), string);
283   
284   return GTK_WIDGET (accel_label);
285 }
286
287 static void
288 gtk_accel_label_destroy (GtkWidget *widget)
289 {
290   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
291
292   gtk_accel_label_set_accel_widget (accel_label, NULL);
293   gtk_accel_label_set_accel_closure (accel_label, NULL);
294
295   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->destroy (widget);
296 }
297
298 static void
299 gtk_accel_label_finalize (GObject *object)
300 {
301   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
302
303   g_free (accel_label->priv->accel_string);
304
305   G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
306 }
307
308 /**
309  * gtk_accel_label_get_accel_widget:
310  * @accel_label: a #GtkAccelLabel
311  *
312  * Fetches the widget monitored by this accelerator label. See
313  * gtk_accel_label_set_accel_widget().
314  *
315  * Returns: (transfer none): the object monitored by the accelerator label, or %NULL.
316  **/
317 GtkWidget*
318 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
319 {
320   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
321
322   return accel_label->priv->accel_widget;
323 }
324
325 /**
326  * gtk_accel_label_get_accel_width:
327  * @accel_label: a #GtkAccelLabel.
328  *
329  * Returns the width needed to display the accelerator key(s).
330  * This is used by menus to align all of the #GtkMenuItem widgets, and shouldn't
331  * be needed by applications.
332  *
333  * Returns: the width needed to display the accelerator key(s).
334  */
335 guint
336 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
337 {
338   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
339
340   return (accel_label->priv->accel_string_width +
341           (accel_label->priv->accel_string_width ? accel_label->priv->accel_padding : 0));
342 }
343
344 static void
345 gtk_accel_label_get_preferred_width (GtkWidget       *widget,
346                                      gint            *min_width,
347                                      gint            *nat_width)
348 {
349   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
350   PangoLayout   *layout;
351   gint           width;
352
353   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->get_preferred_width (widget, min_width, nat_width);
354
355   layout = gtk_widget_create_pango_layout (GTK_WIDGET (widget), 
356                                            gtk_accel_label_get_string (accel_label));
357   pango_layout_get_pixel_size (layout, &width, NULL);
358   accel_label->priv->accel_string_width = width;
359
360   g_object_unref (layout);
361 }
362
363 static gint
364 get_first_baseline (PangoLayout *layout)
365 {
366   PangoLayoutIter *iter;
367   gint result;
368
369   iter = pango_layout_get_iter (layout);
370   result = pango_layout_iter_get_baseline (iter);
371   pango_layout_iter_free (iter);
372
373   return PANGO_PIXELS (result);
374 }
375
376 static gboolean 
377 gtk_accel_label_draw (GtkWidget *widget,
378                       cairo_t   *cr)
379 {
380   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
381   GtkMisc *misc = GTK_MISC (accel_label);
382   GtkTextDirection direction;
383   guint ac_width;
384   GtkAllocation allocation;
385   GtkRequisition requisition;
386
387   direction = gtk_widget_get_direction (widget);
388   ac_width = gtk_accel_label_get_accel_width (accel_label);
389   gtk_widget_get_allocation (widget, &allocation);
390   gtk_widget_get_preferred_size (widget, NULL, &requisition);
391
392   if (allocation.width >= requisition.width + ac_width)
393     {
394       GtkStyleContext *context;
395       PangoLayout *label_layout;
396       PangoLayout *accel_layout;
397       GtkLabel *label = GTK_LABEL (widget);
398
399       gint x;
400       gint y;
401       gint xpad;
402
403       context = gtk_widget_get_style_context (widget);
404       label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
405
406       cairo_save (cr);
407
408       /* XXX: Mad hack: We modify the label's width so it renders
409        * properly in its draw function that we chain to. */
410       if (direction == GTK_TEXT_DIR_RTL)
411         cairo_translate (cr, ac_width, 0);
412       if (gtk_label_get_ellipsize (label))
413         pango_layout_set_width (label_layout,
414                                 pango_layout_get_width (label_layout) 
415                                 - ac_width * PANGO_SCALE);
416       
417       allocation.width -= ac_width;
418       gtk_widget_set_allocation (widget, &allocation);
419       if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
420         GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget,
421                                                                cr);
422       allocation.width += ac_width;
423       gtk_widget_set_allocation (widget, &allocation);
424       if (gtk_label_get_ellipsize (label))
425         pango_layout_set_width (label_layout,
426                                 pango_layout_get_width (label_layout) 
427                                 + ac_width * PANGO_SCALE);
428
429       cairo_restore (cr);
430
431       gtk_misc_get_padding (misc, &xpad, NULL);
432
433       if (direction == GTK_TEXT_DIR_RTL)
434         x = xpad;
435       else
436         x = gtk_widget_get_allocated_width (widget) - xpad - ac_width;
437
438       gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
439
440       accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
441
442       y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
443
444       gtk_style_context_save (context);
445       gtk_style_context_add_class (context, GTK_STYLE_CLASS_ACCELERATOR);
446
447       gtk_render_layout (context, cr, x, y, accel_layout);
448       gtk_style_context_restore (context);
449
450       g_object_unref (accel_layout);
451     }
452   else
453     {
454       if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
455         GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
456     }
457   
458   return FALSE;
459 }
460
461 static void
462 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
463 {
464   GClosure *closure = NULL;
465   GList *clist, *list;
466   
467   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
468   g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
469   
470   clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
471   for (list = clist; list; list = list->next)
472     {
473       /* we just take the first closure used */
474       closure = list->data;
475       break;
476     }
477   g_list_free (clist);
478   gtk_accel_label_set_accel_closure (accel_label, closure);
479 }
480
481 static void
482 accel_widget_weak_ref_cb (GtkAccelLabel *accel_label,
483                           GtkWidget     *old_accel_widget)
484 {
485   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
486   g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
487
488   g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
489                                         refetch_widget_accel_closure,
490                                         accel_label);
491   accel_label->priv->accel_widget = NULL;
492   g_object_notify (G_OBJECT (accel_label), "accel-widget");
493 }
494
495 /**
496  * gtk_accel_label_set_accel_widget:
497  * @accel_label: a #GtkAccelLabel
498  * @accel_widget: the widget to be monitored.
499  *
500  * Sets the widget to be monitored by this accelerator label.
501  */
502 void
503 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
504                                   GtkWidget     *accel_widget)
505 {
506   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
507   if (accel_widget)
508     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
509
510   if (accel_widget != accel_label->priv->accel_widget)
511     {
512       if (accel_label->priv->accel_widget)
513         {
514           gtk_accel_label_set_accel_closure (accel_label, NULL);
515           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
516                                                 refetch_widget_accel_closure,
517                                                 accel_label);
518           g_object_weak_unref (G_OBJECT (accel_label->priv->accel_widget),
519                                (GWeakNotify) accel_widget_weak_ref_cb, accel_label);
520         }
521       accel_label->priv->accel_widget = accel_widget;
522       if (accel_label->priv->accel_widget)
523         {
524           g_object_weak_ref (G_OBJECT (accel_label->priv->accel_widget),
525                              (GWeakNotify) accel_widget_weak_ref_cb, accel_label);
526           g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
527                                    G_CALLBACK (refetch_widget_accel_closure),
528                                    accel_label, G_CONNECT_SWAPPED);
529           refetch_widget_accel_closure (accel_label);
530         }
531       g_object_notify (G_OBJECT (accel_label), "accel-widget");
532     }
533 }
534
535 static void
536 gtk_accel_label_reset (GtkAccelLabel *accel_label)
537 {
538   if (accel_label->priv->accel_string)
539     {
540       g_free (accel_label->priv->accel_string);
541       accel_label->priv->accel_string = NULL;
542     }
543   
544   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
545 }
546
547 static void
548 check_accel_changed (GtkAccelGroup  *accel_group,
549                      guint           keyval,
550                      GdkModifierType modifier,
551                      GClosure       *accel_closure,
552                      GtkAccelLabel  *accel_label)
553 {
554   if (accel_closure == accel_label->priv->accel_closure)
555     gtk_accel_label_reset (accel_label);
556 }
557
558 /**
559  * gtk_accel_label_set_accel_closure:
560  * @accel_label: a #GtkAccelLabel
561  * @accel_closure: the closure to monitor for accelerator changes.
562  *
563  * Sets the closure to be monitored by this accelerator label. The closure
564  * must be connected to an accelerator group; see gtk_accel_group_connect().
565  **/
566 void
567 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
568                                    GClosure      *accel_closure)
569 {
570   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
571   if (accel_closure)
572     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
573
574   if (accel_closure != accel_label->priv->accel_closure)
575     {
576       if (accel_label->priv->accel_closure)
577         {
578           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
579                                                 check_accel_changed,
580                                                 accel_label);
581           accel_label->priv->accel_group = NULL;
582           g_closure_unref (accel_label->priv->accel_closure);
583         }
584       accel_label->priv->accel_closure = accel_closure;
585       if (accel_label->priv->accel_closure)
586         {
587           g_closure_ref (accel_label->priv->accel_closure);
588           accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
589           g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
590                                    G_CALLBACK (check_accel_changed),
591                                    accel_label, 0);
592         }
593       gtk_accel_label_reset (accel_label);
594       g_object_notify (G_OBJECT (accel_label), "accel-closure");
595     }
596 }
597
598 static gboolean
599 find_accel (GtkAccelKey *key,
600             GClosure    *closure,
601             gpointer     data)
602 {
603   return data == (gpointer) closure;
604 }
605
606 static const gchar *
607 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
608 {
609   if (!accel_label->priv->accel_string)
610     gtk_accel_label_refetch (accel_label);
611   
612   return accel_label->priv->accel_string;
613 }
614
615 /* Underscores in key names are better displayed as spaces
616  * E.g., Page_Up should be "Page Up".
617  *
618  * Some keynames also have prefixes that are not suitable
619  * for display, e.g XF86AudioMute, so strip those out, too.
620  *
621  * This function is only called on untranslated keynames,
622  * so no need to be UTF-8 safe.
623  */
624 static void
625 append_without_underscores (GString *s,
626                             gchar   *str)
627 {
628   gchar *p;
629
630   if (g_str_has_prefix (str, "XF86"))
631     p = str + 4;
632   else if (g_str_has_prefix (str, "ISO_"))
633     p = str + 4;
634   else
635     p = str;
636
637   for ( ; *p; p++)
638     {
639       if (*p == '_')
640         g_string_append_c (s, ' ');
641       else
642         g_string_append_c (s, *p);
643     }
644 }
645
646 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
647  * append it to gstring and return TRUE; otherwise return FALSE.
648  * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html 
649  * for the list of special keys. */
650 static gboolean
651 append_keyval_symbol (guint    accelerator_key,
652                       GString *gstring)
653 {
654 #ifdef GDK_WINDOWING_QUARTZ
655   switch (accelerator_key)
656   {
657   case GDK_KEY_Return:
658     /* U+21A9 LEFTWARDS ARROW WITH HOOK */
659     g_string_append (gstring, "\xe2\x86\xa9");
660     return TRUE;
661
662   case GDK_KEY_ISO_Enter:
663     /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
664     g_string_append (gstring, "\xe2\x8c\xa4");
665     return TRUE;
666
667   case GDK_KEY_Left:
668     /* U+2190 LEFTWARDS ARROW */
669     g_string_append (gstring, "\xe2\x86\x90");
670     return TRUE;
671
672   case GDK_KEY_Up:
673     /* U+2191 UPWARDS ARROW */
674     g_string_append (gstring, "\xe2\x86\x91");
675     return TRUE;
676
677   case GDK_KEY_Right:
678     /* U+2192 RIGHTWARDS ARROW */
679     g_string_append (gstring, "\xe2\x86\x92");
680     return TRUE;
681
682   case GDK_KEY_Down:
683     /* U+2193 DOWNWARDS ARROW */
684     g_string_append (gstring, "\xe2\x86\x93");
685     return TRUE;
686
687   case GDK_KEY_Page_Up:
688     /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
689     g_string_append (gstring, "\xe2\x87\x9e");
690     return TRUE;
691
692   case GDK_KEY_Page_Down:
693     /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
694     g_string_append (gstring, "\xe2\x87\x9f");
695     return TRUE;
696
697   case GDK_KEY_Home:
698     /* U+2196 NORTH WEST ARROW */
699     g_string_append (gstring, "\xe2\x86\x96");
700     return TRUE;
701
702   case GDK_KEY_End:
703     /* U+2198 SOUTH EAST ARROW */
704     g_string_append (gstring, "\xe2\x86\x98");
705     return TRUE;
706
707   case GDK_KEY_Escape:
708     /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
709     g_string_append (gstring, "\xe2\x8e\x8b");
710     return TRUE;
711
712   case GDK_KEY_BackSpace:
713     /* U+232B ERASE TO THE LEFT */
714     g_string_append (gstring, "\xe2\x8c\xab");
715     return TRUE;
716
717   case GDK_KEY_Delete:
718     /* U+2326 ERASE TO THE RIGHT */
719     g_string_append (gstring, "\xe2\x8c\xa6");
720     return TRUE;
721
722   default:
723     return FALSE;
724   }
725 #else /* !GDK_WINDOWING_QUARTZ */
726   return FALSE;
727 #endif
728 }
729
730 gchar *
731 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
732                                               guint               accelerator_key,
733                                               GdkModifierType     accelerator_mods)
734 {
735   GString *gstring;
736   gboolean seen_mod = FALSE;
737   gunichar ch;
738   
739   gstring = g_string_new ("");
740   
741   if (accelerator_mods & GDK_SHIFT_MASK)
742     {
743       g_string_append (gstring, klass->mod_name_shift);
744       seen_mod = TRUE;
745     }
746   if (accelerator_mods & GDK_CONTROL_MASK)
747     {
748       if (seen_mod)
749         g_string_append (gstring, klass->mod_separator);
750       g_string_append (gstring, klass->mod_name_control);
751       seen_mod = TRUE;
752     }
753   if (accelerator_mods & GDK_MOD1_MASK)
754     {
755       if (seen_mod)
756         g_string_append (gstring, klass->mod_separator);
757       g_string_append (gstring, klass->mod_name_alt);
758       seen_mod = TRUE;
759     }
760   if (accelerator_mods & GDK_MOD2_MASK)
761     {
762       if (seen_mod)
763         g_string_append (gstring, klass->mod_separator);
764
765       g_string_append (gstring, "Mod2");
766       seen_mod = TRUE;
767     }
768   if (accelerator_mods & GDK_MOD3_MASK)
769     {
770       if (seen_mod)
771         g_string_append (gstring, klass->mod_separator);
772
773       g_string_append (gstring, "Mod3");
774       seen_mod = TRUE;
775     }
776   if (accelerator_mods & GDK_MOD4_MASK)
777     {
778       if (seen_mod)
779         g_string_append (gstring, klass->mod_separator);
780
781       g_string_append (gstring, "Mod4");
782       seen_mod = TRUE;
783     }
784   if (accelerator_mods & GDK_MOD5_MASK)
785     {
786       if (seen_mod)
787         g_string_append (gstring, klass->mod_separator);
788
789       g_string_append (gstring, "Mod5");
790       seen_mod = TRUE;
791     }
792   if (accelerator_mods & GDK_SUPER_MASK)
793     {
794       if (seen_mod)
795         g_string_append (gstring, klass->mod_separator);
796
797       /* This is the text that should appear next to menu accelerators
798        * that use the super key. If the text on this key isn't typically
799        * translated on keyboards used for your language, don't translate
800        * this.
801        */
802       g_string_append (gstring, C_("keyboard label", "Super"));
803       seen_mod = TRUE;
804     }
805   if (accelerator_mods & GDK_HYPER_MASK)
806     {
807       if (seen_mod)
808         g_string_append (gstring, klass->mod_separator);
809
810       /* This is the text that should appear next to menu accelerators
811        * that use the hyper key. If the text on this key isn't typically
812        * translated on keyboards used for your language, don't translate
813        * this.
814        */
815       g_string_append (gstring, C_("keyboard label", "Hyper"));
816       seen_mod = TRUE;
817     }
818   if (accelerator_mods & GDK_META_MASK)
819     {
820       if (seen_mod)
821         g_string_append (gstring, klass->mod_separator);
822
823 #ifndef GDK_WINDOWING_QUARTZ
824       /* This is the text that should appear next to menu accelerators
825        * that use the meta key. If the text on this key isn't typically
826        * translated on keyboards used for your language, don't translate
827        * this.
828        */
829       g_string_append (gstring, C_("keyboard label", "Meta"));
830 #else
831       /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
832       g_string_append (gstring, "\xe2\x8c\x98");
833 #endif
834       seen_mod = TRUE;
835     }
836   if (seen_mod)
837     g_string_append (gstring, klass->mod_separator);
838   
839   ch = gdk_keyval_to_unicode (accelerator_key);
840   if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
841     {
842       switch (ch)
843         {
844         case ' ':
845           g_string_append (gstring, C_("keyboard label", "Space"));
846           break;
847         case '\\':
848           g_string_append (gstring, C_("keyboard label", "Backslash"));
849           break;
850         default:
851           g_string_append_unichar (gstring, g_unichar_toupper (ch));
852           break;
853         }
854     }
855   else if (!append_keyval_symbol (accelerator_key, gstring))
856     {
857       gchar *tmp;
858
859       tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
860       if (tmp != NULL)
861         {
862           if (tmp[0] != 0 && tmp[1] == 0)
863             g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
864           else
865             {
866               const gchar *str;
867               str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
868               if (str == tmp)
869                 append_without_underscores (gstring, tmp);
870               else
871                 g_string_append (gstring, str);
872             }
873         }
874     }
875
876   return g_string_free (gstring, FALSE);
877 }
878
879 /**
880  * gtk_accel_label_refetch:
881  * @accel_label: a #GtkAccelLabel.
882  *
883  * Recreates the string representing the accelerator keys.
884  * This should not be needed since the string is automatically updated whenever
885  * accelerators are added or removed from the associated widget.
886  *
887  * Returns: always returns %FALSE.
888  */
889 gboolean
890 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
891 {
892   gboolean enable_accels;
893
894   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
895
896   if (accel_label->priv->accel_string)
897     {
898       g_free (accel_label->priv->accel_string);
899       accel_label->priv->accel_string = NULL;
900     }
901
902   g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
903                 "gtk-enable-accels", &enable_accels,
904                 NULL);
905
906   if (enable_accels && accel_label->priv->accel_closure)
907     {
908       GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
909
910       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
911         {
912           GtkAccelLabelClass *klass;
913           gchar *tmp;
914
915           klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
916           tmp = _gtk_accel_label_class_get_accelerator_label (klass,
917                                                               key->accel_key,
918                                                               key->accel_mods);
919           accel_label->priv->accel_string = g_strconcat ("   ", tmp, NULL);
920           g_free (tmp);
921         }
922       if (!accel_label->priv->accel_string)
923         accel_label->priv->accel_string = g_strdup ("-/-");
924     }
925   
926   if (!accel_label->priv->accel_string)
927     accel_label->priv->accel_string = g_strdup ("");
928
929   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
930
931   return FALSE;
932 }