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