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