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 _GtkImageMenuItemPriv
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 (GtkImageMenuItemPriv));
201 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
203 GtkImageMenuItemPriv *priv;
205 image_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (image_menu_item,
206 GTK_TYPE_IMAGE_MENU_ITEM,
207 GtkImageMenuItemPriv);
208 priv = image_menu_item->priv;
211 priv->use_stock = FALSE;
216 gtk_image_menu_item_finalize (GObject *object)
218 GtkImageMenuItemPriv *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 GtkImageMenuItemPriv *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 GtkImageMenuItemPriv *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 GtkImageMenuItemPriv *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 GtkImageMenuItemPriv *priv = image_menu_item->priv;
327 GtkPackDirection pack_dir;
329 if (GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
330 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (GTK_WIDGET (menu_item)->parent));
332 pack_dir = GTK_PACK_DIRECTION_LTR;
336 if (priv->image && gtk_widget_get_visible (priv->image))
338 GtkRequisition image_requisition;
339 guint toggle_spacing;
340 gtk_widget_get_child_requisition (priv->image,
343 gtk_widget_style_get (GTK_WIDGET (menu_item),
344 "toggle-spacing", &toggle_spacing,
347 if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
349 if (image_requisition.width > 0)
350 *requisition = image_requisition.width + toggle_spacing;
354 if (image_requisition.height > 0)
355 *requisition = image_requisition.height + toggle_spacing;
361 gtk_image_menu_item_recalculate (GtkImageMenuItem *image_menu_item)
363 GtkImageMenuItemPriv *priv = image_menu_item->priv;
364 GtkStockItem stock_item;
366 const gchar *resolved_label = priv->label;
368 if (priv->use_stock && priv->label)
373 image = gtk_image_new_from_stock (priv->label, GTK_ICON_SIZE_MENU);
374 gtk_image_menu_item_set_image (image_menu_item, image);
377 if (gtk_stock_lookup (priv->label, &stock_item))
378 resolved_label = stock_item.label;
380 gtk_menu_item_set_use_underline (GTK_MENU_ITEM (image_menu_item), TRUE);
384 (gtk_image_menu_item_parent_class)->set_label (GTK_MENU_ITEM (image_menu_item), resolved_label);
389 gtk_image_menu_item_set_label (GtkMenuItem *menu_item,
392 GtkImageMenuItemPriv *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
394 if (priv->label != label)
396 g_free (priv->label);
397 priv->label = g_strdup (label);
399 gtk_image_menu_item_recalculate (GTK_IMAGE_MENU_ITEM (menu_item));
401 g_object_notify (G_OBJECT (menu_item), "label");
406 static G_CONST_RETURN gchar *
407 gtk_image_menu_item_get_label (GtkMenuItem *menu_item)
409 GtkImageMenuItemPriv *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
415 gtk_image_menu_item_size_request (GtkWidget *widget,
416 GtkRequisition *requisition)
418 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
419 GtkImageMenuItemPriv *priv = image_menu_item->priv;
420 gint child_width = 0;
421 gint child_height = 0;
422 GtkPackDirection pack_dir;
424 if (GTK_IS_MENU_BAR (widget->parent))
425 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
427 pack_dir = GTK_PACK_DIRECTION_LTR;
429 if (priv->image && gtk_widget_get_visible (priv->image))
431 GtkRequisition child_requisition;
433 gtk_widget_size_request (priv->image,
436 child_width = child_requisition.width;
437 child_height = child_requisition.height;
440 GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_request (widget, requisition);
442 /* not done with height since that happens via the
443 * toggle_size_request
445 if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
446 requisition->height = MAX (requisition->height, child_height);
448 requisition->width = MAX (requisition->width, child_width);
451 /* Note that GtkMenuShell always size requests before
452 * toggle_size_request, so toggle_size_request will be able to use
453 * priv->image->requisition
458 gtk_image_menu_item_size_allocate (GtkWidget *widget,
459 GtkAllocation *allocation)
461 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
462 GtkImageMenuItemPriv *priv = image_menu_item->priv;
463 GtkPackDirection pack_dir;
465 if (GTK_IS_MENU_BAR (widget->parent))
466 pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
468 pack_dir = GTK_PACK_DIRECTION_LTR;
470 GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate (widget, allocation);
472 if (priv->image && gtk_widget_get_visible (priv->image))
475 GtkRequisition child_requisition;
476 GtkAllocation child_allocation;
477 guint horizontal_padding, toggle_spacing;
479 gtk_widget_style_get (widget,
480 "horizontal-padding", &horizontal_padding,
481 "toggle-spacing", &toggle_spacing,
484 /* Man this is lame hardcoding action, but I can't
485 * come up with a solution that's really better.
488 gtk_widget_get_child_requisition (priv->image,
491 if (pack_dir == GTK_PACK_DIRECTION_LTR ||
492 pack_dir == GTK_PACK_DIRECTION_RTL)
494 offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
495 widget->style->xthickness;
497 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
498 (pack_dir == GTK_PACK_DIRECTION_LTR))
499 x = offset + horizontal_padding +
500 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
501 toggle_spacing - child_requisition.width) / 2;
503 x = widget->allocation.width - offset - horizontal_padding -
504 GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
505 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
506 toggle_spacing - child_requisition.width) / 2;
508 y = (widget->allocation.height - child_requisition.height) / 2;
512 offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
513 widget->style->ythickness;
515 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
516 (pack_dir == GTK_PACK_DIRECTION_TTB))
517 y = offset + horizontal_padding +
518 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
519 toggle_spacing - child_requisition.height) / 2;
521 y = widget->allocation.height - offset - horizontal_padding -
522 GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
523 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
524 toggle_spacing - child_requisition.height) / 2;
526 x = (widget->allocation.width - child_requisition.width) / 2;
529 child_allocation.width = child_requisition.width;
530 child_allocation.height = child_requisition.height;
531 child_allocation.x = widget->allocation.x + MAX (x, 0);
532 child_allocation.y = widget->allocation.y + MAX (y, 0);
534 gtk_widget_size_allocate (priv->image, &child_allocation);
539 gtk_image_menu_item_forall (GtkContainer *container,
540 gboolean include_internals,
541 GtkCallback callback,
542 gpointer callback_data)
544 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
545 GtkImageMenuItemPriv *priv = image_menu_item->priv;
547 GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall (container,
552 if (include_internals && priv->image)
553 (* callback) (priv->image, callback_data);
558 gtk_image_menu_item_activatable_interface_init (GtkActivatableIface *iface)
560 parent_activatable_iface = g_type_interface_peek_parent (iface);
561 iface->update = gtk_image_menu_item_update;
562 iface->sync_action_properties = gtk_image_menu_item_sync_action_properties;
566 activatable_update_stock_id (GtkImageMenuItem *image_menu_item, GtkAction *action)
569 const gchar *stock_id = gtk_action_get_stock_id (action);
571 image = gtk_image_menu_item_get_image (image_menu_item);
573 if (GTK_IS_IMAGE (image) &&
574 stock_id && gtk_icon_factory_lookup_default (stock_id))
576 gtk_image_set_from_stock (GTK_IMAGE (image), stock_id, GTK_ICON_SIZE_MENU);
584 activatable_update_gicon (GtkImageMenuItem *image_menu_item, GtkAction *action)
587 GIcon *icon = gtk_action_get_gicon (action);
588 const gchar *stock_id = gtk_action_get_stock_id (action);
590 image = gtk_image_menu_item_get_image (image_menu_item);
592 if (icon && GTK_IS_IMAGE (image) &&
593 !(stock_id && gtk_icon_factory_lookup_default (stock_id)))
595 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_MENU);
603 activatable_update_icon_name (GtkImageMenuItem *image_menu_item, GtkAction *action)
606 const gchar *icon_name = gtk_action_get_icon_name (action);
608 image = gtk_image_menu_item_get_image (image_menu_item);
610 if (GTK_IS_IMAGE (image) &&
611 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
612 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
614 gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name, GTK_ICON_SIZE_MENU);
619 gtk_image_menu_item_update (GtkActivatable *activatable,
621 const gchar *property_name)
623 GtkImageMenuItem *image_menu_item;
624 gboolean use_appearance;
626 image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
628 parent_activatable_iface->update (activatable, action, property_name);
630 use_appearance = gtk_activatable_get_use_action_appearance (activatable);
634 if (strcmp (property_name, "stock-id") == 0)
635 activatable_update_stock_id (image_menu_item, action);
636 else if (strcmp (property_name, "gicon") == 0)
637 activatable_update_gicon (image_menu_item, action);
638 else if (strcmp (property_name, "icon-name") == 0)
639 activatable_update_icon_name (image_menu_item, action);
643 gtk_image_menu_item_sync_action_properties (GtkActivatable *activatable,
646 GtkImageMenuItem *image_menu_item;
648 gboolean use_appearance;
650 image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
652 parent_activatable_iface->sync_action_properties (activatable, action);
657 use_appearance = gtk_activatable_get_use_action_appearance (activatable);
661 image = gtk_image_menu_item_get_image (image_menu_item);
662 if (image && !GTK_IS_IMAGE (image))
664 gtk_image_menu_item_set_image (image_menu_item, NULL);
670 image = gtk_image_new ();
671 gtk_widget_show (image);
672 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (activatable),
676 if (!activatable_update_stock_id (image_menu_item, action) &&
677 !activatable_update_gicon (image_menu_item, action))
678 activatable_update_icon_name (image_menu_item, action);
680 gtk_image_menu_item_set_always_show_image (image_menu_item,
681 gtk_action_get_always_show_image (action));
686 * gtk_image_menu_item_new:
687 * @returns: a new #GtkImageMenuItem.
689 * Creates a new #GtkImageMenuItem with an empty label.
692 gtk_image_menu_item_new (void)
694 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
698 * gtk_image_menu_item_new_with_label:
699 * @label: the text of the menu item.
700 * @returns: a new #GtkImageMenuItem.
702 * Creates a new #GtkImageMenuItem containing a label.
705 gtk_image_menu_item_new_with_label (const gchar *label)
707 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
714 * gtk_image_menu_item_new_with_mnemonic:
715 * @label: the text of the menu item, with an underscore in front of the
717 * @returns: a new #GtkImageMenuItem
719 * Creates a new #GtkImageMenuItem containing a label. The label
720 * will be created using gtk_label_new_with_mnemonic(), so underscores
721 * in @label indicate the mnemonic for the menu item.
724 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
726 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
727 "use-underline", TRUE,
733 * gtk_image_menu_item_new_from_stock:
734 * @stock_id: the name of the stock item.
735 * @accel_group: (allow-none): the #GtkAccelGroup to add the menu items
736 * accelerator to, or %NULL.
737 * @returns: a new #GtkImageMenuItem.
739 * Creates a new #GtkImageMenuItem containing the image and text from a
740 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
741 * and #GTK_STOCK_APPLY.
743 * If you want this menu item to have changeable accelerators, then pass in
744 * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
745 * appropriate path for the menu item, use gtk_stock_lookup() to look up the
746 * standard accelerator for the stock item, and if one is found, call
747 * gtk_accel_map_add_entry() to register it.
750 gtk_image_menu_item_new_from_stock (const gchar *stock_id,
751 GtkAccelGroup *accel_group)
753 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
756 "accel-group", accel_group,
761 * gtk_image_menu_item_set_use_stock:
762 * @image_menu_item: a #GtkImageMenuItem
763 * @use_stock: %TRUE if the menuitem should use a stock item
765 * If %TRUE, the label set in the menuitem is used as a
766 * stock id to select the stock item for the item.
771 gtk_image_menu_item_set_use_stock (GtkImageMenuItem *image_menu_item,
774 GtkImageMenuItemPriv *priv;
776 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
778 priv = image_menu_item->priv;
780 if (priv->use_stock != use_stock)
782 priv->use_stock = use_stock;
784 gtk_image_menu_item_recalculate (image_menu_item);
786 g_object_notify (G_OBJECT (image_menu_item), "use-stock");
791 * gtk_image_menu_item_get_use_stock:
792 * @image_menu_item: a #GtkImageMenuItem
794 * Checks whether the label set in the menuitem is used as a
795 * stock id to select the stock item for the item.
797 * Returns: %TRUE if the label set in the menuitem is used as a
798 * stock id to select the stock item for the item
803 gtk_image_menu_item_get_use_stock (GtkImageMenuItem *image_menu_item)
805 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
807 return image_menu_item->priv->use_stock;
811 * gtk_image_menu_item_set_always_show_image:
812 * @image_menu_item: a #GtkImageMenuItem
813 * @always_show: %TRUE if the menuitem should always show the image
815 * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images
816 * setting and always show the image, if available.
818 * Use this property if the menuitem would be useless or hard to use
824 gtk_image_menu_item_set_always_show_image (GtkImageMenuItem *image_menu_item,
825 gboolean always_show)
827 GtkImageMenuItemPriv *priv;
829 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
831 priv = image_menu_item->priv;
833 if (priv->always_show_image != always_show)
835 priv->always_show_image = always_show;
839 if (show_image (image_menu_item))
840 gtk_widget_show (priv->image);
842 gtk_widget_hide (priv->image);
845 g_object_notify (G_OBJECT (image_menu_item), "always-show-image");
850 * gtk_image_menu_item_get_always_show_image:
851 * @image_menu_item: a #GtkImageMenuItem
853 * Returns whether the menu item will ignore the #GtkSettings:gtk-menu-images
854 * setting and always show the image, if available.
856 * Returns: %TRUE if the menu item will always show the image
861 gtk_image_menu_item_get_always_show_image (GtkImageMenuItem *image_menu_item)
863 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
865 return image_menu_item->priv->always_show_image;
870 * gtk_image_menu_item_set_accel_group:
871 * @image_menu_item: a #GtkImageMenuItem
872 * @accel_group: the #GtkAccelGroup
874 * Specifies an @accel_group to add the menu items accelerator to
875 * (this only applies to stock items so a stock item must already
876 * be set, make sure to call gtk_image_menu_item_set_use_stock()
877 * and gtk_menu_item_set_label() with a valid stock item first).
879 * If you want this menu item to have changeable accelerators then
880 * you shouldnt need this (see gtk_image_menu_item_new_from_stock()).
885 gtk_image_menu_item_set_accel_group (GtkImageMenuItem *image_menu_item,
886 GtkAccelGroup *accel_group)
888 GtkImageMenuItemPriv *priv;
889 GtkStockItem stock_item;
891 /* Silent return for the constructor */
895 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
896 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
898 priv = image_menu_item->priv;
900 if (priv->use_stock && priv->label && gtk_stock_lookup (priv->label, &stock_item))
901 if (stock_item.keyval)
903 gtk_widget_add_accelerator (GTK_WIDGET (image_menu_item),
910 g_object_notify (G_OBJECT (image_menu_item), "accel-group");
915 * gtk_image_menu_item_set_image:
916 * @image_menu_item: a #GtkImageMenuItem.
917 * @image: (allow-none): a widget to set as the image for the menu item.
919 * Sets the image of @image_menu_item to the given widget.
920 * Note that it depends on the show-menu-images setting whether
921 * the image will be displayed or not.
924 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
927 GtkImageMenuItemPriv *priv;
929 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
931 priv = image_menu_item->priv;
933 if (image == priv->image)
937 gtk_container_remove (GTK_CONTAINER (image_menu_item),
945 gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
947 "visible", show_image (image_menu_item),
951 g_object_notify (G_OBJECT (image_menu_item), "image");
955 * gtk_image_menu_item_get_image:
956 * @image_menu_item: a #GtkImageMenuItem.
957 * @returns: the widget set as image of @image_menu_item.
959 * Gets the widget that is currently set as the image of @image_menu_item.
960 * See gtk_image_menu_item_set_image().
963 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
965 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
967 return image_menu_item->priv->image;
971 gtk_image_menu_item_remove (GtkContainer *container,
974 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
975 GtkImageMenuItemPriv *priv = image_menu_item->priv;
977 if (child == priv->image)
979 gboolean widget_was_visible;
981 widget_was_visible = gtk_widget_get_visible (child);
983 gtk_widget_unparent (child);
986 if (widget_was_visible &&
987 gtk_widget_get_visible (GTK_WIDGET (container)))
988 gtk_widget_queue_resize (GTK_WIDGET (container));
990 g_object_notify (G_OBJECT (image_menu_item), "image");
994 GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove (container, child);
999 show_image_change_notify (GtkImageMenuItem *image_menu_item)
1001 GtkImageMenuItemPriv *priv = image_menu_item->priv;
1005 if (show_image (image_menu_item))
1006 gtk_widget_show (priv->image);
1008 gtk_widget_hide (priv->image);
1013 traverse_container (GtkWidget *widget,
1016 if (GTK_IS_IMAGE_MENU_ITEM (widget))
1017 show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
1018 else if (GTK_IS_CONTAINER (widget))
1019 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1023 gtk_image_menu_item_setting_changed (GtkSettings *settings)
1027 list = gtk_window_list_toplevels ();
1029 for (l = list; l; l = l->next)
1030 gtk_container_forall (GTK_CONTAINER (l->data),
1031 traverse_container, NULL);
1037 gtk_image_menu_item_screen_changed (GtkWidget *widget,
1038 GdkScreen *previous_screen)
1040 GtkSettings *settings;
1041 guint show_image_connection;
1043 if (!gtk_widget_has_screen (widget))
1046 settings = gtk_widget_get_settings (widget);
1048 show_image_connection =
1049 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1050 "gtk-image-menu-item-connection"));
1052 if (show_image_connection)
1055 show_image_connection =
1056 g_signal_connect (settings, "notify::gtk-menu-images",
1057 G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
1058 g_object_set_data (G_OBJECT (settings),
1059 I_("gtk-image-menu-item-connection"),
1060 GUINT_TO_POINTER (show_image_connection));
1062 show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));