]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
documented necessary changes for 1.4 transition.
[~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 Library 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  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library 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-1999.  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   
234   g_return_if_fail (widget != NULL);
235   g_return_if_fail (GTK_IS_ACCEL_LABEL (widget));
236   g_return_if_fail (requisition != NULL);
237   
238   accel_label = GTK_ACCEL_LABEL (widget);
239   
240   if (GTK_WIDGET_CLASS (parent_class)->size_request)
241     GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
242   
243   accel_label->accel_string_width = gdk_string_width (GTK_WIDGET (accel_label)->style->font,
244                                                       accel_label->accel_string);
245 }
246
247 static gint
248 gtk_accel_label_expose_event (GtkWidget      *widget,
249                               GdkEventExpose *event)
250 {
251   GtkMisc *misc;
252   GtkAccelLabel *accel_label;
253   
254   g_return_val_if_fail (widget != NULL, FALSE);
255   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (widget), FALSE);
256   g_return_val_if_fail (event != NULL, FALSE);
257   
258   accel_label = GTK_ACCEL_LABEL (widget);
259   misc = GTK_MISC (accel_label);
260           
261   if (GTK_WIDGET_DRAWABLE (accel_label))
262     {
263       guint ac_width;
264       
265       ac_width = gtk_accel_label_get_accel_width (accel_label);
266       
267       if (widget->allocation.width >= widget->requisition.width + ac_width)
268         {
269           guint x;
270           guint y;
271           
272           widget->allocation.width -= ac_width;
273           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
274             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
275           widget->allocation.width += ac_width;
276           
277           x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
278           
279           y = (widget->allocation.y * (1.0 - misc->yalign) +
280                (widget->allocation.y + widget->allocation.height -
281                 (widget->requisition.height - misc->ypad * 2)) *
282                misc->yalign + widget->style->font->ascent) + 1.5;
283           
284           if (GTK_WIDGET_STATE (accel_label) == GTK_STATE_INSENSITIVE)
285             gdk_draw_string (widget->window,
286                              widget->style->font,
287                              widget->style->white_gc,
288                              x + 1,
289                              y + 1,
290                              accel_label->accel_string);
291           
292           gdk_draw_string (widget->window,
293                            widget->style->font,
294                            widget->style->fg_gc[GTK_WIDGET_STATE (accel_label)],
295                            x,
296                            y,
297                            accel_label->accel_string);
298         }
299       else
300         {
301           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
302             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
303         }
304     }
305   
306   return TRUE;
307 }
308
309 static void
310 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
311 {
312   g_return_if_fail (accel_label != NULL);
313   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
314
315   if (accel_label->queue_id == 0)
316     accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
317                                                    (GtkFunction) gtk_accel_label_refetch_idle,
318                                                    accel_label);
319 }
320
321 void
322 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
323                                   GtkWidget     *accel_widget)
324 {
325   g_return_if_fail (accel_label != NULL);
326   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
327   if (accel_widget != NULL)
328     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
329   
330   if (accel_widget != accel_label->accel_widget)
331     {
332       if (accel_label->accel_widget)
333         {
334           gtk_signal_disconnect_by_func (GTK_OBJECT (accel_label->accel_widget),
335                                          GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
336                                          accel_label);
337           gtk_widget_unref (accel_label->accel_widget);
338         }
339       if (accel_label->queue_id)
340         {
341           gtk_idle_remove (accel_label->queue_id);
342           accel_label->queue_id = 0;
343         }
344       accel_label->accel_widget = accel_widget;
345       if (accel_label->accel_widget)
346         {
347           gtk_widget_ref (accel_label->accel_widget);
348           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
349                                            "add-accelerator",
350                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
351                                            GTK_OBJECT (accel_label));
352           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
353                                            "remove-accelerator",
354                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
355                                            GTK_OBJECT (accel_label));
356         }
357     }
358 }
359
360 static gboolean
361 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
362 {
363   gboolean retval;
364
365   GDK_THREADS_ENTER ();
366   retval = gtk_accel_label_refetch (accel_label);
367   GDK_THREADS_LEAVE ();
368
369   return retval;
370 }
371
372 gboolean
373 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
374 {
375   GtkAccelLabelClass *class;
376
377   g_return_val_if_fail (accel_label != NULL, FALSE);
378   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
379
380   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
381   
382   g_free (accel_label->accel_string);
383   accel_label->accel_string = NULL;
384   
385   if (accel_label->accel_widget)
386     {
387       GtkAccelEntry *entry = NULL;
388       GSList *slist;
389       
390       slist = gtk_accel_group_entries_from_object (GTK_OBJECT (accel_label->accel_widget));
391       for (; slist; slist = slist->next)
392         {
393           entry = slist->data;
394           if (entry->accel_flags & GTK_ACCEL_VISIBLE)
395             {
396               GString *gstring;
397               gboolean had_mod;
398               
399               gstring = g_string_new (accel_label->accel_string);
400               if (gstring->len)
401                 g_string_append (gstring, class->accel_seperator);
402               else
403                 g_string_append (gstring, "   ");
404
405               if (entry->accel_flags & GTK_ACCEL_SIGNAL_VISIBLE)
406                 {
407                   g_string_append (gstring, class->signal_quote1);
408                   g_string_append (gstring, gtk_signal_name (entry->signal_id));
409                   g_string_append (gstring, class->signal_quote2);
410                 }
411               
412               had_mod = FALSE;
413               if (entry->accelerator_mods & GDK_SHIFT_MASK)
414                 {
415                   g_string_append (gstring, class->mod_name_shift);
416                   had_mod = TRUE;
417                 }
418               if (entry->accelerator_mods & GDK_CONTROL_MASK)
419                 {
420                   if (had_mod)
421                     g_string_append (gstring, class->mod_separator);
422                   g_string_append (gstring, class->mod_name_control);
423                   had_mod = TRUE;
424                 }
425               if (entry->accelerator_mods & GDK_MOD1_MASK)
426                 {
427                   if (had_mod)
428                     g_string_append (gstring, class->mod_separator);
429                   g_string_append (gstring, class->mod_name_alt);
430                   had_mod = TRUE;
431                 }
432               
433               if (had_mod)
434                 g_string_append (gstring, class->mod_separator);
435               if (entry->accelerator_key < 0x80 ||
436                   (entry->accelerator_key > 0x80 &&
437                    entry->accelerator_key <= 0xff &&
438                    class->latin1_to_char))
439                 {
440                   switch (entry->accelerator_key)
441                     {
442                     case ' ':
443                       g_string_append (gstring, "Space");
444                       break;
445                     case '\\':
446                       g_string_append (gstring, "Backslash");
447                       break;
448                     default:
449                       g_string_append_c (gstring, toupper (entry->accelerator_key));
450                       break;
451                     }
452                 }
453               else
454                 {
455                   gchar *tmp;
456                   
457                   tmp = gtk_accelerator_name (entry->accelerator_key, 0);
458                   if (tmp[0] != 0 && tmp[1] == 0)
459                     tmp[0] = toupper (tmp[0]);
460                   g_string_append (gstring, tmp);
461                   g_free (tmp);
462                 }
463
464               g_free (accel_label->accel_string);
465               accel_label->accel_string = gstring->str;
466               g_string_free (gstring, FALSE);
467             }
468         }
469     }
470
471   if (!accel_label->accel_string)
472     accel_label->accel_string = g_strdup ("");
473
474   if (accel_label->queue_id)
475     {
476       gtk_idle_remove (accel_label->queue_id);
477       accel_label->queue_id = 0;
478     }
479
480   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
481
482   return FALSE;
483 }