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 "gtkprivate.h"
39 #include <gdk/gdkkeysyms.h>
47 static void gtk_accel_label_set_property (GObject *object,
51 static void gtk_accel_label_get_property (GObject *object,
55 static void gtk_accel_label_destroy (GtkObject *object);
56 static void gtk_accel_label_finalize (GObject *object);
57 static void gtk_accel_label_size_request (GtkWidget *widget,
58 GtkRequisition *requisition);
59 static gboolean gtk_accel_label_expose_event (GtkWidget *widget,
60 GdkEventExpose *event);
61 static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
64 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
67 gtk_accel_label_class_init (GtkAccelLabelClass *class)
69 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
70 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
71 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
73 gobject_class->finalize = gtk_accel_label_finalize;
74 gobject_class->set_property = gtk_accel_label_set_property;
75 gobject_class->get_property = gtk_accel_label_get_property;
77 object_class->destroy = gtk_accel_label_destroy;
79 widget_class->size_request = gtk_accel_label_size_request;
80 widget_class->expose_event = gtk_accel_label_expose_event;
82 class->signal_quote1 = g_strdup ("<:");
83 class->signal_quote2 = g_strdup (":>");
85 #ifndef GDK_WINDOWING_QUARTZ
86 /* This is the text that should appear next to menu accelerators
87 * that use the shift key. If the text on this key isn't typically
88 * translated on keyboards used for your language, don't translate
91 * Don't include the prefix "keyboard label|" in the translation.
93 class->mod_name_shift = g_strdup (Q_("keyboard label|Shift"));
94 /* This is the text that should appear next to menu accelerators
95 * that use the control key. If the text on this key isn't typically
96 * translated on keyboards used for your language, don't translate
99 * Don't include the prefix "keyboard label|" in the translation.
101 class->mod_name_control = g_strdup (Q_("keyboard label|Ctrl"));
102 /* This is the text that should appear next to menu accelerators
103 * that use the alt key. If the text on this key isn't typically
104 * translated on keyboards used for your language, don't translate
107 * Don't include the prefix "keyboard label|" in the translation.
109 class->mod_name_alt = g_strdup (Q_("keyboard label|Alt"));
110 class->mod_separator = g_strdup ("+");
111 #else /* GDK_WINDOWING_QUARTZ */
113 /* U+21E7 UPWARDS WHITE ARROW */
114 class->mod_name_shift = g_strdup ("\xe2\x87\xa7");
115 /* U+2303 UP ARROWHEAD */
116 class->mod_name_control = g_strdup ("\xe2\x8c\x83");
117 /* U+2325 OPTION KEY */
118 class->mod_name_alt = g_strdup ("\xe2\x8c\xa5");
119 class->mod_separator = g_strdup ("");
121 #endif /* GDK_WINDOWING_QUARTZ */
123 class->accel_seperator = g_strdup (" / ");
124 class->latin1_to_char = TRUE;
126 g_object_class_install_property (gobject_class,
128 g_param_spec_boxed ("accel-closure",
129 P_("Accelerator Closure"),
130 P_("The closure to be monitored for accelerator changes"),
132 GTK_PARAM_READWRITE));
133 g_object_class_install_property (gobject_class,
135 g_param_spec_object ("accel-widget",
136 P_("Accelerator Widget"),
137 P_("The widget to be monitored for accelerator changes"),
139 GTK_PARAM_READWRITE));
143 gtk_accel_label_set_property (GObject *object,
148 GtkAccelLabel *accel_label;
150 accel_label = GTK_ACCEL_LABEL (object);
154 case PROP_ACCEL_CLOSURE:
155 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
157 case PROP_ACCEL_WIDGET:
158 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 gtk_accel_label_get_property (GObject *object,
172 GtkAccelLabel *accel_label;
174 accel_label = GTK_ACCEL_LABEL (object);
178 case PROP_ACCEL_CLOSURE:
179 g_value_set_boxed (value, accel_label->accel_closure);
181 case PROP_ACCEL_WIDGET:
182 g_value_set_object (value, accel_label->accel_widget);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 gtk_accel_label_init (GtkAccelLabel *accel_label)
193 accel_label->accel_padding = 3;
194 accel_label->accel_widget = NULL;
195 accel_label->accel_closure = NULL;
196 accel_label->accel_group = NULL;
197 accel_label->accel_string = NULL;
201 gtk_accel_label_new (const gchar *string)
203 GtkAccelLabel *accel_label;
205 g_return_val_if_fail (string != NULL, NULL);
207 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
209 gtk_label_set_text (GTK_LABEL (accel_label), string);
211 return GTK_WIDGET (accel_label);
215 gtk_accel_label_destroy (GtkObject *object)
217 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
219 gtk_accel_label_set_accel_widget (accel_label, NULL);
220 gtk_accel_label_set_accel_closure (accel_label, NULL);
222 GTK_OBJECT_CLASS (gtk_accel_label_parent_class)->destroy (object);
226 gtk_accel_label_finalize (GObject *object)
228 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
230 g_free (accel_label->accel_string);
232 G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
236 * gtk_accel_label_get_accel_widget:
237 * @accel_label: a #GtkAccelLabel
239 * Fetches the widget monitored by this accelerator label. See
240 * gtk_accel_label_set_accel_widget().
242 * Return value: the object monitored by the accelerator label,
246 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
248 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
250 return accel_label->accel_widget;
254 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
256 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
258 return (accel_label->accel_string_width +
259 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
263 gtk_accel_label_size_request (GtkWidget *widget,
264 GtkRequisition *requisition)
266 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
270 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request)
271 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request (widget, requisition);
273 layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
274 pango_layout_get_pixel_size (layout, &width, NULL);
275 accel_label->accel_string_width = width;
277 g_object_unref (layout);
281 get_first_baseline (PangoLayout *layout)
283 PangoLayoutIter *iter;
286 iter = pango_layout_get_iter (layout);
287 result = pango_layout_iter_get_baseline (iter);
288 pango_layout_iter_free (iter);
290 return PANGO_PIXELS (result);
294 gtk_accel_label_expose_event (GtkWidget *widget,
295 GdkEventExpose *event)
297 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
298 GtkMisc *misc = GTK_MISC (accel_label);
299 GtkTextDirection direction;
301 direction = gtk_widget_get_direction (widget);
303 if (GTK_WIDGET_DRAWABLE (accel_label))
307 ac_width = gtk_accel_label_get_accel_width (accel_label);
309 if (widget->allocation.width >= widget->requisition.width + ac_width)
311 PangoLayout *label_layout;
312 PangoLayout *accel_layout;
313 GtkLabel *label = GTK_LABEL (widget);
318 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
320 if (direction == GTK_TEXT_DIR_RTL)
321 widget->allocation.x += ac_width;
322 widget->allocation.width -= ac_width;
323 if (gtk_label_get_ellipsize (label))
324 pango_layout_set_width (label_layout,
325 pango_layout_get_width (label_layout)
326 - ac_width * PANGO_SCALE);
328 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
329 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
330 if (direction == GTK_TEXT_DIR_RTL)
331 widget->allocation.x -= ac_width;
332 widget->allocation.width += ac_width;
333 if (gtk_label_get_ellipsize (label))
334 pango_layout_set_width (label_layout,
335 pango_layout_get_width (label_layout)
336 + ac_width * PANGO_SCALE);
338 if (direction == GTK_TEXT_DIR_RTL)
339 x = widget->allocation.x + misc->xpad;
341 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
343 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
345 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
347 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
349 gtk_paint_layout (widget->style,
351 GTK_WIDGET_STATE (widget),
359 g_object_unref (accel_layout);
363 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
364 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
372 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
374 GClosure *closure = NULL;
377 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
378 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
380 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
381 for (list = clist; list; list = list->next)
383 /* we just take the first closure used */
384 closure = list->data;
388 gtk_accel_label_set_accel_closure (accel_label, closure);
392 * gtk_accel_label_set_accel_widget:
393 * @accel_label: a #GtkAccelLabel
394 * @accel_widget: the widget to be monitored.
396 * Sets the widget to be monitored by this accelerator label.
399 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
400 GtkWidget *accel_widget)
402 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
404 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
406 if (accel_widget != accel_label->accel_widget)
408 if (accel_label->accel_widget)
410 gtk_accel_label_set_accel_closure (accel_label, NULL);
411 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
412 refetch_widget_accel_closure,
414 g_object_unref (accel_label->accel_widget);
416 accel_label->accel_widget = accel_widget;
417 if (accel_label->accel_widget)
419 g_object_ref (accel_label->accel_widget);
420 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
421 G_CALLBACK (refetch_widget_accel_closure),
422 accel_label, G_CONNECT_SWAPPED);
423 refetch_widget_accel_closure (accel_label);
425 g_object_notify (G_OBJECT (accel_label), "accel-widget");
430 gtk_accel_label_reset (GtkAccelLabel *accel_label)
432 if (accel_label->accel_string)
434 g_free (accel_label->accel_string);
435 accel_label->accel_string = NULL;
438 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
442 check_accel_changed (GtkAccelGroup *accel_group,
444 GdkModifierType modifier,
445 GClosure *accel_closure,
446 GtkAccelLabel *accel_label)
448 if (accel_closure == accel_label->accel_closure)
449 gtk_accel_label_reset (accel_label);
453 * gtk_accel_label_set_accel_closure:
454 * @accel_label: a #GtkAccelLabel
455 * @accel_closure: the closure to monitor for accelerator changes.
457 * Sets the closure to be monitored by this accelerator label. The closure
458 * must be connected to an accelerator group; see gtk_accel_group_connect().
461 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
462 GClosure *accel_closure)
464 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
466 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
468 if (accel_closure != accel_label->accel_closure)
470 if (accel_label->accel_closure)
472 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
475 accel_label->accel_group = NULL;
476 g_closure_unref (accel_label->accel_closure);
478 accel_label->accel_closure = accel_closure;
479 if (accel_label->accel_closure)
481 g_closure_ref (accel_label->accel_closure);
482 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
483 g_signal_connect_object (accel_label->accel_group, "accel_changed",
484 G_CALLBACK (check_accel_changed),
487 gtk_accel_label_reset (accel_label);
488 g_object_notify (G_OBJECT (accel_label), "accel-closure");
493 find_accel (GtkAccelKey *key,
497 return data == (gpointer) closure;
501 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
503 if (!accel_label->accel_string)
504 gtk_accel_label_refetch (accel_label);
506 return accel_label->accel_string;
509 /* Underscores in key names are better displayed as spaces
510 * E.g., Page_Up should be "Page Up"
513 substitute_underscores (char *str)
517 for (p = str; *p; p++)
522 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
523 * append it to gstring and return TRUE; otherwise return FALSE.
524 * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html
525 * for the list of special keys. */
527 append_keyval_symbol (guint accelerator_key,
530 #ifdef GDK_WINDOWING_QUARTZ
531 switch (accelerator_key)
534 /* U+21A9 LEFTWARDS ARROW WITH HOOK */
535 g_string_append (gstring, "\xe2\x86\xa9");
539 /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
540 g_string_append (gstring, "\xe2\x8c\xa4");
544 /* U+2190 LEFTWARDS ARROW */
545 g_string_append (gstring, "\xe2\x86\x90");
549 /* U+2191 UPWARDS ARROW */
550 g_string_append (gstring, "\xe2\x86\x91");
554 /* U+2192 RIGHTWARDS ARROW */
555 g_string_append (gstring, "\xe2\x86\x92");
559 /* U+2193 DOWNWARDS ARROW */
560 g_string_append (gstring, "\xe2\x86\x93");
564 /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
565 g_string_append (gstring, "\xe2\x87\x9e");
569 /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
570 g_string_append (gstring, "\xe2\x87\x9f");
574 /* U+2196 NORTH WEST ARROW */
575 g_string_append (gstring, "\xe2\x86\x96");
579 /* U+2198 SOUTH EAST ARROW */
580 g_string_append (gstring, "\xe2\x86\x98");
584 /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
585 g_string_append (gstring, "\xe2\x8e\x8b");
589 /* U+232B ERASE TO THE LEFT */
590 g_string_append (gstring, "\xe2\x8c\xab");
594 /* U+2326 ERASE TO THE RIGHT */
595 g_string_append (gstring, "\xe2\x8c\xa6");
601 #else /* !GDK_WINDOWING_QUARTZ */
607 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
608 guint accelerator_key,
609 GdkModifierType accelerator_mods)
612 gboolean seen_mod = FALSE;
615 gstring = g_string_new ("");
617 if (accelerator_mods & GDK_SHIFT_MASK)
619 g_string_append (gstring, klass->mod_name_shift);
622 if (accelerator_mods & GDK_CONTROL_MASK)
625 g_string_append (gstring, klass->mod_separator);
626 g_string_append (gstring, klass->mod_name_control);
629 if (accelerator_mods & GDK_MOD1_MASK)
632 g_string_append (gstring, klass->mod_separator);
633 g_string_append (gstring, klass->mod_name_alt);
636 if (accelerator_mods & GDK_MOD2_MASK)
639 g_string_append (gstring, klass->mod_separator);
641 g_string_append (gstring, "Mod2");
644 if (accelerator_mods & GDK_MOD3_MASK)
647 g_string_append (gstring, klass->mod_separator);
649 g_string_append (gstring, "Mod3");
652 if (accelerator_mods & GDK_MOD4_MASK)
655 g_string_append (gstring, klass->mod_separator);
657 g_string_append (gstring, "Mod4");
660 if (accelerator_mods & GDK_MOD5_MASK)
663 g_string_append (gstring, klass->mod_separator);
665 g_string_append (gstring, "Mod5");
668 if (accelerator_mods & GDK_SUPER_MASK)
671 g_string_append (gstring, klass->mod_separator);
673 /* This is the text that should appear next to menu accelerators
674 * that use the super key. If the text on this key isn't typically
675 * translated on keyboards used for your language, don't translate
677 * And do not translate the part before the |.
679 g_string_append (gstring, Q_("keyboard label|Super"));
682 if (accelerator_mods & GDK_HYPER_MASK)
685 g_string_append (gstring, klass->mod_separator);
687 /* This is the text that should appear next to menu accelerators
688 * that use the hyper key. If the text on this key isn't typically
689 * translated on keyboards used for your language, don't translate
691 * And do not translate the part before the |.
693 g_string_append (gstring, Q_("keyboard label|Hyper"));
696 if (accelerator_mods & GDK_META_MASK)
699 g_string_append (gstring, klass->mod_separator);
701 #ifndef GDK_WINDOWING_QUARTZ
702 /* This is the text that should appear next to menu accelerators
703 * that use the meta key. If the text on this key isn't typically
704 * translated on keyboards used for your language, don't translate
706 * And do not translate the part before the |.
708 g_string_append (gstring, Q_("keyboard label|Meta"));
710 /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
711 g_string_append (gstring, "\xe2\x8c\x98");
716 g_string_append (gstring, klass->mod_separator);
718 ch = gdk_keyval_to_unicode (accelerator_key);
719 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
720 (ch < 0x80 || klass->latin1_to_char))
725 /* do not translate the part before the | */
726 g_string_append (gstring, Q_("keyboard label|Space"));
729 /* do not translate the part before the | */
730 g_string_append (gstring, Q_("keyboard label|Backslash"));
733 g_string_append_unichar (gstring, g_unichar_toupper (ch));
737 else if (!append_keyval_symbol (accelerator_key, gstring))
741 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
744 if (tmp[0] != 0 && tmp[1] == 0)
745 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
751 strcpy (msg, "keyboard label|");
752 g_strlcat (msg, tmp, 128);
756 g_string_append (gstring, tmp);
757 substitute_underscores (gstring->str);
760 g_string_append (gstring, str);
765 return g_string_free (gstring, FALSE);
769 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
771 gboolean enable_accels;
773 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
775 if (accel_label->accel_string)
777 g_free (accel_label->accel_string);
778 accel_label->accel_string = NULL;
781 g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
782 "gtk-enable-accels", &enable_accels,
785 if (enable_accels && accel_label->accel_closure)
787 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
789 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
791 GtkAccelLabelClass *klass;
794 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
795 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
798 accel_label->accel_string = g_strconcat (" ", tmp, NULL);
801 if (!accel_label->accel_string)
802 accel_label->accel_string = g_strdup ("-/-");
805 if (!accel_label->accel_string)
806 accel_label->accel_string = g_strdup ("");
808 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
813 #define __GTK_ACCEL_LABEL_C__
814 #include "gtkaliasdef.c"