]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Merge branch 'master' into broadway
[~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_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   guint          accel_padding;      /* should be style property? */
104   GtkWidget     *accel_widget;       /* done*/
105   GClosure      *accel_closure;      /* has set function */
106   GtkAccelGroup *accel_group;        /* set by set_accel_closure() */
107   gchar         *accel_string;       /* has set function */
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 #define GTK_ACCEL_LABEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ACCEL_LABEL, GtkAccelLabelPrivate))
131
132
133 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
134
135 static void
136 gtk_accel_label_class_init (GtkAccelLabelClass *class)
137 {
138   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
139   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
140   
141   gobject_class->finalize = gtk_accel_label_finalize;
142   gobject_class->set_property = gtk_accel_label_set_property;
143   gobject_class->get_property = gtk_accel_label_get_property;
144
145   widget_class->draw = gtk_accel_label_draw;
146   widget_class->get_preferred_width = gtk_accel_label_get_preferred_width;
147   widget_class->destroy = gtk_accel_label_destroy;
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 = GTK_ACCEL_LABEL_GET_PRIVATE (accel_label);
254
255   priv->accel_padding = 3;
256   priv->accel_widget = NULL;
257   priv->accel_closure = NULL;
258   priv->accel_group = NULL;
259   priv->accel_string = NULL;
260
261   accel_label->priv = priv;
262 }
263
264 /**
265  * gtk_accel_label_new:
266  * @string: the label string. Must be non-%NULL.
267  *
268  * Creates a new #GtkAccelLabel.
269  *
270  * Returns: a new #GtkAccelLabel.
271  */
272 GtkWidget*
273 gtk_accel_label_new (const gchar *string)
274 {
275   GtkAccelLabel *accel_label;
276   
277   g_return_val_if_fail (string != NULL, NULL);
278   
279   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
280   
281   gtk_label_set_text (GTK_LABEL (accel_label), string);
282   
283   return GTK_WIDGET (accel_label);
284 }
285
286 static void
287 gtk_accel_label_destroy (GtkWidget *widget)
288 {
289   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
290
291   gtk_accel_label_set_accel_widget (accel_label, NULL);
292   gtk_accel_label_set_accel_closure (accel_label, NULL);
293
294   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->destroy (widget);
295 }
296
297 static void
298 gtk_accel_label_finalize (GObject *object)
299 {
300   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
301
302   g_free (accel_label->priv->accel_string);
303
304   G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
305 }
306
307 /**
308  * gtk_accel_label_get_accel_widget:
309  * @accel_label: a #GtkAccelLabel
310  *
311  * Fetches the widget monitored by this accelerator label. See
312  * gtk_accel_label_set_accel_widget().
313  *
314  * Returns: (transfer none): the object monitored by the accelerator label, or %NULL.
315  **/
316 GtkWidget*
317 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
318 {
319   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
320
321   return accel_label->priv->accel_widget;
322 }
323
324 /**
325  * gtk_accel_label_get_accel_width:
326  * @accel_label: a #GtkAccelLabel.
327  *
328  * Returns the width needed to display the accelerator key(s).
329  * This is used by menus to align all of the #GtkMenuItem widgets, and shouldn't
330  * be needed by applications.
331  *
332  * Returns: the width needed to display the accelerator key(s).
333  */
334 guint
335 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
336 {
337   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
338
339   return (accel_label->priv->accel_string_width +
340           (accel_label->priv->accel_string_width ? accel_label->priv->accel_padding : 0));
341 }
342
343 static void
344 gtk_accel_label_get_preferred_width (GtkWidget       *widget,
345                                      gint            *min_width,
346                                      gint            *nat_width)
347 {
348   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
349   PangoLayout   *layout;
350   gint           width;
351
352   GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->get_preferred_width (widget, min_width, nat_width);
353
354   layout = gtk_widget_create_pango_layout (GTK_WIDGET (widget), 
355                                            gtk_accel_label_get_string (accel_label));
356   pango_layout_get_pixel_size (layout, &width, NULL);
357   accel_label->priv->accel_string_width = width;
358
359   g_object_unref (layout);
360 }
361
362 static gint
363 get_first_baseline (PangoLayout *layout)
364 {
365   PangoLayoutIter *iter;
366   gint result;
367
368   iter = pango_layout_get_iter (layout);
369   result = pango_layout_iter_get_baseline (iter);
370   pango_layout_iter_free (iter);
371
372   return PANGO_PIXELS (result);
373 }
374
375 static gboolean 
376 gtk_accel_label_draw (GtkWidget *widget,
377                       cairo_t   *cr)
378 {
379   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
380   GtkMisc *misc = GTK_MISC (accel_label);
381   GtkTextDirection direction;
382   guint ac_width;
383   GtkAllocation allocation;
384   GtkRequisition requisition;
385
386   direction = gtk_widget_get_direction (widget);
387   ac_width = gtk_accel_label_get_accel_width (accel_label);
388   gtk_widget_get_allocation (widget, &allocation);
389   gtk_widget_get_preferred_size (widget, &requisition, NULL);
390
391   if (allocation.width >= requisition.width + ac_width)
392     {
393       GtkStyleContext *context;
394       PangoLayout *label_layout;
395       PangoLayout *accel_layout;
396       GtkLabel *label = GTK_LABEL (widget);
397
398       gint x;
399       gint y;
400       gint xpad;
401
402       context = gtk_widget_get_style_context (widget);
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_style_context_save (context);
444       gtk_style_context_add_class (context, GTK_STYLE_CLASS_ACCELERATOR);
445       gtk_style_context_set_state (context, gtk_widget_get_state_flags (widget));
446
447       gtk_render_layout (context, cr, x, y, accel_layout);
448       gtk_style_context_restore (context);
449
450       g_object_unref (accel_layout);
451     }
452   else
453     {
454       if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
455         GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
456     }
457   
458   return FALSE;
459 }
460
461 static void
462 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
463 {
464   GClosure *closure = NULL;
465   GList *clist, *list;
466   
467   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
468   g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
469   
470   clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
471   for (list = clist; list; list = list->next)
472     {
473       /* we just take the first closure used */
474       closure = list->data;
475       break;
476     }
477   g_list_free (clist);
478   gtk_accel_label_set_accel_closure (accel_label, closure);
479 }
480
481 /**
482  * gtk_accel_label_set_accel_widget:
483  * @accel_label: a #GtkAccelLabel
484  * @accel_widget: the widget to be monitored.
485  *
486  * Sets the widget to be monitored by this accelerator label. 
487  **/
488 void
489 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
490                                   GtkWidget     *accel_widget)
491 {
492   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
493   if (accel_widget)
494     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
495     
496   if (accel_widget != accel_label->priv->accel_widget)
497     {
498       if (accel_label->priv->accel_widget)
499         {
500           gtk_accel_label_set_accel_closure (accel_label, NULL);
501           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
502                                                 refetch_widget_accel_closure,
503                                                 accel_label);
504           g_object_unref (accel_label->priv->accel_widget);
505         }
506       accel_label->priv->accel_widget = accel_widget;
507       if (accel_label->priv->accel_widget)
508         {
509           g_object_ref (accel_label->priv->accel_widget);
510           g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
511                                    G_CALLBACK (refetch_widget_accel_closure),
512                                    accel_label, G_CONNECT_SWAPPED);
513           refetch_widget_accel_closure (accel_label);
514         }
515       g_object_notify (G_OBJECT (accel_label), "accel-widget");
516     }
517 }
518
519 static void
520 gtk_accel_label_reset (GtkAccelLabel *accel_label)
521 {
522   if (accel_label->priv->accel_string)
523     {
524       g_free (accel_label->priv->accel_string);
525       accel_label->priv->accel_string = NULL;
526     }
527   
528   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
529 }
530
531 static void
532 check_accel_changed (GtkAccelGroup  *accel_group,
533                      guint           keyval,
534                      GdkModifierType modifier,
535                      GClosure       *accel_closure,
536                      GtkAccelLabel  *accel_label)
537 {
538   if (accel_closure == accel_label->priv->accel_closure)
539     gtk_accel_label_reset (accel_label);
540 }
541
542 /**
543  * gtk_accel_label_set_accel_closure:
544  * @accel_label: a #GtkAccelLabel
545  * @accel_closure: the closure to monitor for accelerator changes.
546  *
547  * Sets the closure to be monitored by this accelerator label. The closure
548  * must be connected to an accelerator group; see gtk_accel_group_connect().
549  **/
550 void
551 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
552                                    GClosure      *accel_closure)
553 {
554   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
555   if (accel_closure)
556     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
557
558   if (accel_closure != accel_label->priv->accel_closure)
559     {
560       if (accel_label->priv->accel_closure)
561         {
562           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
563                                                 check_accel_changed,
564                                                 accel_label);
565           accel_label->priv->accel_group = NULL;
566           g_closure_unref (accel_label->priv->accel_closure);
567         }
568       accel_label->priv->accel_closure = accel_closure;
569       if (accel_label->priv->accel_closure)
570         {
571           g_closure_ref (accel_label->priv->accel_closure);
572           accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
573           g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
574                                    G_CALLBACK (check_accel_changed),
575                                    accel_label, 0);
576         }
577       gtk_accel_label_reset (accel_label);
578       g_object_notify (G_OBJECT (accel_label), "accel-closure");
579     }
580 }
581
582 static gboolean
583 find_accel (GtkAccelKey *key,
584             GClosure    *closure,
585             gpointer     data)
586 {
587   return data == (gpointer) closure;
588 }
589
590 static const gchar *
591 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
592 {
593   if (!accel_label->priv->accel_string)
594     gtk_accel_label_refetch (accel_label);
595   
596   return accel_label->priv->accel_string;
597 }
598
599 /* Underscores in key names are better displayed as spaces
600  * E.g., Page_Up should be "Page Up".
601  *
602  * Some keynames also have prefixes that are not suitable
603  * for display, e.g XF86AudioMute, so strip those out, too.
604  *
605  * This function is only called on untranslated keynames,
606  * so no need to be UTF-8 safe.
607  */
608 static void
609 append_without_underscores (GString *s,
610                             gchar   *str)
611 {
612   gchar *p;
613
614   if (g_str_has_prefix (str, "XF86"))
615     p = str + 4;
616   else if (g_str_has_prefix (str, "ISO_"))
617     p = str + 4;
618   else
619     p = str;
620
621   for ( ; *p; p++)
622     {
623       if (*p == '_')
624         g_string_append_c (s, ' ');
625       else
626         g_string_append_c (s, *p);
627     }
628 }
629
630 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
631  * append it to gstring and return TRUE; otherwise return FALSE.
632  * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html 
633  * for the list of special keys. */
634 static gboolean
635 append_keyval_symbol (guint    accelerator_key,
636                       GString *gstring)
637 {
638 #ifdef GDK_WINDOWING_QUARTZ
639   switch (accelerator_key)
640   {
641   case GDK_KEY_Return:
642     /* U+21A9 LEFTWARDS ARROW WITH HOOK */
643     g_string_append (gstring, "\xe2\x86\xa9");
644     return TRUE;
645
646   case GDK_KEY_ISO_Enter:
647     /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
648     g_string_append (gstring, "\xe2\x8c\xa4");
649     return TRUE;
650
651   case GDK_KEY_Left:
652     /* U+2190 LEFTWARDS ARROW */
653     g_string_append (gstring, "\xe2\x86\x90");
654     return TRUE;
655
656   case GDK_KEY_Up:
657     /* U+2191 UPWARDS ARROW */
658     g_string_append (gstring, "\xe2\x86\x91");
659     return TRUE;
660
661   case GDK_KEY_Right:
662     /* U+2192 RIGHTWARDS ARROW */
663     g_string_append (gstring, "\xe2\x86\x92");
664     return TRUE;
665
666   case GDK_KEY_Down:
667     /* U+2193 DOWNWARDS ARROW */
668     g_string_append (gstring, "\xe2\x86\x93");
669     return TRUE;
670
671   case GDK_KEY_Page_Up:
672     /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
673     g_string_append (gstring, "\xe2\x87\x9e");
674     return TRUE;
675
676   case GDK_KEY_Page_Down:
677     /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
678     g_string_append (gstring, "\xe2\x87\x9f");
679     return TRUE;
680
681   case GDK_KEY_Home:
682     /* U+2196 NORTH WEST ARROW */
683     g_string_append (gstring, "\xe2\x86\x96");
684     return TRUE;
685
686   case GDK_KEY_End:
687     /* U+2198 SOUTH EAST ARROW */
688     g_string_append (gstring, "\xe2\x86\x98");
689     return TRUE;
690
691   case GDK_KEY_Escape:
692     /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
693     g_string_append (gstring, "\xe2\x8e\x8b");
694     return TRUE;
695
696   case GDK_KEY_BackSpace:
697     /* U+232B ERASE TO THE LEFT */
698     g_string_append (gstring, "\xe2\x8c\xab");
699     return TRUE;
700
701   case GDK_KEY_Delete:
702     /* U+2326 ERASE TO THE RIGHT */
703     g_string_append (gstring, "\xe2\x8c\xa6");
704     return TRUE;
705
706   default:
707     return FALSE;
708   }
709 #else /* !GDK_WINDOWING_QUARTZ */
710   return FALSE;
711 #endif
712 }
713
714 gchar *
715 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
716                                               guint               accelerator_key,
717                                               GdkModifierType     accelerator_mods)
718 {
719   GString *gstring;
720   gboolean seen_mod = FALSE;
721   gunichar ch;
722   
723   gstring = g_string_new ("");
724   
725   if (accelerator_mods & GDK_SHIFT_MASK)
726     {
727       g_string_append (gstring, klass->mod_name_shift);
728       seen_mod = TRUE;
729     }
730   if (accelerator_mods & GDK_CONTROL_MASK)
731     {
732       if (seen_mod)
733         g_string_append (gstring, klass->mod_separator);
734       g_string_append (gstring, klass->mod_name_control);
735       seen_mod = TRUE;
736     }
737   if (accelerator_mods & GDK_MOD1_MASK)
738     {
739       if (seen_mod)
740         g_string_append (gstring, klass->mod_separator);
741       g_string_append (gstring, klass->mod_name_alt);
742       seen_mod = TRUE;
743     }
744   if (accelerator_mods & GDK_MOD2_MASK)
745     {
746       if (seen_mod)
747         g_string_append (gstring, klass->mod_separator);
748
749       g_string_append (gstring, "Mod2");
750       seen_mod = TRUE;
751     }
752   if (accelerator_mods & GDK_MOD3_MASK)
753     {
754       if (seen_mod)
755         g_string_append (gstring, klass->mod_separator);
756
757       g_string_append (gstring, "Mod3");
758       seen_mod = TRUE;
759     }
760   if (accelerator_mods & GDK_MOD4_MASK)
761     {
762       if (seen_mod)
763         g_string_append (gstring, klass->mod_separator);
764
765       g_string_append (gstring, "Mod4");
766       seen_mod = TRUE;
767     }
768   if (accelerator_mods & GDK_MOD5_MASK)
769     {
770       if (seen_mod)
771         g_string_append (gstring, klass->mod_separator);
772
773       g_string_append (gstring, "Mod5");
774       seen_mod = TRUE;
775     }
776   if (accelerator_mods & GDK_SUPER_MASK)
777     {
778       if (seen_mod)
779         g_string_append (gstring, klass->mod_separator);
780
781       /* This is the text that should appear next to menu accelerators
782        * that use the super key. If the text on this key isn't typically
783        * translated on keyboards used for your language, don't translate
784        * this.
785        */
786       g_string_append (gstring, C_("keyboard label", "Super"));
787       seen_mod = TRUE;
788     }
789   if (accelerator_mods & GDK_HYPER_MASK)
790     {
791       if (seen_mod)
792         g_string_append (gstring, klass->mod_separator);
793
794       /* This is the text that should appear next to menu accelerators
795        * that use the hyper key. If the text on this key isn't typically
796        * translated on keyboards used for your language, don't translate
797        * this.
798        */
799       g_string_append (gstring, C_("keyboard label", "Hyper"));
800       seen_mod = TRUE;
801     }
802   if (accelerator_mods & GDK_META_MASK)
803     {
804       if (seen_mod)
805         g_string_append (gstring, klass->mod_separator);
806
807 #ifndef GDK_WINDOWING_QUARTZ
808       /* This is the text that should appear next to menu accelerators
809        * that use the meta key. If the text on this key isn't typically
810        * translated on keyboards used for your language, don't translate
811        * this.
812        */
813       g_string_append (gstring, C_("keyboard label", "Meta"));
814 #else
815       /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
816       g_string_append (gstring, "\xe2\x8c\x98");
817 #endif
818       seen_mod = TRUE;
819     }
820   if (seen_mod)
821     g_string_append (gstring, klass->mod_separator);
822   
823   ch = gdk_keyval_to_unicode (accelerator_key);
824   if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
825     {
826       switch (ch)
827         {
828         case ' ':
829           g_string_append (gstring, C_("keyboard label", "Space"));
830           break;
831         case '\\':
832           g_string_append (gstring, C_("keyboard label", "Backslash"));
833           break;
834         default:
835           g_string_append_unichar (gstring, g_unichar_toupper (ch));
836           break;
837         }
838     }
839   else if (!append_keyval_symbol (accelerator_key, gstring))
840     {
841       gchar *tmp;
842
843       tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
844       if (tmp != NULL)
845         {
846           if (tmp[0] != 0 && tmp[1] == 0)
847             g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
848           else
849             {
850               const gchar *str;
851               str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
852               if (str == tmp)
853                 append_without_underscores (gstring, tmp);
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 }