]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Move destroy signal to GtkWidget
[~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      (GtkWidget          *widget);
122 static void         gtk_accel_label_finalize     (GObject            *object);
123 static gboolean     gtk_accel_label_draw         (GtkWidget          *widget,
124                                                   cairo_t            *cr);
125 static const gchar *gtk_accel_label_get_string   (GtkAccelLabel      *accel_label);
126
127
128 static void         gtk_accel_label_get_preferred_width (GtkWidget           *widget,
129                                                          gint                *min_width,
130                                                          gint                *nat_width);
131
132 #define GTK_ACCEL_LABEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ACCEL_LABEL, GtkAccelLabelPrivate))
133
134
135 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
136
137 static void
138 gtk_accel_label_class_init (GtkAccelLabelClass *class)
139 {
140   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
141   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
142   
143   gobject_class->finalize = gtk_accel_label_finalize;
144   gobject_class->set_property = gtk_accel_label_set_property;
145   gobject_class->get_property = gtk_accel_label_get_property;
146
147   widget_class->draw = gtk_accel_label_draw;
148   widget_class->get_preferred_width = gtk_accel_label_get_preferred_width;
149   widget_class->destroy = gtk_accel_label_destroy;
150
151   class->signal_quote1 = g_strdup ("<:");
152   class->signal_quote2 = g_strdup (":>");
153
154 #ifndef GDK_WINDOWING_QUARTZ
155   /* This is the text that should appear next to menu accelerators
156    * that use the shift key. If the text on this key isn't typically
157    * translated on keyboards used for your language, don't translate
158    * this.
159    */
160   class->mod_name_shift = g_strdup (C_("keyboard label", "Shift"));
161   /* This is the text that should appear next to menu accelerators
162    * that use the control 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_control = g_strdup (C_("keyboard label", "Ctrl"));
167   /* This is the text that should appear next to menu accelerators
168    * that use the alt 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_alt = g_strdup (C_("keyboard label", "Alt"));
173   class->mod_separator = g_strdup ("+");
174 #else /* GDK_WINDOWING_QUARTZ */
175
176   /* U+21E7 UPWARDS WHITE ARROW */
177   class->mod_name_shift = g_strdup ("\xe2\x87\xa7");
178   /* U+2303 UP ARROWHEAD */
179   class->mod_name_control = g_strdup ("\xe2\x8c\x83");
180   /* U+2325 OPTION KEY */
181   class->mod_name_alt = g_strdup ("\xe2\x8c\xa5");
182   class->mod_separator = g_strdup ("");
183
184 #endif /* GDK_WINDOWING_QUARTZ */
185
186   g_object_class_install_property (gobject_class,
187                                    PROP_ACCEL_CLOSURE,
188                                    g_param_spec_boxed ("accel-closure",
189                                                        P_("Accelerator Closure"),
190                                                        P_("The closure to be monitored for accelerator changes"),
191                                                        G_TYPE_CLOSURE,
192                                                        GTK_PARAM_READWRITE));
193   g_object_class_install_property (gobject_class,
194                                    PROP_ACCEL_WIDGET,
195                                    g_param_spec_object ("accel-widget",
196                                                         P_("Accelerator Widget"),
197                                                         P_("The widget to be monitored for accelerator changes"),
198                                                         GTK_TYPE_WIDGET,
199                                                         GTK_PARAM_READWRITE));
200
201   g_type_class_add_private (gobject_class, sizeof (GtkAccelLabelPrivate));
202 }
203
204 static void
205 gtk_accel_label_set_property (GObject      *object,
206                               guint         prop_id,
207                               const GValue *value,
208                               GParamSpec   *pspec)
209 {
210   GtkAccelLabel  *accel_label;
211
212   accel_label = GTK_ACCEL_LABEL (object);
213
214   switch (prop_id)
215     {
216     case PROP_ACCEL_CLOSURE:
217       gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
218       break;
219     case PROP_ACCEL_WIDGET:
220       gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
221       break;
222     default:
223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224       break;
225     }
226 }
227
228 static void
229 gtk_accel_label_get_property (GObject    *object,
230                               guint       prop_id,
231                               GValue     *value,
232                               GParamSpec *pspec)
233 {
234   GtkAccelLabel  *accel_label;
235
236   accel_label = GTK_ACCEL_LABEL (object);
237
238   switch (prop_id)
239     {
240     case PROP_ACCEL_CLOSURE:
241       g_value_set_boxed (value, accel_label->priv->accel_closure);
242       break;
243     case PROP_ACCEL_WIDGET:
244       g_value_set_object (value, accel_label->priv->accel_widget);
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249     }
250 }
251
252 static void
253 gtk_accel_label_init (GtkAccelLabel *accel_label)
254 {
255   GtkAccelLabelPrivate *priv = GTK_ACCEL_LABEL_GET_PRIVATE (accel_label);
256
257   priv->accel_padding = 3;
258   priv->accel_widget = NULL;
259   priv->accel_closure = NULL;
260   priv->accel_group = NULL;
261   priv->accel_string = NULL;
262
263   accel_label->priv = priv;
264 }
265
266 /**
267  * gtk_accel_label_new:
268  * @string: the label string. Must be non-%NULL.
269  *
270  * Creates a new #GtkAccelLabel.
271  *
272  * Returns: a new #GtkAccelLabel.
273  */
274 GtkWidget*
275 gtk_accel_label_new (const gchar *string)
276 {
277   GtkAccelLabel *accel_label;
278   
279   g_return_val_if_fail (string != NULL, NULL);
280   
281   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
282   
283   gtk_label_set_text (GTK_LABEL (accel_label), string);
284   
285   return GTK_WIDGET (accel_label);
286 }
287
288 static void
289 gtk_accel_label_destroy (GtkWidget *widget)
290 {
291   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
292
293   gtk_accel_label_set_accel_widget (accel_label, NULL);
294   gtk_accel_label_set_accel_closure (accel_label, NULL);
295
296   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->destroy (widget);
297 }
298
299 static void
300 gtk_accel_label_finalize (GObject *object)
301 {
302   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
303
304   g_free (accel_label->priv->accel_string);
305
306   G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
307 }
308
309 /**
310  * gtk_accel_label_get_accel_widget:
311  * @accel_label: a #GtkAccelLabel
312  *
313  * Fetches the widget monitored by this accelerator label. See
314  * gtk_accel_label_set_accel_widget().
315  *
316  * Returns: (transfer none): the object monitored by the accelerator label, or %NULL.
317  **/
318 GtkWidget*
319 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
320 {
321   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
322
323   return accel_label->priv->accel_widget;
324 }
325
326 /**
327  * gtk_accel_label_get_accel_width:
328  * @accel_label: a #GtkAccelLabel.
329  *
330  * Returns the width needed to display the accelerator key(s).
331  * This is used by menus to align all of the #GtkMenuItem widgets, and shouldn't
332  * be needed by applications.
333  *
334  * Returns: the width needed to display the accelerator key(s).
335  */
336 guint
337 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
338 {
339   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
340
341   return (accel_label->priv->accel_string_width +
342           (accel_label->priv->accel_string_width ? accel_label->priv->accel_padding : 0));
343 }
344
345 static void
346 gtk_accel_label_get_preferred_width (GtkWidget       *widget,
347                                      gint            *min_width,
348                                      gint            *nat_width)
349 {
350   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
351   PangoLayout   *layout;
352   gint           width;
353
354   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->get_preferred_width (widget, min_width, nat_width);
355
356   layout = gtk_widget_create_pango_layout (GTK_WIDGET (widget), 
357                                            gtk_accel_label_get_string (accel_label));
358   pango_layout_get_pixel_size (layout, &width, NULL);
359   accel_label->priv->accel_string_width = width;
360
361   g_object_unref (layout);
362 }
363
364 static gint
365 get_first_baseline (PangoLayout *layout)
366 {
367   PangoLayoutIter *iter;
368   gint result;
369
370   iter = pango_layout_get_iter (layout);
371   result = pango_layout_iter_get_baseline (iter);
372   pango_layout_iter_free (iter);
373
374   return PANGO_PIXELS (result);
375 }
376
377 static gboolean 
378 gtk_accel_label_draw (GtkWidget *widget,
379                       cairo_t   *cr)
380 {
381   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
382   GtkMisc *misc = GTK_MISC (accel_label);
383   GtkTextDirection direction;
384   guint ac_width;
385   GtkAllocation allocation;
386   GtkRequisition requisition;
387
388   direction = gtk_widget_get_direction (widget);
389   ac_width = gtk_accel_label_get_accel_width (accel_label);
390   gtk_widget_get_allocation (widget, &allocation);
391   gtk_widget_get_preferred_size (widget, &requisition, NULL);
392
393   if (allocation.width >= requisition.width + ac_width)
394     {
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       label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
404
405       cairo_save (cr);
406
407       /* XXX: Mad hack: We modify the label's width so it renders
408        * properly in its draw function that we chain to. */
409       if (direction == GTK_TEXT_DIR_RTL)
410         cairo_translate (cr, ac_width, 0);
411       if (gtk_label_get_ellipsize (label))
412         pango_layout_set_width (label_layout,
413                                 pango_layout_get_width (label_layout) 
414                                 - ac_width * PANGO_SCALE);
415       
416       allocation.width -= ac_width;
417       gtk_widget_set_allocation (widget, &allocation);
418       if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
419         GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget,
420                                                                cr);
421       allocation.width += ac_width;
422       gtk_widget_set_allocation (widget, &allocation);
423       if (gtk_label_get_ellipsize (label))
424         pango_layout_set_width (label_layout,
425                                 pango_layout_get_width (label_layout) 
426                                 + ac_width * PANGO_SCALE);
427
428       cairo_restore (cr);
429
430       gtk_misc_get_padding (misc, &xpad, NULL);
431
432       if (direction == GTK_TEXT_DIR_RTL)
433         x = xpad;
434       else
435         x = gtk_widget_get_allocated_width (widget) - xpad - ac_width;
436
437       gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
438
439       accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
440
441       y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
442
443       gtk_paint_layout (gtk_widget_get_style (widget),
444                         cr,
445                         gtk_widget_get_state (widget),
446                         FALSE,
447                         widget,
448                         "accellabel",
449                         x, y,
450                         accel_layout);                            
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 /**
484  * gtk_accel_label_set_accel_widget:
485  * @accel_label: a #GtkAccelLabel
486  * @accel_widget: the widget to be monitored.
487  *
488  * Sets the widget to be monitored by this accelerator label. 
489  **/
490 void
491 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
492                                   GtkWidget     *accel_widget)
493 {
494   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
495   if (accel_widget)
496     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
497     
498   if (accel_widget != accel_label->priv->accel_widget)
499     {
500       if (accel_label->priv->accel_widget)
501         {
502           gtk_accel_label_set_accel_closure (accel_label, NULL);
503           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
504                                                 refetch_widget_accel_closure,
505                                                 accel_label);
506           g_object_unref (accel_label->priv->accel_widget);
507         }
508       accel_label->priv->accel_widget = accel_widget;
509       if (accel_label->priv->accel_widget)
510         {
511           g_object_ref (accel_label->priv->accel_widget);
512           g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
513                                    G_CALLBACK (refetch_widget_accel_closure),
514                                    accel_label, G_CONNECT_SWAPPED);
515           refetch_widget_accel_closure (accel_label);
516         }
517       g_object_notify (G_OBJECT (accel_label), "accel-widget");
518     }
519 }
520
521 static void
522 gtk_accel_label_reset (GtkAccelLabel *accel_label)
523 {
524   if (accel_label->priv->accel_string)
525     {
526       g_free (accel_label->priv->accel_string);
527       accel_label->priv->accel_string = NULL;
528     }
529   
530   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
531 }
532
533 static void
534 check_accel_changed (GtkAccelGroup  *accel_group,
535                      guint           keyval,
536                      GdkModifierType modifier,
537                      GClosure       *accel_closure,
538                      GtkAccelLabel  *accel_label)
539 {
540   if (accel_closure == accel_label->priv->accel_closure)
541     gtk_accel_label_reset (accel_label);
542 }
543
544 /**
545  * gtk_accel_label_set_accel_closure:
546  * @accel_label: a #GtkAccelLabel
547  * @accel_closure: the closure to monitor for accelerator changes.
548  *
549  * Sets the closure to be monitored by this accelerator label. The closure
550  * must be connected to an accelerator group; see gtk_accel_group_connect().
551  **/
552 void
553 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
554                                    GClosure      *accel_closure)
555 {
556   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
557   if (accel_closure)
558     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
559
560   if (accel_closure != accel_label->priv->accel_closure)
561     {
562       if (accel_label->priv->accel_closure)
563         {
564           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
565                                                 check_accel_changed,
566                                                 accel_label);
567           accel_label->priv->accel_group = NULL;
568           g_closure_unref (accel_label->priv->accel_closure);
569         }
570       accel_label->priv->accel_closure = accel_closure;
571       if (accel_label->priv->accel_closure)
572         {
573           g_closure_ref (accel_label->priv->accel_closure);
574           accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
575           g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
576                                    G_CALLBACK (check_accel_changed),
577                                    accel_label, 0);
578         }
579       gtk_accel_label_reset (accel_label);
580       g_object_notify (G_OBJECT (accel_label), "accel-closure");
581     }
582 }
583
584 static gboolean
585 find_accel (GtkAccelKey *key,
586             GClosure    *closure,
587             gpointer     data)
588 {
589   return data == (gpointer) closure;
590 }
591
592 static const gchar *
593 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
594 {
595   if (!accel_label->priv->accel_string)
596     gtk_accel_label_refetch (accel_label);
597   
598   return accel_label->priv->accel_string;
599 }
600
601 /* Underscores in key names are better displayed as spaces
602  * E.g., Page_Up should be "Page Up"
603  */
604 static void
605 substitute_underscores (char *str)
606 {
607   char *p;
608
609   for (p = str; *p; p++)
610     if (*p == '_')
611       *p = ' ';
612 }
613
614 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
615  * append it to gstring and return TRUE; otherwise return FALSE.
616  * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html 
617  * for the list of special keys. */
618 static gboolean
619 append_keyval_symbol (guint    accelerator_key,
620                       GString *gstring)
621 {
622 #ifdef GDK_WINDOWING_QUARTZ
623   switch (accelerator_key)
624   {
625   case GDK_KEY_Return:
626     /* U+21A9 LEFTWARDS ARROW WITH HOOK */
627     g_string_append (gstring, "\xe2\x86\xa9");
628     return TRUE;
629
630   case GDK_KEY_ISO_Enter:
631     /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
632     g_string_append (gstring, "\xe2\x8c\xa4");
633     return TRUE;
634
635   case GDK_KEY_Left:
636     /* U+2190 LEFTWARDS ARROW */
637     g_string_append (gstring, "\xe2\x86\x90");
638     return TRUE;
639
640   case GDK_KEY_Up:
641     /* U+2191 UPWARDS ARROW */
642     g_string_append (gstring, "\xe2\x86\x91");
643     return TRUE;
644
645   case GDK_KEY_Right:
646     /* U+2192 RIGHTWARDS ARROW */
647     g_string_append (gstring, "\xe2\x86\x92");
648     return TRUE;
649
650   case GDK_KEY_Down:
651     /* U+2193 DOWNWARDS ARROW */
652     g_string_append (gstring, "\xe2\x86\x93");
653     return TRUE;
654
655   case GDK_KEY_Page_Up:
656     /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
657     g_string_append (gstring, "\xe2\x87\x9e");
658     return TRUE;
659
660   case GDK_KEY_Page_Down:
661     /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
662     g_string_append (gstring, "\xe2\x87\x9f");
663     return TRUE;
664
665   case GDK_KEY_Home:
666     /* U+2196 NORTH WEST ARROW */
667     g_string_append (gstring, "\xe2\x86\x96");
668     return TRUE;
669
670   case GDK_KEY_End:
671     /* U+2198 SOUTH EAST ARROW */
672     g_string_append (gstring, "\xe2\x86\x98");
673     return TRUE;
674
675   case GDK_KEY_Escape:
676     /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
677     g_string_append (gstring, "\xe2\x8e\x8b");
678     return TRUE;
679
680   case GDK_KEY_BackSpace:
681     /* U+232B ERASE TO THE LEFT */
682     g_string_append (gstring, "\xe2\x8c\xab");
683     return TRUE;
684
685   case GDK_KEY_Delete:
686     /* U+2326 ERASE TO THE RIGHT */
687     g_string_append (gstring, "\xe2\x8c\xa6");
688     return TRUE;
689
690   default:
691     return FALSE;
692   }
693 #else /* !GDK_WINDOWING_QUARTZ */
694   return FALSE;
695 #endif
696 }
697
698 gchar *
699 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
700                                               guint               accelerator_key,
701                                               GdkModifierType     accelerator_mods)
702 {
703   GString *gstring;
704   gboolean seen_mod = FALSE;
705   gunichar ch;
706   
707   gstring = g_string_new ("");
708   
709   if (accelerator_mods & GDK_SHIFT_MASK)
710     {
711       g_string_append (gstring, klass->mod_name_shift);
712       seen_mod = TRUE;
713     }
714   if (accelerator_mods & GDK_CONTROL_MASK)
715     {
716       if (seen_mod)
717         g_string_append (gstring, klass->mod_separator);
718       g_string_append (gstring, klass->mod_name_control);
719       seen_mod = TRUE;
720     }
721   if (accelerator_mods & GDK_MOD1_MASK)
722     {
723       if (seen_mod)
724         g_string_append (gstring, klass->mod_separator);
725       g_string_append (gstring, klass->mod_name_alt);
726       seen_mod = TRUE;
727     }
728   if (accelerator_mods & GDK_MOD2_MASK)
729     {
730       if (seen_mod)
731         g_string_append (gstring, klass->mod_separator);
732
733       g_string_append (gstring, "Mod2");
734       seen_mod = TRUE;
735     }
736   if (accelerator_mods & GDK_MOD3_MASK)
737     {
738       if (seen_mod)
739         g_string_append (gstring, klass->mod_separator);
740
741       g_string_append (gstring, "Mod3");
742       seen_mod = TRUE;
743     }
744   if (accelerator_mods & GDK_MOD4_MASK)
745     {
746       if (seen_mod)
747         g_string_append (gstring, klass->mod_separator);
748
749       g_string_append (gstring, "Mod4");
750       seen_mod = TRUE;
751     }
752   if (accelerator_mods & GDK_MOD5_MASK)
753     {
754       if (seen_mod)
755         g_string_append (gstring, klass->mod_separator);
756
757       g_string_append (gstring, "Mod5");
758       seen_mod = TRUE;
759     }
760   if (accelerator_mods & GDK_SUPER_MASK)
761     {
762       if (seen_mod)
763         g_string_append (gstring, klass->mod_separator);
764
765       /* This is the text that should appear next to menu accelerators
766        * that use the super key. If the text on this key isn't typically
767        * translated on keyboards used for your language, don't translate
768        * this.
769        */
770       g_string_append (gstring, C_("keyboard label", "Super"));
771       seen_mod = TRUE;
772     }
773   if (accelerator_mods & GDK_HYPER_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 hyper 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", "Hyper"));
784       seen_mod = TRUE;
785     }
786   if (accelerator_mods & GDK_META_MASK)
787     {
788       if (seen_mod)
789         g_string_append (gstring, klass->mod_separator);
790
791 #ifndef GDK_WINDOWING_QUARTZ
792       /* This is the text that should appear next to menu accelerators
793        * that use the meta key. If the text on this key isn't typically
794        * translated on keyboards used for your language, don't translate
795        * this.
796        */
797       g_string_append (gstring, C_("keyboard label", "Meta"));
798 #else
799       /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
800       g_string_append (gstring, "\xe2\x8c\x98");
801 #endif
802       seen_mod = TRUE;
803     }
804   if (seen_mod)
805     g_string_append (gstring, klass->mod_separator);
806   
807   ch = gdk_keyval_to_unicode (accelerator_key);
808   if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
809     {
810       switch (ch)
811         {
812         case ' ':
813           g_string_append (gstring, C_("keyboard label", "Space"));
814           break;
815         case '\\':
816           g_string_append (gstring, C_("keyboard label", "Backslash"));
817           break;
818         default:
819           g_string_append_unichar (gstring, g_unichar_toupper (ch));
820           break;
821         }
822     }
823   else if (!append_keyval_symbol (accelerator_key, gstring))
824     {
825       gchar *tmp;
826
827       tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
828       if (tmp != NULL)
829         {
830           if (tmp[0] != 0 && tmp[1] == 0)
831             g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
832           else
833             {
834               const gchar *str;
835               str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
836               if (str == tmp)
837                 {
838                   g_string_append (gstring, tmp);
839                   substitute_underscores (gstring->str);
840                 }
841               else
842                 g_string_append (gstring, str);
843             }
844         }
845     }
846
847   return g_string_free (gstring, FALSE);
848 }
849
850 /**
851  * gtk_accel_label_refetch:
852  * @accel_label: a #GtkAccelLabel.
853  *
854  * Recreates the string representing the accelerator keys.
855  * This should not be needed since the string is automatically updated whenever
856  * accelerators are added or removed from the associated widget.
857  *
858  * Returns: always returns %FALSE.
859  */
860 gboolean
861 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
862 {
863   gboolean enable_accels;
864
865   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
866
867   if (accel_label->priv->accel_string)
868     {
869       g_free (accel_label->priv->accel_string);
870       accel_label->priv->accel_string = NULL;
871     }
872
873   g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
874                 "gtk-enable-accels", &enable_accels,
875                 NULL);
876
877   if (enable_accels && accel_label->priv->accel_closure)
878     {
879       GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
880
881       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
882         {
883           GtkAccelLabelClass *klass;
884           gchar *tmp;
885
886           klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
887           tmp = _gtk_accel_label_class_get_accelerator_label (klass,
888                                                               key->accel_key,
889                                                               key->accel_mods);
890           accel_label->priv->accel_string = g_strconcat ("   ", tmp, NULL);
891           g_free (tmp);
892         }
893       if (!accel_label->priv->accel_string)
894         accel_label->priv->accel_string = g_strdup ("-/-");
895     }
896   
897   if (!accel_label->priv->accel_string)
898     accel_label->priv->accel_string = g_strdup ("");
899
900   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
901
902   return FALSE;
903 }