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/.
29 #include "gtkaccellabel.h"
30 #include "gtkaccelmap.h"
32 #include "gtksignal.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 GtkAccelLabelClass *accel_label_class = NULL;
62 static GtkLabelClass *parent_class = NULL;
66 gtk_accel_label_get_type (void)
68 static GtkType accel_label_type = 0;
70 if (!accel_label_type)
72 static const GtkTypeInfo accel_label_info =
75 sizeof (GtkAccelLabel),
76 sizeof (GtkAccelLabelClass),
77 (GtkClassInitFunc) gtk_accel_label_class_init,
78 (GtkObjectInitFunc) gtk_accel_label_init,
79 /* reserved_1 */ NULL,
80 /* reserved_2 */ NULL,
81 (GtkClassInitFunc) NULL,
84 accel_label_type = gtk_type_unique (GTK_TYPE_LABEL, &accel_label_info);
87 return accel_label_type;
91 gtk_accel_label_class_init (GtkAccelLabelClass *class)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
94 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
95 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
97 accel_label_class = class;
98 parent_class = g_type_class_peek_parent (class);
100 gobject_class->finalize = gtk_accel_label_finalize;
101 gobject_class->set_property = gtk_accel_label_set_property;
102 gobject_class->get_property = gtk_accel_label_get_property;
104 object_class->destroy = gtk_accel_label_destroy;
106 widget_class->size_request = gtk_accel_label_size_request;
107 widget_class->expose_event = gtk_accel_label_expose_event;
109 class->signal_quote1 = g_strdup ("<:");
110 class->signal_quote2 = g_strdup (":>");
111 /* This is the text that should appear next to menu accelerators
112 * that use the shift key. If the text on this key isn't typically
113 * translated on keyboards used for your language, don't translate
116 class->mod_name_shift = g_strdup (_("Shift"));
117 /* This is the text that should appear next to menu accelerators
118 * that use the control key. If the text on this key isn't typically
119 * translated on keyboards used for your language, don't translate
122 class->mod_name_control = g_strdup (_("Ctrl"));
123 /* This is the text that should appear next to menu accelerators
124 * that use the alt key. If the text on this key isn't typically
125 * translated on keyboards used for your language, don't translate
128 class->mod_name_alt = g_strdup (_("Alt"));
129 class->mod_separator = g_strdup ("+");
130 class->accel_seperator = g_strdup (" / ");
131 class->latin1_to_char = TRUE;
133 g_object_class_install_property (G_OBJECT_CLASS (object_class),
135 g_param_spec_boxed ("accel_closure",
136 _("Accelerator Closure"),
137 _("The closure to be monitored for accelerator changes"),
139 G_PARAM_READABLE | G_PARAM_WRITABLE));
140 g_object_class_install_property (G_OBJECT_CLASS (object_class),
142 g_param_spec_object ("accel_widget",
143 _("Accelerator Widget"),
144 _("The widget to be monitored for accelerator changes"),
146 G_PARAM_READABLE | G_PARAM_WRITABLE));
150 gtk_accel_label_set_property (GObject *object,
155 GtkAccelLabel *accel_label;
157 accel_label = GTK_ACCEL_LABEL (object);
161 case PROP_ACCEL_CLOSURE:
162 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
164 case PROP_ACCEL_WIDGET:
165 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 gtk_accel_label_get_property (GObject *object,
179 GtkAccelLabel *accel_label;
181 accel_label = GTK_ACCEL_LABEL (object);
185 case PROP_ACCEL_CLOSURE:
186 g_value_set_boxed (value, accel_label->accel_closure);
188 case PROP_ACCEL_WIDGET:
189 g_value_set_object (value, accel_label->accel_widget);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gtk_accel_label_init (GtkAccelLabel *accel_label)
200 accel_label->queue_id = 0;
201 accel_label->accel_padding = 3;
202 accel_label->accel_widget = NULL;
203 accel_label->accel_closure = NULL;
204 accel_label->accel_group = NULL;
205 accel_label->accel_string = NULL;
207 gtk_accel_label_refetch (accel_label);
211 gtk_accel_label_new (const gchar *string)
213 GtkAccelLabel *accel_label;
215 g_return_val_if_fail (string != NULL, NULL);
217 accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
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 if (accel_label->queue_id)
242 gtk_idle_remove (accel_label->queue_id);
243 accel_label->queue_id = 0;
245 g_free (accel_label->accel_string);
247 G_OBJECT_CLASS (parent_class)->finalize (object);
251 * gtk_accel_label_get_accel_widget:
252 * @accel_label: a #GtkAccelLabel
254 * Fetches the widget monitored by this accelerator label. See
255 * gtk_accel_label_set_accel_widget().
257 * Return value: the object monitored by the accelerator label,
261 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
263 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
265 return accel_label->accel_widget;
269 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
271 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
273 return (accel_label->accel_string_width +
274 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
278 gtk_accel_label_size_request (GtkWidget *widget,
279 GtkRequisition *requisition)
281 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
285 if (GTK_WIDGET_CLASS (parent_class)->size_request)
286 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
288 layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
289 pango_layout_get_pixel_size (layout, &width, NULL);
290 accel_label->accel_string_width = width;
292 g_object_unref (G_OBJECT (layout));
296 gtk_accel_label_expose_event (GtkWidget *widget,
297 GdkEventExpose *event)
299 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
300 GtkMisc *misc = GTK_MISC (accel_label);
303 if (GTK_WIDGET_DRAWABLE (accel_label))
307 ac_width = gtk_accel_label_get_accel_width (accel_label);
309 if (widget->allocation.width >= widget->requisition.width + ac_width)
314 widget->allocation.width -= ac_width;
315 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
316 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
317 widget->allocation.width += ac_width;
319 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
321 y = (widget->allocation.y * (1.0 - misc->yalign) +
322 (widget->allocation.y + widget->allocation.height -
323 (widget->requisition.height - misc->ypad * 2)) *
326 layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
328 gtk_paint_layout (widget->style,
330 GTK_WIDGET_STATE (widget),
338 g_object_unref (G_OBJECT (layout));
342 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
343 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
351 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
353 GClosure *closure = NULL;
356 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
357 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
359 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
360 for (list = clist; list; list = list->next)
362 /* we just take the first closure used */
363 closure = list->data;
367 gtk_accel_label_set_accel_closure (accel_label, closure);
371 * gtk_accel_label_set_accel_widget:
372 * @accel_label: a #GtkAccelLabel
373 * @accel_widget: the widget to be monitored.
375 * Sets the widget to be monitored by this accelerator label.
378 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
379 GtkWidget *accel_widget)
381 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
383 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
385 if (accel_widget != accel_label->accel_widget)
387 if (accel_label->accel_widget)
389 gtk_accel_label_set_accel_closure (accel_label, NULL);
390 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
391 (gpointer) refetch_widget_accel_closure,
393 g_object_unref (accel_label->accel_widget);
395 accel_label->accel_widget = accel_widget;
396 if (accel_label->accel_widget)
398 g_object_ref (accel_label->accel_widget);
399 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
400 G_CALLBACK (refetch_widget_accel_closure),
401 accel_label, G_CONNECT_SWAPPED);
402 refetch_widget_accel_closure (accel_label);
404 g_object_notify (G_OBJECT (accel_label), "accel_widget");
409 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
411 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
413 if (accel_label->queue_id == 0)
414 accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
415 (GtkFunction) gtk_accel_label_refetch_idle,
420 check_accel_changed (GtkAccelGroup *accel_group,
422 GdkModifierType modifier,
423 GClosure *accel_closure,
424 GtkAccelLabel *accel_label)
426 if (accel_closure == accel_label->accel_closure)
427 gtk_accel_label_queue_refetch (accel_label);
431 * gtk_accel_label_set_accel_closure:
432 * @accel_label: a #GtkAccelLabel
433 * @accel_closure: the closure to monitor for accelerator changes.
435 * Sets the closure to be monitored by this accelerator label. The closure
436 * must be connected to an accelerator group; see gtk_accel_group_connect().
439 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
440 GClosure *accel_closure)
442 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
444 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
446 if (accel_closure != accel_label->accel_closure)
448 if (accel_label->accel_closure)
450 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
451 (gpointer) check_accel_changed,
453 accel_label->accel_group = NULL;
454 g_closure_unref (accel_label->accel_closure);
456 accel_label->accel_closure = accel_closure;
457 if (accel_label->accel_closure)
459 g_closure_ref (accel_label->accel_closure);
460 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
461 g_signal_connect_object (accel_label->accel_group, "accel_changed",
462 G_CALLBACK (check_accel_changed),
465 gtk_accel_label_queue_refetch (accel_label);
466 g_object_notify (G_OBJECT (accel_label), "accel_closure");
471 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
475 GDK_THREADS_ENTER ();
476 retval = gtk_accel_label_refetch (accel_label);
477 GDK_THREADS_LEAVE ();
483 find_accel (GtkAccelKey *key,
487 return data == (gpointer) closure;
491 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
493 GtkAccelLabelClass *class;
495 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
497 class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
499 g_free (accel_label->accel_string);
500 accel_label->accel_string = NULL;
502 if (accel_label->accel_closure)
504 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
506 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
509 gboolean seen_mod = FALSE;
512 gstring = g_string_new (accel_label->accel_string);
513 g_string_append (gstring, gstring->len ? class->accel_seperator : " ");
515 if (key->accel_mods & GDK_SHIFT_MASK)
517 g_string_append (gstring, class->mod_name_shift);
520 if (key->accel_mods & GDK_CONTROL_MASK)
523 g_string_append (gstring, class->mod_separator);
524 g_string_append (gstring, class->mod_name_control);
527 if (key->accel_mods & GDK_MOD1_MASK)
530 g_string_append (gstring, class->mod_separator);
531 g_string_append (gstring, class->mod_name_alt);
535 g_string_append (gstring, class->mod_separator);
537 ch = gdk_keyval_to_unicode (key->accel_key);
538 if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
539 (ch < 0x80 || class->latin1_to_char))
544 g_string_append (gstring, "Space");
547 g_string_append (gstring, "Backslash");
550 g_string_append_unichar (gstring, g_unichar_toupper (ch));
558 tmp = gtk_accelerator_name (key->accel_key, 0);
559 if (tmp[0] != 0 && tmp[1] == 0)
560 tmp[0] = g_ascii_toupper (tmp[0]);
561 g_string_append (gstring, tmp);
564 g_free (accel_label->accel_string);
565 accel_label->accel_string = gstring->str;
566 g_string_free (gstring, FALSE);
568 if (!accel_label->accel_string)
569 accel_label->accel_string = g_strdup ("-/-");
572 if (!accel_label->accel_string)
573 accel_label->accel_string = g_strdup ("");
575 if (accel_label->queue_id)
577 gtk_idle_remove (accel_label->queue_id);
578 accel_label->queue_id = 0;
581 gtk_widget_queue_resize (GTK_WIDGET (accel_label));