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