]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
Patch from Matthias Clasen to remove remove all instances of
[~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 #include <ctype.h>
32 #include "gtkmain.h"
33 #include "gtksignal.h"
34 #include "gtkaccellabel.h"
35 #include "gtkintl.h"
36
37 enum {
38   PROP_0,
39   PROP_ACCEL_WIDGET
40 };
41
42 static void     gtk_accel_label_class_init   (GtkAccelLabelClass *klass);
43 static void     gtk_accel_label_init         (GtkAccelLabel      *accel_label);
44 static void     gtk_accel_label_set_property (GObject            *object,
45                                               guint               prop_id,
46                                               const GValue       *value,
47                                               GParamSpec         *pspec);
48 static void     gtk_accel_label_get_property (GObject            *object,
49                                               guint               prop_id,
50                                               GValue             *value,
51                                               GParamSpec         *pspec);
52 static void     gtk_accel_label_destroy      (GtkObject          *object);
53 static void     gtk_accel_label_finalize     (GObject            *object);
54 static void     gtk_accel_label_size_request (GtkWidget          *widget,
55                                               GtkRequisition     *requisition);
56 static gboolean gtk_accel_label_expose_event (GtkWidget          *widget,
57                                               GdkEventExpose     *event);
58 static gboolean gtk_accel_label_refetch_idle (GtkAccelLabel      *accel_label);
59
60 static GtkAccelLabelClass *accel_label_class = NULL;
61 static GtkLabelClass *parent_class = NULL;
62
63
64 GtkType
65 gtk_accel_label_get_type (void)
66 {
67   static GtkType accel_label_type = 0;
68   
69   if (!accel_label_type)
70     {
71       static const GtkTypeInfo accel_label_info =
72       {
73         "GtkAccelLabel",
74         sizeof (GtkAccelLabel),
75         sizeof (GtkAccelLabelClass),
76         (GtkClassInitFunc) gtk_accel_label_class_init,
77         (GtkObjectInitFunc) gtk_accel_label_init,
78         /* reserved_1 */ NULL,
79         /* reserved_2 */ NULL,
80         (GtkClassInitFunc) NULL,
81       };
82       
83       accel_label_type = gtk_type_unique (GTK_TYPE_LABEL, &accel_label_info);
84     }
85   
86   return accel_label_type;
87 }
88
89 static void
90 gtk_accel_label_class_init (GtkAccelLabelClass *class)
91 {
92   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
93   GtkObjectClass *object_class;
94   GtkWidgetClass *widget_class;
95   GtkMiscClass *misc_class;
96   GtkLabelClass *label_class;
97   
98   accel_label_class = class;
99   object_class = (GtkObjectClass*) class;
100   widget_class = (GtkWidgetClass*) class;
101   misc_class = (GtkMiscClass*) class;
102   label_class = (GtkLabelClass*) class;
103   
104   parent_class = gtk_type_class (GTK_TYPE_LABEL);
105   
106   gobject_class->finalize = gtk_accel_label_finalize;
107   gobject_class->set_property = gtk_accel_label_set_property;
108   gobject_class->get_property = gtk_accel_label_get_property;
109   
110   object_class->destroy = gtk_accel_label_destroy;
111   
112   g_object_class_install_property (G_OBJECT_CLASS(object_class),
113                                    PROP_ACCEL_WIDGET,
114                                    g_param_spec_object ("accel_widget",
115                                                         _("Accelerator widget"),
116                                                         _("The widget monitored by this accelerator label"),
117                                                         GTK_TYPE_WIDGET,
118                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
119
120    
121   widget_class->size_request = gtk_accel_label_size_request;
122   widget_class->expose_event = gtk_accel_label_expose_event;
123
124   class->signal_quote1 = g_strdup ("<:");
125   class->signal_quote2 = g_strdup (":>");
126   class->mod_name_shift = g_strdup ("Shft");
127   class->mod_name_control = g_strdup ("Ctl");
128   class->mod_name_alt = g_strdup ("Alt");
129   class->mod_separator = g_strdup ("+");
130   class->accel_seperator = g_strdup (" / ");
131   class->latin1_to_char = TRUE;
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_WIDGET:
147       gtk_accel_label_set_accel_widget (accel_label, (GtkWidget*) g_value_get_object (value));
148       break;
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152     }
153 }
154
155 static void gtk_accel_label_get_property (GObject    *object,
156                                           guint       prop_id,
157                                           GValue     *value,
158                                           GParamSpec *pspec)
159 {
160   GtkAccelLabel  *accel_label;
161
162   accel_label = GTK_ACCEL_LABEL (object);
163
164   switch (prop_id)
165     {
166     case PROP_ACCEL_WIDGET:
167        g_value_set_object (value, G_OBJECT (accel_label->accel_widget));
168       break;
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171       break;
172     }
173 }
174
175 static void
176 gtk_accel_label_init (GtkAccelLabel *accel_label)
177 {
178   accel_label->queue_id = 0;
179   accel_label->accel_padding = 3;
180   accel_label->accel_widget = NULL;
181   accel_label->accel_string = NULL;
182   
183   gtk_accel_label_refetch (accel_label);
184 }
185
186 GtkWidget*
187 gtk_accel_label_new (const gchar *string)
188 {
189   GtkAccelLabel *accel_label;
190   
191   g_return_val_if_fail (string != NULL, NULL);
192   
193   accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
194   
195   gtk_label_set_text (GTK_LABEL (accel_label), string);
196   
197   return GTK_WIDGET (accel_label);
198 }
199
200 static void
201 gtk_accel_label_destroy (GtkObject *object)
202 {
203   GtkAccelLabel *accel_label;
204   
205   g_return_if_fail (GTK_IS_ACCEL_LABEL (object));
206   
207   accel_label = GTK_ACCEL_LABEL (object);
208
209   gtk_accel_label_set_accel_widget (accel_label, NULL);
210   
211   GTK_OBJECT_CLASS (parent_class)->destroy (object);
212 }
213
214 static void
215 gtk_accel_label_finalize (GObject *object)
216 {
217   GtkAccelLabel *accel_label;
218   
219   g_return_if_fail (GTK_IS_ACCEL_LABEL (object));
220   
221   accel_label = GTK_ACCEL_LABEL (object);
222   
223   g_free (accel_label->accel_string);
224   
225   G_OBJECT_CLASS (parent_class)->finalize (object);
226 }
227
228 /**
229  * gtk_accel_label_get_accel_widget:
230  * @accel_label: a #GtkAccelLabel
231  *
232  * Fetches the widget monitored by this accelerator label. See
233  * gtk_accel_label_set_accel_widget().
234  *
235  * Return value: the widget monitored by the accelerator label,
236  *               or %NULL.
237  **/
238 GtkWidget *
239 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
240 {
241   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
242
243   return accel_label->accel_widget;
244 }
245
246 guint
247 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
248 {
249   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
250   
251   return (accel_label->accel_string_width +
252           (accel_label->accel_string_width ? accel_label->accel_padding : 0));
253 }
254
255 static void
256 gtk_accel_label_size_request (GtkWidget      *widget,
257                               GtkRequisition *requisition)
258 {
259   GtkAccelLabel *accel_label;
260   PangoLayout *layout;
261   gint width;
262   
263   g_return_if_fail (GTK_IS_ACCEL_LABEL (widget));
264   g_return_if_fail (requisition != NULL);
265   
266   accel_label = GTK_ACCEL_LABEL (widget);
267   
268   if (GTK_WIDGET_CLASS (parent_class)->size_request)
269     GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
270
271   layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
272   pango_layout_get_pixel_size (layout, &width, NULL);
273   accel_label->accel_string_width = width;
274   
275   g_object_unref (G_OBJECT (layout));
276 }
277
278 static gboolean 
279 gtk_accel_label_expose_event (GtkWidget      *widget,
280                               GdkEventExpose *event)
281 {
282   GtkMisc *misc;
283   GtkAccelLabel *accel_label;
284   PangoLayout *layout;
285   
286   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (widget), FALSE);
287   g_return_val_if_fail (event != NULL, FALSE);
288   
289   accel_label = GTK_ACCEL_LABEL (widget);
290   misc = GTK_MISC (accel_label);
291           
292   if (GTK_WIDGET_DRAWABLE (accel_label))
293     {
294       guint ac_width;
295       
296       ac_width = gtk_accel_label_get_accel_width (accel_label);
297       
298       if (widget->allocation.width >= widget->requisition.width + ac_width)
299         {
300           guint x;
301           guint y;
302           
303           widget->allocation.width -= ac_width;
304           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
305             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
306           widget->allocation.width += ac_width;
307           
308           x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
309           
310           y = (widget->allocation.y * (1.0 - misc->yalign) +
311                (widget->allocation.y + widget->allocation.height -
312                 (widget->requisition.height - misc->ypad * 2)) *
313                misc->yalign) + 1.5;
314           
315           layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
316
317           gtk_paint_layout (widget->style,
318                             widget->window,
319                             GTK_WIDGET_STATE (widget),
320                             FALSE,
321                             &event->area,
322                             widget,
323                             "accellabel",
324                             x, y,
325                             layout);                            
326
327           g_object_unref (G_OBJECT (layout));
328         }
329       else
330         {
331           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
332             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
333         }
334     }
335   
336   return TRUE;
337 }
338
339 static void
340 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
341 {
342   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
343
344   if (accel_label->queue_id == 0)
345     accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
346                                                    (GtkFunction) gtk_accel_label_refetch_idle,
347                                                    accel_label);
348 }
349
350 void
351 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
352                                   GtkWidget     *accel_widget)
353 {
354   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
355   if (accel_widget != NULL)
356     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
357   
358   if (accel_widget != accel_label->accel_widget)
359     {
360       if (accel_label->accel_widget)
361         {
362           gtk_signal_disconnect_by_func (GTK_OBJECT (accel_label->accel_widget),
363                                          GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
364                                          accel_label);
365           gtk_widget_unref (accel_label->accel_widget);
366         }
367       if (accel_label->queue_id)
368         {
369           gtk_idle_remove (accel_label->queue_id);
370           accel_label->queue_id = 0;
371         }
372       accel_label->accel_widget = accel_widget;
373       if (accel_label->accel_widget)
374         {
375           gtk_widget_ref (accel_label->accel_widget);
376           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
377                                            "add-accelerator",
378                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
379                                            GTK_OBJECT (accel_label));
380           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
381                                            "remove-accelerator",
382                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
383                                            GTK_OBJECT (accel_label));
384         }
385        g_object_notify (G_OBJECT (accel_label), "accel_widget");
386     }
387 }
388
389 static gboolean
390 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
391 {
392   gboolean retval;
393
394   GDK_THREADS_ENTER ();
395   retval = gtk_accel_label_refetch (accel_label);
396   GDK_THREADS_LEAVE ();
397
398   return retval;
399 }
400
401 gboolean
402 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
403 {
404   GtkAccelLabelClass *class;
405
406   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
407
408   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
409   
410   g_free (accel_label->accel_string);
411   accel_label->accel_string = NULL;
412   
413   if (accel_label->accel_widget)
414     {
415       GtkAccelEntry *entry = NULL;
416       GSList *slist;
417       
418       slist = gtk_accel_group_entries_from_object (GTK_OBJECT (accel_label->accel_widget));
419       for (; slist; slist = slist->next)
420         {
421           entry = slist->data;
422           if (entry->accel_flags & GTK_ACCEL_VISIBLE)
423             {
424               GString *gstring;
425               gboolean had_mod;
426               
427               gstring = g_string_new (accel_label->accel_string);
428               if (gstring->len)
429                 g_string_append (gstring, class->accel_seperator);
430               else
431                 g_string_append (gstring, "   ");
432
433               if (entry->accel_flags & GTK_ACCEL_SIGNAL_VISIBLE)
434                 {
435                   g_string_append (gstring, class->signal_quote1);
436                   g_string_append (gstring, gtk_signal_name (entry->signal_id));
437                   g_string_append (gstring, class->signal_quote2);
438                 }
439               
440               had_mod = FALSE;
441               if (entry->accelerator_mods & GDK_SHIFT_MASK)
442                 {
443                   g_string_append (gstring, class->mod_name_shift);
444                   had_mod = TRUE;
445                 }
446               if (entry->accelerator_mods & GDK_CONTROL_MASK)
447                 {
448                   if (had_mod)
449                     g_string_append (gstring, class->mod_separator);
450                   g_string_append (gstring, class->mod_name_control);
451                   had_mod = TRUE;
452                 }
453               if (entry->accelerator_mods & GDK_MOD1_MASK)
454                 {
455                   if (had_mod)
456                     g_string_append (gstring, class->mod_separator);
457                   g_string_append (gstring, class->mod_name_alt);
458                   had_mod = TRUE;
459                 }
460               
461               if (had_mod)
462                 g_string_append (gstring, class->mod_separator);
463               if (entry->accelerator_key < 0x80 ||
464                   (entry->accelerator_key > 0x80 &&
465                    entry->accelerator_key <= 0xff &&
466                    class->latin1_to_char))
467                 {
468                   switch (entry->accelerator_key)
469                     {
470                     case ' ':
471                       g_string_append (gstring, "Space");
472                       break;
473                     case '\\':
474                       g_string_append (gstring, "Backslash");
475                       break;
476                     default:
477                       g_string_append_c (gstring, toupper (entry->accelerator_key));
478                       break;
479                     }
480                 }
481               else
482                 {
483                   gchar *tmp;
484                   
485                   tmp = gtk_accelerator_name (entry->accelerator_key, 0);
486                   if (tmp[0] != 0 && tmp[1] == 0)
487                     tmp[0] = toupper (tmp[0]);
488                   g_string_append (gstring, tmp);
489                   g_free (tmp);
490                 }
491
492               g_free (accel_label->accel_string);
493               accel_label->accel_string = gstring->str;
494               g_string_free (gstring, FALSE);
495             }
496         }
497     }
498
499   if (!accel_label->accel_string)
500     accel_label->accel_string = g_strdup ("");
501
502   if (accel_label->queue_id)
503     {
504       gtk_idle_remove (accel_label->queue_id);
505       accel_label->queue_id = 0;
506     }
507
508   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
509
510   return FALSE;
511 }