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