]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccellabel.c
c1998e5ec09046ee75c89c6d90e183be0bfd547a
[~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 (object != NULL);
206   g_return_if_fail (GTK_IS_ACCEL_LABEL (object));
207   
208   accel_label = GTK_ACCEL_LABEL (object);
209
210   gtk_accel_label_set_accel_widget (accel_label, NULL);
211   
212   GTK_OBJECT_CLASS (parent_class)->destroy (object);
213 }
214
215 static void
216 gtk_accel_label_finalize (GObject *object)
217 {
218   GtkAccelLabel *accel_label;
219   
220   g_return_if_fail (GTK_IS_ACCEL_LABEL (object));
221   
222   accel_label = GTK_ACCEL_LABEL (object);
223   
224   g_free (accel_label->accel_string);
225   
226   G_OBJECT_CLASS (parent_class)->finalize (object);
227 }
228
229 /**
230  * gtk_accel_label_get_accel_widget:
231  * @accel_label: a #GtkAccelLabel
232  *
233  * Fetches the widget monitored by this accelerator label. See
234  * gtk_accel_label_set_accel_widget().
235  *
236  * Return value: the widget monitored by the accelerator label,
237  *               or %NULL.
238  **/
239 GtkWidget *
240 gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
241 {
242   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);
243
244   return accel_label->accel_widget;
245 }
246
247 guint
248 gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
249 {
250   g_return_val_if_fail (accel_label != NULL, 0);
251   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
252   
253   return (accel_label->accel_string_width +
254           (accel_label->accel_string_width ? accel_label->accel_padding : 0));
255 }
256
257 static void
258 gtk_accel_label_size_request (GtkWidget      *widget,
259                               GtkRequisition *requisition)
260 {
261   GtkAccelLabel *accel_label;
262   PangoLayout *layout;
263   gint width;
264   
265   g_return_if_fail (widget != NULL);
266   g_return_if_fail (GTK_IS_ACCEL_LABEL (widget));
267   g_return_if_fail (requisition != NULL);
268   
269   accel_label = GTK_ACCEL_LABEL (widget);
270   
271   if (GTK_WIDGET_CLASS (parent_class)->size_request)
272     GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
273
274   layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
275   pango_layout_get_pixel_size (layout, &width, NULL);
276   accel_label->accel_string_width = width;
277   
278   g_object_unref (G_OBJECT (layout));
279 }
280
281 static gboolean 
282 gtk_accel_label_expose_event (GtkWidget      *widget,
283                               GdkEventExpose *event)
284 {
285   GtkMisc *misc;
286   GtkAccelLabel *accel_label;
287   PangoLayout *layout;
288   
289   g_return_val_if_fail (widget != NULL, FALSE);
290   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (widget), FALSE);
291   g_return_val_if_fail (event != NULL, FALSE);
292   
293   accel_label = GTK_ACCEL_LABEL (widget);
294   misc = GTK_MISC (accel_label);
295           
296   if (GTK_WIDGET_DRAWABLE (accel_label))
297     {
298       guint ac_width;
299       
300       ac_width = gtk_accel_label_get_accel_width (accel_label);
301       
302       if (widget->allocation.width >= widget->requisition.width + ac_width)
303         {
304           guint x;
305           guint y;
306           
307           widget->allocation.width -= ac_width;
308           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
309             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
310           widget->allocation.width += ac_width;
311           
312           x = widget->allocation.x + widget->allocation.width - misc->xpad - ac_width;
313           
314           y = (widget->allocation.y * (1.0 - misc->yalign) +
315                (widget->allocation.y + widget->allocation.height -
316                 (widget->requisition.height - misc->ypad * 2)) *
317                misc->yalign) + 1.5;
318           
319           layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
320
321           gtk_paint_layout (widget->style,
322                             widget->window,
323                             GTK_WIDGET_STATE (widget),
324                             FALSE,
325                             &event->area,
326                             widget,
327                             "accellabel",
328                             x, y,
329                             layout);                            
330
331           g_object_unref (G_OBJECT (layout));
332         }
333       else
334         {
335           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
336             GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
337         }
338     }
339   
340   return TRUE;
341 }
342
343 static void
344 gtk_accel_label_queue_refetch (GtkAccelLabel *accel_label)
345 {
346   g_return_if_fail (accel_label != NULL);
347   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
348
349   if (accel_label->queue_id == 0)
350     accel_label->queue_id = gtk_idle_add_priority (G_PRIORITY_HIGH_IDLE,
351                                                    (GtkFunction) gtk_accel_label_refetch_idle,
352                                                    accel_label);
353 }
354
355 void
356 gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
357                                   GtkWidget     *accel_widget)
358 {
359   g_return_if_fail (accel_label != NULL);
360   g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
361   if (accel_widget != NULL)
362     g_return_if_fail (GTK_IS_WIDGET (accel_widget));
363   
364   if (accel_widget != accel_label->accel_widget)
365     {
366       if (accel_label->accel_widget)
367         {
368           gtk_signal_disconnect_by_func (GTK_OBJECT (accel_label->accel_widget),
369                                          GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
370                                          accel_label);
371           gtk_widget_unref (accel_label->accel_widget);
372         }
373       if (accel_label->queue_id)
374         {
375           gtk_idle_remove (accel_label->queue_id);
376           accel_label->queue_id = 0;
377         }
378       accel_label->accel_widget = accel_widget;
379       if (accel_label->accel_widget)
380         {
381           gtk_widget_ref (accel_label->accel_widget);
382           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
383                                            "add-accelerator",
384                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
385                                            GTK_OBJECT (accel_label));
386           gtk_signal_connect_object_after (GTK_OBJECT (accel_label->accel_widget),
387                                            "remove-accelerator",
388                                            GTK_SIGNAL_FUNC (gtk_accel_label_queue_refetch),
389                                            GTK_OBJECT (accel_label));
390         }
391        g_object_notify (G_OBJECT (accel_label), "accel_widget");
392     }
393 }
394
395 static gboolean
396 gtk_accel_label_refetch_idle (GtkAccelLabel *accel_label)
397 {
398   gboolean retval;
399
400   GDK_THREADS_ENTER ();
401   retval = gtk_accel_label_refetch (accel_label);
402   GDK_THREADS_LEAVE ();
403
404   return retval;
405 }
406
407 gboolean
408 gtk_accel_label_refetch (GtkAccelLabel *accel_label)
409 {
410   GtkAccelLabelClass *class;
411
412   g_return_val_if_fail (accel_label != NULL, FALSE);
413   g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);
414
415   class = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
416   
417   g_free (accel_label->accel_string);
418   accel_label->accel_string = NULL;
419   
420   if (accel_label->accel_widget)
421     {
422       GtkAccelEntry *entry = NULL;
423       GSList *slist;
424       
425       slist = gtk_accel_group_entries_from_object (GTK_OBJECT (accel_label->accel_widget));
426       for (; slist; slist = slist->next)
427         {
428           entry = slist->data;
429           if (entry->accel_flags & GTK_ACCEL_VISIBLE)
430             {
431               GString *gstring;
432               gboolean had_mod;
433               
434               gstring = g_string_new (accel_label->accel_string);
435               if (gstring->len)
436                 g_string_append (gstring, class->accel_seperator);
437               else
438                 g_string_append (gstring, "   ");
439
440               if (entry->accel_flags & GTK_ACCEL_SIGNAL_VISIBLE)
441                 {
442                   g_string_append (gstring, class->signal_quote1);
443                   g_string_append (gstring, gtk_signal_name (entry->signal_id));
444                   g_string_append (gstring, class->signal_quote2);
445                 }
446               
447               had_mod = FALSE;
448               if (entry->accelerator_mods & GDK_SHIFT_MASK)
449                 {
450                   g_string_append (gstring, class->mod_name_shift);
451                   had_mod = TRUE;
452                 }
453               if (entry->accelerator_mods & GDK_CONTROL_MASK)
454                 {
455                   if (had_mod)
456                     g_string_append (gstring, class->mod_separator);
457                   g_string_append (gstring, class->mod_name_control);
458                   had_mod = TRUE;
459                 }
460               if (entry->accelerator_mods & GDK_MOD1_MASK)
461                 {
462                   if (had_mod)
463                     g_string_append (gstring, class->mod_separator);
464                   g_string_append (gstring, class->mod_name_alt);
465                   had_mod = TRUE;
466                 }
467               
468               if (had_mod)
469                 g_string_append (gstring, class->mod_separator);
470               if (entry->accelerator_key < 0x80 ||
471                   (entry->accelerator_key > 0x80 &&
472                    entry->accelerator_key <= 0xff &&
473                    class->latin1_to_char))
474                 {
475                   switch (entry->accelerator_key)
476                     {
477                     case ' ':
478                       g_string_append (gstring, "Space");
479                       break;
480                     case '\\':
481                       g_string_append (gstring, "Backslash");
482                       break;
483                     default:
484                       g_string_append_c (gstring, toupper (entry->accelerator_key));
485                       break;
486                     }
487                 }
488               else
489                 {
490                   gchar *tmp;
491                   
492                   tmp = gtk_accelerator_name (entry->accelerator_key, 0);
493                   if (tmp[0] != 0 && tmp[1] == 0)
494                     tmp[0] = toupper (tmp[0]);
495                   g_string_append (gstring, tmp);
496                   g_free (tmp);
497                 }
498
499               g_free (accel_label->accel_string);
500               accel_label->accel_string = gstring->str;
501               g_string_free (gstring, FALSE);
502             }
503         }
504     }
505
506   if (!accel_label->accel_string)
507     accel_label->accel_string = g_strdup ("");
508
509   if (accel_label->queue_id)
510     {
511       gtk_idle_remove (accel_label->queue_id);
512       accel_label->queue_id = 0;
513     }
514
515   gtk_widget_queue_resize (GTK_WIDGET (accel_label));
516
517   return FALSE;
518 }