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