]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Fixed accel label to do its derived work in ->get_width() instead of the old ->size_r...
[~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: 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_widget_size_request (widget, &requisition);
409
410       if (allocation.width >= requisition.width + ac_width)
411         {
412           PangoLayout *label_layout;
413           PangoLayout *accel_layout;
414           GtkLabel *label = GTK_LABEL (widget);
415
416           gint x;
417           gint y;
418           gint xpad;
419
420           label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
421
422           if (direction == GTK_TEXT_DIR_RTL)
423             allocation.x += ac_width;
424           allocation.width -= ac_width;
425           if (gtk_label_get_ellipsize (label))
426             pango_layout_set_width (label_layout,
427                                     pango_layout_get_width (label_layout) 
428                                     - ac_width * PANGO_SCALE);
429           
430           if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
431             GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
432           if (direction == GTK_TEXT_DIR_RTL)
433             allocation.x -= ac_width;
434           allocation.width += ac_width;
435           if (gtk_label_get_ellipsize (label))
436             pango_layout_set_width (label_layout,
437                                     pango_layout_get_width (label_layout) 
438                                     + ac_width * PANGO_SCALE);
439
440           gtk_misc_get_padding (misc, &xpad, NULL);
441
442           if (direction == GTK_TEXT_DIR_RTL)
443             x = allocation.x + xpad;
444           else
445             x = allocation.x + allocation.width - xpad - ac_width;
446
447           gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
448
449           accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
450
451           y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
452
453           gtk_paint_layout (gtk_widget_get_style (widget),
454                             gtk_widget_get_window (widget),
455                             gtk_widget_get_state (widget),
456                             FALSE,
457                             &event->area,
458                             widget,
459                             "accellabel",
460                             x, y,
461                             accel_layout);                            
462
463           g_object_unref (accel_layout);
464         }
465       else
466         {
467           if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
468             GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
469         }
470     }
471   
472   return FALSE;
473 }
474
475 static void
476 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
477 {
478   GClosure *closure = NULL;
479   GList *clist, *list;
480   
481   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
482   g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
483   
484   clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
485   for (list = clist; list; list = list->next)
486     {
487       /* we just take the first closure used */
488       closure = list->data;
489       break;
490     }
491   g_list_free (clist);
492   gtk_accel_label_set_accel_closure (accel_label, closure);
493 }
494
495 /**
496  * gtk_accel_label_set_accel_widget:
497  * @accel_label: a #GtkAccelLabel
498  * @accel_widget: the widget to be monitored.
499  *
500  * Sets the widget to be monitored by this accelerator label. 
501  **/
502 void
503 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
504                                   GtkWidget     *accel_widget)
505 {
506   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
507   if (accel_widget)
508     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
509     
510   if (accel_widget != accel_label->priv->accel_widget)
511     {
512       if (accel_label->priv->accel_widget)
513         {
514           gtk_accel_label_set_accel_closure (accel_label, NULL);
515           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
516                                                 refetch_widget_accel_closure,
517                                                 accel_label);
518           g_object_unref (accel_label->priv->accel_widget);
519         }
520       accel_label->priv->accel_widget = accel_widget;
521       if (accel_label->priv->accel_widget)
522         {
523           g_object_ref (accel_label->priv->accel_widget);
524           g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
525                                    G_CALLBACK (refetch_widget_accel_closure),
526                                    accel_label, G_CONNECT_SWAPPED);
527           refetch_widget_accel_closure (accel_label);
528         }
529       g_object_notify (G_OBJECT (accel_label), "accel-widget");
530     }
531 }
532
533 static void
534 gtk_accel_label_reset (GtkAccelLabel *accel_label)
535 {
536   if (accel_label->priv->accel_string)
537     {
538       g_free (accel_label->priv->accel_string);
539       accel_label->priv->accel_string = NULL;
540     }
541   
542   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
543 }
544
545 static void
546 check_accel_changed (GtkAccelGroup  *accel_group,
547                      guint           keyval,
548                      GdkModifierType modifier,
549                      GClosure       *accel_closure,
550                      GtkAccelLabel  *accel_label)
551 {
552   if (accel_closure == accel_label->priv->accel_closure)
553     gtk_accel_label_reset (accel_label);
554 }
555
556 /**
557  * gtk_accel_label_set_accel_closure:
558  * @accel_label: a #GtkAccelLabel
559  * @accel_closure: the closure to monitor for accelerator changes.
560  *
561  * Sets the closure to be monitored by this accelerator label. The closure
562  * must be connected to an accelerator group; see gtk_accel_group_connect().
563  **/
564 void
565 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
566                                    GClosure      *accel_closure)
567 {
568   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
569   if (accel_closure)
570     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
571
572   if (accel_closure != accel_label->priv->accel_closure)
573     {
574       if (accel_label->priv->accel_closure)
575         {
576           g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
577                                                 check_accel_changed,
578                                                 accel_label);
579           accel_label->priv->accel_group = NULL;
580           g_closure_unref (accel_label->priv->accel_closure);
581         }
582       accel_label->priv->accel_closure = accel_closure;
583       if (accel_label->priv->accel_closure)
584         {
585           g_closure_ref (accel_label->priv->accel_closure);
586           accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
587           g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
588                                    G_CALLBACK (check_accel_changed),
589                                    accel_label, 0);
590         }
591       gtk_accel_label_reset (accel_label);
592       g_object_notify (G_OBJECT (accel_label), "accel-closure");
593     }
594 }
595
596 static gboolean
597 find_accel (GtkAccelKey *key,
598             GClosure    *closure,
599             gpointer     data)
600 {
601   return data == (gpointer) closure;
602 }
603
604 static const gchar *
605 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
606 {
607   if (!accel_label->priv->accel_string)
608     gtk_accel_label_refetch (accel_label);
609   
610   return accel_label->priv->accel_string;
611 }
612
613 /* Underscores in key names are better displayed as spaces
614  * E.g., Page_Up should be "Page Up"
615  */
616 static void
617 substitute_underscores (char *str)
618 {
619   char *p;
620
621   for (p = str; *p; p++)
622     if (*p == '_')
623       *p = ' ';
624 }
625
626 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
627  * append it to gstring and return TRUE; otherwise return FALSE.
628  * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html 
629  * for the list of special keys. */
630 static gboolean
631 append_keyval_symbol (guint    accelerator_key,
632                       GString *gstring)
633 {
634 #ifdef GDK_WINDOWING_QUARTZ
635   switch (accelerator_key)
636   {
637   case GDK_Return:
638     /* U+21A9 LEFTWARDS ARROW WITH HOOK */
639     g_string_append (gstring, "\xe2\x86\xa9");
640     return TRUE;
641
642   case GDK_ISO_Enter:
643     /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
644     g_string_append (gstring, "\xe2\x8c\xa4");
645     return TRUE;
646
647   case GDK_Left:
648     /* U+2190 LEFTWARDS ARROW */
649     g_string_append (gstring, "\xe2\x86\x90");
650     return TRUE;
651
652   case GDK_Up:
653     /* U+2191 UPWARDS ARROW */
654     g_string_append (gstring, "\xe2\x86\x91");
655     return TRUE;
656
657   case GDK_Right:
658     /* U+2192 RIGHTWARDS ARROW */
659     g_string_append (gstring, "\xe2\x86\x92");
660     return TRUE;
661
662   case GDK_Down:
663     /* U+2193 DOWNWARDS ARROW */
664     g_string_append (gstring, "\xe2\x86\x93");
665     return TRUE;
666
667   case GDK_Page_Up:
668     /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
669     g_string_append (gstring, "\xe2\x87\x9e");
670     return TRUE;
671
672   case GDK_Page_Down:
673     /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
674     g_string_append (gstring, "\xe2\x87\x9f");
675     return TRUE;
676
677   case GDK_Home:
678     /* U+2196 NORTH WEST ARROW */
679     g_string_append (gstring, "\xe2\x86\x96");
680     return TRUE;
681
682   case GDK_End:
683     /* U+2198 SOUTH EAST ARROW */
684     g_string_append (gstring, "\xe2\x86\x98");
685     return TRUE;
686
687   case GDK_Escape:
688     /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
689     g_string_append (gstring, "\xe2\x8e\x8b");
690     return TRUE;
691
692   case GDK_BackSpace:
693     /* U+232B ERASE TO THE LEFT */
694     g_string_append (gstring, "\xe2\x8c\xab");
695     return TRUE;
696
697   case GDK_Delete:
698     /* U+2326 ERASE TO THE RIGHT */
699     g_string_append (gstring, "\xe2\x8c\xa6");
700     return TRUE;
701
702   default:
703     return FALSE;
704   }
705 #else /* !GDK_WINDOWING_QUARTZ */
706   return FALSE;
707 #endif
708 }
709
710 gchar *
711 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
712                                               guint               accelerator_key,
713                                               GdkModifierType     accelerator_mods)
714 {
715   GString *gstring;
716   gboolean seen_mod = FALSE;
717   gunichar ch;
718   
719   gstring = g_string_new ("");
720   
721   if (accelerator_mods & GDK_SHIFT_MASK)
722     {
723       g_string_append (gstring, klass->mod_name_shift);
724       seen_mod = TRUE;
725     }
726   if (accelerator_mods & GDK_CONTROL_MASK)
727     {
728       if (seen_mod)
729         g_string_append (gstring, klass->mod_separator);
730       g_string_append (gstring, klass->mod_name_control);
731       seen_mod = TRUE;
732     }
733   if (accelerator_mods & GDK_MOD1_MASK)
734     {
735       if (seen_mod)
736         g_string_append (gstring, klass->mod_separator);
737       g_string_append (gstring, klass->mod_name_alt);
738       seen_mod = TRUE;
739     }
740   if (accelerator_mods & GDK_MOD2_MASK)
741     {
742       if (seen_mod)
743         g_string_append (gstring, klass->mod_separator);
744
745       g_string_append (gstring, "Mod2");
746       seen_mod = TRUE;
747     }
748   if (accelerator_mods & GDK_MOD3_MASK)
749     {
750       if (seen_mod)
751         g_string_append (gstring, klass->mod_separator);
752
753       g_string_append (gstring, "Mod3");
754       seen_mod = TRUE;
755     }
756   if (accelerator_mods & GDK_MOD4_MASK)
757     {
758       if (seen_mod)
759         g_string_append (gstring, klass->mod_separator);
760
761       g_string_append (gstring, "Mod4");
762       seen_mod = TRUE;
763     }
764   if (accelerator_mods & GDK_MOD5_MASK)
765     {
766       if (seen_mod)
767         g_string_append (gstring, klass->mod_separator);
768
769       g_string_append (gstring, "Mod5");
770       seen_mod = TRUE;
771     }
772   if (accelerator_mods & GDK_SUPER_MASK)
773     {
774       if (seen_mod)
775         g_string_append (gstring, klass->mod_separator);
776
777       /* This is the text that should appear next to menu accelerators
778        * that use the super key. If the text on this key isn't typically
779        * translated on keyboards used for your language, don't translate
780        * this.
781        */
782       g_string_append (gstring, C_("keyboard label", "Super"));
783       seen_mod = TRUE;
784     }
785   if (accelerator_mods & GDK_HYPER_MASK)
786     {
787       if (seen_mod)
788         g_string_append (gstring, klass->mod_separator);
789
790       /* This is the text that should appear next to menu accelerators
791        * that use the hyper key. If the text on this key isn't typically
792        * translated on keyboards used for your language, don't translate
793        * this.
794        */
795       g_string_append (gstring, C_("keyboard label", "Hyper"));
796       seen_mod = TRUE;
797     }
798   if (accelerator_mods & GDK_META_MASK)
799     {
800       if (seen_mod)
801         g_string_append (gstring, klass->mod_separator);
802
803 #ifndef GDK_WINDOWING_QUARTZ
804       /* This is the text that should appear next to menu accelerators
805        * that use the meta key. If the text on this key isn't typically
806        * translated on keyboards used for your language, don't translate
807        * this.
808        */
809       g_string_append (gstring, C_("keyboard label", "Meta"));
810 #else
811       /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
812       g_string_append (gstring, "\xe2\x8c\x98");
813 #endif
814       seen_mod = TRUE;
815     }
816   if (seen_mod)
817     g_string_append (gstring, klass->mod_separator);
818   
819   ch = gdk_keyval_to_unicode (accelerator_key);
820   if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
821     {
822       switch (ch)
823         {
824         case ' ':
825           g_string_append (gstring, C_("keyboard label", "Space"));
826           break;
827         case '\\':
828           g_string_append (gstring, C_("keyboard label", "Backslash"));
829           break;
830         default:
831           g_string_append_unichar (gstring, g_unichar_toupper (ch));
832           break;
833         }
834     }
835   else if (!append_keyval_symbol (accelerator_key, gstring))
836     {
837       gchar *tmp;
838
839       tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
840       if (tmp != NULL)
841         {
842           if (tmp[0] != 0 && tmp[1] == 0)
843             g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
844           else
845             {
846               const gchar *str;
847               str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
848               if (str == tmp)
849                 {
850                   g_string_append (gstring, tmp);
851                   substitute_underscores (gstring->str);
852                 }
853               else
854                 g_string_append (gstring, str);
855             }
856         }
857     }
858
859   return g_string_free (gstring, FALSE);
860 }
861
862 /**
863  * gtk_accel_label_refetch:
864  * @accel_label: a #GtkAccelLabel.
865  *
866  * Recreates the string representing the accelerator keys.
867  * This should not be needed since the string is automatically updated whenever
868  * accelerators are added or removed from the associated widget.
869  *
870  * Returns: always returns %FALSE.
871  */
872 gboolean
873 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
874 {
875   gboolean enable_accels;
876
877   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
878
879   if (accel_label->priv->accel_string)
880     {
881       g_free (accel_label->priv->accel_string);
882       accel_label->priv->accel_string = NULL;
883     }
884
885   g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
886                 "gtk-enable-accels", &enable_accels,
887                 NULL);
888
889   if (enable_accels && accel_label->priv->accel_closure)
890     {
891       GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
892
893       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
894         {
895           GtkAccelLabelClass *klass;
896           gchar *tmp;
897
898           klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
899           tmp = _gtk_accel_label_class_get_accelerator_label (klass,
900                                                               key->accel_key,
901                                                               key->accel_mods);
902           accel_label->priv->accel_string = g_strconcat ("   ", tmp, NULL);
903           g_free (tmp);
904         }
905       if (!accel_label->priv->accel_string)
906         accel_label->priv->accel_string = g_strdup ("-/-");
907     }
908   
909   if (!accel_label->priv->accel_string)
910     accel_label->priv->accel_string = g_strdup ("");
911
912   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
913
914   return FALSE;
915 }