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/.
34 #include "gtkaccellabel.h"
35 #include "gtkaccelmap.h"
45 static void gtk_accel_label_class_init (GtkAccelLabelClass *klass);
46 static void gtk_accel_label_init (GtkAccelLabel *accel_label);
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 static GtkLabelClass *parent_class = NULL;
67 gtk_accel_label_get_type (void)
69 static GType accel_label_type = 0;
71 if (!accel_label_type)
73 static const GTypeInfo accel_label_info =
75 sizeof (GtkAccelLabelClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc) gtk_accel_label_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (GtkAccelLabel),
83 (GInstanceInitFunc) gtk_accel_label_init,
87 g_type_register_static (GTK_TYPE_LABEL, "GtkAccelLabel",
88 &accel_label_info, 0);
91 return accel_label_type;
95 gtk_accel_label_class_init (GtkAccelLabelClass *class)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
98 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
99 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
101 parent_class = g_type_class_peek_parent (class);
103 gobject_class->finalize = gtk_accel_label_finalize;
104 gobject_class->set_property = gtk_accel_label_set_property;
105 gobject_class->get_property = gtk_accel_label_get_property;
107 object_class->destroy = gtk_accel_label_destroy;
109 widget_class->size_request = gtk_accel_label_size_request;
110 widget_class->expose_event = gtk_accel_label_expose_event;
112 class->signal_quote1 = g_strdup ("<:");
113 class->signal_quote2 = g_strdup (":>");
114 /* This is the text that should appear next to menu accelerators
115 * that use the shift key. If the text on this key isn't typically
116 * translated on keyboards used for your language, don't translate
119 class->mod_name_shift = g_strdup (_("Shift"));
120 /* This is the text that should appear next to menu accelerators
121 * that use the control key. If the text on this key isn't typically
122 * translated on keyboards used for your language, don't translate
125 class->mod_name_control = g_strdup (_("Ctrl"));
126 /* This is the text that should appear next to menu accelerators
127 * that use the alt key. If the text on this key isn't typically
128 * translated on keyboards used for your language, don't translate
131 class->mod_name_alt = g_strdup (_("Alt"));
132 class->mod_separator = g_strdup ("+");
133 class->accel_seperator = g_strdup (" / ");
134 class->latin1_to_char = TRUE;
136 g_object_class_install_property (gobject_class,
138 g_param_spec_boxed ("accel_closure",
139 P_("Accelerator Closure"),
140 P_("The closure to be monitored for accelerator changes"),
142 G_PARAM_READABLE | G_PARAM_WRITABLE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_object ("accel_widget",
146 P_("Accelerator Widget"),
147 P_("The widget to be monitored for accelerator changes"),
149 G_PARAM_READABLE | G_PARAM_WRITABLE));
153 gtk_accel_label_set_property (GObject *object,
158 GtkAccelLabel *accel_label;
160 accel_label = GTK_ACCEL_LABEL (object);
164 case PROP_ACCEL_CLOSURE:
165 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
167 case PROP_ACCEL_WIDGET:
168 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 gtk_accel_label_get_property (GObject *object,
182 GtkAccelLabel *accel_label;
184 accel_label = GTK_ACCEL_LABEL (object);
188 case PROP_ACCEL_CLOSURE:
189 g_value_set_boxed (value, accel_label->accel_closure);
191 case PROP_ACCEL_WIDGET:
192 g_value_set_object (value, accel_label->accel_widget);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 gtk_accel_label_init (GtkAccelLabel *accel_label)
203 accel_label->accel_padding = 3;
204 accel_label->accel_widget = NULL;
205 accel_label->accel_closure = NULL;
206 accel_label->accel_group = NULL;
207 accel_label->accel_string = NULL;
211 gtk_accel_label_new (const gchar *string)
213 GtkAccelLabel *accel_label;
215 g_return_val_if_fail (string != NULL, NULL);
217 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
219 gtk_label_set_text (GTK_LABEL (accel_label), string);
221 return GTK_WIDGET (accel_label);
225 gtk_accel_label_destroy (GtkObject *object)
227 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
229 gtk_accel_label_set_accel_widget (accel_label, NULL);
230 gtk_accel_label_set_accel_closure (accel_label, NULL);
232 GTK_OBJECT_CLASS (parent_class)->destroy (object);
236 gtk_accel_label_finalize (GObject *object)
238 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
240 g_free (accel_label->accel_string);
242 G_OBJECT_CLASS (parent_class)->finalize (object);
246 * gtk_accel_label_get_accel_widget:
247 * @accel_label: a #GtkAccelLabel
249 * Fetches the widget monitored by this accelerator label. See
250 * gtk_accel_label_set_accel_widget().
252 * Return value: the object monitored by the accelerator label,
256 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
258 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
260 return accel_label->accel_widget;
264 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
266 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
268 return (accel_label->accel_string_width +
269 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
273 gtk_accel_label_size_request (GtkWidget *widget,
274 GtkRequisition *requisition)
276 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
280 if (GTK_WIDGET_CLASS (parent_class)->size_request)
281 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
283 layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
284 pango_layout_get_pixel_size (layout, &width, NULL);
285 accel_label->accel_string_width = width;
287 g_object_unref (layout);
291 get_first_baseline (PangoLayout *layout)
293 PangoLayoutIter *iter;
296 iter = pango_layout_get_iter (layout);
297 result = pango_layout_iter_get_baseline (iter);
298 pango_layout_iter_free (iter);
300 return PANGO_PIXELS (result);
304 gtk_accel_label_expose_event (GtkWidget *widget,
305 GdkEventExpose *event)
307 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
308 GtkMisc *misc = GTK_MISC (accel_label);
309 GtkTextDirection direction;
311 direction = gtk_widget_get_direction (widget);
313 if (GTK_WIDGET_DRAWABLE (accel_label))
317 ac_width = gtk_accel_label_get_accel_width (accel_label);
319 if (widget->allocation.width >= widget->requisition.width + ac_width)
321 PangoLayout *label_layout;
322 PangoLayout *accel_layout;
323 GtkLabel *label = GTK_LABEL (widget);
328 if (direction == GTK_TEXT_DIR_RTL)
329 widget->allocation.x += ac_width;
330 widget->allocation.width -= ac_width;
331 if (gtk_label_get_ellipsize (label))
332 pango_layout_set_width (label->layout,
333 pango_layout_get_width (label->layout)
334 - ac_width * PANGO_SCALE);
336 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
337 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
338 if (direction == GTK_TEXT_DIR_RTL)
339 widget->allocation.x -= ac_width;
340 widget->allocation.width += ac_width;
341 if (gtk_label_get_ellipsize (label))
342 pango_layout_set_width (label->layout,
343 pango_layout_get_width (label->layout)
344 + ac_width * PANGO_SCALE);
346 if (direction == GTK_TEXT_DIR_RTL)
347 x = widget->allocation.x + misc->xpad;
349 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
351 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
352 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
354 accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
356 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
358 gtk_paint_layout (widget->style,
360 GTK_WIDGET_STATE (widget),
368 g_object_unref (accel_layout);
372 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
373 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
381 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
383 GClosure *closure = NULL;
386 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
387 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
389 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
390 for (list = clist; list; list = list->next)
392 /* we just take the first closure used */
393 closure = list->data;
397 gtk_accel_label_set_accel_closure (accel_label, closure);
401 * gtk_accel_label_set_accel_widget:
402 * @accel_label: a #GtkAccelLabel
403 * @accel_widget: the widget to be monitored.
405 * Sets the widget to be monitored by this accelerator label.
408 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
409 GtkWidget *accel_widget)
411 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
413 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
415 if (accel_widget != accel_label->accel_widget)
417 if (accel_label->accel_widget)
419 gtk_accel_label_set_accel_closure (accel_label, NULL);
420 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
421 refetch_widget_accel_closure,
423 g_object_unref (accel_label->accel_widget);
425 accel_label->accel_widget = accel_widget;
426 if (accel_label->accel_widget)
428 g_object_ref (accel_label->accel_widget);
429 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
430 G_CALLBACK (refetch_widget_accel_closure),
431 accel_label, G_CONNECT_SWAPPED);
432 refetch_widget_accel_closure (accel_label);
434 g_object_notify (G_OBJECT (accel_label), "accel_widget");
439 gtk_accel_label_reset (GtkAccelLabel *accel_label)
441 if (accel_label->accel_string)
443 g_free (accel_label->accel_string);
444 accel_label->accel_string = NULL;
447 gtk_widget_queue_resize (GTK_WIDGET (accel_label));
451 check_accel_changed (GtkAccelGroup *accel_group,
453 GdkModifierType modifier,
454 GClosure *accel_closure,
455 GtkAccelLabel *accel_label)
457 if (accel_closure == accel_label->accel_closure)
458 gtk_accel_label_reset (accel_label);
462 * gtk_accel_label_set_accel_closure:
463 * @accel_label: a #GtkAccelLabel
464 * @accel_closure: the closure to monitor for accelerator changes.
466 * Sets the closure to be monitored by this accelerator label. The closure
467 * must be connected to an accelerator group; see gtk_accel_group_connect().
470 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
471 GClosure *accel_closure)
473 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
475 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
477 if (accel_closure != accel_label->accel_closure)
479 if (accel_label->accel_closure)
481 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
484 accel_label->accel_group = NULL;
485 g_closure_unref (accel_label->accel_closure);
487 accel_label->accel_closure = accel_closure;
488 if (accel_label->accel_closure)
490 g_closure_ref (accel_label->accel_closure);
491 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
492 g_signal_connect_object (accel_label->accel_group, "accel_changed",
493 G_CALLBACK (check_accel_changed),
496 gtk_accel_label_reset (accel_label);
497 g_object_notify (G_OBJECT (accel_label), "accel_closure");
502 find_accel (GtkAccelKey *key,
506 return data == (gpointer) closure;
510 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
512 if (!accel_label->accel_string)
513 gtk_accel_label_refetch (accel_label);
515 return accel_label->accel_string;
518 /* Underscores in key names are better displayed as spaces
519 * E.g., Page_Up should be "Page Up"
522 substitute_underscores (char *str)
526 for (p = str; *p; p++)
532 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
533 guint accelerator_key,
534 GdkModifierType accelerator_mods)
537 gboolean seen_mod = FALSE;
540 gstring = g_string_new ("");
542 if (accelerator_mods & GDK_SHIFT_MASK)
544 g_string_append (gstring, klass->mod_name_shift);
547 if (accelerator_mods & GDK_CONTROL_MASK)
550 g_string_append (gstring, klass->mod_separator);
551 g_string_append (gstring, klass->mod_name_control);
554 if (accelerator_mods & GDK_MOD1_MASK)
557 g_string_append (gstring, klass->mod_separator);
558 g_string_append (gstring, klass->mod_name_alt);
562 g_string_append (gstring, klass->mod_separator);
564 ch = gdk_keyval_to_unicode (accelerator_key);
565 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
566 (ch < 0x80 || klass->latin1_to_char))
571 g_string_append (gstring, "Space");
574 g_string_append (gstring, "Backslash");
577 g_string_append_unichar (gstring, g_unichar_toupper (ch));
585 tmp = gtk_accelerator_name (accelerator_key, 0);
586 if (tmp[0] != 0 && tmp[1] == 0)
587 tmp[0] = g_ascii_toupper (tmp[0]);
588 substitute_underscores (tmp);
589 g_string_append (gstring, tmp);
593 return g_string_free (gstring, FALSE);
597 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
599 GtkAccelLabelClass *class;
601 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
603 class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
605 if (accel_label->accel_string)
607 g_free (accel_label->accel_string);
608 accel_label->accel_string = NULL;
611 if (accel_label->accel_closure)
613 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
615 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
617 GtkAccelLabelClass *klass;
620 klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
621 tmp = _gtk_accel_label_class_get_accelerator_label (klass,
624 accel_label->accel_string = g_strconcat (" ", tmp, NULL);
627 if (!accel_label->accel_string)
628 accel_label->accel_string = g_strdup ("-/-");
631 if (!accel_label->accel_string)
632 accel_label->accel_string = g_strdup ("");
634 gtk_widget_queue_resize (GTK_WIDGET (accel_label));