]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
d05376247e54e74a45df9a5281b9dd31c31e85a6
[~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 gboolean gtk_accel_label_refetch_idle (GtkAccelLabel      *accel_label);
60
61 static GtkLabelClass *parent_class = NULL;
62
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                                                        _("Accelerator Closure"),
138                                                        _("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                                                         _("Accelerator Widget"),
145                                                         _("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->queue_id = 0;
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   gtk_accel_label_refetch (accel_label);
209 }
210
211 GtkWidget*
212 gtk_accel_label_new (const gchar *string)
213 {
214   GtkAccelLabel *accel_label;
215   
216   g_return_val_if_fail (string != NULL, NULL);
217   
218   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
219   
220   gtk_label_set_text (GTK_LABEL (accel_label), string);
221   
222   return GTK_WIDGET (accel_label);
223 }
224
225 static void
226 gtk_accel_label_destroy (GtkObject *object)
227 {
228   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
229
230   gtk_accel_label_set_accel_widget (accel_label, NULL);
231   gtk_accel_label_set_accel_closure (accel_label, NULL);
232   
233   GTK_OBJECT_CLASS (parent_class)->destroy (object);
234 }
235
236 static void
237 gtk_accel_label_finalize (GObject *object)
238 {
239   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
240
241   if (accel_label->queue_id)
242     {
243       gtk_idle_remove (accel_label->queue_id);
244       accel_label->queue_id = 0;
245     }
246   g_free (accel_label->accel_string);
247   
248   G_OBJECT_CLASS (parent_class)->finalize (object);
249 }
250
251 /**
252  * gtk_accel_label_get_accel_widget:
253  * @accel_label: a #GtkAccelLabel
254  *
255  * Fetches the widget monitored by this accelerator label. See
256  * gtk_accel_label_set_accel_widget().
257  *
258  * Return value: the object monitored by the accelerator label,
259  *               or %NULL.
260  **/
261 GtkWidget*
262 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
263 {
264   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
265
266   return accel_label->accel_widget;
267 }
268
269 guint
270 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
271 {
272   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
273   
274   return (accel_label->accel_string_width +
275           (accel_label->accel_string_width ? accel_label->accel_padding : 0));
276 }
277
278 static void
279 gtk_accel_label_size_request (GtkWidget      *widget,
280                               GtkRequisition *requisition)
281 {
282   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
283   PangoLayout *layout;
284   gint width;
285   
286   if (GTK_WIDGET_CLASS (parent_class)->size_request)
287     GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
288
289   layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
290   pango_layout_get_pixel_size (layout, &width, NULL);
291   accel_label->accel_string_width = width;
292   
293   g_object_unref (layout);
294 }
295
296 static gboolean 
297 gtk_accel_label_expose_event (GtkWidget      *widget,
298                               GdkEventExpose *event)
299 {
300   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
301   GtkMisc *misc = GTK_MISC (accel_label);
302   PangoLayout *layout;
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 (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 FALSE;
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 /**
372  * gtk_accel_label_set_accel_widget:
373  * @accel_label: a #GtkAccelLabel
374  * @accel_widget: the widget to be monitored.
375  *
376  * Sets the widget to be monitored by this accelerator label. 
377  **/
378 void
379 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
380                                   GtkWidget     *accel_widget)
381 {
382   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
383   if (accel_widget)
384     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
385     
386   if (accel_widget != accel_label->accel_widget)
387     {
388       if (accel_label->accel_widget)
389         {
390           gtk_accel_label_set_accel_closure (accel_label, NULL);
391           g_signal_handlers_disconnect_by_func (accel_label->accel_widget,
392                                                 refetch_widget_accel_closure,
393                                                 accel_label);
394           g_object_unref (accel_label->accel_widget);
395         }
396       accel_label->accel_widget = accel_widget;
397       if (accel_label->accel_widget)
398         {
399           g_object_ref (accel_label->accel_widget);
400           g_signal_connect_object (accel_label->accel_widget, "accel_closures_changed",
401                                    G_CALLBACK (refetch_widget_accel_closure),
402                                    accel_label, G_CONNECT_SWAPPED);
403           refetch_widget_accel_closure (accel_label);
404         }
405       g_object_notify (G_OBJECT (accel_label), "accel_widget");
406     }
407 }
408
409 static void
410 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
411 {
412   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
413
414   if (accel_label->queue_id == 0)
415     accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
416                                                    (GtkFunction) gtk_accel_label_refetch_idle,
417                                                    accel_label);
418 }
419
420 static void
421 check_accel_changed (GtkAccelGroup  *accel_group,
422                      guint           keyval,
423                      GdkModifierType modifier,
424                      GClosure       *accel_closure,
425                      GtkAccelLabel  *accel_label)
426 {
427   if (accel_closure == accel_label->accel_closure)
428     gtk_accel_label_queue_refetch (accel_label);
429 }
430
431 /**
432  * gtk_accel_label_set_accel_closure:
433  * @accel_label: a #GtkAccelLabel
434  * @accel_closure: the closure to monitor for accelerator changes.
435  *
436  * Sets the closure to be monitored by this accelerator label. The closure
437  * must be connected to an accelerator group; see gtk_accel_group_connect().
438  **/
439 void
440 gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
441                                    GClosure      *accel_closure)
442 {
443   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
444   if (accel_closure)
445     g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);
446
447   if (accel_closure != accel_label->accel_closure)
448     {
449       if (accel_label->accel_closure)
450         {
451           g_signal_handlers_disconnect_by_func (accel_label->accel_group,
452                                                 check_accel_changed,
453                                                 accel_label);
454           accel_label->accel_group = NULL;
455           g_closure_unref (accel_label->accel_closure);
456         }
457       accel_label->accel_closure = accel_closure;
458       if (accel_label->accel_closure)
459         {
460           g_closure_ref (accel_label->accel_closure);
461           accel_label->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
462           g_signal_connect_object (accel_label->accel_group, "accel_changed",
463                                    G_CALLBACK (check_accel_changed),
464                                    accel_label, 0);
465         }
466       gtk_accel_label_queue_refetch (accel_label);
467       g_object_notify (G_OBJECT (accel_label), "accel_closure");
468     }
469 }
470
471 static gboolean
472 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
473 {
474   gboolean retval;
475
476   GDK_THREADS_ENTER ();
477   retval = gtk_accel_label_refetch (accel_label);
478   GDK_THREADS_LEAVE ();
479
480   return retval;
481 }
482
483 static gboolean
484 find_accel (GtkAccelKey *key,
485             GClosure    *closure,
486             gpointer     data)
487 {
488   return data == (gpointer) closure;
489 }
490
491 gboolean
492 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
493 {
494   GtkAccelLabelClass *class;
495
496   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
497
498   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
499
500   g_free (accel_label->accel_string);
501   accel_label->accel_string = NULL;
502
503   if (accel_label->accel_closure)
504     {
505       GtkAccelKey *key = gtk_accel_group_find (accel_label->accel_group, find_accel, accel_label->accel_closure);
506
507       if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
508         {
509           GString *gstring;
510           gboolean seen_mod = FALSE;
511           gunichar ch;
512           
513           gstring = g_string_new (accel_label->accel_string);
514           g_string_append (gstring, gstring->len ? class->accel_seperator : "   ");
515           
516           if (key->accel_mods & GDK_SHIFT_MASK)
517             {
518               g_string_append (gstring, class->mod_name_shift);
519               seen_mod = TRUE;
520             }
521           if (key->accel_mods & GDK_CONTROL_MASK)
522             {
523               if (seen_mod)
524                 g_string_append (gstring, class->mod_separator);
525               g_string_append (gstring, class->mod_name_control);
526               seen_mod = TRUE;
527             }
528           if (key->accel_mods & GDK_MOD1_MASK)
529             {
530               if (seen_mod)
531                 g_string_append (gstring, class->mod_separator);
532               g_string_append (gstring, class->mod_name_alt);
533               seen_mod = TRUE;
534             }
535           if (seen_mod)
536             g_string_append (gstring, class->mod_separator);
537
538           ch = gdk_keyval_to_unicode (key->accel_key);
539           if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
540               (ch < 0x80 || class->latin1_to_char))
541             {
542               switch (ch)
543                 {
544                 case ' ':
545                   g_string_append (gstring, "Space");
546                   break;
547                 case '\\':
548                   g_string_append (gstring, "Backslash");
549                   break;
550                 default:
551                   g_string_append_unichar (gstring, g_unichar_toupper (ch));
552                   break;
553                 }
554             }
555           else
556             {
557               gchar *tmp;
558               
559               tmp = gtk_accelerator_name (key->accel_key, 0);
560               if (tmp[0] != 0 && tmp[1] == 0)
561                 tmp[0] = g_ascii_toupper (tmp[0]);
562               g_string_append (gstring, tmp);
563               g_free (tmp);
564             }
565           g_free (accel_label->accel_string);
566           accel_label->accel_string = gstring->str;
567           g_string_free (gstring, FALSE);
568         }
569       if (!accel_label->accel_string)
570         accel_label->accel_string = g_strdup ("-/-");
571     }
572   
573   if (!accel_label->accel_string)
574     accel_label->accel_string = g_strdup ("");
575
576   if (accel_label->queue_id)
577     {
578       gtk_idle_remove (accel_label->queue_id);
579       accel_label->queue_id = 0;
580     }
581
582   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
583
584   return FALSE;
585 }