]> Pileus Git - ~andy/gtk/blob - gtk/gtkimagemenuitem.c
Bug 546756 – gnome-panel crashed with SIGSEGV in
[~andy/gtk] / gtk / gtkimagemenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28 #include "gtkimagemenuitem.h"
29 #include "gtkaccellabel.h"
30 #include "gtkintl.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkimage.h"
34 #include "gtkmenubar.h"
35 #include "gtkcontainer.h"
36 #include "gtkwindow.h"
37 #include "gtkprivate.h"
38 #include "gtkalias.h"
39
40 static void gtk_image_menu_item_destroy              (GtkObject        *object);
41 static void gtk_image_menu_item_size_request         (GtkWidget        *widget,
42                                                       GtkRequisition   *requisition);
43 static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
44                                                       GtkAllocation    *allocation);
45 static void gtk_image_menu_item_remove               (GtkContainer          *container,
46                                                       GtkWidget             *child);
47 static void gtk_image_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
48                                                       gint                  *requisition);
49
50 static void gtk_image_menu_item_forall     (GtkContainer   *container,
51                                             gboolean        include_internals,
52                                             GtkCallback     callback,
53                                             gpointer        callback_data);
54
55 static void gtk_image_menu_item_set_property (GObject         *object,
56                                               guint            prop_id,
57                                               const GValue    *value,
58                                               GParamSpec      *pspec);
59 static void gtk_image_menu_item_get_property (GObject         *object,
60                                               guint            prop_id,
61                                               GValue          *value,
62                                               GParamSpec      *pspec);
63 static void gtk_image_menu_item_screen_changed (GtkWidget        *widget,
64                                                 GdkScreen        *previous_screen);
65
66
67 enum {
68   PROP_0,
69   PROP_IMAGE
70 };
71
72 G_DEFINE_TYPE (GtkImageMenuItem, gtk_image_menu_item, GTK_TYPE_MENU_ITEM)
73
74 static void
75 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
76 {
77   GObjectClass *gobject_class = (GObjectClass*) klass;
78   GtkObjectClass *object_class = (GtkObjectClass*) klass;
79   GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
80   GtkMenuItemClass *menu_item_class = (GtkMenuItemClass*) klass;
81   GtkContainerClass *container_class = (GtkContainerClass*) klass;
82
83   object_class->destroy = gtk_image_menu_item_destroy;
84
85   widget_class->screen_changed = gtk_image_menu_item_screen_changed;
86   widget_class->size_request = gtk_image_menu_item_size_request;
87   widget_class->size_allocate = gtk_image_menu_item_size_allocate;
88
89   container_class->forall = gtk_image_menu_item_forall;
90   container_class->remove = gtk_image_menu_item_remove;
91   
92   menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
93
94   gobject_class->set_property = gtk_image_menu_item_set_property;
95   gobject_class->get_property = gtk_image_menu_item_get_property;
96   
97   g_object_class_install_property (gobject_class,
98                                    PROP_IMAGE,
99                                    g_param_spec_object ("image",
100                                                         P_("Image widget"),
101                                                         P_("Child widget to appear next to the menu text"),
102                                                         GTK_TYPE_WIDGET,
103                                                         GTK_PARAM_READWRITE));
104
105   gtk_settings_install_property (g_param_spec_boolean ("gtk-menu-images",
106                                                        P_("Show menu images"),
107                                                        P_("Whether images should be shown in menus"),
108                                                        TRUE,
109                                                        GTK_PARAM_READWRITE));
110   
111 }
112
113 static void
114 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
115 {
116   image_menu_item->image = NULL;
117 }
118
119 static void
120 gtk_image_menu_item_set_property (GObject         *object,
121                                   guint            prop_id,
122                                   const GValue    *value,
123                                   GParamSpec      *pspec)
124 {
125   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
126   
127   switch (prop_id)
128     {
129     case PROP_IMAGE:
130       {
131         GtkWidget *image;
132
133         image = (GtkWidget*) g_value_get_object (value);
134
135         gtk_image_menu_item_set_image (image_menu_item, image);
136       }
137       break;
138     default:
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140       break;
141     }
142 }
143 static void
144 gtk_image_menu_item_get_property (GObject         *object,
145                                   guint            prop_id,
146                                   GValue          *value,
147                                   GParamSpec      *pspec)
148 {
149   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
150   
151   switch (prop_id)
152     {
153     case PROP_IMAGE:
154       g_value_set_object (value,
155                           (GObject*) image_menu_item->image);
156       break;
157     default:
158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159       break;
160     }
161 }
162
163 static gboolean
164 show_image (GtkImageMenuItem *image_menu_item)
165 {
166   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
167   gboolean show;
168
169   g_object_get (settings, "gtk-menu-images", &show, NULL);
170
171   return show;
172 }
173
174 static void
175 gtk_image_menu_item_destroy (GtkObject *object)
176 {
177   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
178
179   if (image_menu_item->image)
180     gtk_container_remove (GTK_CONTAINER (image_menu_item),
181                           image_menu_item->image);
182
183   GTK_OBJECT_CLASS (gtk_image_menu_item_parent_class)->destroy (object);
184 }
185
186 static void
187 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
188                                          gint        *requisition)
189 {
190   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
191   GtkPackDirection pack_dir;
192   
193   if (GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
194     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (GTK_WIDGET (menu_item)->parent));
195   else
196     pack_dir = GTK_PACK_DIRECTION_LTR;
197
198   *requisition = 0;
199
200   if (image_menu_item->image && show_image (image_menu_item))
201     {
202       GtkRequisition image_requisition;
203       guint toggle_spacing;
204       gtk_widget_get_child_requisition (image_menu_item->image,
205                                         &image_requisition);
206
207       gtk_widget_style_get (GTK_WIDGET (menu_item),
208                             "toggle-spacing", &toggle_spacing,
209                             NULL);
210       
211       if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
212         {
213           if (image_requisition.width > 0)
214             *requisition = image_requisition.width + toggle_spacing;
215         }
216       else
217         {
218           if (image_requisition.height > 0)
219             *requisition = image_requisition.height + toggle_spacing;
220         }
221     }
222 }
223
224
225 static void
226 gtk_image_menu_item_size_request (GtkWidget      *widget,
227                                   GtkRequisition *requisition)
228 {
229   GtkImageMenuItem *image_menu_item;
230   gint child_width = 0;
231   gint child_height = 0;
232   GtkPackDirection pack_dir;
233   
234   if (GTK_IS_MENU_BAR (widget->parent))
235     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
236   else
237     pack_dir = GTK_PACK_DIRECTION_LTR;
238
239   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
240   
241   if (image_menu_item->image && 
242       GTK_WIDGET_VISIBLE (image_menu_item->image) && 
243       show_image (image_menu_item))
244     {
245       GtkRequisition child_requisition;
246       
247       gtk_widget_size_request (image_menu_item->image,
248                                &child_requisition);
249
250       child_width = child_requisition.width;
251       child_height = child_requisition.height;
252     }
253   
254   (* GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_request) (widget, requisition);
255
256   /* not done with height since that happens via the
257    * toggle_size_request
258    */
259   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
260     requisition->height = MAX (requisition->height, child_height);
261   else
262     requisition->width = MAX (requisition->width, child_width);
263     
264   
265   /* Note that GtkMenuShell always size requests before
266    * toggle_size_request, so toggle_size_request will be able to use
267    * image_menu_item->image->requisition
268    */
269 }
270
271 static void
272 gtk_image_menu_item_size_allocate (GtkWidget     *widget,
273                                    GtkAllocation *allocation)
274 {
275   GtkImageMenuItem *image_menu_item;
276   GtkPackDirection pack_dir;
277   
278   if (GTK_IS_MENU_BAR (widget->parent))
279     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
280   else
281     pack_dir = GTK_PACK_DIRECTION_LTR;
282   
283   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
284   
285   (* GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate) (widget, allocation);
286
287   if (image_menu_item->image && show_image (image_menu_item))
288     {
289       gint x, y, offset;
290       GtkRequisition child_requisition;
291       GtkAllocation child_allocation;
292       guint horizontal_padding, toggle_spacing;
293
294       gtk_widget_style_get (widget,
295                             "horizontal-padding", &horizontal_padding,
296                             "toggle-spacing", &toggle_spacing,
297                             NULL);
298       
299       /* Man this is lame hardcoding action, but I can't
300        * come up with a solution that's really better.
301        */
302
303       gtk_widget_get_child_requisition (image_menu_item->image,
304                                         &child_requisition);
305
306       if (pack_dir == GTK_PACK_DIRECTION_LTR ||
307           pack_dir == GTK_PACK_DIRECTION_RTL)
308         {
309           offset = GTK_CONTAINER (image_menu_item)->border_width +
310             widget->style->xthickness;
311           
312           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
313               (pack_dir == GTK_PACK_DIRECTION_LTR))
314             x = offset + horizontal_padding +
315               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
316                toggle_spacing - child_requisition.width) / 2;
317           else
318             x = widget->allocation.width - offset - horizontal_padding -
319               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
320               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
321                toggle_spacing - child_requisition.width) / 2;
322           
323           y = (widget->allocation.height - child_requisition.height) / 2;
324         }
325       else
326         {
327           offset = GTK_CONTAINER (image_menu_item)->border_width +
328             widget->style->ythickness;
329           
330           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
331               (pack_dir == GTK_PACK_DIRECTION_TTB))
332             y = offset + horizontal_padding +
333               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
334                toggle_spacing - child_requisition.height) / 2;
335           else
336             y = widget->allocation.height - offset - horizontal_padding -
337               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
338               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
339                toggle_spacing - child_requisition.height) / 2;
340
341           x = (widget->allocation.width - child_requisition.width) / 2;
342         }
343       
344       child_allocation.width = child_requisition.width;
345       child_allocation.height = child_requisition.height;
346       child_allocation.x = widget->allocation.x + MAX (x, 0);
347       child_allocation.y = widget->allocation.y + MAX (y, 0);
348
349       gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
350     }
351 }
352
353 static void
354 gtk_image_menu_item_forall (GtkContainer   *container,
355                             gboolean        include_internals,
356                             GtkCallback     callback,
357                             gpointer        callback_data)
358 {
359   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
360   
361   (* GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall) (container,
362                                                                       include_internals,
363                                                                       callback,
364                                                                       callback_data);
365
366   if (include_internals && image_menu_item->image)
367     (* callback) (image_menu_item->image, callback_data);
368 }
369
370 /**
371  * gtk_image_menu_item_new:
372  * @returns: a new #GtkImageMenuItem.
373  *
374  * Creates a new #GtkImageMenuItem with an empty label.
375  **/
376 GtkWidget*
377 gtk_image_menu_item_new (void)
378 {
379   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
380 }
381
382 /**
383  * gtk_image_menu_item_new_with_label:
384  * @label: the text of the menu item.
385  * @returns: a new #GtkImageMenuItem.
386  *
387  * Creates a new #GtkImageMenuItem containing a label. 
388  **/
389 GtkWidget*
390 gtk_image_menu_item_new_with_label (const gchar *label)
391 {
392   GtkImageMenuItem *image_menu_item;
393   GtkWidget *accel_label;
394   
395   image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
396
397   accel_label = gtk_accel_label_new (label);
398   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
399
400   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
401   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
402                                     GTK_WIDGET (image_menu_item));
403   gtk_widget_show (accel_label);
404
405   return GTK_WIDGET(image_menu_item);
406 }
407
408
409 /**
410  * gtk_image_menu_item_new_with_mnemonic:
411  * @label: the text of the menu item, with an underscore in front of the
412  *         mnemonic character
413  * @returns: a new #GtkImageMenuItem
414  *
415  * Creates a new #GtkImageMenuItem containing a label. The label
416  * will be created using gtk_label_new_with_mnemonic(), so underscores
417  * in @label indicate the mnemonic for the menu item.
418  **/
419 GtkWidget*
420 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
421 {
422   GtkImageMenuItem *image_menu_item;
423   GtkWidget *accel_label;
424   
425   image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
426
427   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
428   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
429   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
430
431   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
432   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
433                                     GTK_WIDGET (image_menu_item));
434   gtk_widget_show (accel_label);
435
436   return GTK_WIDGET(image_menu_item);
437 }
438
439 /**
440  * gtk_image_menu_item_new_from_stock:
441  * @stock_id: the name of the stock item.
442  * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
443  *   or %NULL.
444  * @returns: a new #GtkImageMenuItem.
445  *
446  * Creates a new #GtkImageMenuItem containing the image and text from a 
447  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
448  * and #GTK_STOCK_APPLY.
449  *
450  * If you want this menu item to have changeable accelerators, then pass in
451  * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
452  * appropriate path for the menu item, use gtk_stock_lookup() to look up the
453  * standard accelerator for the stock item, and if one is found, call
454  * gtk_accel_map_add_entry() to register it.
455  **/
456 GtkWidget*
457 gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
458                                     GtkAccelGroup    *accel_group)
459 {
460   GtkWidget *image;
461   GtkStockItem stock_item;
462   GtkWidget *item;
463
464   g_return_val_if_fail (stock_id != NULL, NULL);
465
466   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
467
468   if (gtk_stock_lookup (stock_id, &stock_item))
469     {
470       item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
471
472       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
473       
474       if (stock_item.keyval && accel_group)
475         gtk_widget_add_accelerator (item,
476                                     "activate",
477                                     accel_group,
478                                     stock_item.keyval,
479                                     stock_item.modifier,
480                                     GTK_ACCEL_VISIBLE);
481     }
482   else
483     {
484       item = gtk_image_menu_item_new_with_mnemonic (stock_id);
485
486       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
487     }
488
489   gtk_widget_show (image);
490   return item;
491 }
492
493 /** 
494  * gtk_image_menu_item_set_image:
495  * @image_menu_item: a #GtkImageMenuItem.
496  * @image: a widget to set as the image for the menu item.
497  * 
498  * Sets the image of @image_menu_item to the given widget.
499  * Note that it depends on the show-menu-images setting whether
500  * the image will be displayed or not.
501  **/ 
502 void
503 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
504                                GtkWidget        *image)
505 {
506   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
507
508   if (image == image_menu_item->image)
509     return;
510
511   if (image_menu_item->image)
512     gtk_container_remove (GTK_CONTAINER (image_menu_item),
513                           image_menu_item->image);
514
515   image_menu_item->image = image;
516
517   if (image == NULL)
518     return;
519
520   gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
521   g_object_set (image, 
522                 "visible", show_image (image_menu_item),
523                 "no-show-all", TRUE,
524                 NULL);
525
526   g_object_notify (G_OBJECT (image_menu_item), "image");
527 }
528
529 /**
530  * gtk_image_menu_item_get_image:
531  * @image_menu_item: a #GtkImageMenuItem.
532  * @returns: the widget set as image of @image_menu_item.
533  *
534  * Gets the widget that is currently set as the image of @image_menu_item.
535  * See gtk_image_menu_item_set_image().
536  **/
537 GtkWidget*
538 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
539 {
540   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
541
542   return image_menu_item->image;
543 }
544
545 static void
546 gtk_image_menu_item_remove (GtkContainer *container,
547                             GtkWidget    *child)
548 {
549   GtkImageMenuItem *image_menu_item;
550
551   image_menu_item = GTK_IMAGE_MENU_ITEM (container);
552
553   if (child == image_menu_item->image)
554     {
555       gboolean widget_was_visible;
556       
557       widget_was_visible = GTK_WIDGET_VISIBLE (child);
558       
559       gtk_widget_unparent (child);
560       image_menu_item->image = NULL;
561       
562       if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
563         gtk_widget_queue_resize (GTK_WIDGET (container));
564
565       g_object_notify (G_OBJECT (image_menu_item), "image");
566     }
567   else
568     {
569       (* GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove) (container, child);
570     }
571 }
572
573 static void 
574 show_image_change_notify (GtkImageMenuItem *image_menu_item)
575 {
576   if (image_menu_item->image)
577     {
578       if (show_image (image_menu_item))
579         gtk_widget_show (image_menu_item->image);
580       else
581         gtk_widget_hide (image_menu_item->image);
582     }
583 }
584
585 static void
586 traverse_container (GtkWidget *widget,
587                     gpointer   data)
588 {
589   if (GTK_IS_IMAGE_MENU_ITEM (widget))
590     show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
591   else if (GTK_IS_CONTAINER (widget))
592     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
593 }
594
595 static void
596 gtk_image_menu_item_setting_changed (GtkSettings *settings)
597 {
598   GList *list, *l;
599
600   list = gtk_window_list_toplevels ();
601
602   for (l = list; l; l = l->next)
603     gtk_container_forall (GTK_CONTAINER (l->data), 
604                           traverse_container, NULL);
605
606   g_list_free (list);  
607 }
608
609 static void
610 gtk_image_menu_item_screen_changed (GtkWidget *widget,
611                                     GdkScreen *previous_screen)
612 {
613   GtkSettings *settings;
614   guint show_image_connection;
615
616   if (!gtk_widget_has_screen (widget))
617     return;
618
619   settings = gtk_widget_get_settings (widget);
620   
621   show_image_connection = 
622     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
623                                          "gtk-image-menu-item-connection"));
624   
625   if (show_image_connection)
626     return;
627
628   show_image_connection =
629     g_signal_connect (settings, "notify::gtk-menu-images",
630                       G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
631   g_object_set_data (G_OBJECT (settings), 
632                      I_("gtk-image-menu-item-connection"),
633                      GUINT_TO_POINTER (show_image_connection));
634
635   show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
636 }
637
638 #define __GTK_IMAGE_MENU_ITEM_C__
639 #include "gtkaliasdef.c"