1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkAccelLabel: GtkLabel with accelerator monitoring facilities.
5 * Copyright (C) 1998 Tim Janik
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.
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.
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.
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/.
33 #include "gtkaccellabel.h"
34 #include "gtkaccelmap.h"
36 #include "gtksizerequest.h"
37 #include "gtkprivate.h"
40 #include <gdk/gdkkeysyms.h>
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
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.
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().
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.
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.
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.
73 * <title>Creating a simple menu item with an accelerator key.</title>
75 * GtkWidget *save_item;
76 * GtkAccelGroup *accel_group;
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);
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);
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);
103 struct _GtkAccelLabelPrivate
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 */
113 static void gtk_accel_label_set_property (GObject *object,
117 static void gtk_accel_label_get_property (GObject *object,
121 static void gtk_accel_label_destroy (GtkObject *object);
122 static void gtk_accel_label_finalize (GObject *object);
123 static gboolean gtk_accel_label_draw (GtkWidget *widget,
125 static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
128 static void gtk_accel_label_get_preferred_width (GtkWidget *widget,
132 #define GTK_ACCEL_LABEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ACCEL_LABEL, GtkAccelLabelPrivate))
135 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
138 gtk_accel_label_class_init (GtkAccelLabelClass *class)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
141 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
142 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
144 gobject_class->finalize = gtk_accel_label_finalize;
145 gobject_class->set_property = gtk_accel_label_set_property;
146 gobject_class->get_property = gtk_accel_label_get_property;
148 object_class->destroy = gtk_accel_label_destroy;
150 widget_class->draw = gtk_accel_label_draw;
151 widget_class->get_preferred_width = gtk_accel_label_get_preferred_width;
153 class->signal_quote1 = g_strdup ("<:");
154 class->signal_quote2 = g_strdup (":>");
156 #ifndef GDK_WINDOWING_QUARTZ
157 /* This is the text that should appear next to menu accelerators
158 * that use the shift key. If the text on this key isn't typically
159 * translated on keyboards used for your language, don't translate
162 class->mod_name_shift = g_strdup (C_("keyboard label", "Shift"));
163 /* This is the text that should appear next to menu accelerators
164 * that use the control key. If the text on this key isn't typically
165 * translated on keyboards used for your language, don't translate
168 class->mod_name_control = g_strdup (C_("keyboard label", "Ctrl"));
169 /* This is the text that should appear next to menu accelerators
170 * that use the alt key. If the text on this key isn't typically
171 * translated on keyboards used for your language, don't translate
174 class->mod_name_alt = g_strdup (C_("keyboard label", "Alt"));
175 class->mod_separator = g_strdup ("+");
176 #else /* GDK_WINDOWING_QUARTZ */
178 /* U+21E7 UPWARDS WHITE ARROW */
179 class->mod_name_shift = g_strdup ("\xe2\x87\xa7");
180 /* U+2303 UP ARROWHEAD */
181 class->mod_name_control = g_strdup ("\xe2\x8c\x83");
182 /* U+2325 OPTION KEY */
183 class->mod_name_alt = g_strdup ("\xe2\x8c\xa5");
184 class->mod_separator = g_strdup ("");
186 #endif /* GDK_WINDOWING_QUARTZ */
188 g_object_class_install_property (gobject_class,
190 g_param_spec_boxed ("accel-closure",
191 P_("Accelerator Closure"),
192 P_("The closure to be monitored for accelerator changes"),
194 GTK_PARAM_READWRITE));
195 g_object_class_install_property (gobject_class,
197 g_param_spec_object ("accel-widget",
198 P_("Accelerator Widget"),
199 P_("The widget to be monitored for accelerator changes"),
201 GTK_PARAM_READWRITE));
203 g_type_class_add_private (gobject_class, sizeof (GtkAccelLabelPrivate));
207 gtk_accel_label_set_property (GObject *object,
212 GtkAccelLabel *accel_label;
214 accel_label = GTK_ACCEL_LABEL (object);
218 case PROP_ACCEL_CLOSURE:
219 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
221 case PROP_ACCEL_WIDGET:
222 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gtk_accel_label_get_property (GObject *object,
236 GtkAccelLabel *accel_label;
238 accel_label = GTK_ACCEL_LABEL (object);
242 case PROP_ACCEL_CLOSURE:
243 g_value_set_boxed (value, accel_label->priv->accel_closure);
245 case PROP_ACCEL_WIDGET:
246 g_value_set_object (value, accel_label->priv->accel_widget);
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gtk_accel_label_init (GtkAccelLabel *accel_label)
257 GtkAccelLabelPrivate *priv = GTK_ACCEL_LABEL_GET_PRIVATE (accel_label);
259 priv->accel_padding = 3;
260 priv->accel_widget = NULL;
261 priv->accel_closure = NULL;
262 priv->accel_group = NULL;
263 priv->accel_string = NULL;
265 accel_label->priv = priv;
269 * gtk_accel_label_new:
270 * @string: the label string. Must be non-%NULL.
272 * Creates a new #GtkAccelLabel.
274 * Returns: a new #GtkAccelLabel.
277 gtk_accel_label_new (const gchar *string)
279 GtkAccelLabel *accel_label;
281 g_return_val_if_fail (string != NULL, NULL);
283 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
285 gtk_label_set_text (GTK_LABEL (accel_label), string);
287 return GTK_WIDGET (accel_label);
291 gtk_accel_label_destroy (GtkObject *object)
293 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
295 gtk_accel_label_set_accel_widget (accel_label, NULL);
296 gtk_accel_label_set_accel_closure (accel_label, NULL);
298 GTK_OBJECT_CLASS (gtk_accel_label_parent_class)->destroy (object);
302 gtk_accel_label_finalize (GObject *object)
304 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
306 g_free (accel_label->priv->accel_string);
308 G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
312 * gtk_accel_label_get_accel_widget:
313 * @accel_label: a #GtkAccelLabel
315 * Fetches the widget monitored by this accelerator label. See
316 * gtk_accel_label_set_accel_widget().
318 * Returns: (transfer none): the object monitored by the accelerator label, or %NULL.
321 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
323 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
325 return accel_label->priv->accel_widget;
329 * gtk_accel_label_get_accel_width:
330 * @accel_label: a #GtkAccelLabel.
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.
336 * Returns: the width needed to display the accelerator key(s).
339 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
341 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
343 return (accel_label->priv->accel_string_width +
344 (accel_label->priv->accel_string_width ? accel_label->priv->accel_padding : 0));
348 gtk_accel_label_get_preferred_width (GtkWidget *widget,
352 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
356 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->get_preferred_width (widget, min_width, nat_width);
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;
363 g_object_unref (layout);
367 get_first_baseline (PangoLayout *layout)
369 PangoLayoutIter *iter;
372 iter = pango_layout_get_iter (layout);
373 result = pango_layout_iter_get_baseline (iter);
374 pango_layout_iter_free (iter);
376 return PANGO_PIXELS (result);
380 gtk_accel_label_draw (GtkWidget *widget,
383 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
384 GtkMisc *misc = GTK_MISC (accel_label);
385 GtkTextDirection direction;
387 GtkAllocation allocation;
388 GtkRequisition requisition;
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, &requisition, NULL);
395 if (allocation.width >= requisition.width + ac_width)
397 PangoLayout *label_layout;
398 PangoLayout *accel_layout;
399 GtkLabel *label = GTK_LABEL (widget);
405 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
409 /* XXX: Mad hack: We modify the label's width so it renders
410 * properly in its draw function that we chain to. */
411 if (direction == GTK_TEXT_DIR_RTL)
412 cairo_translate (cr, ac_width, 0);
413 if (gtk_label_get_ellipsize (label))
414 pango_layout_set_width (label_layout,
415 pango_layout_get_width (label_layout)
416 - ac_width * PANGO_SCALE);
418 allocation.width -= ac_width;
419 gtk_widget_set_allocation (widget, &allocation);
420 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
421 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget,
423 allocation.width += ac_width;
424 gtk_widget_set_allocation (widget, &allocation);
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);
432 gtk_misc_get_padding (misc, &xpad, NULL);
434 if (direction == GTK_TEXT_DIR_RTL)
437 x = gtk_widget_get_allocated_width (widget) - xpad - ac_width;
439 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
441 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
443 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
445 gtk_paint_layout (gtk_widget_get_style (widget),
447 gtk_widget_get_state (widget),
454 g_object_unref (accel_layout);
458 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
459 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
466 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
468 GClosure *closure = NULL;
471 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
472 g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
474 clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
475 for (list = clist; list; list = list->next)
477 /* we just take the first closure used */
478 closure = list->data;
482 gtk_accel_label_set_accel_closure (accel_label, closure);
486 * gtk_accel_label_set_accel_widget:
487 * @accel_label: a #GtkAccelLabel
488 * @accel_widget: the widget to be monitored.
490 * Sets the widget to be monitored by this accelerator label.
493 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
494 GtkWidget *accel_widget)
496 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
498 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
500 if (accel_widget != accel_label->priv->accel_widget)
502 if (accel_label->priv->accel_widget)
504 gtk_accel_label_set_accel_closure (accel_label, NULL);
505 g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
506 refetch_widget_accel_closure,
508 g_object_unref (accel_label->priv->accel_widget);
510 accel_label->priv->accel_widget = accel_widget;
511 if (accel_label->priv->accel_widget)
513 g_object_ref (accel_label->priv->accel_widget);
514 g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
515 G_CALLBACK (refetch_widget_accel_closure),
516 accel_label, G_CONNECT_SWAPPED);
517 refetch_widget_accel_closure (accel_label);
519 g_object_notify (G_OBJECT (accel_label), "accel-widget");
524 gtk_accel_label_reset (GtkAccelLabel *accel_label)
526 if (accel_label->priv->accel_string)
528 g_free (accel_label->priv->accel_string);
529 accel_label->priv->accel_string = NULL;
532 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
536 check_accel_changed (GtkAccelGroup *accel_group,
538 GdkModifierType modifier,
539 GClosure *accel_closure,
540 GtkAccelLabel *accel_label)
542 if (accel_closure == accel_label->priv->accel_closure)
543 gtk_accel_label_reset (accel_label);
547 * gtk_accel_label_set_accel_closure:
548 * @accel_label: a #GtkAccelLabel
549 * @accel_closure: the closure to monitor for accelerator changes.
551 * Sets the closure to be monitored by this accelerator label. The closure
552 * must be connected to an accelerator group; see gtk_accel_group_connect().
555 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
556 GClosure *accel_closure)
558 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
560 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
562 if (accel_closure != accel_label->priv->accel_closure)
564 if (accel_label->priv->accel_closure)
566 g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
569 accel_label->priv->accel_group = NULL;
570 g_closure_unref (accel_label->priv->accel_closure);
572 accel_label->priv->accel_closure = accel_closure;
573 if (accel_label->priv->accel_closure)
575 g_closure_ref (accel_label->priv->accel_closure);
576 accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
577 g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
578 G_CALLBACK (check_accel_changed),
581 gtk_accel_label_reset (accel_label);
582 g_object_notify (G_OBJECT (accel_label), "accel-closure");
587 find_accel (GtkAccelKey *key,
591 return data == (gpointer) closure;
595 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
597 if (!accel_label->priv->accel_string)
598 gtk_accel_label_refetch (accel_label);
600 return accel_label->priv->accel_string;
603 /* Underscores in key names are better displayed as spaces
604 * E.g., Page_Up should be "Page Up"
607 substitute_underscores (char *str)
611 for (p = str; *p; p++)
616 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
617 * append it to gstring and return TRUE; otherwise return FALSE.
618 * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html
619 * for the list of special keys. */
621 append_keyval_symbol (guint accelerator_key,
624 #ifdef GDK_WINDOWING_QUARTZ
625 switch (accelerator_key)
628 /* U+21A9 LEFTWARDS ARROW WITH HOOK */
629 g_string_append (gstring, "\xe2\x86\xa9");
632 case GDK_KEY_ISO_Enter:
633 /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
634 g_string_append (gstring, "\xe2\x8c\xa4");
638 /* U+2190 LEFTWARDS ARROW */
639 g_string_append (gstring, "\xe2\x86\x90");
643 /* U+2191 UPWARDS ARROW */
644 g_string_append (gstring, "\xe2\x86\x91");
648 /* U+2192 RIGHTWARDS ARROW */
649 g_string_append (gstring, "\xe2\x86\x92");
653 /* U+2193 DOWNWARDS ARROW */
654 g_string_append (gstring, "\xe2\x86\x93");
657 case GDK_KEY_Page_Up:
658 /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
659 g_string_append (gstring, "\xe2\x87\x9e");
662 case GDK_KEY_Page_Down:
663 /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
664 g_string_append (gstring, "\xe2\x87\x9f");
668 /* U+2196 NORTH WEST ARROW */
669 g_string_append (gstring, "\xe2\x86\x96");
673 /* U+2198 SOUTH EAST ARROW */
674 g_string_append (gstring, "\xe2\x86\x98");
678 /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
679 g_string_append (gstring, "\xe2\x8e\x8b");
682 case GDK_KEY_BackSpace:
683 /* U+232B ERASE TO THE LEFT */
684 g_string_append (gstring, "\xe2\x8c\xab");
688 /* U+2326 ERASE TO THE RIGHT */
689 g_string_append (gstring, "\xe2\x8c\xa6");
695 #else /* !GDK_WINDOWING_QUARTZ */
701 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
702 guint accelerator_key,
703 GdkModifierType accelerator_mods)
706 gboolean seen_mod = FALSE;
709 gstring = g_string_new ("");
711 if (accelerator_mods & GDK_SHIFT_MASK)
713 g_string_append (gstring, klass->mod_name_shift);
716 if (accelerator_mods & GDK_CONTROL_MASK)
719 g_string_append (gstring, klass->mod_separator);
720 g_string_append (gstring, klass->mod_name_control);
723 if (accelerator_mods & GDK_MOD1_MASK)
726 g_string_append (gstring, klass->mod_separator);
727 g_string_append (gstring, klass->mod_name_alt);
730 if (accelerator_mods & GDK_MOD2_MASK)
733 g_string_append (gstring, klass->mod_separator);
735 g_string_append (gstring, "Mod2");
738 if (accelerator_mods & GDK_MOD3_MASK)
741 g_string_append (gstring, klass->mod_separator);
743 g_string_append (gstring, "Mod3");
746 if (accelerator_mods & GDK_MOD4_MASK)
749 g_string_append (gstring, klass->mod_separator);
751 g_string_append (gstring, "Mod4");
754 if (accelerator_mods & GDK_MOD5_MASK)
757 g_string_append (gstring, klass->mod_separator);
759 g_string_append (gstring, "Mod5");
762 if (accelerator_mods & GDK_SUPER_MASK)
765 g_string_append (gstring, klass->mod_separator);
767 /* This is the text that should appear next to menu accelerators
768 * that use the super key. If the text on this key isn't typically
769 * translated on keyboards used for your language, don't translate
772 g_string_append (gstring, C_("keyboard label", "Super"));
775 if (accelerator_mods & GDK_HYPER_MASK)
778 g_string_append (gstring, klass->mod_separator);
780 /* This is the text that should appear next to menu accelerators
781 * that use the hyper key. If the text on this key isn't typically
782 * translated on keyboards used for your language, don't translate
785 g_string_append (gstring, C_("keyboard label", "Hyper"));
788 if (accelerator_mods & GDK_META_MASK)
791 g_string_append (gstring, klass->mod_separator);
793 #ifndef GDK_WINDOWING_QUARTZ
794 /* This is the text that should appear next to menu accelerators
795 * that use the meta key. If the text on this key isn't typically
796 * translated on keyboards used for your language, don't translate
799 g_string_append (gstring, C_("keyboard label", "Meta"));
801 /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
802 g_string_append (gstring, "\xe2\x8c\x98");
807 g_string_append (gstring, klass->mod_separator);
809 ch = gdk_keyval_to_unicode (accelerator_key);
810 if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
815 g_string_append (gstring, C_("keyboard label", "Space"));
818 g_string_append (gstring, C_("keyboard label", "Backslash"));
821 g_string_append_unichar (gstring, g_unichar_toupper (ch));
825 else if (!append_keyval_symbol (accelerator_key, gstring))
829 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
832 if (tmp[0] != 0 && tmp[1] == 0)
833 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
837 str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
840 g_string_append (gstring, tmp);
841 substitute_underscores (gstring->str);
844 g_string_append (gstring, str);
849 return g_string_free (gstring, FALSE);
853 * gtk_accel_label_refetch:
854 * @accel_label: a #GtkAccelLabel.
856 * Recreates the string representing the accelerator keys.
857 * This should not be needed since the string is automatically updated whenever
858 * accelerators are added or removed from the associated widget.
860 * Returns: always returns %FALSE.
863 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
865 gboolean enable_accels;
867 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
869 if (accel_label->priv->accel_string)
871 g_free (accel_label->priv->accel_string);
872 accel_label->priv->accel_string = NULL;
875 g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
876 "gtk-enable-accels", &enable_accels,
879 if (enable_accels && accel_label->priv->accel_closure)
881 GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
883 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
885 GtkAccelLabelClass *klass;
888 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
889 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
892 accel_label->priv->accel_string = g_strconcat (" ", tmp, NULL);
895 if (!accel_label->priv->accel_string)
896 accel_label->priv->accel_string = g_strdup ("-/-");
899 if (!accel_label->priv->accel_string)
900 accel_label->priv->accel_string = g_strdup ("");
902 gtk_widget_queue_resize (GTK_WIDGET (accel_label));