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