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_class_init (GtkAccelLabelClass *klass);
47 static void gtk_accel_label_init (GtkAccelLabel *accel_label);
48 static void gtk_accel_label_set_property (GObject *object,
52 static void gtk_accel_label_get_property (GObject *object,
56 static void gtk_accel_label_destroy (GtkObject *object);
57 static void gtk_accel_label_finalize (GObject *object);
58 static void gtk_accel_label_size_request (GtkWidget *widget,
59 GtkRequisition *requisition);
60 static gboolean gtk_accel_label_expose_event (GtkWidget *widget,
61 GdkEventExpose *event);
62 static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
65 static GtkLabelClass *parent_class = NULL;
68 gtk_accel_label_get_type (void)
70 static GType accel_label_type = 0;
72 if (!accel_label_type)
74 static const GTypeInfo accel_label_info =
76 sizeof (GtkAccelLabelClass),
78 NULL, /* base_finalize */
79 (GClassInitFunc) gtk_accel_label_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
82 sizeof (GtkAccelLabel),
84 (GInstanceInitFunc) gtk_accel_label_init,
88 g_type_register_static (GTK_TYPE_LABEL, "GtkAccelLabel",
89 &accel_label_info, 0);
92 return accel_label_type;
96 gtk_accel_label_class_init (GtkAccelLabelClass *class)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
99 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
100 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
102 parent_class = g_type_class_peek_parent (class);
104 gobject_class->finalize = gtk_accel_label_finalize;
105 gobject_class->set_property = gtk_accel_label_set_property;
106 gobject_class->get_property = gtk_accel_label_get_property;
108 object_class->destroy = gtk_accel_label_destroy;
110 widget_class->size_request = gtk_accel_label_size_request;
111 widget_class->expose_event = gtk_accel_label_expose_event;
113 class->signal_quote1 = g_strdup ("<:");
114 class->signal_quote2 = g_strdup (":>");
115 /* This is the text that should appear next to menu accelerators
116 * that use the shift key. If the text on this key isn't typically
117 * translated on keyboards used for your language, don't translate
119 * And do not translate the part before the |.
121 class->mod_name_shift = g_strdup (Q_("keyboard label|Shift"));
122 /* This is the text that should appear next to menu accelerators
123 * that use the control key. If the text on this key isn't typically
124 * translated on keyboards used for your language, don't translate
126 * And do not translate the part before the |.
128 class->mod_name_control = g_strdup (Q_("keyboard label|Ctrl"));
129 /* This is the text that should appear next to menu accelerators
130 * that use the alt key. If the text on this key isn't typically
131 * translated on keyboards used for your language, don't translate
133 * And do not translate the part before the |.
135 class->mod_name_alt = g_strdup (Q_("keyboard label|Alt"));
136 class->mod_separator = g_strdup ("+");
137 class->accel_seperator = g_strdup (" / ");
138 class->latin1_to_char = TRUE;
140 g_object_class_install_property (gobject_class,
142 g_param_spec_boxed ("accel-closure",
143 P_("Accelerator Closure"),
144 P_("The closure to be monitored for accelerator changes"),
146 GTK_PARAM_READWRITE));
147 g_object_class_install_property (gobject_class,
149 g_param_spec_object ("accel-widget",
150 P_("Accelerator Widget"),
151 P_("The widget to be monitored for accelerator changes"),
153 GTK_PARAM_READWRITE));
157 gtk_accel_label_set_property (GObject *object,
162 GtkAccelLabel *accel_label;
164 accel_label = GTK_ACCEL_LABEL (object);
168 case PROP_ACCEL_CLOSURE:
169 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
171 case PROP_ACCEL_WIDGET:
172 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 gtk_accel_label_get_property (GObject *object,
186 GtkAccelLabel *accel_label;
188 accel_label = GTK_ACCEL_LABEL (object);
192 case PROP_ACCEL_CLOSURE:
193 g_value_set_boxed (value, accel_label->accel_closure);
195 case PROP_ACCEL_WIDGET:
196 g_value_set_object (value, accel_label->accel_widget);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 gtk_accel_label_init (GtkAccelLabel *accel_label)
207 accel_label->accel_padding = 3;
208 accel_label->accel_widget = NULL;
209 accel_label->accel_closure = NULL;
210 accel_label->accel_group = NULL;
211 accel_label->accel_string = NULL;
215 gtk_accel_label_new (const gchar *string)
217 GtkAccelLabel *accel_label;
219 g_return_val_if_fail (string != NULL, NULL);
221 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
223 gtk_label_set_text (GTK_LABEL (accel_label), string);
225 return GTK_WIDGET (accel_label);
229 gtk_accel_label_destroy (GtkObject *object)
231 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
233 gtk_accel_label_set_accel_widget (accel_label, NULL);
234 gtk_accel_label_set_accel_closure (accel_label, NULL);
236 GTK_OBJECT_CLASS (parent_class)->destroy (object);
240 gtk_accel_label_finalize (GObject *object)
242 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
244 g_free (accel_label->accel_string);
246 G_OBJECT_CLASS (parent_class)->finalize (object);
250 * gtk_accel_label_get_accel_widget:
251 * @accel_label: a #GtkAccelLabel
253 * Fetches the widget monitored by this accelerator label. See
254 * gtk_accel_label_set_accel_widget().
256 * Return value: the object monitored by the accelerator label,
260 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
262 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
264 return accel_label->accel_widget;
268 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
270 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
272 return (accel_label->accel_string_width +
273 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
277 gtk_accel_label_size_request (GtkWidget *widget,
278 GtkRequisition *requisition)
280 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
284 if (GTK_WIDGET_CLASS (parent_class)->size_request)
285 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
287 layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
288 pango_layout_get_pixel_size (layout, &width, NULL);
289 accel_label->accel_string_width = width;
291 g_object_unref (layout);
295 get_first_baseline (PangoLayout *layout)
297 PangoLayoutIter *iter;
300 iter = pango_layout_get_iter (layout);
301 result = pango_layout_iter_get_baseline (iter);
302 pango_layout_iter_free (iter);
304 return PANGO_PIXELS (result);
308 gtk_accel_label_expose_event (GtkWidget *widget,
309 GdkEventExpose *event)
311 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
312 GtkMisc *misc = GTK_MISC (accel_label);
313 GtkTextDirection direction;
315 direction = gtk_widget_get_direction (widget);
317 if (GTK_WIDGET_DRAWABLE (accel_label))
321 ac_width = gtk_accel_label_get_accel_width (accel_label);
323 if (widget->allocation.width >= widget->requisition.width + ac_width)
325 PangoLayout *label_layout;
326 PangoLayout *accel_layout;
327 GtkLabel *label = GTK_LABEL (widget);
332 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
334 if (direction == GTK_TEXT_DIR_RTL)
335 widget->allocation.x += ac_width;
336 widget->allocation.width -= ac_width;
337 if (gtk_label_get_ellipsize (label))
338 pango_layout_set_width (label_layout,
339 pango_layout_get_width (label_layout)
340 - ac_width * PANGO_SCALE);
342 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
343 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
344 if (direction == GTK_TEXT_DIR_RTL)
345 widget->allocation.x -= ac_width;
346 widget->allocation.width += ac_width;
347 if (gtk_label_get_ellipsize (label))
348 pango_layout_set_width (label_layout,
349 pango_layout_get_width (label_layout)
350 + ac_width * PANGO_SCALE);
352 if (direction == GTK_TEXT_DIR_RTL)
353 x = widget->allocation.x + misc->xpad;
355 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
357 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
359 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
361 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
363 gtk_paint_layout (widget->style,
365 GTK_WIDGET_STATE (widget),
373 g_object_unref (accel_layout);
377 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
378 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
386 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
388 GClosure *closure = NULL;
391 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
392 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
394 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
395 for (list = clist; list; list = list->next)
397 /* we just take the first closure used */
398 closure = list->data;
402 gtk_accel_label_set_accel_closure (accel_label, closure);
406 * gtk_accel_label_set_accel_widget:
407 * @accel_label: a #GtkAccelLabel
408 * @accel_widget: the widget to be monitored.
410 * Sets the widget to be monitored by this accelerator label.
413 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
414 GtkWidget *accel_widget)
416 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
418 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
420 if (accel_widget != accel_label->accel_widget)
422 if (accel_label->accel_widget)
424 gtk_accel_label_set_accel_closure (accel_label, NULL);
425 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
426 refetch_widget_accel_closure,
428 g_object_unref (accel_label->accel_widget);
430 accel_label->accel_widget = accel_widget;
431 if (accel_label->accel_widget)
433 g_object_ref (accel_label->accel_widget);
434 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
435 G_CALLBACK (refetch_widget_accel_closure),
436 accel_label, G_CONNECT_SWAPPED);
437 refetch_widget_accel_closure (accel_label);
439 g_object_notify (G_OBJECT (accel_label), "accel-widget");
444 gtk_accel_label_reset (GtkAccelLabel *accel_label)
446 if (accel_label->accel_string)
448 g_free (accel_label->accel_string);
449 accel_label->accel_string = NULL;
452 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
456 check_accel_changed (GtkAccelGroup *accel_group,
458 GdkModifierType modifier,
459 GClosure *accel_closure,
460 GtkAccelLabel *accel_label)
462 if (accel_closure == accel_label->accel_closure)
463 gtk_accel_label_reset (accel_label);
467 * gtk_accel_label_set_accel_closure:
468 * @accel_label: a #GtkAccelLabel
469 * @accel_closure: the closure to monitor for accelerator changes.
471 * Sets the closure to be monitored by this accelerator label. The closure
472 * must be connected to an accelerator group; see gtk_accel_group_connect().
475 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
476 GClosure *accel_closure)
478 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
480 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
482 if (accel_closure != accel_label->accel_closure)
484 if (accel_label->accel_closure)
486 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
489 accel_label->accel_group = NULL;
490 g_closure_unref (accel_label->accel_closure);
492 accel_label->accel_closure = accel_closure;
493 if (accel_label->accel_closure)
495 g_closure_ref (accel_label->accel_closure);
496 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
497 g_signal_connect_object (accel_label->accel_group, "accel_changed",
498 G_CALLBACK (check_accel_changed),
501 gtk_accel_label_reset (accel_label);
502 g_object_notify (G_OBJECT (accel_label), "accel-closure");
507 find_accel (GtkAccelKey *key,
511 return data == (gpointer) closure;
515 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
517 if (!accel_label->accel_string)
518 gtk_accel_label_refetch (accel_label);
520 return accel_label->accel_string;
523 /* Underscores in key names are better displayed as spaces
524 * E.g., Page_Up should be "Page Up"
527 substitute_underscores (char *str)
531 for (p = str; *p; p++)
537 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
538 guint accelerator_key,
539 GdkModifierType accelerator_mods)
542 gboolean seen_mod = FALSE;
545 gstring = g_string_new ("");
547 if (accelerator_mods & GDK_SHIFT_MASK)
549 g_string_append (gstring, klass->mod_name_shift);
552 if (accelerator_mods & GDK_CONTROL_MASK)
555 g_string_append (gstring, klass->mod_separator);
556 g_string_append (gstring, klass->mod_name_control);
559 if (accelerator_mods & GDK_MOD1_MASK)
562 g_string_append (gstring, klass->mod_separator);
563 g_string_append (gstring, klass->mod_name_alt);
567 g_string_append (gstring, klass->mod_separator);
569 ch = gdk_keyval_to_unicode (accelerator_key);
570 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
571 (ch < 0x80 || klass->latin1_to_char))
576 /* do not translate the part before the | */
577 g_string_append (gstring, Q_("keyboard label|Space"));
580 /* do not translate the part before the | */
581 g_string_append (gstring, Q_("keyboard label|Backslash"));
584 g_string_append_unichar (gstring, g_unichar_toupper (ch));
592 tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
595 if (tmp[0] != 0 && tmp[1] == 0)
596 g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
602 strcpy (msg, "keyboard label|");
603 g_strlcat (msg, tmp, 128);
604 str = dgettext (GETTEXT_PACKAGE, msg);
607 g_string_append (gstring, tmp);
608 substitute_underscores (gstring->str);
611 g_string_append (gstring, str);
616 return g_string_free (gstring, FALSE);
620 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
622 GtkAccelLabelClass *class;
624 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
626 class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
628 if (accel_label->accel_string)
630 g_free (accel_label->accel_string);
631 accel_label->accel_string = NULL;
634 if (accel_label->accel_closure)
636 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
638 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
640 GtkAccelLabelClass *klass;
643 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
644 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
647 accel_label->accel_string = g_strconcat (" ", tmp, NULL);
650 if (!accel_label->accel_string)
651 accel_label->accel_string = g_strdup ("-/-");
654 if (!accel_label->accel_string)
655 accel_label->accel_string = g_strdup ("");
657 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
662 #define __GTK_ACCEL_LABEL_C__
663 #include "gtkaliasdef.c"