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