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/.
32 #include "gtkaccellabel.h"
33 #include "gtkaccelmap.h"
43 static void gtk_accel_label_class_init (GtkAccelLabelClass *klass);
44 static void gtk_accel_label_init (GtkAccelLabel *accel_label);
45 static void gtk_accel_label_set_property (GObject *object,
49 static void gtk_accel_label_get_property (GObject *object,
53 static void gtk_accel_label_destroy (GtkObject *object);
54 static void gtk_accel_label_finalize (GObject *object);
55 static void gtk_accel_label_size_request (GtkWidget *widget,
56 GtkRequisition *requisition);
57 static gboolean gtk_accel_label_expose_event (GtkWidget *widget,
58 GdkEventExpose *event);
59 static gboolean gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label);
61 static GtkLabelClass *parent_class = NULL;
65 gtk_accel_label_get_type (void)
67 static GType accel_label_type = 0;
69 if (!accel_label_type)
71 static const GTypeInfo accel_label_info =
73 sizeof (GtkAccelLabelClass),
75 NULL, /* base_finalize */
76 (GClassInitFunc) gtk_accel_label_class_init,
77 NULL, /* class_finalize */
78 NULL, /* class_data */
79 sizeof (GtkAccelLabel),
81 (GInstanceInitFunc) gtk_accel_label_init,
85 g_type_register_static (GTK_TYPE_LABEL, "GtkAccelLabel",
86 &accel_label_info, 0);
89 return accel_label_type;
93 gtk_accel_label_class_init (GtkAccelLabelClass *class)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
96 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
97 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
99 parent_class = g_type_class_peek_parent (class);
101 gobject_class->finalize = gtk_accel_label_finalize;
102 gobject_class->set_property = gtk_accel_label_set_property;
103 gobject_class->get_property = gtk_accel_label_get_property;
105 object_class->destroy = gtk_accel_label_destroy;
107 widget_class->size_request = gtk_accel_label_size_request;
108 widget_class->expose_event = gtk_accel_label_expose_event;
110 class->signal_quote1 = g_strdup ("<:");
111 class->signal_quote2 = g_strdup (":>");
112 /* This is the text that should appear next to menu accelerators
113 * that use the shift key. If the text on this key isn't typically
114 * translated on keyboards used for your language, don't translate
117 class->mod_name_shift = g_strdup (_("Shift"));
118 /* This is the text that should appear next to menu accelerators
119 * that use the control key. If the text on this key isn't typically
120 * translated on keyboards used for your language, don't translate
123 class->mod_name_control = g_strdup (_("Ctrl"));
124 /* This is the text that should appear next to menu accelerators
125 * that use the alt key. If the text on this key isn't typically
126 * translated on keyboards used for your language, don't translate
129 class->mod_name_alt = g_strdup (_("Alt"));
130 class->mod_separator = g_strdup ("+");
131 class->accel_seperator = g_strdup (" / ");
132 class->latin1_to_char = TRUE;
134 g_object_class_install_property (gobject_class,
136 g_param_spec_boxed ("accel_closure",
137 _("Accelerator Closure"),
138 _("The closure to be monitored for accelerator changes"),
140 G_PARAM_READABLE | G_PARAM_WRITABLE));
141 g_object_class_install_property (gobject_class,
143 g_param_spec_object ("accel_widget",
144 _("Accelerator Widget"),
145 _("The widget to be monitored for accelerator changes"),
147 G_PARAM_READABLE | G_PARAM_WRITABLE));
151 gtk_accel_label_set_property (GObject *object,
156 GtkAccelLabel *accel_label;
158 accel_label = GTK_ACCEL_LABEL (object);
162 case PROP_ACCEL_CLOSURE:
163 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
165 case PROP_ACCEL_WIDGET:
166 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 gtk_accel_label_get_property (GObject *object,
180 GtkAccelLabel *accel_label;
182 accel_label = GTK_ACCEL_LABEL (object);
186 case PROP_ACCEL_CLOSURE:
187 g_value_set_boxed (value, accel_label->accel_closure);
189 case PROP_ACCEL_WIDGET:
190 g_value_set_object (value, accel_label->accel_widget);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gtk_accel_label_init (GtkAccelLabel *accel_label)
201 accel_label->queue_id = 0;
202 accel_label->accel_padding = 3;
203 accel_label->accel_widget = NULL;
204 accel_label->accel_closure = NULL;
205 accel_label->accel_group = NULL;
206 accel_label->accel_string = NULL;
210 gtk_accel_label_new (const gchar *string)
212 GtkAccelLabel *accel_label;
214 g_return_val_if_fail (string != NULL, NULL);
216 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
218 gtk_label_set_text (GTK_LABEL (accel_label), string);
220 return GTK_WIDGET (accel_label);
224 gtk_accel_label_destroy (GtkObject *object)
226 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
228 gtk_accel_label_set_accel_widget (accel_label, NULL);
229 gtk_accel_label_set_accel_closure (accel_label, NULL);
231 GTK_OBJECT_CLASS (parent_class)->destroy (object);
235 gtk_accel_label_finalize (GObject *object)
237 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
239 if (accel_label->queue_id)
241 gtk_idle_remove (accel_label->queue_id);
242 accel_label->queue_id = 0;
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, accel_label->accel_string);
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;
331 if (direction == GTK_TEXT_DIR_RTL)
332 widget->allocation.x += ac_width;
333 widget->allocation.width -= ac_width;
334 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
335 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
336 if (direction == GTK_TEXT_DIR_RTL)
337 widget->allocation.x -= ac_width;
338 widget->allocation.width += ac_width;
340 if (direction == GTK_TEXT_DIR_RTL)
341 x = widget->allocation.x + misc->xpad;
343 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
345 label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
346 gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
348 accel_layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
350 y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
352 gtk_paint_layout (widget->style,
354 GTK_WIDGET_STATE (widget),
362 g_object_unref (accel_layout);
366 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
367 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
375 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
377 GClosure *closure = NULL;
380 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
381 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
383 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
384 for (list = clist; list; list = list->next)
386 /* we just take the first closure used */
387 closure = list->data;
391 gtk_accel_label_set_accel_closure (accel_label, closure);
395 * gtk_accel_label_set_accel_widget:
396 * @accel_label: a #GtkAccelLabel
397 * @accel_widget: the widget to be monitored.
399 * Sets the widget to be monitored by this accelerator label.
402 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
403 GtkWidget *accel_widget)
405 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
407 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
409 if (accel_widget != accel_label->accel_widget)
411 if (accel_label->accel_widget)
413 gtk_accel_label_set_accel_closure (accel_label, NULL);
414 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
415 refetch_widget_accel_closure,
417 g_object_unref (accel_label->accel_widget);
419 accel_label->accel_widget = accel_widget;
420 if (accel_label->accel_widget)
422 g_object_ref (accel_label->accel_widget);
423 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
424 G_CALLBACK (refetch_widget_accel_closure),
425 accel_label, G_CONNECT_SWAPPED);
426 refetch_widget_accel_closure (accel_label);
428 g_object_notify (G_OBJECT (accel_label), "accel_widget");
433 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
435 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
437 if (accel_label->queue_id == 0)
438 accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
439 (GtkFunction) gtk_accel_label_refetch_idle,
444 check_accel_changed (GtkAccelGroup *accel_group,
446 GdkModifierType modifier,
447 GClosure *accel_closure,
448 GtkAccelLabel *accel_label)
450 if (accel_closure == accel_label->accel_closure)
451 gtk_accel_label_queue_refetch (accel_label);
455 * gtk_accel_label_set_accel_closure:
456 * @accel_label: a #GtkAccelLabel
457 * @accel_closure: the closure to monitor for accelerator changes.
459 * Sets the closure to be monitored by this accelerator label. The closure
460 * must be connected to an accelerator group; see gtk_accel_group_connect().
463 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
464 GClosure *accel_closure)
466 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
468 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
470 if (accel_closure != accel_label->accel_closure)
472 if (accel_label->accel_closure)
474 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
477 accel_label->accel_group = NULL;
478 g_closure_unref (accel_label->accel_closure);
480 accel_label->accel_closure = accel_closure;
481 if (accel_label->accel_closure)
483 g_closure_ref (accel_label->accel_closure);
484 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
485 g_signal_connect_object (accel_label->accel_group, "accel_changed",
486 G_CALLBACK (check_accel_changed),
489 gtk_accel_label_queue_refetch (accel_label);
490 g_object_notify (G_OBJECT (accel_label), "accel_closure");
495 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
499 GDK_THREADS_ENTER ();
500 retval = gtk_accel_label_refetch (accel_label);
501 GDK_THREADS_LEAVE ();
507 find_accel (GtkAccelKey *key,
511 return data == (gpointer) closure;
515 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
517 GtkAccelLabelClass *class;
519 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
521 class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
523 g_free (accel_label->accel_string);
524 accel_label->accel_string = NULL;
526 if (accel_label->accel_closure)
528 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
530 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
533 gboolean seen_mod = FALSE;
536 gstring = g_string_new (accel_label->accel_string);
537 g_string_append (gstring, gstring->len ? class->accel_seperator : " ");
539 if (key->accel_mods & GDK_SHIFT_MASK)
541 g_string_append (gstring, class->mod_name_shift);
544 if (key->accel_mods & GDK_CONTROL_MASK)
547 g_string_append (gstring, class->mod_separator);
548 g_string_append (gstring, class->mod_name_control);
551 if (key->accel_mods & GDK_MOD1_MASK)
554 g_string_append (gstring, class->mod_separator);
555 g_string_append (gstring, class->mod_name_alt);
559 g_string_append (gstring, class->mod_separator);
561 ch = gdk_keyval_to_unicode (key->accel_key);
562 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
563 (ch < 0x80 || class->latin1_to_char))
568 g_string_append (gstring, "Space");
571 g_string_append (gstring, "Backslash");
574 g_string_append_unichar (gstring, g_unichar_toupper (ch));
582 tmp = gtk_accelerator_name (key->accel_key, 0);
583 if (tmp[0] != 0 && tmp[1] == 0)
584 tmp[0] = g_ascii_toupper (tmp[0]);
585 g_string_append (gstring, tmp);
588 g_free (accel_label->accel_string);
589 accel_label->accel_string = gstring->str;
590 g_string_free (gstring, FALSE);
592 if (!accel_label->accel_string)
593 accel_label->accel_string = g_strdup ("-/-");
596 if (!accel_label->accel_string)
597 accel_label->accel_string = g_strdup ("");
599 if (accel_label->queue_id)
601 gtk_idle_remove (accel_label->queue_id);
602 accel_label->queue_id = 0;
605 gtk_widget_queue_resize (GTK_WIDGET (accel_label));