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