]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Provide access to the string representation of accelerators used in
[~andy/gtk] / gtk / gtkaccellabel.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GtkAccelLabel: GtkLabel with accelerator monitoring facilities.
5  * Copyright (C) 1998 Tim Janik
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/. 
28  */
29
30 #include <config.h>
31 #include <string.h>
32
33 #include "gtkalias.h"
34 #include "gtkaccellabel.h"
35 #include "gtkaccelmap.h"
36 #include "gtkmain.h"
37 #include "gtkintl.h"
38
39 enum {
40   PROP_0,
41   PROP_ACCEL_CLOSURE,
42   PROP_ACCEL_WIDGET
43 };
44
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,
48                                                   guint               prop_id,
49                                                   const GValue       *value,
50                                                   GParamSpec         *pspec);
51 static void         gtk_accel_label_get_property (GObject            *object,
52                                                   guint               prop_id,
53                                                   GValue             *value,
54                                                   GParamSpec         *pspec);
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);
62
63
64 static GtkLabelClass *parent_class = NULL;
65
66 GType
67 gtk_accel_label_get_type (void)
68 {
69   static GType accel_label_type = 0;
70   
71   if (!accel_label_type)
72     {
73       static const GTypeInfo accel_label_info =
74       {
75         sizeof (GtkAccelLabelClass),
76         NULL,           /* base_init */
77         NULL,           /* base_finalize */
78         (GClassInitFunc) gtk_accel_label_class_init,
79         NULL,           /* class_finalize */
80         NULL,           /* class_data */
81         sizeof (GtkAccelLabel),
82         0,              /* n_preallocs */
83         (GInstanceInitFunc) gtk_accel_label_init,
84       };
85       
86       accel_label_type =
87         g_type_register_static (GTK_TYPE_LABEL, "GtkAccelLabel",
88                                 &accel_label_info, 0);
89     }
90   
91   return accel_label_type;
92 }
93
94 static void
95 gtk_accel_label_class_init (GtkAccelLabelClass *class)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
98   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
99   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
100   
101   parent_class = g_type_class_peek_parent (class);
102   
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;
106   
107   object_class->destroy = gtk_accel_label_destroy;
108    
109   widget_class->size_request = gtk_accel_label_size_request;
110   widget_class->expose_event = gtk_accel_label_expose_event;
111
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
117    * this.
118    */
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
123    * this.
124    */
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
129    * this.
130    */
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;
135   
136   g_object_class_install_property (gobject_class,
137                                    PROP_ACCEL_CLOSURE,
138                                    g_param_spec_boxed ("accel_closure",
139                                                        P_("Accelerator Closure"),
140                                                        P_("The closure to be monitored for accelerator changes"),
141                                                        G_TYPE_CLOSURE,
142                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
143   g_object_class_install_property (gobject_class,
144                                    PROP_ACCEL_WIDGET,
145                                    g_param_spec_object ("accel_widget",
146                                                         P_("Accelerator Widget"),
147                                                         P_("The widget to be monitored for accelerator changes"),
148                                                         GTK_TYPE_WIDGET,
149                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
150 }
151
152 static void
153 gtk_accel_label_set_property (GObject      *object,
154                               guint         prop_id,
155                               const GValue *value,
156                               GParamSpec   *pspec)
157 {
158   GtkAccelLabel  *accel_label;
159
160   accel_label = GTK_ACCEL_LABEL (object);
161
162   switch (prop_id)
163     {
164     case PROP_ACCEL_CLOSURE:
165       gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
166       break;
167     case PROP_ACCEL_WIDGET:
168       gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173     }
174 }
175
176 static void
177 gtk_accel_label_get_property (GObject    *object,
178                               guint       prop_id,
179                               GValue     *value,
180                               GParamSpec *pspec)
181 {
182   GtkAccelLabel  *accel_label;
183
184   accel_label = GTK_ACCEL_LABEL (object);
185
186   switch (prop_id)
187     {
188     case PROP_ACCEL_CLOSURE:
189       g_value_set_boxed (value, accel_label->accel_closure);
190       break;
191     case PROP_ACCEL_WIDGET:
192       g_value_set_object (value, accel_label->accel_widget);
193       break;
194     default:
195       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196       break;
197     }
198 }
199
200 static void
201 gtk_accel_label_init (GtkAccelLabel *accel_label)
202 {
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;
208 }
209
210 GtkWidget*
211 gtk_accel_label_new (const gchar *string)
212 {
213   GtkAccelLabel *accel_label;
214   
215   g_return_val_if_fail (string != NULL, NULL);
216   
217   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
218   
219   gtk_label_set_text (GTK_LABEL (accel_label), string);
220   
221   return GTK_WIDGET (accel_label);
222 }
223
224 static void
225 gtk_accel_label_destroy (GtkObject *object)
226 {
227   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
228
229   gtk_accel_label_set_accel_widget (accel_label, NULL);
230   gtk_accel_label_set_accel_closure (accel_label, NULL);
231   
232   GTK_OBJECT_CLASS (parent_class)->destroy (object);
233 }
234
235 static void
236 gtk_accel_label_finalize (GObject *object)
237 {
238   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
239
240   g_free (accel_label->accel_string);
241   
242   G_OBJECT_CLASS (parent_class)->finalize (object);
243 }
244
245 /**
246  * gtk_accel_label_get_accel_widget:
247  * @accel_label: a #GtkAccelLabel
248  *
249  * Fetches the widget monitored by this accelerator label. See
250  * gtk_accel_label_set_accel_widget().
251  *
252  * Return value: the object monitored by the accelerator label,
253  *               or %NULL.
254  **/
255 GtkWidget*
256 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
257 {
258   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
259
260   return accel_label->accel_widget;
261 }
262
263 guint
264 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
265 {
266   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
267   
268   return (accel_label->accel_string_width +
269           (accel_label->accel_string_width ? accel_label->accel_padding : 0));
270 }
271
272 static void
273 gtk_accel_label_size_request (GtkWidget      *widget,
274                               GtkRequisition *requisition)
275 {
276   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
277   PangoLayout *layout;
278   gint width;
279   
280   if (GTK_WIDGET_CLASS (parent_class)->size_request)
281     GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
282
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;
286   
287   g_object_unref (layout);
288 }
289
290 static gint
291 get_first_baseline (PangoLayout *layout)
292 {
293   PangoLayoutIter *iter;
294   gint result;
295
296   iter = pango_layout_get_iter (layout);
297   result = pango_layout_iter_get_baseline (iter);
298   pango_layout_iter_free (iter);
299
300   return PANGO_PIXELS (result);
301 }
302
303 static gboolean 
304 gtk_accel_label_expose_event (GtkWidget      *widget,
305                               GdkEventExpose *event)
306 {
307   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
308   GtkMisc *misc = GTK_MISC (accel_label);
309   GtkTextDirection direction;
310
311   direction = gtk_widget_get_direction (widget);
312
313   if (GTK_WIDGET_DRAWABLE (accel_label))
314     {
315       guint ac_width;
316       
317       ac_width = gtk_accel_label_get_accel_width (accel_label);
318       
319       if (widget->allocation.width >= widget->requisition.width + ac_width)
320         {
321           PangoLayout *label_layout;
322           PangoLayout *accel_layout;
323           GtkLabel *label = GTK_LABEL (widget);
324
325           gint x;
326           gint y;
327           
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);
335           
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);
345           
346           if (direction == GTK_TEXT_DIR_RTL)
347             x = widget->allocation.x + misc->xpad;
348           else
349             x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
350
351           label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
352           gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
353
354           accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
355
356           y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
357
358           gtk_paint_layout (widget->style,
359                             widget->window,
360                             GTK_WIDGET_STATE (widget),
361                             FALSE,
362                             &event->area,
363                             widget,
364                             "accellabel",
365                             x, y,
366                             accel_layout);                            
367
368           g_object_unref (accel_layout);
369         }
370       else
371         {
372           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
373             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
374         }
375     }
376   
377   return FALSE;
378 }
379
380 static void
381 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
382 {
383   GClosure *closure = NULL;
384   GList *clist, *list;
385   
386   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
387   g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
388   
389   clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
390   for (list = clist; list; list = list->next)
391     {
392       /* we just take the first closure used */
393       closure = list->data;
394       break;
395     }
396   g_list_free (clist);
397   gtk_accel_label_set_accel_closure (accel_label, closure);
398 }
399
400 /**
401  * gtk_accel_label_set_accel_widget:
402  * @accel_label: a #GtkAccelLabel
403  * @accel_widget: the widget to be monitored.
404  *
405  * Sets the widget to be monitored by this accelerator label. 
406  **/
407 void
408 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
409                                   GtkWidget     *accel_widget)
410 {
411   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
412   if (accel_widget)
413     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
414     
415   if (accel_widget != accel_label->accel_widget)
416     {
417       if (accel_label->accel_widget)
418         {
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,
422                                                 accel_label);
423           g_object_unref (accel_label->accel_widget);
424         }
425       accel_label->accel_widget = accel_widget;
426       if (accel_label->accel_widget)
427         {
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);
433         }
434       g_object_notify (G_OBJECT (accel_label), "accel_widget");
435     }
436 }
437
438 static void
439 gtk_accel_label_reset (GtkAccelLabel *accel_label)
440 {
441   if (accel_label->accel_string)
442     {
443       g_free (accel_label->accel_string);
444       accel_label->accel_string = NULL;
445     }
446   
447   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
448 }
449
450 static void
451 check_accel_changed (GtkAccelGroup  *accel_group,
452                      guint           keyval,
453                      GdkModifierType modifier,
454                      GClosure       *accel_closure,
455                      GtkAccelLabel  *accel_label)
456 {
457   if (accel_closure == accel_label->accel_closure)
458     gtk_accel_label_reset (accel_label);
459 }
460
461 /**
462  * gtk_accel_label_set_accel_closure:
463  * @accel_label: a #GtkAccelLabel
464  * @accel_closure: the closure to monitor for accelerator changes.
465  *
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().
468  **/
469 void
470 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
471                                    GClosure      *accel_closure)
472 {
473   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
474   if (accel_closure)
475     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
476
477   if (accel_closure != accel_label->accel_closure)
478     {
479       if (accel_label->accel_closure)
480         {
481           g_signal_handlers_disconnect_by_func (accel_label->accel_group,
482                                                 check_accel_changed,
483                                                 accel_label);
484           accel_label->accel_group = NULL;
485           g_closure_unref (accel_label->accel_closure);
486         }
487       accel_label->accel_closure = accel_closure;
488       if (accel_label->accel_closure)
489         {
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),
494                                    accel_label, 0);
495         }
496       gtk_accel_label_reset (accel_label);
497       g_object_notify (G_OBJECT (accel_label), "accel_closure");
498     }
499 }
500
501 static gboolean
502 find_accel (GtkAccelKey *key,
503             GClosure    *closure,
504             gpointer     data)
505 {
506   return data == (gpointer) closure;
507 }
508
509 static const gchar *
510 gtk_accel_label_get_string (GtkAccelLabel *accel_label)
511 {
512   if (!accel_label->accel_string)
513     gtk_accel_label_refetch (accel_label);
514   
515   return accel_label->accel_string;
516 }
517
518 /* Underscores in key names are better displayed as spaces
519  * E.g., Page_Up should be "Page Up"
520  */
521 static void
522 substitute_underscores (char *str)
523 {
524   char *p;
525
526   for (p = str; *p; p++)
527     if (*p == '_')
528       *p = ' ';
529 }
530
531 gchar *
532 _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
533                                               guint               accelerator_key,
534                                               GdkModifierType     accelerator_mods)
535 {
536   GString *gstring;
537   gboolean seen_mod = FALSE;
538   gunichar ch;
539   
540   gstring = g_string_new ("");
541   
542   if (accelerator_mods & GDK_SHIFT_MASK)
543     {
544       g_string_append (gstring, klass->mod_name_shift);
545       seen_mod = TRUE;
546     }
547   if (accelerator_mods & GDK_CONTROL_MASK)
548     {
549       if (seen_mod)
550         g_string_append (gstring, klass->mod_separator);
551       g_string_append (gstring, klass->mod_name_control);
552       seen_mod = TRUE;
553     }
554   if (accelerator_mods & GDK_MOD1_MASK)
555     {
556       if (seen_mod)
557         g_string_append (gstring, klass->mod_separator);
558       g_string_append (gstring, klass->mod_name_alt);
559       seen_mod = TRUE;
560     }
561   if (seen_mod)
562     g_string_append (gstring, klass->mod_separator);
563   
564   ch = gdk_keyval_to_unicode (accelerator_key);
565   if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
566       (ch < 0x80 || klass->latin1_to_char))
567     {
568       switch (ch)
569         {
570         case ' ':
571           g_string_append (gstring, "Space");
572           break;
573         case '\\':
574           g_string_append (gstring, "Backslash");
575           break;
576         default:
577           g_string_append_unichar (gstring, g_unichar_toupper (ch));
578           break;
579         }
580     }
581   else
582     {
583       gchar *tmp;
584       
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);
590       g_free (tmp);
591     }
592
593   return g_string_free (gstring, FALSE);
594 }
595
596 gboolean
597 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
598 {
599   GtkAccelLabelClass *class;
600
601   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
602
603   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
604
605   if (accel_label->accel_string)
606     {
607       g_free (accel_label->accel_string);
608       accel_label->accel_string = NULL;
609     }
610
611   if (accel_label->accel_closure)
612     {
613       GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
614
615       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
616         {
617           GtkAccelLabelClass *klass;
618           gchar *tmp;
619
620           klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
621           tmp = _gtk_accel_label_class_get_accelerator_label (klass,
622                                                               key->accel_key,
623                                                               key->accel_mods);
624           accel_label->accel_string = g_strconcat ("   ", tmp, NULL);
625           g_free (tmp);
626         }
627       if (!accel_label->accel_string)
628         accel_label->accel_string = g_strdup ("-/-");
629     }
630   
631   if (!accel_label->accel_string)
632     accel_label->accel_string = g_strdup ("");
633
634   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
635
636   return FALSE;
637 }