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 class->mod_name_shift = g_strdup ("Shift");
112 class->mod_name_control = g_strdup ("Ctrl");
113 class->mod_name_alt = g_strdup ("Alt");
114 class->mod_separator = g_strdup ("+");
115 class->accel_seperator = g_strdup (" / ");
116 class->latin1_to_char = TRUE;
118 g_object_class_install_property (G_OBJECT_CLASS (object_class),
120 g_param_spec_boxed ("accel_closure",
121 _("Accelerator Closure"),
122 _("The closure to be monitored for accelerator changes"),
124 G_PARAM_READABLE | G_PARAM_WRITABLE));
125 g_object_class_install_property (G_OBJECT_CLASS (object_class),
127 g_param_spec_object ("accel_widget",
128 _("Accelerator Widget"),
129 _("The widget to be monitored for accelerator changes"),
131 G_PARAM_READABLE | G_PARAM_WRITABLE));
135 gtk_accel_label_set_property (GObject *object,
140 GtkAccelLabel *accel_label;
142 accel_label = GTK_ACCEL_LABEL (object);
146 case PROP_ACCEL_CLOSURE:
147 gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
149 case PROP_ACCEL_WIDGET:
150 gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gtk_accel_label_get_property (GObject *object,
164 GtkAccelLabel *accel_label;
166 accel_label = GTK_ACCEL_LABEL (object);
170 case PROP_ACCEL_CLOSURE:
171 g_value_set_boxed (value, accel_label->accel_closure);
173 case PROP_ACCEL_WIDGET:
174 g_value_set_object (value, accel_label->accel_widget);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gtk_accel_label_init (GtkAccelLabel *accel_label)
185 accel_label->queue_id = 0;
186 accel_label->accel_padding = 3;
187 accel_label->accel_widget = NULL;
188 accel_label->accel_closure = NULL;
189 accel_label->accel_group = NULL;
190 accel_label->accel_string = NULL;
192 gtk_accel_label_refetch (accel_label);
196 gtk_accel_label_new (const gchar *string)
198 GtkAccelLabel *accel_label;
200 g_return_val_if_fail (string != NULL, NULL);
202 accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
204 gtk_label_set_text (GTK_LABEL (accel_label), string);
206 return GTK_WIDGET (accel_label);
210 gtk_accel_label_destroy (GtkObject *object)
212 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
214 gtk_accel_label_set_accel_widget (accel_label, NULL);
215 gtk_accel_label_set_accel_closure (accel_label, NULL);
217 GTK_OBJECT_CLASS (parent_class)->destroy (object);
221 gtk_accel_label_finalize (GObject *object)
223 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
225 if (accel_label->queue_id)
227 gtk_idle_remove (accel_label->queue_id);
228 accel_label->queue_id = 0;
230 g_free (accel_label->accel_string);
232 G_OBJECT_CLASS (parent_class)->finalize (object);
236 * gtk_accel_label_get_accel_widget:
237 * @accel_label: a #GtkAccelLabel
239 * Fetches the widget monitored by this accelerator label. See
240 * gtk_accel_label_set_accel_widget().
242 * Return value: the object monitored by the accelerator label,
246 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
248 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
250 return accel_label->accel_widget;
254 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
256 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
258 return (accel_label->accel_string_width +
259 (accel_label->accel_string_width ? accel_label->accel_padding : 0));
263 gtk_accel_label_size_request (GtkWidget *widget,
264 GtkRequisition *requisition)
266 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
270 if (GTK_WIDGET_CLASS (parent_class)->size_request)
271 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
273 layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
274 pango_layout_get_pixel_size (layout, &width, NULL);
275 accel_label->accel_string_width = width;
277 g_object_unref (G_OBJECT (layout));
281 gtk_accel_label_expose_event (GtkWidget *widget,
282 GdkEventExpose *event)
284 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
285 GtkMisc *misc = GTK_MISC (accel_label);
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)
299 widget->allocation.width -= ac_width;
300 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
301 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
302 widget->allocation.width += ac_width;
304 x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
306 y = (widget->allocation.y * (1.0 - misc->yalign) +
307 (widget->allocation.y + widget->allocation.height -
308 (widget->requisition.height - misc->ypad * 2)) *
311 layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
313 gtk_paint_layout (widget->style,
315 GTK_WIDGET_STATE (widget),
323 g_object_unref (G_OBJECT (layout));
327 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
328 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
336 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
338 GClosure *closure = NULL;
341 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
342 g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
344 clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
345 for (list = clist; list; list = list->next)
347 /* we just take the first closure used */
348 closure = list->data;
352 gtk_accel_label_set_accel_closure (accel_label, closure);
356 * gtk_accel_label_set_accel_widget:
357 * @accel_label: a #GtkAccelLabel
358 * @accel_widget: the widget to be monitored.
360 * Sets the widget to be monitored by this accelerator label.
363 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
364 GtkWidget *accel_widget)
366 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
368 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
370 if (accel_widget != accel_label->accel_widget)
372 if (accel_label->accel_widget)
374 gtk_accel_label_set_accel_closure (accel_label, NULL);
375 g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
376 G_CALLBACK (refetch_widget_accel_closure),
378 g_object_unref (accel_label->accel_widget);
380 accel_label->accel_widget = accel_widget;
381 if (accel_label->accel_widget)
383 g_object_ref (accel_label->accel_widget);
384 g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
385 G_CALLBACK (refetch_widget_accel_closure),
386 accel_label, G_CONNECT_SWAPPED);
387 refetch_widget_accel_closure (accel_label);
389 g_object_notify (G_OBJECT (accel_label), "accel_widget");
394 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
396 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
398 if (accel_label->queue_id == 0)
399 accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
400 (GtkFunction) gtk_accel_label_refetch_idle,
405 check_accel_changed (GtkAccelGroup *accel_group,
407 GdkModifierType modifier,
408 GClosure *accel_closure,
409 GtkAccelLabel *accel_label)
411 if (accel_closure == accel_label->accel_closure)
412 gtk_accel_label_queue_refetch (accel_label);
416 * gtk_accel_label_set_accel_closure:
417 * @accel_label: a #GtkAccelLabel
418 * @accel_closure: the closure to monitor for accelerator changes.
420 * Sets the closure to be monitored by this accelerator label. The closure
421 * must be connected to an accelerator group; see gtk_accel_group_connect().
424 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
425 GClosure *accel_closure)
427 g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
429 g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
431 if (accel_closure != accel_label->accel_closure)
433 if (accel_label->accel_closure)
435 g_signal_handlers_disconnect_by_func (accel_label->accel_group,
436 G_CALLBACK (check_accel_changed),
438 accel_label->accel_group = NULL;
439 g_closure_unref (accel_label->accel_closure);
441 accel_label->accel_closure = accel_closure;
442 if (accel_label->accel_closure)
444 g_closure_ref (accel_label->accel_closure);
445 accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
446 g_signal_connect_object (accel_label->accel_group, "accel_changed",
447 G_CALLBACK (check_accel_changed),
450 gtk_accel_label_queue_refetch (accel_label);
451 g_object_notify (G_OBJECT (accel_label), "accel_closure");
456 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
460 GDK_THREADS_ENTER ();
461 retval = gtk_accel_label_refetch (accel_label);
462 GDK_THREADS_LEAVE ();
468 find_accel (GtkAccelKey *key,
472 return data == (gpointer) closure;
476 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
478 GtkAccelLabelClass *class;
480 g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
482 class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
484 g_free (accel_label->accel_string);
485 accel_label->accel_string = NULL;
487 if (accel_label->accel_closure)
489 GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
491 if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
494 gboolean seen_mod = FALSE;
496 gstring = g_string_new (accel_label->accel_string);
497 g_string_append (gstring, gstring->len ? class->accel_seperator : " ");
499 if (key->accel_mods & GDK_SHIFT_MASK)
501 g_string_append (gstring, class->mod_name_shift);
504 if (key->accel_mods & GDK_CONTROL_MASK)
507 g_string_append (gstring, class->mod_separator);
508 g_string_append (gstring, class->mod_name_control);
511 if (key->accel_mods & GDK_MOD1_MASK)
514 g_string_append (gstring, class->mod_separator);
515 g_string_append (gstring, class->mod_name_alt);
519 g_string_append (gstring, class->mod_separator);
520 if (key->accel_key < 0x80 ||
521 (key->accel_key > 0x80 &&
522 key->accel_key <= 0xff &&
523 class->latin1_to_char))
525 switch (key->accel_key)
528 g_string_append (gstring, "Space");
531 g_string_append (gstring, "Backslash");
534 g_string_append_unichar (gstring, g_unichar_toupper (key->accel_key));
542 tmp = gtk_accelerator_name (key->accel_key, 0);
543 if (tmp[0] != 0 && tmp[1] == 0)
544 tmp[0] = g_unichar_totitle (tmp[0]);
545 g_string_append (gstring, tmp);
548 g_free (accel_label->accel_string);
549 accel_label->accel_string = gstring->str;
550 g_string_free (gstring, FALSE);
552 if (!accel_label->accel_string)
553 accel_label->accel_string = g_strdup ("-/-");
556 if (!accel_label->accel_string)
557 accel_label->accel_string = g_strdup ("");
559 if (accel_label->queue_id)
561 gtk_idle_remove (accel_label->queue_id);
562 accel_label->queue_id = 0;
565 gtk_widget_queue_resize (GTK_WIDGET (accel_label));