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 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request (widget, requisition);
272 layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
273 pango_layout_get_pixel_size (layout, &width, NULL);
274 accel_label->accel_string_width = width;
276 g_object_unref (layout);
280 get_first_baseline (PangoLayout *layout)
282 PangoLayoutIter *iter;
285 iter = pango_layout_get_iter (layout);
286 result = pango_layout_iter_get_baseline (iter);
287 pango_layout_iter_free (iter);
289 return PANGO_PIXELS (result);
293 gtk_accel_label_expose_event (GtkWidget *widget,
294 GdkEventExpose *event)
296 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
297 GtkMisc *misc = GTK_MISC (accel_label);
298 GtkTextDirection direction;
300 direction = gtk_widget_get_direction (widget);
302 if (GTK_WIDGET_DRAWABLE (accel_label))
306 ac_width = gtk_accel_label_get_accel_width (accel_label);
308 if (widget->allocation.width >= widget->requisition.width + ac_width)
310 PangoLayout *label_layout;
311 PangoLayout *accel_layout;
312 GtkLabel *label = GTK_LABEL (widget);
317 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
319 if (direction == GTK_TEXT_DIR_RTL)
320 widget->allocation.x += ac_width;
321 widget->allocation.width -= ac_width;
322 if (gtk_label_get_ellipsize (label))
323 pango_layout_set_width (label_layout,
324 pango_layout_get_width (label_layout)
325 - ac_width * PANGO_SCALE);
327 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
328 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
329 if (direction == GTK_TEXT_DIR_RTL)
330 widget->allocation.x -= ac_width;
331 widget->allocation.width += ac_width;
332 if (gtk_label_get_ellipsize (label))
333 pango_layout_set_width (label_layout,
334 pango_layout_get_width (label_layout)
335 + ac_width * PANGO_SCALE);
337 if (direction == GTK_TEXT_DIR_RTL)
338 x = widget->allocation.x + misc->xpad;
340 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
342 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
344 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
346 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
348 gtk_paint_layout (widget->style,
350 GTK_WIDGET_STATE (widget),
358 g_object_unref (accel_layout);
362 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
363 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
371 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
373 GClosure *closure = NULL;
376 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
377 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
379 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
380 for (list = clist; list; list = list->next)
382 /* we just take the first closure used */
383 closure = list->data;
387 gtk_accel_label_set_accel_closure (accel_label, closure);
391 * gtk_accel_label_set_accel_widget:
392 * @accel_label: a #GtkAccelLabel
393 * @accel_widget: the widget to be monitored.
395 * Sets the widget to be monitored by this accelerator label.
398 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
399 GtkWidget *accel_widget)
401 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
403 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
405 if (accel_widget != accel_label->accel_widget)
407 if (accel_label->accel_widget)
409 gtk_accel_label_set_accel_closure (accel_label, NULL);
410 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
411 refetch_widget_accel_closure,
413 g_object_unref (accel_label->accel_widget);
415 accel_label->accel_widget = accel_widget;
416 if (accel_label->accel_widget)
418 g_object_ref (accel_label->accel_widget);
419 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
420 G_CALLBACK (refetch_widget_accel_closure),
421 accel_label, G_CONNECT_SWAPPED);
422 refetch_widget_accel_closure (accel_label);
424 g_object_notify (G_OBJECT (accel_label), "accel-widget");
429 gtk_accel_label_reset (GtkAccelLabel *accel_label)
431 if (accel_label->accel_string)
433 g_free (accel_label->accel_string);
434 accel_label->accel_string = NULL;
437 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
441 check_accel_changed (GtkAccelGroup *accel_group,
443 GdkModifierType modifier,
444 GClosure *accel_closure,
445 GtkAccelLabel *accel_label)
447 if (accel_closure == accel_label->accel_closure)
448 gtk_accel_label_reset (accel_label);
452 * gtk_accel_label_set_accel_closure:
453 * @accel_label: a #GtkAccelLabel
454 * @accel_closure: the closure to monitor for accelerator changes.
456 * Sets the closure to be monitored by this accelerator label. The closure
457 * must be connected to an accelerator group; see gtk_accel_group_connect().
460 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
461 GClosure *accel_closure)
463 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
465 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
467 if (accel_closure != accel_label->accel_closure)
469 if (accel_label->accel_closure)
471 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
474 accel_label->accel_group = NULL;
475 g_closure_unref (accel_label->accel_closure);
477 accel_label->accel_closure = accel_closure;
478 if (accel_label->accel_closure)
480 g_closure_ref (accel_label->accel_closure);
481 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
482 g_signal_connect_object (accel_label->accel_group, "accel_changed",
483 G_CALLBACK (check_accel_changed),
486 gtk_accel_label_reset (accel_label);
487 g_object_notify (G_OBJECT (accel_label), "accel-closure");
492 find_accel (GtkAccelKey *key,
496 return data == (gpointer) closure;
500 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
502 if (!accel_label->accel_string)
503 gtk_accel_label_refetch (accel_label);
505 return accel_label->accel_string;
508 /* Underscores in key names are better displayed as spaces
509 * E.g., Page_Up should be "Page Up"
512 substitute_underscores (char *str)
516 for (p = str; *p; p++)
521 /* On Mac, if the key has symbolic representation (e.g. arrow keys),
522 * append it to gstring and return TRUE; otherwise return FALSE.
523 * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html
524 * for the list of special keys. */
526 append_keyval_symbol (guint accelerator_key,
529 #ifdef GDK_WINDOWING_QUARTZ
530 switch (accelerator_key)
533 /* U+21A9 LEFTWARDS ARROW WITH HOOK */
534 g_string_append (gstring, "\xe2\x86\xa9");
538 /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
539 g_string_append (gstring, "\xe2\x8c\xa4");
543 /* U+2190 LEFTWARDS ARROW */
544 g_string_append (gstring, "\xe2\x86\x90");
548 /* U+2191 UPWARDS ARROW */
549 g_string_append (gstring, "\xe2\x86\x91");
553 /* U+2192 RIGHTWARDS ARROW */
554 g_string_append (gstring, "\xe2\x86\x92");
558 /* U+2193 DOWNWARDS ARROW */
559 g_string_append (gstring, "\xe2\x86\x93");
563 /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
564 g_string_append (gstring, "\xe2\x87\x9e");
568 /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
569 g_string_append (gstring, "\xe2\x87\x9f");
573 /* U+2196 NORTH WEST ARROW */
574 g_string_append (gstring, "\xe2\x86\x96");
578 /* U+2198 SOUTH EAST ARROW */
579 g_string_append (gstring, "\xe2\x86\x98");
583 /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
584 g_string_append (gstring, "\xe2\x8e\x8b");
588 /* U+232B ERASE TO THE LEFT */
589 g_string_append (gstring, "\xe2\x8c\xab");
593 /* U+2326 ERASE TO THE RIGHT */
594 g_string_append (gstring, "\xe2\x8c\xa6");
600 #else /* !GDK_WINDOWING_QUARTZ */
606 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
607 guint accelerator_key,
608 GdkModifierType accelerator_mods)
611 gboolean seen_mod = FALSE;
614 gstring = g_string_new ("");
616 if (accelerator_mods & GDK_SHIFT_MASK)
618 g_string_append (gstring, klass->mod_name_shift);
621 if (accelerator_mods & GDK_CONTROL_MASK)
624 g_string_append (gstring, klass->mod_separator);
625 g_string_append (gstring, klass->mod_name_control);
628 if (accelerator_mods & GDK_MOD1_MASK)
631 g_string_append (gstring, klass->mod_separator);
632 g_string_append (gstring, klass->mod_name_alt);
635 if (accelerator_mods & GDK_MOD2_MASK)
638 g_string_append (gstring, klass->mod_separator);
640 g_string_append (gstring, "Mod2");
643 if (accelerator_mods & GDK_MOD3_MASK)
646 g_string_append (gstring, klass->mod_separator);
648 g_string_append (gstring, "Mod3");
651 if (accelerator_mods & GDK_MOD4_MASK)
654 g_string_append (gstring, klass->mod_separator);
656 g_string_append (gstring, "Mod4");
659 if (accelerator_mods & GDK_MOD5_MASK)
662 g_string_append (gstring, klass->mod_separator);
664 g_string_append (gstring, "Mod5");
667 if (accelerator_mods & GDK_SUPER_MASK)
670 g_string_append (gstring, klass->mod_separator);
672 /* This is the text that should appear next to menu accelerators
673 * that use the super key. If the text on this key isn't typically
674 * translated on keyboards used for your language, don't translate
676 * And do not translate the part before the |.
678 g_string_append (gstring, Q_("keyboard label|Super"));
681 if (accelerator_mods & GDK_HYPER_MASK)
684 g_string_append (gstring, klass->mod_separator);
686 /* This is the text that should appear next to menu accelerators
687 * that use the hyper key. If the text on this key isn't typically
688 * translated on keyboards used for your language, don't translate
690 * And do not translate the part before the |.
692 g_string_append (gstring, Q_("keyboard label|Hyper"));
695 if (accelerator_mods & GDK_META_MASK)
698 g_string_append (gstring, klass->mod_separator);
700 #ifndef GDK_WINDOWING_QUARTZ
701 /* This is the text that should appear next to menu accelerators
702 * that use the meta key. If the text on this key isn't typically
703 * translated on keyboards used for your language, don't translate
705 * And do not translate the part before the |.
707 g_string_append (gstring, Q_("keyboard label|Meta"));
709 /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
710 g_string_append (gstring, "\xe2\x8c\x98");
715 g_string_append (gstring, klass->mod_separator);
717 ch = gdk_keyval_to_unicode (accelerator_key);
718 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
719 (ch < 0x80 || klass->latin1_to_char))
724 /* do not translate the part before the | */
725 g_string_append (gstring, Q_("keyboard label|Space"));
728 /* do not translate the part before the | */
729 g_string_append (gstring, Q_("keyboard label|Backslash"));
732 g_string_append_unichar (gstring, g_unichar_toupper (ch));
736 else if (!append_keyval_symbol (accelerator_key, gstring))
740 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
743 if (tmp[0] != 0 && tmp[1] == 0)
744 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
750 strcpy (msg, "keyboard label|");
751 g_strlcat (msg, tmp, 128);
755 g_string_append (gstring, tmp);
756 substitute_underscores (gstring->str);
759 g_string_append (gstring, str);
764 return g_string_free (gstring, FALSE);
768 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
770 gboolean enable_accels;
772 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
774 if (accel_label->accel_string)
776 g_free (accel_label->accel_string);
777 accel_label->accel_string = NULL;
780 g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
781 "gtk-enable-accels", &enable_accels,
784 if (enable_accels && accel_label->accel_closure)
786 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
788 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
790 GtkAccelLabelClass *klass;
793 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
794 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
797 accel_label->accel_string = g_strconcat (" ", tmp, NULL);
800 if (!accel_label->accel_string)
801 accel_label->accel_string = g_strdup ("-/-");
804 if (!accel_label->accel_string)
805 accel_label->accel_string = g_strdup ("");
807 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
812 #define __GTK_ACCEL_LABEL_C__
813 #include "gtkaliasdef.c"