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