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_set_property (GObject *object,
50
static void gtk_accel_label_get_property (GObject *object,
54
static void gtk_accel_label_destroy (GtkObject *object);
55
static void gtk_accel_label_finalize (GObject *object);
56
static void gtk_accel_label_size_request (GtkWidget *widget,
57
GtkRequisition *requisition);
58
static gboolean gtk_accel_label_expose_event (GtkWidget *widget,
59
GdkEventExpose *event);
60
static const gchar *gtk_accel_label_get_string (GtkAccelLabel *accel_label);
63
G_DEFINE_TYPE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL)
66
gtk_accel_label_class_init (GtkAccelLabelClass *class)
68
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
69
GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
70
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
72
gobject_class->finalize = gtk_accel_label_finalize;
73
gobject_class->set_property = gtk_accel_label_set_property;
74
gobject_class->get_property = gtk_accel_label_get_property;
76
object_class->destroy = gtk_accel_label_destroy;
78
widget_class->size_request = gtk_accel_label_size_request;
79
widget_class->expose_event = gtk_accel_label_expose_event;
81
class->signal_quote1 = g_strdup ("<:");
82
class->signal_quote2 = g_strdup (":>");
83
/* This is the text that should appear next to menu accelerators
84
* that use the shift key. If the text on this key isn't typically
85
* translated on keyboards used for your language, don't translate
87
* And do not translate the part before the |.
89
class->mod_name_shift = g_strdup (Q_("keyboard label|Shift"));
90
/* This is the text that should appear next to menu accelerators
91
* that use the control key. If the text on this key isn't typically
92
* translated on keyboards used for your language, don't translate
94
* And do not translate the part before the |.
96
class->mod_name_control = g_strdup (Q_("keyboard label|Ctrl"));
97
/* This is the text that should appear next to menu accelerators
98
* that use the alt key. If the text on this key isn't typically
99
* translated on keyboards used for your language, don't translate
101
* And do not translate the part before the |.
103
class->mod_name_alt = g_strdup (Q_("keyboard label|Alt"));
104
class->mod_separator = g_strdup ("+");
105
class->accel_seperator = g_strdup (" / ");
106
class->latin1_to_char = TRUE;
108
g_object_class_install_property (gobject_class,
110
g_param_spec_boxed ("accel-closure",
111
P_("Accelerator Closure"),
112
P_("The closure to be monitored for accelerator changes"),
114
GTK_PARAM_READWRITE));
115
g_object_class_install_property (gobject_class,
117
g_param_spec_object ("accel-widget",
118
P_("Accelerator Widget"),
119
P_("The widget to be monitored for accelerator changes"),
121
GTK_PARAM_READWRITE));
125
gtk_accel_label_set_property (GObject *object,
130
GtkAccelLabel *accel_label;
132
accel_label = GTK_ACCEL_LABEL (object);
136
case PROP_ACCEL_CLOSURE:
137
gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
139
case PROP_ACCEL_WIDGET:
140
gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
143
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149
gtk_accel_label_get_property (GObject *object,
154
GtkAccelLabel *accel_label;
156
accel_label = GTK_ACCEL_LABEL (object);
160
case PROP_ACCEL_CLOSURE:
161
g_value_set_boxed (value, accel_label->accel_closure);
163
case PROP_ACCEL_WIDGET:
164
g_value_set_object (value, accel_label->accel_widget);
167
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173
gtk_accel_label_init (GtkAccelLabel *accel_label)
175
accel_label->accel_padding = 3;
176
accel_label->accel_widget = NULL;
177
accel_label->accel_closure = NULL;
178
accel_label->accel_group = NULL;
179
accel_label->accel_string = NULL;
183
gtk_accel_label_new (const gchar *string)
185
GtkAccelLabel *accel_label;
187
g_return_val_if_fail (string != NULL, NULL);
189
accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
191
gtk_label_set_text (GTK_LABEL (accel_label), string);
193
return GTK_WIDGET (accel_label);
197
gtk_accel_label_destroy (GtkObject *object)
199
GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
201
gtk_accel_label_set_accel_widget (accel_label, NULL);
202
gtk_accel_label_set_accel_closure (accel_label, NULL);
204
GTK_OBJECT_CLASS (gtk_accel_label_parent_class)->destroy (object);
208
gtk_accel_label_finalize (GObject *object)
210
GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
212
g_free (accel_label->accel_string);
214
G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
218
* gtk_accel_label_get_accel_widget:
219
* @accel_label: a #GtkAccelLabel
221
* Fetches the widget monitored by this accelerator label. See
222
* gtk_accel_label_set_accel_widget().
224
* Return value: the object monitored by the accelerator label,
228
gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
230
g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
232
return accel_label->accel_widget;
236
gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
238
g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
240
return (accel_label->accel_string_width +
241
(accel_label->accel_string_width ? accel_label->accel_padding : 0));
245
gtk_accel_label_size_request (GtkWidget *widget,
246
GtkRequisition *requisition)
248
GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
252
if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request)
253
GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->size_request (widget, requisition);
255
layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
256
pango_layout_get_pixel_size (layout, &width, NULL);
257
accel_label->accel_string_width = width;
259
g_object_unref (layout);
263
get_first_baseline (PangoLayout *layout)
265
PangoLayoutIter *iter;
268
iter = pango_layout_get_iter (layout);
269
result = pango_layout_iter_get_baseline (iter);
270
pango_layout_iter_free (iter);
272
return PANGO_PIXELS (result);
276
gtk_accel_label_expose_event (GtkWidget *widget,
277
GdkEventExpose *event)
279
GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
280
GtkMisc *misc = GTK_MISC (accel_label);
281
GtkTextDirection direction;
283
direction = gtk_widget_get_direction (widget);
285
if (GTK_WIDGET_DRAWABLE (accel_label))
289
ac_width = gtk_accel_label_get_accel_width (accel_label);
291
if (widget->allocation.width >= widget->requisition.width + ac_width)
293
PangoLayout *label_layout;
294
PangoLayout *accel_layout;
295
GtkLabel *label = GTK_LABEL (widget);
300
label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
302
if (direction == GTK_TEXT_DIR_RTL)
303
widget->allocation.x += ac_width;
304
widget->allocation.width -= ac_width;
305
if (gtk_label_get_ellipsize (label))
306
pango_layout_set_width (label_layout,
307
pango_layout_get_width (label_layout)
308
- ac_width * PANGO_SCALE);
310
if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
311
GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
312
if (direction == GTK_TEXT_DIR_RTL)
313
widget->allocation.x -= ac_width;
314
widget->allocation.width += ac_width;
315
if (gtk_label_get_ellipsize (label))
316
pango_layout_set_width (label_layout,
317
pango_layout_get_width (label_layout)
318
+ ac_width * PANGO_SCALE);
320
if (direction == GTK_TEXT_DIR_RTL)
321
x = widget->allocation.x + misc->xpad;
323
x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
325
gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
327
accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
329
y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
331
gtk_paint_layout (widget->style,
333
GTK_WIDGET_STATE (widget),
341
g_object_unref (accel_layout);
345
if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
346
GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
354
refetch_widget_accel_closure (GtkAccelLabel *accel_label)
356
GClosure *closure = NULL;
359
g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
360
g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
362
clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
363
for (list = clist; list; list = list->next)
365
/* we just take the first closure used */
366
closure = list->data;
370
gtk_accel_label_set_accel_closure (accel_label, closure);
374
* gtk_accel_label_set_accel_widget:
375
* @accel_label: a #GtkAccelLabel
376
* @accel_widget: the widget to be monitored.
378
* Sets the widget to be monitored by this accelerator label.
381
gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
382
GtkWidget *accel_widget)
384
g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
386
g_return_if_fail (GTK_IS_WIDGET (accel_widget));
388
if (accel_widget != accel_label->accel_widget)
390
if (accel_label->accel_widget)
392
gtk_accel_label_set_accel_closure (accel_label, NULL);
393
g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
394
refetch_widget_accel_closure,
396
g_object_unref (accel_label->accel_widget);
398
accel_label->accel_widget = accel_widget;
399
if (accel_label->accel_widget)
401
g_object_ref (accel_label->accel_widget);
402
g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
403
G_CALLBACK (refetch_widget_accel_closure),
404
accel_label, G_CONNECT_SWAPPED);
405
refetch_widget_accel_closure (accel_label);
407
g_object_notify (G_OBJECT (accel_label), "accel-widget");
412
gtk_accel_label_reset (GtkAccelLabel *accel_label)
414
if (accel_label->accel_string)
416
g_free (accel_label->accel_string);
417
accel_label->accel_string = NULL;
420
gtk_widget_queue_resize (GTK_WIDGET (accel_label));
424
check_accel_changed (GtkAccelGroup *accel_group,
426
GdkModifierType modifier,
427
GClosure *accel_closure,
428
GtkAccelLabel *accel_label)
430
if (accel_closure == accel_label->accel_closure)
431
gtk_accel_label_reset (accel_label);
435
* gtk_accel_label_set_accel_closure:
436
* @accel_label: a #GtkAccelLabel
437
* @accel_closure: the closure to monitor for accelerator changes.
439
* Sets the closure to be monitored by this accelerator label. The closure
440
* must be connected to an accelerator group; see gtk_accel_group_connect().
443
gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
444
GClosure *accel_closure)
446
g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
448
g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
450
if (accel_closure != accel_label->accel_closure)
452
if (accel_label->accel_closure)
454
g_signal_handlers_disconnect_by_func (accel_label->accel_group,
457
accel_label->accel_group = NULL;
458
g_closure_unref (accel_label->accel_closure);
460
accel_label->accel_closure = accel_closure;
461
if (accel_label->accel_closure)
463
g_closure_ref (accel_label->accel_closure);
464
accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
465
g_signal_connect_object (accel_label->accel_group, "accel_changed",
466
G_CALLBACK (check_accel_changed),
469
gtk_accel_label_reset (accel_label);
470
g_object_notify (G_OBJECT (accel_label), "accel-closure");
475
find_accel (GtkAccelKey *key,
479
return data == (gpointer) closure;
483
gtk_accel_label_get_string (GtkAccelLabel *accel_label)
485
if (!accel_label->accel_string)
486
gtk_accel_label_refetch (accel_label);
488
return accel_label->accel_string;
491
/* Underscores in key names are better displayed as spaces
492
* E.g., Page_Up should be "Page Up"
495
substitute_underscores (char *str)
499
for (p = str; *p; p++)
505
_gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
506
guint accelerator_key,
507
GdkModifierType accelerator_mods)
510
gboolean seen_mod = FALSE;
513
gstring = g_string_new ("");
515
if (accelerator_mods & GDK_SHIFT_MASK)
517
g_string_append (gstring, klass->mod_name_shift);
520
if (accelerator_mods & GDK_CONTROL_MASK)
523
g_string_append (gstring, klass->mod_separator);
524
g_string_append (gstring, klass->mod_name_control);
527
if (accelerator_mods & GDK_MOD1_MASK)
530
g_string_append (gstring, klass->mod_separator);
531
g_string_append (gstring, klass->mod_name_alt);
534
if (accelerator_mods & GDK_MOD2_MASK)
537
g_string_append (gstring, klass->mod_separator);
539
g_string_append (gstring, "Mod2");
542
if (accelerator_mods & GDK_MOD3_MASK)
545
g_string_append (gstring, klass->mod_separator);
547
g_string_append (gstring, "Mod3");
550
if (accelerator_mods & GDK_MOD4_MASK)
553
g_string_append (gstring, klass->mod_separator);
555
g_string_append (gstring, "Mod4");
558
if (accelerator_mods & GDK_MOD5_MASK)
561
g_string_append (gstring, klass->mod_separator);
563
g_string_append (gstring, "Mod5");
566
if (accelerator_mods & GDK_SUPER_MASK)
569
g_string_append (gstring, klass->mod_separator);
571
/* This is the text that should appear next to menu accelerators
572
* that use the super key. If the text on this key isn't typically
573
* translated on keyboards used for your language, don't translate
575
* And do not translate the part before the |.
577
g_string_append (gstring, Q_("keyboard label|Super"));
580
if (accelerator_mods & GDK_HYPER_MASK)
583
g_string_append (gstring, klass->mod_separator);
585
/* This is the text that should appear next to menu accelerators
586
* that use the hyper key. If the text on this key isn't typically
587
* translated on keyboards used for your language, don't translate
589
* And do not translate the part before the |.
591
g_string_append (gstring, Q_("keyboard label|Hyper"));
594
if (accelerator_mods & GDK_META_MASK)
597
g_string_append (gstring, klass->mod_separator);
599
/* This is the text that should appear next to menu accelerators
600
* that use the meta key. If the text on this key isn't typically
601
* translated on keyboards used for your language, don't translate
603
* And do not translate the part before the |.
605
g_string_append (gstring, Q_("keyboard label|Meta"));
609
g_string_append (gstring, klass->mod_separator);
611
ch = gdk_keyval_to_unicode (accelerator_key);
612
if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
613
(ch < 0x80 || klass->latin1_to_char))
618
/* do not translate the part before the | */
619
g_string_append (gstring, Q_("keyboard label|Space"));
622
/* do not translate the part before the | */
623
g_string_append (gstring, Q_("keyboard label|Backslash"));
626
g_string_append_unichar (gstring, g_unichar_toupper (ch));
634
tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
637
if (tmp[0] != 0 && tmp[1] == 0)
638
g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
644
strcpy (msg, "keyboard label|");
645
g_strlcat (msg, tmp, 128);
646
str = dgettext (GETTEXT_PACKAGE, msg);
649
g_string_append (gstring, tmp);
650
substitute_underscores (gstring->str);
653
g_string_append (gstring, str);
658
return g_string_free (gstring, FALSE);
662
gtk_accel_label_refetch (GtkAccelLabel *accel_label)
664
g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
666
if (accel_label->accel_string)
668
g_free (accel_label->accel_string);
669
accel_label->accel_string = NULL;
672
if (accel_label->accel_closure)
674
GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
676
if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
678
GtkAccelLabelClass *klass;
681
klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
682
tmp = _gtk_accel_label_class_get_accelerator_label (klass,
685
accel_label->accel_string = g_strconcat (" ", tmp, NULL);
688
if (!accel_label->accel_string)
689
accel_label->accel_string = g_strdup ("-/-");
692
if (!accel_label->accel_string)
693
accel_label->accel_string = g_strdup ("");
695
gtk_widget_queue_resize (GTK_WIDGET (accel_label));
700
#define __GTK_ACCEL_LABEL_C__
701
#include "gtkaliasdef.c"