]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Remove improper use of g_locale_to_utf8() to convert from latin-1, and do
[~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 #include "gtkaccellabel.h"
30 #include "gtkaccelmap.h"
31 #include "gtkmain.h"
32 #include "gtksignal.h"
33 #include "gtkintl.h"
34
35 #include <string.h>
36 #include <ctype.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 gboolean gtk_accel_label_refetch_idle (GtkAccelLabel      *accel_label);
61
62 static GtkAccelLabelClass *accel_label_class = NULL;
63 static GtkLabelClass *parent_class = NULL;
64
65
66 GtkType
67 gtk_accel_label_get_type (void)
68 {
69   static GtkType accel_label_type = 0;
70   
71   if (!accel_label_type)
72     {
73       static const GtkTypeInfo accel_label_info =
74       {
75         "GtkAccelLabel",
76         sizeof (GtkAccelLabel),
77         sizeof (GtkAccelLabelClass),
78         (GtkClassInitFunc) gtk_accel_label_class_init,
79         (GtkObjectInitFunc) gtk_accel_label_init,
80         /* reserved_1 */ NULL,
81         /* reserved_2 */ NULL,
82         (GtkClassInitFunc) NULL,
83       };
84       
85       accel_label_type = gtk_type_unique (GTK_TYPE_LABEL, &accel_label_info);
86     }
87   
88   return accel_label_type;
89 }
90
91 static void
92 gtk_accel_label_class_init (GtkAccelLabelClass *class)
93 {
94   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
95   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
96   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
97   
98   accel_label_class = class;
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   class->mod_name_shift = g_strdup ("Shift");
113   class->mod_name_control = g_strdup ("Ctrl");
114   class->mod_name_alt = g_strdup ("Alt");
115   class->mod_separator = g_strdup ("+");
116   class->accel_seperator = g_strdup (" / ");
117   class->latin1_to_char = TRUE;
118   
119   g_object_class_install_property (G_OBJECT_CLASS (object_class),
120                                    PROP_ACCEL_CLOSURE,
121                                    g_param_spec_boxed ("accel_closure",
122                                                        _("Accelerator Closure"),
123                                                        _("The closure to be monitored for accelerator changes"),
124                                                        G_TYPE_CLOSURE,
125                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
126   g_object_class_install_property (G_OBJECT_CLASS (object_class),
127                                    PROP_ACCEL_WIDGET,
128                                    g_param_spec_object ("accel_widget",
129                                                         _("Accelerator Widget"),
130                                                         _("The widget to be monitored for accelerator changes"),
131                                                         GTK_TYPE_WIDGET,
132                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
133 }
134
135 static void
136 gtk_accel_label_set_property (GObject      *object,
137                               guint         prop_id,
138                               const GValue *value,
139                               GParamSpec   *pspec)
140 {
141   GtkAccelLabel  *accel_label;
142
143   accel_label = GTK_ACCEL_LABEL (object);
144
145   switch (prop_id)
146     {
147     case PROP_ACCEL_CLOSURE:
148       gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
149       break;
150     case PROP_ACCEL_WIDGET:
151       gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155       break;
156     }
157 }
158
159 static void
160 gtk_accel_label_get_property (GObject    *object,
161                               guint       prop_id,
162                               GValue     *value,
163                               GParamSpec *pspec)
164 {
165   GtkAccelLabel  *accel_label;
166
167   accel_label = GTK_ACCEL_LABEL (object);
168
169   switch (prop_id)
170     {
171     case PROP_ACCEL_CLOSURE:
172       g_value_set_boxed (value, accel_label->accel_closure);
173       break;
174     case PROP_ACCEL_WIDGET:
175       g_value_set_object (value, accel_label->accel_widget);
176       break;
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179       break;
180     }
181 }
182
183 static void
184 gtk_accel_label_init (GtkAccelLabel *accel_label)
185 {
186   accel_label->queue_id = 0;
187   accel_label->accel_padding = 3;
188   accel_label->accel_widget = NULL;
189   accel_label->accel_closure = NULL;
190   accel_label->accel_group = NULL;
191   accel_label->accel_string = NULL;
192   
193   gtk_accel_label_refetch (accel_label);
194 }
195
196 GtkWidget*
197 gtk_accel_label_new (const gchar *string)
198 {
199   GtkAccelLabel *accel_label;
200   
201   g_return_val_if_fail (string != NULL, NULL);
202   
203   accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
204   
205   gtk_label_set_text (GTK_LABEL (accel_label), string);
206   
207   return GTK_WIDGET (accel_label);
208 }
209
210 static void
211 gtk_accel_label_destroy (GtkObject *object)
212 {
213   GtkAccelLabel *accel_label;
214   
215   g_return_if_fail (GTK_IS_ACCEL_LABEL (object));
216   
217   accel_label = GTK_ACCEL_LABEL (object);
218
219   gtk_accel_label_set_accel_widget (accel_label, NULL);
220   gtk_accel_label_set_accel_closure (accel_label, NULL);
221   
222   GTK_OBJECT_CLASS (parent_class)->destroy (object);
223 }
224
225 static void
226 gtk_accel_label_finalize (GObject *object)
227 {
228   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
229
230   if (accel_label->queue_id)
231     {
232       gtk_idle_remove (accel_label->queue_id);
233       accel_label->queue_id = 0;
234     }
235   g_free (accel_label->accel_string);
236   
237   G_OBJECT_CLASS (parent_class)->finalize (object);
238 }
239
240 /**
241  * gtk_accel_label_get_accel_widget:
242  * @accel_label: a #GtkAccelLabel
243  *
244  * Fetches the widget monitored by this accelerator label. See
245  * gtk_accel_label_set_accel_widget().
246  *
247  * Return value: the object monitored by the accelerator label,
248  *               or %NULL.
249  **/
250 GtkWidget*
251 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
252 {
253   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
254
255   return accel_label->accel_widget;
256 }
257
258 guint
259 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
260 {
261   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
262   
263   return (accel_label->accel_string_width +
264           (accel_label->accel_string_width ? accel_label->accel_padding : 0));
265 }
266
267 static void
268 gtk_accel_label_size_request (GtkWidget      *widget,
269                               GtkRequisition *requisition)
270 {
271   GtkAccelLabel *accel_label;
272   PangoLayout *layout;
273   gint width;
274   
275   g_return_if_fail (GTK_IS_ACCEL_LABEL (widget));
276   g_return_if_fail (requisition != NULL);
277   
278   accel_label = GTK_ACCEL_LABEL (widget);
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, accel_label->accel_string);
284   pango_layout_get_pixel_size (layout, &width, NULL);
285   accel_label->accel_string_width = width;
286   
287   g_object_unref (G_OBJECT (layout));
288 }
289
290 static gboolean 
291 gtk_accel_label_expose_event (GtkWidget      *widget,
292                               GdkEventExpose *event)
293 {
294   GtkMisc *misc;
295   GtkAccelLabel *accel_label;
296   PangoLayout *layout;
297   
298   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (widget), FALSE);
299   g_return_val_if_fail (event != NULL, FALSE);
300   
301   accel_label = GTK_ACCEL_LABEL (widget);
302   misc = GTK_MISC (accel_label);
303           
304   if (GTK_WIDGET_DRAWABLE (accel_label))
305     {
306       guint ac_width;
307       
308       ac_width = gtk_accel_label_get_accel_width (accel_label);
309       
310       if (widget->allocation.width >= widget->requisition.width + ac_width)
311         {
312           guint x;
313           guint y;
314           
315           widget->allocation.width -= ac_width;
316           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
317             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
318           widget->allocation.width += ac_width;
319           
320           x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
321           
322           y = (widget->allocation.y * (1.0 - misc->yalign) +
323                (widget->allocation.y + widget->allocation.height -
324                 (widget->requisition.height - misc->ypad * 2)) *
325                misc->yalign) + 1.5;
326           
327           layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
328
329           gtk_paint_layout (widget->style,
330                             widget->window,
331                             GTK_WIDGET_STATE (widget),
332                             FALSE,
333                             &event->area,
334                             widget,
335                             "accellabel",
336                             x, y,
337                             layout);                            
338
339           g_object_unref (G_OBJECT (layout));
340         }
341       else
342         {
343           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
344             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
345         }
346     }
347   
348   return TRUE;
349 }
350
351 static void
352 refetch_widget_accel_closure (GtkAccelLabel *accel_label)
353 {
354   GClosure *closure = NULL;
355   GList *clist, *list;
356   
357   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
358   g_return_if_fail (GTK_IS_WIDGET (accel_label->accel_widget));
359   
360   clist = gtk_widget_list_accel_closures (accel_label->accel_widget);
361   for (list = clist; list; list = list->next)
362     {
363       /* we just take the first closure used */
364       closure = list->data;
365       break;
366     }
367   g_list_free (clist);
368   gtk_accel_label_set_accel_closure (accel_label, closure);
369 }
370
371 void
372 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
373                                   GtkWidget     *accel_widget)
374 {
375   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
376   if (accel_widget)
377     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
378     
379   if (accel_widget != accel_label->accel_widget)
380     {
381       if (accel_label->accel_widget)
382         {
383           gtk_accel_label_set_accel_closure (accel_label, NULL);
384           g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
385                                                 G_CALLBACK (refetch_widget_accel_closure),
386                                                 accel_label);
387           g_object_unref (accel_label->accel_widget);
388         }
389       accel_label->accel_widget = accel_widget;
390       if (accel_label->accel_widget)
391         {
392           g_object_ref (accel_label->accel_widget);
393           g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
394                                    G_CALLBACK (refetch_widget_accel_closure),
395                                    accel_label, G_CONNECT_SWAPPED);
396           refetch_widget_accel_closure (accel_label);
397         }
398       g_object_notify (G_OBJECT (accel_label), "accel_widget");
399     }
400 }
401
402 static void
403 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
404 {
405   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
406
407   if (accel_label->queue_id == 0)
408     accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
409                                                    (GtkFunction) gtk_accel_label_refetch_idle,
410                                                    accel_label);
411 }
412
413 static void
414 check_accel_changed (GtkAccelGroup  *accel_group,
415                      guint           keyval,
416                      GdkModifierType modifier,
417                      GClosure       *accel_closure,
418                      GtkAccelLabel  *accel_label)
419 {
420   if (accel_closure == accel_label->accel_closure)
421     gtk_accel_label_queue_refetch (accel_label);
422 }
423
424 void
425 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
426                                    GClosure      *accel_closure)
427 {
428   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
429   if (accel_closure)
430     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
431
432   if (accel_closure != accel_label->accel_closure)
433     {
434       if (accel_label->accel_closure)
435         {
436           g_signal_handlers_disconnect_by_func (accel_label->accel_group,
437                                                 G_CALLBACK (check_accel_changed),
438                                                 accel_label);
439           accel_label->accel_group = NULL;
440           g_closure_unref (accel_label->accel_closure);
441         }
442       accel_label->accel_closure = accel_closure;
443       if (accel_label->accel_closure)
444         {
445           g_closure_ref (accel_label->accel_closure);
446           accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
447           g_signal_connect_object (accel_label->accel_group, "accel_changed",
448                                    G_CALLBACK (check_accel_changed),
449                                    accel_label, 0);
450         }
451       gtk_accel_label_queue_refetch (accel_label);
452       g_object_notify (G_OBJECT (accel_label), "accel_closure");
453     }
454 }
455
456 static gboolean
457 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
458 {
459   gboolean retval;
460
461   GDK_THREADS_ENTER ();
462   retval = gtk_accel_label_refetch (accel_label);
463   GDK_THREADS_LEAVE ();
464
465   return retval;
466 }
467
468 static gboolean
469 find_accel (GtkAccelKey *key,
470             GClosure    *closure,
471             gpointer     data)
472 {
473   return data == (gpointer) closure;
474 }
475
476 gboolean
477 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
478 {
479   GtkAccelLabelClass *class;
480   gchar *utf8;
481
482   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
483
484   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
485
486   g_free (accel_label->accel_string);
487   accel_label->accel_string = NULL;
488
489   if (accel_label->accel_closure)
490     {
491       GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
492
493       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
494         {
495           GString *gstring;
496           gboolean seen_mod = FALSE;
497           
498           gstring = g_string_new (accel_label->accel_string);
499           g_string_append (gstring, gstring->len ? class->accel_seperator : "   ");
500           
501           if (key->accel_mods & GDK_SHIFT_MASK)
502             {
503               g_string_append (gstring, class->mod_name_shift);
504               seen_mod = TRUE;
505             }
506           if (key->accel_mods & GDK_CONTROL_MASK)
507             {
508               if (seen_mod)
509                 g_string_append (gstring, class->mod_separator);
510               g_string_append (gstring, class->mod_name_control);
511               seen_mod = TRUE;
512             }
513           if (key->accel_mods & GDK_MOD1_MASK)
514             {
515               if (seen_mod)
516                 g_string_append (gstring, class->mod_separator);
517               g_string_append (gstring, class->mod_name_alt);
518               seen_mod = TRUE;
519             }
520           if (seen_mod)
521             g_string_append (gstring, class->mod_separator);
522           if (key->accel_key < 0x80 ||
523               (key->accel_key > 0x80 &&
524                key->accel_key <= 0xff &&
525                class->latin1_to_char))
526             {
527               switch (key->accel_key)
528                 {
529                 case ' ':
530                   g_string_append (gstring, "Space");
531                   break;
532                 case '\\':
533                   g_string_append (gstring, "Backslash");
534                   break;
535                 default:
536                   g_string_append_unichar (gstring, toupper (key->accel_key));
537                   break;
538                 }
539             }
540           else
541             {
542               gchar *tmp;
543               
544               tmp = gtk_accelerator_name (key->accel_key, 0);
545               if (tmp[0] != 0 && tmp[1] == 0)
546                 tmp[0] = toupper (tmp[0]);
547               g_string_append (gstring, tmp);
548               g_free (tmp);
549             }
550           g_free (accel_label->accel_string);
551           accel_label->accel_string = gstring->str;
552           g_string_free (gstring, FALSE);
553         }
554       if (!accel_label->accel_string)
555         accel_label->accel_string = g_strdup ("-/-");
556     }
557   
558   if (!accel_label->accel_string)
559     accel_label->accel_string = g_strdup ("");
560
561   if (accel_label->queue_id)
562     {
563       gtk_idle_remove (accel_label->queue_id);
564       accel_label->queue_id = 0;
565     }
566
567   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
568
569   return FALSE;
570 }