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"
46 static void gtk_accel_label_set_property (GObject *object,
50 static void gtk_accel_label_get_property (GObject *object,
54 static void gtk_accel_label_destroy (GtkObject *object);
55 static void gtk_accel_label_finalize (GObject *object);
56 static void gtk_accel_label_size_request (GtkWidget *widget,
57 GtkRequisition *requisition);
58 static gboolean gtk_accel_label_expose_event (GtkWidget *widget,
59 GdkEventExpose *event);
60 static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
63 G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
66 gtk_accel_label_class_init (GtkAccelLabelClass *class)
68 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
69 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
70 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
72 gobject_class->finalize = gtk_accel_label_finalize;
73 gobject_class->set_property = gtk_accel_label_set_property;
74 gobject_class->get_property = gtk_accel_label_get_property;
76 object_class->destroy = gtk_accel_label_destroy;
78 widget_class->size_request = gtk_accel_label_size_request;
79 widget_class->expose_event = gtk_accel_label_expose_event;
81 class->signal_quote1 = g_strdup ("<:");
82 class->signal_quote2 = g_strdup (":>");
83 /* This is the text that should appear next to menu accelerators
84 * that use the shift key. If the text on this key isn't typically
85 * translated on keyboards used for your language, don't translate
88 * Don't include the prefix "keyboard label|" in the translation.
90 class->mod_name_shift = g_strdup (Q_("keyboard label|Shift"));
91 /* This is the text that should appear next to menu accelerators
92 * that use the control key. If the text on this key isn't typically
93 * translated on keyboards used for your language, don't translate
96 * Don't include the prefix "keyboard label|" in the translation.
98 class->mod_name_control = g_strdup (Q_("keyboard label|Ctrl"));
99 /* This is the text that should appear next to menu accelerators
100 * that use the alt key. If the text on this key isn't typically
101 * translated on keyboards used for your language, don't translate
104 * Don't include the prefix "keyboard label|" in the translation.
106 class->mod_name_alt = g_strdup (Q_("keyboard label|Alt"));
107 class->mod_separator = g_strdup ("+");
108 class->accel_seperator = g_strdup (" / ");
109 class->latin1_to_char = TRUE;
111 g_object_class_install_property (gobject_class,
113 g_param_spec_boxed ("accel-closure",
114 P_("Accelerator Closure"),
115 P_("The closure to be monitored for accelerator changes"),
117 GTK_PARAM_READWRITE));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_object ("accel-widget",
121 P_("Accelerator Widget"),
122 P_("The widget to be monitored for accelerator changes"),
124 GTK_PARAM_READWRITE));
128 gtk_accel_label_set_property (GObject *object,
133 GtkAccelLabel *accel_label;
135 accel_label = GTK_ACCEL_LABEL (object);
139 case PROP_ACCEL_CLOSURE:
140 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
142 case PROP_ACCEL_WIDGET:
143 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 gtk_accel_label_get_property (GObject *object,
157 GtkAccelLabel *accel_label;
159 accel_label = GTK_ACCEL_LABEL (object);
163 case PROP_ACCEL_CLOSURE:
164 g_value_set_boxed (value, accel_label->accel_closure);
166 case PROP_ACCEL_WIDGET:
167 g_value_set_object (value, accel_label->accel_widget);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gtk_accel_label_init (GtkAccelLabel *accel_label)
178 accel_label->accel_padding = 3;
179 accel_label->accel_widget = NULL;
180 accel_label->accel_closure = NULL;
181 accel_label->accel_group = NULL;
182 accel_label->accel_string = NULL;
186 gtk_accel_label_new (const gchar *string)
188 GtkAccelLabel *accel_label;
190 g_return_val_if_fail (string != NULL, NULL);
192 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
194 gtk_label_set_text (GTK_LABEL (accel_label), string);
196 return GTK_WIDGET (accel_label);
200 gtk_accel_label_destroy (GtkObject *object)
202 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
204 gtk_accel_label_set_accel_widget (accel_label, NULL);
205 gtk_accel_label_set_accel_closure (accel_label, NULL);
207 GTK_OBJECT_CLASS (gtk_accel_label_parent_class)->destroy (object);
211 gtk_accel_label_finalize (GObject *object)
213 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
215 g_free (accel_label->accel_string);
217 G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
221 * gtk_accel_label_get_accel_widget:
222 * @accel_label: a #GtkAccelLabel
224 * Fetches the widget monitored by this accelerator label. See
225 * gtk_accel_label_set_accel_widget().
227 * Return value: the object monitored by the accelerator label,
231 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
233 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
235 return accel_label->accel_widget;
239 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
241 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
243 return (accel_label->accel_string_width +
244 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
248 gtk_accel_label_size_request (GtkWidget *widget,
249 GtkRequisition *requisition)
251 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
255 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request)
256 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request (widget, requisition);
258 layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
259 pango_layout_get_pixel_size (layout, &width, NULL);
260 accel_label->accel_string_width = width;
262 g_object_unref (layout);
266 get_first_baseline (PangoLayout *layout)
268 PangoLayoutIter *iter;
271 iter = pango_layout_get_iter (layout);
272 result = pango_layout_iter_get_baseline (iter);
273 pango_layout_iter_free (iter);
275 return PANGO_PIXELS (result);
279 gtk_accel_label_expose_event (GtkWidget *widget,
280 GdkEventExpose *event)
282 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
283 GtkMisc *misc = GTK_MISC (accel_label);
284 GtkTextDirection direction;
286 direction = gtk_widget_get_direction (widget);
288 if (GTK_WIDGET_DRAWABLE (accel_label))
292 ac_width = gtk_accel_label_get_accel_width (accel_label);
294 if (widget->allocation.width >= widget->requisition.width + ac_width)
296 PangoLayout *label_layout;
297 PangoLayout *accel_layout;
298 GtkLabel *label = GTK_LABEL (widget);
303 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
305 if (direction == GTK_TEXT_DIR_RTL)
306 widget->allocation.x += ac_width;
307 widget->allocation.width -= ac_width;
308 if (gtk_label_get_ellipsize (label))
309 pango_layout_set_width (label_layout,
310 pango_layout_get_width (label_layout)
311 - ac_width * PANGO_SCALE);
313 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
314 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
315 if (direction == GTK_TEXT_DIR_RTL)
316 widget->allocation.x -= ac_width;
317 widget->allocation.width += ac_width;
318 if (gtk_label_get_ellipsize (label))
319 pango_layout_set_width (label_layout,
320 pango_layout_get_width (label_layout)
321 + ac_width * PANGO_SCALE);
323 if (direction == GTK_TEXT_DIR_RTL)
324 x = widget->allocation.x + misc->xpad;
326 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
328 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
330 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
332 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
334 gtk_paint_layout (widget->style,
336 GTK_WIDGET_STATE (widget),
344 g_object_unref (accel_layout);
348 if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
349 GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
357 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
359 GClosure *closure = NULL;
362 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
363 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
365 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
366 for (list = clist; list; list = list->next)
368 /* we just take the first closure used */
369 closure = list->data;
373 gtk_accel_label_set_accel_closure (accel_label, closure);
377 * gtk_accel_label_set_accel_widget:
378 * @accel_label: a #GtkAccelLabel
379 * @accel_widget: the widget to be monitored.
381 * Sets the widget to be monitored by this accelerator label.
384 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
385 GtkWidget *accel_widget)
387 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
389 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
391 if (accel_widget != accel_label->accel_widget)
393 if (accel_label->accel_widget)
395 gtk_accel_label_set_accel_closure (accel_label, NULL);
396 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
397 refetch_widget_accel_closure,
399 g_object_unref (accel_label->accel_widget);
401 accel_label->accel_widget = accel_widget;
402 if (accel_label->accel_widget)
404 g_object_ref (accel_label->accel_widget);
405 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
406 G_CALLBACK (refetch_widget_accel_closure),
407 accel_label, G_CONNECT_SWAPPED);
408 refetch_widget_accel_closure (accel_label);
410 g_object_notify (G_OBJECT (accel_label), "accel-widget");
415 gtk_accel_label_reset (GtkAccelLabel *accel_label)
417 if (accel_label->accel_string)
419 g_free (accel_label->accel_string);
420 accel_label->accel_string = NULL;
423 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
427 check_accel_changed (GtkAccelGroup *accel_group,
429 GdkModifierType modifier,
430 GClosure *accel_closure,
431 GtkAccelLabel *accel_label)
433 if (accel_closure == accel_label->accel_closure)
434 gtk_accel_label_reset (accel_label);
438 * gtk_accel_label_set_accel_closure:
439 * @accel_label: a #GtkAccelLabel
440 * @accel_closure: the closure to monitor for accelerator changes.
442 * Sets the closure to be monitored by this accelerator label. The closure
443 * must be connected to an accelerator group; see gtk_accel_group_connect().
446 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
447 GClosure *accel_closure)
449 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
451 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
453 if (accel_closure != accel_label->accel_closure)
455 if (accel_label->accel_closure)
457 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
460 accel_label->accel_group = NULL;
461 g_closure_unref (accel_label->accel_closure);
463 accel_label->accel_closure = accel_closure;
464 if (accel_label->accel_closure)
466 g_closure_ref (accel_label->accel_closure);
467 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
468 g_signal_connect_object (accel_label->accel_group, "accel_changed",
469 G_CALLBACK (check_accel_changed),
472 gtk_accel_label_reset (accel_label);
473 g_object_notify (G_OBJECT (accel_label), "accel-closure");
478 find_accel (GtkAccelKey *key,
482 return data == (gpointer) closure;
486 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
488 if (!accel_label->accel_string)
489 gtk_accel_label_refetch (accel_label);
491 return accel_label->accel_string;
494 /* Underscores in key names are better displayed as spaces
495 * E.g., Page_Up should be "Page Up"
498 substitute_underscores (char *str)
502 for (p = str; *p; p++)
508 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
509 guint accelerator_key,
510 GdkModifierType accelerator_mods)
513 gboolean seen_mod = FALSE;
516 gstring = g_string_new ("");
518 if (accelerator_mods & GDK_SHIFT_MASK)
520 g_string_append (gstring, klass->mod_name_shift);
523 if (accelerator_mods & GDK_CONTROL_MASK)
526 g_string_append (gstring, klass->mod_separator);
527 g_string_append (gstring, klass->mod_name_control);
530 if (accelerator_mods & GDK_MOD1_MASK)
533 g_string_append (gstring, klass->mod_separator);
534 g_string_append (gstring, klass->mod_name_alt);
537 if (accelerator_mods & GDK_MOD2_MASK)
540 g_string_append (gstring, klass->mod_separator);
542 g_string_append (gstring, "Mod2");
545 if (accelerator_mods & GDK_MOD3_MASK)
548 g_string_append (gstring, klass->mod_separator);
550 g_string_append (gstring, "Mod3");
553 if (accelerator_mods & GDK_MOD4_MASK)
556 g_string_append (gstring, klass->mod_separator);
558 g_string_append (gstring, "Mod4");
561 if (accelerator_mods & GDK_MOD5_MASK)
564 g_string_append (gstring, klass->mod_separator);
566 g_string_append (gstring, "Mod5");
569 if (accelerator_mods & GDK_SUPER_MASK)
572 g_string_append (gstring, klass->mod_separator);
574 /* This is the text that should appear next to menu accelerators
575 * that use the super key. If the text on this key isn't typically
576 * translated on keyboards used for your language, don't translate
578 * And do not translate the part before the |.
580 g_string_append (gstring, Q_("keyboard label|Super"));
583 if (accelerator_mods & GDK_HYPER_MASK)
586 g_string_append (gstring, klass->mod_separator);
588 /* This is the text that should appear next to menu accelerators
589 * that use the hyper key. If the text on this key isn't typically
590 * translated on keyboards used for your language, don't translate
592 * And do not translate the part before the |.
594 g_string_append (gstring, Q_("keyboard label|Hyper"));
597 if (accelerator_mods & GDK_META_MASK)
600 g_string_append (gstring, klass->mod_separator);
602 /* This is the text that should appear next to menu accelerators
603 * that use the meta key. If the text on this key isn't typically
604 * translated on keyboards used for your language, don't translate
606 * And do not translate the part before the |.
608 g_string_append (gstring, Q_("keyboard label|Meta"));
612 g_string_append (gstring, klass->mod_separator);
614 ch = gdk_keyval_to_unicode (accelerator_key);
615 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
616 (ch < 0x80 || klass->latin1_to_char))
621 /* do not translate the part before the | */
622 g_string_append (gstring, Q_("keyboard label|Space"));
625 /* do not translate the part before the | */
626 g_string_append (gstring, Q_("keyboard label|Backslash"));
629 g_string_append_unichar (gstring, g_unichar_toupper (ch));
637 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
640 if (tmp[0] != 0 && tmp[1] == 0)
641 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
647 strcpy (msg, "keyboard label|");
648 g_strlcat (msg, tmp, 128);
649 str = dgettext (GETTEXT_PACKAGE, msg);
652 g_string_append (gstring, tmp);
653 substitute_underscores (gstring->str);
656 g_string_append (gstring, str);
661 return g_string_free (gstring, FALSE);
665 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
667 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
669 if (accel_label->accel_string)
671 g_free (accel_label->accel_string);
672 accel_label->accel_string = NULL;
675 if (accel_label->accel_closure)
677 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
679 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
681 GtkAccelLabelClass *klass;
684 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
685 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
688 accel_label->accel_string = g_strconcat (" ", tmp, NULL);
691 if (!accel_label->accel_string)
692 accel_label->accel_string = g_strdup ("-/-");
695 if (!accel_label->accel_string)
696 accel_label->accel_string = g_strdup ("");
698 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
703 #define __GTK_ACCEL_LABEL_C__
704 #include "gtkaliasdef.c"