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, see <http://www.gnu.org/licenses/>.
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/.
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
34 #include "gtksizerequest.h"
35 #include "gtkprivate.h"
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
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.
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().
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.
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.
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.
69 * <title>Creating a simple menu item with an accelerator key.</title>
71 * GtkWidget *save_item;
72 * GtkAccelGroup *accel_group;
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);
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);
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);
99 struct _GtkAccelLabelPrivate
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 */
108 guint accel_key; /* manual accel key specification if != 0 */
109 GdkModifierType accel_mods;
112 static void gtk_accel_label_set_property (GObject *object,
116 static void gtk_accel_label_get_property (GObject *object,
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,
124 static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
127 static void gtk_accel_label_get_preferred_width (GtkWidget *widget,
132 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
135 gtk_accel_label_class_init (GtkAccelLabelClass *class)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
138 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
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;
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;
148 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_ACCEL_LABEL);
150 class->signal_quote1 = g_strdup ("<:");
151 class->signal_quote2 = g_strdup (":>");
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
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
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
171 class->mod_name_alt = g_strdup (C_("keyboard label", "Alt"));
172 class->mod_separator = g_strdup ("+");
173 #else /* GDK_WINDOWING_QUARTZ */
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 ("");
183 #endif /* GDK_WINDOWING_QUARTZ */
185 g_object_class_install_property (gobject_class,
187 g_param_spec_boxed ("accel-closure",
188 P_("Accelerator Closure"),
189 P_("The closure to be monitored for accelerator changes"),
191 GTK_PARAM_READWRITE));
192 g_object_class_install_property (gobject_class,
194 g_param_spec_object ("accel-widget",
195 P_("Accelerator Widget"),
196 P_("The widget to be monitored for accelerator changes"),
198 GTK_PARAM_READWRITE));
200 g_type_class_add_private (gobject_class, sizeof (GtkAccelLabelPrivate));
204 gtk_accel_label_set_property (GObject *object,
209 GtkAccelLabel *accel_label;
211 accel_label = GTK_ACCEL_LABEL (object);
215 case PROP_ACCEL_CLOSURE:
216 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
218 case PROP_ACCEL_WIDGET:
219 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 gtk_accel_label_get_property (GObject *object,
233 GtkAccelLabel *accel_label;
235 accel_label = GTK_ACCEL_LABEL (object);
239 case PROP_ACCEL_CLOSURE:
240 g_value_set_boxed (value, accel_label->priv->accel_closure);
242 case PROP_ACCEL_WIDGET:
243 g_value_set_object (value, accel_label->priv->accel_widget);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gtk_accel_label_init (GtkAccelLabel *accel_label)
254 GtkAccelLabelPrivate *priv;
256 accel_label->priv = G_TYPE_INSTANCE_GET_PRIVATE (accel_label,
257 GTK_TYPE_ACCEL_LABEL,
258 GtkAccelLabelPrivate);
259 priv = accel_label->priv;
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;
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 (GtkWidget *widget)
293 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
295 gtk_accel_label_set_accel_widget (accel_label, NULL);
296 gtk_accel_label_set_accel_closure (accel_label, NULL);
298 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->destroy (widget);
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, NULL, &requisition);
395 if (allocation.width >= requisition.width + ac_width)
397 GtkStyleContext *context;
398 PangoLayout *label_layout;
399 PangoLayout *accel_layout;
400 GtkLabel *label = GTK_LABEL (widget);
406 context = gtk_widget_get_style_context (widget);
407 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
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);
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,
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);
434 gtk_misc_get_padding (misc, &xpad, NULL);
436 if (direction == GTK_TEXT_DIR_RTL)
439 x = gtk_widget_get_allocated_width (widget) - xpad - ac_width;
441 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
443 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
445 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
447 gtk_style_context_save (context);
448 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ACCELERATOR);
450 gtk_render_layout (context, cr, x, y, accel_layout);
451 gtk_style_context_restore (context);
453 g_object_unref (accel_layout);
457 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw)
458 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
465 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
467 GClosure *closure = NULL;
470 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
471 g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
473 clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
474 for (list = clist; list; list = list->next)
476 /* we just take the first closure used */
477 closure = list->data;
481 gtk_accel_label_set_accel_closure (accel_label, closure);
485 accel_widget_weak_ref_cb (GtkAccelLabel *accel_label,
486 GtkWidget *old_accel_widget)
488 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
489 g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
491 g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
492 refetch_widget_accel_closure,
494 accel_label->priv->accel_widget = NULL;
495 g_object_notify (G_OBJECT (accel_label), "accel-widget");
499 * gtk_accel_label_set_accel_widget:
500 * @accel_label: a #GtkAccelLabel
501 * @accel_widget: the widget to be monitored.
503 * Sets the widget to be monitored by this accelerator label.
506 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
507 GtkWidget *accel_widget)
509 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
511 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
513 if (accel_widget != accel_label->priv->accel_widget)
515 if (accel_label->priv->accel_widget)
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,
521 g_object_weak_unref (G_OBJECT (accel_label->priv->accel_widget),
522 (GWeakNotify) accel_widget_weak_ref_cb, accel_label);
524 accel_label->priv->accel_widget = accel_widget;
525 if (accel_label->priv->accel_widget)
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);
534 g_object_notify (G_OBJECT (accel_label), "accel-widget");
539 gtk_accel_label_reset (GtkAccelLabel *accel_label)
541 if (accel_label->priv->accel_string)
543 g_free (accel_label->priv->accel_string);
544 accel_label->priv->accel_string = NULL;
547 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
551 check_accel_changed (GtkAccelGroup *accel_group,
553 GdkModifierType modifier,
554 GClosure *accel_closure,
555 GtkAccelLabel *accel_label)
557 if (accel_closure == accel_label->priv->accel_closure)
558 gtk_accel_label_reset (accel_label);
562 * gtk_accel_label_set_accel_closure:
563 * @accel_label: a #GtkAccelLabel
564 * @accel_closure: the closure to monitor for accelerator changes.
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().
570 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
571 GClosure *accel_closure)
573 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
575 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
577 if (accel_closure != accel_label->priv->accel_closure)
579 if (accel_label->priv->accel_closure)
581 g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
584 accel_label->priv->accel_group = NULL;
585 g_closure_unref (accel_label->priv->accel_closure);
587 accel_label->priv->accel_closure = accel_closure;
588 if (accel_label->priv->accel_closure)
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),
596 gtk_accel_label_reset (accel_label);
597 g_object_notify (G_OBJECT (accel_label), "accel-closure");
602 find_accel (GtkAccelKey *key,
606 return data == (gpointer) closure;
610 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
612 if (!accel_label->priv->accel_string)
613 gtk_accel_label_refetch (accel_label);
615 return accel_label->priv->accel_string;
618 /* Underscores in key names are better displayed as spaces
619 * E.g., Page_Up should be "Page Up".
621 * Some keynames also have prefixes that are not suitable
622 * for display, e.g XF86AudioMute, so strip those out, too.
624 * This function is only called on untranslated keynames,
625 * so no need to be UTF-8 safe.
628 append_without_underscores (GString *s,
633 if (g_str_has_prefix (str, "XF86"))
635 else if (g_str_has_prefix (str, "ISO_"))
643 g_string_append_c (s, ' ');
645 g_string_append_c (s, *p);
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. */
654 append_keyval_symbol (guint accelerator_key,
657 #ifdef GDK_WINDOWING_QUARTZ
658 switch (accelerator_key)
661 /* U+21A9 LEFTWARDS ARROW WITH HOOK */
662 g_string_append (gstring, "\xe2\x86\xa9");
665 case GDK_KEY_ISO_Enter:
666 /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
667 g_string_append (gstring, "\xe2\x8c\xa4");
671 /* U+2190 LEFTWARDS ARROW */
672 g_string_append (gstring, "\xe2\x86\x90");
676 /* U+2191 UPWARDS ARROW */
677 g_string_append (gstring, "\xe2\x86\x91");
681 /* U+2192 RIGHTWARDS ARROW */
682 g_string_append (gstring, "\xe2\x86\x92");
686 /* U+2193 DOWNWARDS ARROW */
687 g_string_append (gstring, "\xe2\x86\x93");
690 case GDK_KEY_Page_Up:
691 /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
692 g_string_append (gstring, "\xe2\x87\x9e");
695 case GDK_KEY_Page_Down:
696 /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
697 g_string_append (gstring, "\xe2\x87\x9f");
701 /* U+2196 NORTH WEST ARROW */
702 g_string_append (gstring, "\xe2\x86\x96");
706 /* U+2198 SOUTH EAST ARROW */
707 g_string_append (gstring, "\xe2\x86\x98");
711 /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
712 g_string_append (gstring, "\xe2\x8e\x8b");
715 case GDK_KEY_BackSpace:
716 /* U+232B ERASE TO THE LEFT */
717 g_string_append (gstring, "\xe2\x8c\xab");
721 /* U+2326 ERASE TO THE RIGHT */
722 g_string_append (gstring, "\xe2\x8c\xa6");
728 #else /* !GDK_WINDOWING_QUARTZ */
734 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
735 guint accelerator_key,
736 GdkModifierType accelerator_mods)
739 gboolean seen_mod = FALSE;
742 gstring = g_string_new ("");
744 if (accelerator_mods & GDK_SHIFT_MASK)
746 g_string_append (gstring, klass->mod_name_shift);
749 if (accelerator_mods & GDK_CONTROL_MASK)
752 g_string_append (gstring, klass->mod_separator);
753 g_string_append (gstring, klass->mod_name_control);
756 if (accelerator_mods & GDK_MOD1_MASK)
759 g_string_append (gstring, klass->mod_separator);
760 g_string_append (gstring, klass->mod_name_alt);
763 if (accelerator_mods & GDK_MOD2_MASK)
766 g_string_append (gstring, klass->mod_separator);
768 g_string_append (gstring, "Mod2");
771 if (accelerator_mods & GDK_MOD3_MASK)
774 g_string_append (gstring, klass->mod_separator);
776 g_string_append (gstring, "Mod3");
779 if (accelerator_mods & GDK_MOD4_MASK)
782 g_string_append (gstring, klass->mod_separator);
784 g_string_append (gstring, "Mod4");
787 if (accelerator_mods & GDK_MOD5_MASK)
790 g_string_append (gstring, klass->mod_separator);
792 g_string_append (gstring, "Mod5");
795 if (accelerator_mods & GDK_SUPER_MASK)
798 g_string_append (gstring, klass->mod_separator);
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
805 g_string_append (gstring, C_("keyboard label", "Super"));
808 if (accelerator_mods & GDK_HYPER_MASK)
811 g_string_append (gstring, klass->mod_separator);
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
818 g_string_append (gstring, C_("keyboard label", "Hyper"));
821 if (accelerator_mods & GDK_META_MASK)
824 g_string_append (gstring, klass->mod_separator);
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
832 g_string_append (gstring, C_("keyboard label", "Meta"));
834 /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
835 g_string_append (gstring, "\xe2\x8c\x98");
840 g_string_append (gstring, klass->mod_separator);
842 ch = gdk_keyval_to_unicode (accelerator_key);
843 if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
848 g_string_append (gstring, C_("keyboard label", "Space"));
851 g_string_append (gstring, C_("keyboard label", "Backslash"));
854 g_string_append_unichar (gstring, g_unichar_toupper (ch));
858 else if (!append_keyval_symbol (accelerator_key, gstring))
862 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
865 if (tmp[0] != 0 && tmp[1] == 0)
866 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
870 str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
872 append_without_underscores (gstring, tmp);
874 g_string_append (gstring, str);
879 return g_string_free (gstring, FALSE);
883 * gtk_accel_label_refetch:
884 * @accel_label: a #GtkAccelLabel.
886 * Recreates the string representing the accelerator keys.
887 * This should not be needed since the string is automatically updated whenever
888 * accelerators are added or removed from the associated widget.
890 * Returns: always returns %FALSE.
893 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
895 gboolean enable_accels;
897 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
899 if (accel_label->priv->accel_string)
901 g_free (accel_label->priv->accel_string);
902 accel_label->priv->accel_string = NULL;
905 g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
906 "gtk-enable-accels", &enable_accels,
909 if (enable_accels && (accel_label->priv->accel_closure || accel_label->priv->accel_key))
911 gboolean have_accel = FALSE;
913 GdkModifierType accel_mods;
915 /* First check for a manual accel set with _set_accel() */
916 if (accel_label->priv->accel_key)
918 accel_mods = accel_label->priv->accel_mods;
919 accel_key = accel_label->priv->accel_key;
923 /* If we don't have a hardcoded value, check the accel group */
928 key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
930 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
932 accel_key = key->accel_key;
933 accel_mods = key->accel_mods;
938 /* If we found a key using either method, set it */
941 GtkAccelLabelClass *klass;
944 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
945 tmp = _gtk_accel_label_class_get_accelerator_label (klass, accel_key, accel_mods);
946 accel_label->priv->accel_string = g_strconcat (" ", tmp, NULL);
951 /* Otherwise we have a closure with no key. Show "-/-". */
952 accel_label->priv->accel_string = g_strdup ("-/-");
955 if (!accel_label->priv->accel_string)
956 accel_label->priv->accel_string = g_strdup ("");
958 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
964 * gtk_accel_label_set_accel:
965 * @accel_label: a #GtkAccelLabel
966 * @accelerator_key: a keyval, or 0
967 * @accelerator_mods: the modifier mask for the accel
969 * Manually sets a keyval and modifier mask as the accelerator rendered
972 * If a keyval and modifier are explicitly set then these values are
973 * used regardless of any associated accel closure or widget.
975 * Providing an @accelerator_key of 0 removes the manual setting.
980 gtk_accel_label_set_accel (GtkAccelLabel *accel_label,
981 guint accelerator_key,
982 GdkModifierType accelerator_mods)
984 accel_label->priv->accel_key = accelerator_key;
985 accel_label->priv->accel_mods = accelerator_mods;
987 gtk_accel_label_reset (accel_label);