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