1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2001 Red Hat, Inc.
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.
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.
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.
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/.
28 #include "gtkimagemenuitem.h"
29 #include "gtkaccellabel.h"
32 #include "gtkiconfactory.h"
34 #include "gtkmenubar.h"
35 #include "gtkcontainer.h"
36 #include "gtkwindow.h"
37 #include "gtkactivatable.h"
38 #include "gtkprivate.h"
42 struct _GtkImageMenuItemPrivate
48 guint always_show_image : 1;
56 PROP_ALWAYS_SHOW_IMAGE
59 static GtkActivatableIface *parent_activatable_iface;
61 static void gtk_image_menu_item_destroy (GtkObject *object);
62 static void gtk_image_menu_item_size_request (GtkWidget *widget,
63 GtkRequisition *requisition);
64 static void gtk_image_menu_item_size_allocate (GtkWidget *widget,
65 GtkAllocation *allocation);
66 static void gtk_image_menu_item_map (GtkWidget *widget);
67 static void gtk_image_menu_item_remove (GtkContainer *container,
69 static void gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
71 static void gtk_image_menu_item_set_label (GtkMenuItem *menu_item,
73 static G_CONST_RETURN gchar *gtk_image_menu_item_get_label (GtkMenuItem *menu_item);
75 static void gtk_image_menu_item_forall (GtkContainer *container,
76 gboolean include_internals,
78 gpointer callback_data);
80 static void gtk_image_menu_item_finalize (GObject *object);
81 static void gtk_image_menu_item_set_property (GObject *object,
85 static void gtk_image_menu_item_get_property (GObject *object,
89 static void gtk_image_menu_item_screen_changed (GtkWidget *widget,
90 GdkScreen *previous_screen);
92 static void gtk_image_menu_item_recalculate (GtkImageMenuItem *image_menu_item);
94 static void gtk_image_menu_item_activatable_interface_init (GtkActivatableIface *iface);
95 static void gtk_image_menu_item_update (GtkActivatable *activatable,
97 const gchar *property_name);
98 static void gtk_image_menu_item_sync_action_properties (GtkActivatable *activatable,
102 G_DEFINE_TYPE_WITH_CODE (GtkImageMenuItem, gtk_image_menu_item, GTK_TYPE_MENU_ITEM,
103 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
104 gtk_image_menu_item_activatable_interface_init))
108 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
110 GObjectClass *gobject_class = (GObjectClass*) klass;
111 GtkObjectClass *object_class = (GtkObjectClass*) klass;
112 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
113 GtkMenuItemClass *menu_item_class = (GtkMenuItemClass*) klass;
114 GtkContainerClass *container_class = (GtkContainerClass*) klass;
116 object_class->destroy = gtk_image_menu_item_destroy;
118 widget_class->screen_changed = gtk_image_menu_item_screen_changed;
119 widget_class->size_request = gtk_image_menu_item_size_request;
120 widget_class->size_allocate = gtk_image_menu_item_size_allocate;
121 widget_class->map = gtk_image_menu_item_map;
123 container_class->forall = gtk_image_menu_item_forall;
124 container_class->remove = gtk_image_menu_item_remove;
126 menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
127 menu_item_class->set_label = gtk_image_menu_item_set_label;
128 menu_item_class->get_label = gtk_image_menu_item_get_label;
130 gobject_class->finalize = gtk_image_menu_item_finalize;
131 gobject_class->set_property = gtk_image_menu_item_set_property;
132 gobject_class->get_property = gtk_image_menu_item_get_property;
134 g_object_class_install_property (gobject_class,
136 g_param_spec_object ("image",
138 P_("Child widget to appear next to the menu text"),
140 GTK_PARAM_READWRITE));
142 * GtkImageMenuItem:use-stock:
144 * If %TRUE, the label set in the menuitem is used as a
145 * stock id to select the stock item for the item.
149 g_object_class_install_property (gobject_class,
151 g_param_spec_boolean ("use-stock",
153 P_("Whether to use the label text to create a stock menu item"),
155 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
158 * GtkImageMenuItem:always-show-image:
160 * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images
161 * setting and always show the image, if available.
163 * Use this property if the menuitem would be useless or hard to use
168 g_object_class_install_property (gobject_class,
169 PROP_ALWAYS_SHOW_IMAGE,
170 g_param_spec_boolean ("always-show-image",
171 P_("Always show image"),
172 P_("Whether the image will always be shown"),
174 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
177 * GtkImageMenuItem:accel-group:
179 * The Accel Group to use for stock accelerator keys
183 g_object_class_install_property (gobject_class,
185 g_param_spec_object ("accel-group",
187 P_("The Accel Group to use for stock accelerator keys"),
188 GTK_TYPE_ACCEL_GROUP,
189 GTK_PARAM_WRITABLE));
191 gtk_settings_install_property (g_param_spec_boolean ("gtk-menu-images",
192 P_("Show menu images"),
193 P_("Whether images should be shown in menus"),
195 GTK_PARAM_READWRITE));
197 g_type_class_add_private (object_class, sizeof (GtkImageMenuItemPrivate));
201 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
203 GtkImageMenuItemPrivate *priv;
205 image_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (image_menu_item,
206 GTK_TYPE_IMAGE_MENU_ITEM,
207 GtkImageMenuItemPrivate);
208 priv = image_menu_item->priv;
211 priv->use_stock = FALSE;
216 gtk_image_menu_item_finalize (GObject *object)
218 GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (object)->priv;
220 g_free (priv->label);
223 G_OBJECT_CLASS (gtk_image_menu_item_parent_class)->finalize (object);
227 gtk_image_menu_item_set_property (GObject *object,
232 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
237 gtk_image_menu_item_set_image (image_menu_item, (GtkWidget *) g_value_get_object (value));
240 gtk_image_menu_item_set_use_stock (image_menu_item, g_value_get_boolean (value));
242 case PROP_ALWAYS_SHOW_IMAGE:
243 gtk_image_menu_item_set_always_show_image (image_menu_item, g_value_get_boolean (value));
245 case PROP_ACCEL_GROUP:
246 gtk_image_menu_item_set_accel_group (image_menu_item, g_value_get_object (value));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gtk_image_menu_item_get_property (GObject *object,
260 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
265 g_value_set_object (value, gtk_image_menu_item_get_image (image_menu_item));
268 g_value_set_boolean (value, gtk_image_menu_item_get_use_stock (image_menu_item));
270 case PROP_ALWAYS_SHOW_IMAGE:
271 g_value_set_boolean (value, gtk_image_menu_item_get_always_show_image (image_menu_item));
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 show_image (GtkImageMenuItem *image_menu_item)
282 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
283 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
286 if (priv->always_show_image)
289 g_object_get (settings, "gtk-menu-images", &show, NULL);
295 gtk_image_menu_item_map (GtkWidget *widget)
297 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
298 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
300 GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->map (widget);
303 g_object_set (priv->image,
304 "visible", show_image (image_menu_item),
309 gtk_image_menu_item_destroy (GtkObject *object)
311 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
312 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
315 gtk_container_remove (GTK_CONTAINER (image_menu_item),
318 GTK_OBJECT_CLASS (gtk_image_menu_item_parent_class)->destroy (object);
322 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
325 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
326 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
327 GtkPackDirection pack_dir;
329 GtkWidget *widget = GTK_WIDGET (menu_item);
331 parent = gtk_widget_get_parent (widget);
333 if (GTK_IS_MENU_BAR (parent))
334 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
336 pack_dir = GTK_PACK_DIRECTION_LTR;
340 if (priv->image && gtk_widget_get_visible (priv->image))
342 GtkRequisition image_requisition;
343 guint toggle_spacing;
344 gtk_widget_get_child_requisition (priv->image,
347 gtk_widget_style_get (GTK_WIDGET (menu_item),
348 "toggle-spacing", &toggle_spacing,
351 if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
353 if (image_requisition.width > 0)
354 *requisition = image_requisition.width + toggle_spacing;
358 if (image_requisition.height > 0)
359 *requisition = image_requisition.height + toggle_spacing;
365 gtk_image_menu_item_recalculate (GtkImageMenuItem *image_menu_item)
367 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
368 GtkStockItem stock_item;
370 const gchar *resolved_label = priv->label;
372 if (priv->use_stock && priv->label)
377 image = gtk_image_new_from_stock (priv->label, GTK_ICON_SIZE_MENU);
378 gtk_image_menu_item_set_image (image_menu_item, image);
381 if (gtk_stock_lookup (priv->label, &stock_item))
382 resolved_label = stock_item.label;
384 gtk_menu_item_set_use_underline (GTK_MENU_ITEM (image_menu_item), TRUE);
388 (gtk_image_menu_item_parent_class)->set_label (GTK_MENU_ITEM (image_menu_item), resolved_label);
393 gtk_image_menu_item_set_label (GtkMenuItem *menu_item,
396 GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
398 if (priv->label != label)
400 g_free (priv->label);
401 priv->label = g_strdup (label);
403 gtk_image_menu_item_recalculate (GTK_IMAGE_MENU_ITEM (menu_item));
405 g_object_notify (G_OBJECT (menu_item), "label");
410 static G_CONST_RETURN gchar *
411 gtk_image_menu_item_get_label (GtkMenuItem *menu_item)
413 GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
419 gtk_image_menu_item_size_request (GtkWidget *widget,
420 GtkRequisition *requisition)
422 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
423 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
424 gint child_width = 0;
425 gint child_height = 0;
426 GtkPackDirection pack_dir;
429 parent = gtk_widget_get_parent (widget);
431 if (GTK_IS_MENU_BAR (parent))
432 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
434 pack_dir = GTK_PACK_DIRECTION_LTR;
436 if (priv->image && gtk_widget_get_visible (priv->image))
438 GtkRequisition child_requisition;
440 gtk_widget_size_request (priv->image,
443 child_width = child_requisition.width;
444 child_height = child_requisition.height;
447 GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_request (widget, requisition);
449 /* not done with height since that happens via the
450 * toggle_size_request
452 if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
453 requisition->height = MAX (requisition->height, child_height);
455 requisition->width = MAX (requisition->width, child_width);
458 /* Note that GtkMenuShell always size requests before
459 * toggle_size_request, so toggle_size_request will be able to use
460 * priv->image->requisition
465 gtk_image_menu_item_size_allocate (GtkWidget *widget,
466 GtkAllocation *allocation)
468 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
469 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
470 GtkAllocation widget_allocation;
471 GtkPackDirection pack_dir;
474 parent = gtk_widget_get_parent (widget);
476 if (GTK_IS_MENU_BAR (parent))
477 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
479 pack_dir = GTK_PACK_DIRECTION_LTR;
481 GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate (widget, allocation);
483 if (priv->image && gtk_widget_get_visible (priv->image))
486 GtkRequisition child_requisition;
487 GtkAllocation child_allocation;
488 guint horizontal_padding, toggle_spacing;
490 gtk_widget_style_get (widget,
491 "horizontal-padding", &horizontal_padding,
492 "toggle-spacing", &toggle_spacing,
495 /* Man this is lame hardcoding action, but I can't
496 * come up with a solution that's really better.
499 gtk_widget_get_child_requisition (priv->image,
502 gtk_widget_get_allocation (widget, &widget_allocation);
504 if (pack_dir == GTK_PACK_DIRECTION_LTR ||
505 pack_dir == GTK_PACK_DIRECTION_RTL)
507 offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
508 gtk_widget_get_style (widget)->xthickness;
510 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
511 (pack_dir == GTK_PACK_DIRECTION_LTR))
512 x = offset + horizontal_padding +
513 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
514 toggle_spacing - child_requisition.width) / 2;
516 x = widget_allocation.width - offset - horizontal_padding -
517 GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
518 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
519 toggle_spacing - child_requisition.width) / 2;
521 y = (widget_allocation.height - child_requisition.height) / 2;
525 offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
526 gtk_widget_get_style (widget)->ythickness;
528 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
529 (pack_dir == GTK_PACK_DIRECTION_TTB))
530 y = offset + horizontal_padding +
531 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
532 toggle_spacing - child_requisition.height) / 2;
534 y = widget_allocation.height - offset - horizontal_padding -
535 GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
536 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
537 toggle_spacing - child_requisition.height) / 2;
539 x = (widget_allocation.width - child_requisition.width) / 2;
542 child_allocation.width = child_requisition.width;
543 child_allocation.height = child_requisition.height;
544 child_allocation.x = widget_allocation.x + MAX (x, 0);
545 child_allocation.y = widget_allocation.y + MAX (y, 0);
547 gtk_widget_size_allocate (priv->image, &child_allocation);
552 gtk_image_menu_item_forall (GtkContainer *container,
553 gboolean include_internals,
554 GtkCallback callback,
555 gpointer callback_data)
557 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
558 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
560 GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall (container,
565 if (include_internals && priv->image)
566 (* callback) (priv->image, callback_data);
571 gtk_image_menu_item_activatable_interface_init (GtkActivatableIface *iface)
573 parent_activatable_iface = g_type_interface_peek_parent (iface);
574 iface->update = gtk_image_menu_item_update;
575 iface->sync_action_properties = gtk_image_menu_item_sync_action_properties;
579 activatable_update_stock_id (GtkImageMenuItem *image_menu_item, GtkAction *action)
582 const gchar *stock_id = gtk_action_get_stock_id (action);
584 image = gtk_image_menu_item_get_image (image_menu_item);
586 if (GTK_IS_IMAGE (image) &&
587 stock_id && gtk_icon_factory_lookup_default (stock_id))
589 gtk_image_set_from_stock (GTK_IMAGE (image), stock_id, GTK_ICON_SIZE_MENU);
597 activatable_update_gicon (GtkImageMenuItem *image_menu_item, GtkAction *action)
600 GIcon *icon = gtk_action_get_gicon (action);
601 const gchar *stock_id = gtk_action_get_stock_id (action);
603 image = gtk_image_menu_item_get_image (image_menu_item);
605 if (icon && GTK_IS_IMAGE (image) &&
606 !(stock_id && gtk_icon_factory_lookup_default (stock_id)))
608 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_MENU);
616 activatable_update_icon_name (GtkImageMenuItem *image_menu_item, GtkAction *action)
619 const gchar *icon_name = gtk_action_get_icon_name (action);
621 image = gtk_image_menu_item_get_image (image_menu_item);
623 if (GTK_IS_IMAGE (image) &&
624 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
625 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
627 gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name, GTK_ICON_SIZE_MENU);
632 gtk_image_menu_item_update (GtkActivatable *activatable,
634 const gchar *property_name)
636 GtkImageMenuItem *image_menu_item;
637 gboolean use_appearance;
639 image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
641 parent_activatable_iface->update (activatable, action, property_name);
643 use_appearance = gtk_activatable_get_use_action_appearance (activatable);
647 if (strcmp (property_name, "stock-id") == 0)
648 activatable_update_stock_id (image_menu_item, action);
649 else if (strcmp (property_name, "gicon") == 0)
650 activatable_update_gicon (image_menu_item, action);
651 else if (strcmp (property_name, "icon-name") == 0)
652 activatable_update_icon_name (image_menu_item, action);
656 gtk_image_menu_item_sync_action_properties (GtkActivatable *activatable,
659 GtkImageMenuItem *image_menu_item;
661 gboolean use_appearance;
663 image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
665 parent_activatable_iface->sync_action_properties (activatable, action);
670 use_appearance = gtk_activatable_get_use_action_appearance (activatable);
674 image = gtk_image_menu_item_get_image (image_menu_item);
675 if (image && !GTK_IS_IMAGE (image))
677 gtk_image_menu_item_set_image (image_menu_item, NULL);
683 image = gtk_image_new ();
684 gtk_widget_show (image);
685 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (activatable),
689 if (!activatable_update_stock_id (image_menu_item, action) &&
690 !activatable_update_gicon (image_menu_item, action))
691 activatable_update_icon_name (image_menu_item, action);
693 gtk_image_menu_item_set_always_show_image (image_menu_item,
694 gtk_action_get_always_show_image (action));
699 * gtk_image_menu_item_new:
700 * @returns: a new #GtkImageMenuItem.
702 * Creates a new #GtkImageMenuItem with an empty label.
705 gtk_image_menu_item_new (void)
707 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
711 * gtk_image_menu_item_new_with_label:
712 * @label: the text of the menu item.
713 * @returns: a new #GtkImageMenuItem.
715 * Creates a new #GtkImageMenuItem containing a label.
718 gtk_image_menu_item_new_with_label (const gchar *label)
720 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
727 * gtk_image_menu_item_new_with_mnemonic:
728 * @label: the text of the menu item, with an underscore in front of the
730 * @returns: a new #GtkImageMenuItem
732 * Creates a new #GtkImageMenuItem containing a label. The label
733 * will be created using gtk_label_new_with_mnemonic(), so underscores
734 * in @label indicate the mnemonic for the menu item.
737 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
739 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
740 "use-underline", TRUE,
746 * gtk_image_menu_item_new_from_stock:
747 * @stock_id: the name of the stock item.
748 * @accel_group: (allow-none): the #GtkAccelGroup to add the menu items
749 * accelerator to, or %NULL.
750 * @returns: a new #GtkImageMenuItem.
752 * Creates a new #GtkImageMenuItem containing the image and text from a
753 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
754 * and #GTK_STOCK_APPLY.
756 * If you want this menu item to have changeable accelerators, then pass in
757 * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
758 * appropriate path for the menu item, use gtk_stock_lookup() to look up the
759 * standard accelerator for the stock item, and if one is found, call
760 * gtk_accel_map_add_entry() to register it.
763 gtk_image_menu_item_new_from_stock (const gchar *stock_id,
764 GtkAccelGroup *accel_group)
766 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
769 "accel-group", accel_group,
774 * gtk_image_menu_item_set_use_stock:
775 * @image_menu_item: a #GtkImageMenuItem
776 * @use_stock: %TRUE if the menuitem should use a stock item
778 * If %TRUE, the label set in the menuitem is used as a
779 * stock id to select the stock item for the item.
784 gtk_image_menu_item_set_use_stock (GtkImageMenuItem *image_menu_item,
787 GtkImageMenuItemPrivate *priv;
789 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
791 priv = image_menu_item->priv;
793 if (priv->use_stock != use_stock)
795 priv->use_stock = use_stock;
797 gtk_image_menu_item_recalculate (image_menu_item);
799 g_object_notify (G_OBJECT (image_menu_item), "use-stock");
804 * gtk_image_menu_item_get_use_stock:
805 * @image_menu_item: a #GtkImageMenuItem
807 * Checks whether the label set in the menuitem is used as a
808 * stock id to select the stock item for the item.
810 * Returns: %TRUE if the label set in the menuitem is used as a
811 * stock id to select the stock item for the item
816 gtk_image_menu_item_get_use_stock (GtkImageMenuItem *image_menu_item)
818 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
820 return image_menu_item->priv->use_stock;
824 * gtk_image_menu_item_set_always_show_image:
825 * @image_menu_item: a #GtkImageMenuItem
826 * @always_show: %TRUE if the menuitem should always show the image
828 * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images
829 * setting and always show the image, if available.
831 * Use this property if the menuitem would be useless or hard to use
837 gtk_image_menu_item_set_always_show_image (GtkImageMenuItem *image_menu_item,
838 gboolean always_show)
840 GtkImageMenuItemPrivate *priv;
842 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
844 priv = image_menu_item->priv;
846 if (priv->always_show_image != always_show)
848 priv->always_show_image = always_show;
852 if (show_image (image_menu_item))
853 gtk_widget_show (priv->image);
855 gtk_widget_hide (priv->image);
858 g_object_notify (G_OBJECT (image_menu_item), "always-show-image");
863 * gtk_image_menu_item_get_always_show_image:
864 * @image_menu_item: a #GtkImageMenuItem
866 * Returns whether the menu item will ignore the #GtkSettings:gtk-menu-images
867 * setting and always show the image, if available.
869 * Returns: %TRUE if the menu item will always show the image
874 gtk_image_menu_item_get_always_show_image (GtkImageMenuItem *image_menu_item)
876 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
878 return image_menu_item->priv->always_show_image;
883 * gtk_image_menu_item_set_accel_group:
884 * @image_menu_item: a #GtkImageMenuItem
885 * @accel_group: the #GtkAccelGroup
887 * Specifies an @accel_group to add the menu items accelerator to
888 * (this only applies to stock items so a stock item must already
889 * be set, make sure to call gtk_image_menu_item_set_use_stock()
890 * and gtk_menu_item_set_label() with a valid stock item first).
892 * If you want this menu item to have changeable accelerators then
893 * you shouldnt need this (see gtk_image_menu_item_new_from_stock()).
898 gtk_image_menu_item_set_accel_group (GtkImageMenuItem *image_menu_item,
899 GtkAccelGroup *accel_group)
901 GtkImageMenuItemPrivate *priv;
902 GtkStockItem stock_item;
904 /* Silent return for the constructor */
908 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
909 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
911 priv = image_menu_item->priv;
913 if (priv->use_stock && priv->label && gtk_stock_lookup (priv->label, &stock_item))
914 if (stock_item.keyval)
916 gtk_widget_add_accelerator (GTK_WIDGET (image_menu_item),
923 g_object_notify (G_OBJECT (image_menu_item), "accel-group");
928 * gtk_image_menu_item_set_image:
929 * @image_menu_item: a #GtkImageMenuItem.
930 * @image: (allow-none): a widget to set as the image for the menu item.
932 * Sets the image of @image_menu_item to the given widget.
933 * Note that it depends on the show-menu-images setting whether
934 * the image will be displayed or not.
937 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
940 GtkImageMenuItemPrivate *priv;
942 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
944 priv = image_menu_item->priv;
946 if (image == priv->image)
950 gtk_container_remove (GTK_CONTAINER (image_menu_item),
958 gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
960 "visible", show_image (image_menu_item),
964 g_object_notify (G_OBJECT (image_menu_item), "image");
968 * gtk_image_menu_item_get_image:
969 * @image_menu_item: a #GtkImageMenuItem.
970 * @returns: the widget set as image of @image_menu_item.
972 * Gets the widget that is currently set as the image of @image_menu_item.
973 * See gtk_image_menu_item_set_image().
976 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
978 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
980 return image_menu_item->priv->image;
984 gtk_image_menu_item_remove (GtkContainer *container,
987 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
988 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
990 if (child == priv->image)
992 gboolean widget_was_visible;
994 widget_was_visible = gtk_widget_get_visible (child);
996 gtk_widget_unparent (child);
999 if (widget_was_visible &&
1000 gtk_widget_get_visible (GTK_WIDGET (container)))
1001 gtk_widget_queue_resize (GTK_WIDGET (container));
1003 g_object_notify (G_OBJECT (image_menu_item), "image");
1007 GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove (container, child);
1012 show_image_change_notify (GtkImageMenuItem *image_menu_item)
1014 GtkImageMenuItemPrivate *priv = image_menu_item->priv;
1018 if (show_image (image_menu_item))
1019 gtk_widget_show (priv->image);
1021 gtk_widget_hide (priv->image);
1026 traverse_container (GtkWidget *widget,
1029 if (GTK_IS_IMAGE_MENU_ITEM (widget))
1030 show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
1031 else if (GTK_IS_CONTAINER (widget))
1032 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1036 gtk_image_menu_item_setting_changed (GtkSettings *settings)
1040 list = gtk_window_list_toplevels ();
1042 for (l = list; l; l = l->next)
1043 gtk_container_forall (GTK_CONTAINER (l->data),
1044 traverse_container, NULL);
1050 gtk_image_menu_item_screen_changed (GtkWidget *widget,
1051 GdkScreen *previous_screen)
1053 GtkSettings *settings;
1054 guint show_image_connection;
1056 if (!gtk_widget_has_screen (widget))
1059 settings = gtk_widget_get_settings (widget);
1061 show_image_connection =
1062 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1063 "gtk-image-menu-item-connection"));
1065 if (show_image_connection)
1068 show_image_connection =
1069 g_signal_connect (settings, "notify::gtk-menu-images",
1070 G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
1071 g_object_set_data (G_OBJECT (settings),
1072 I_("gtk-image-menu-item-connection"),
1073 GUINT_TO_POINTER (show_image_connection));
1075 show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));