ImageMenuItem

A GtkImageMenuItem is a menu item which has an icon next to the text label.

This is functionally equivalent to:

GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
GtkWidget *icon = gtk_image_new_from_icon_name ("folder-music-symbolic", GTK_ICON_SIZE_MENU);
GtkWidget *label = gtk_label_new ("Music");
GtkWidget *menu_item = gtk_menu_item_new ();

gtk_container_add (GTK_CONTAINER (box), icon);
gtk_container_add (GTK_CONTAINER (box), label);

gtk_container_add (GTK_CONTAINER (menu_item), box);

gtk_widget_show_all (menu_item);

Note that the user may disable display of menu icons using the #GtkSettings:gtk-menu-images setting, so make sure to still fill in the text label. If you want to ensure that your menu items show an icon you are strongly encouraged to use a #GtkMenuItem with a #GtkImage instead.

#GtkImageMenuItem has been deprecated since GTK+ 3.10. If you want to display an icon in a menu item, you should use #GtkMenuItem and pack a #GtkBox with a #GtkImage and a #GtkLabel instead. You should also consider using #GtkBuilder and the XML #GMenu description for creating menus, by following the [GMenu guide]https://developer.gnome.org/GMenu/. You should consider using icons in menu items only sparingly, and for "objects" (or "nouns") elements only, like bookmarks, files, and links; "actions" (or "verbs") should not have icons.

Furthermore, if you would like to display keyboard accelerator, you must pack the accel label into the box using gtk.box.Box.packEnd and align the label, otherwise the accelerator will not display correctly. The following code snippet adds a keyboard accelerator to the menu item, with a key binding of Ctrl+M:

GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
GtkWidget *icon = gtk_image_new_from_icon_name ("folder-music-symbolic", GTK_ICON_SIZE_MENU);
GtkWidget *label = gtk_accel_label_new ("Music");
GtkWidget *menu_item = gtk_menu_item_new ();
GtkAccelGroup *accel_group = gtk_accel_group_new ();

gtk_container_add (GTK_CONTAINER (box), icon);

gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
gtk_label_set_xalign (GTK_LABEL (label), 0.0);

gtk_widget_add_accelerator (menu_item, "activate", accel_group,
                            GDK_KEY_m, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menu_item);

gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);

gtk_container_add (GTK_CONTAINER (menu_item), box);

gtk_widget_show_all (menu_item);

Constructors

this
this(void* ptr, Flag!"Take" take)
this
this()

Creates a new #GtkImageMenuItem with an empty label.

Members

Functions

getAlwaysShowImage
bool getAlwaysShowImage()

Returns whether the menu item will ignore the #GtkSettings:gtk-menu-images setting and always show the image, if available.

getImage
gtk.widget.Widget getImage()

Gets the widget that is currently set as the image of image_menu_item. See gtk.image_menu_item.ImageMenuItem.setImage.

getUseStock
bool getUseStock()

Checks whether the label set in the menuitem is used as a stock id to select the stock item for the item.

self
ImageMenuItem self()

Returns this, for use in with statements.

setAccelGroup
void setAccelGroup(gtk.accel_group.AccelGroup accelGroup)

Specifies an accel_group to add the menu items accelerator to (this only applies to stock items so a stock item must already be set, make sure to call gtk.image_menu_item.ImageMenuItem.setUseStock and gtk.menu_item.MenuItem.setLabel with a valid stock item first).

setAlwaysShowImage
void setAlwaysShowImage(bool alwaysShow)

If true, the menu item will ignore the #GtkSettings:gtk-menu-images setting and always show the image, if available.

setImage
void setImage(gtk.widget.Widget image)

Sets the image of image_menu_item to the given widget. Note that it depends on the show-menu-images setting whether the image will be displayed or not.

setUseStock
void setUseStock(bool useStock)

If true, the label set in the menuitem is used as a stock id to select the stock item for the item.

Properties

_gType
GType _gType [@property getter]
accelGroup
gtk.accel_group.AccelGroup accelGroup [@property setter]

Set accelGroup property.

alwaysShowImage
bool alwaysShowImage [@property getter]

Get alwaysShowImage property.

alwaysShowImage
bool alwaysShowImage [@property setter]

Set alwaysShowImage property.

image
gtk.widget.Widget image [@property getter]

Get image property.

image
gtk.widget.Widget image [@property setter]

Set image property.

useStock
bool useStock [@property getter]

Get useStock property.

useStock
bool useStock [@property setter]

Set useStock property.

Static functions

_getGType
GType _getGType()
builder
ImageMenuItemGidBuilder builder()

Get builder for gtk.image_menu_item.ImageMenuItem

newFromStock
gtk.image_menu_item.ImageMenuItem newFromStock(string stockId, gtk.accel_group.AccelGroup accelGroup)

Creates a new #GtkImageMenuItem containing the image and text from a stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK and #GTK_STOCK_APPLY.

newWithLabel
gtk.image_menu_item.ImageMenuItem newWithLabel(string label)

Creates a new #GtkImageMenuItem containing a label.

newWithMnemonic
gtk.image_menu_item.ImageMenuItem newWithMnemonic(string label)

Creates a new #GtkImageMenuItem containing a label. The label will be created using gtk.label.Label.newWithMnemonic, so underscores in label indicate the mnemonic for the menu item.

Inherited Members

From MenuItem

_getGType
GType _getGType()
_gType
GType _gType [@property getter]
self
MenuItem self()

Returns this, for use in with statements.

builder
MenuItemGidBuilder builder()

Get builder for gtk.menu_item.MenuItem

accelPath
string accelPath [@property getter]

Get accelPath property.

accelPath
string accelPath [@property setter]

Set accelPath property.

label
string label [@property getter]

Get label property.

label
string label [@property setter]

Set label property.

rightJustified
bool rightJustified [@property getter]

Get rightJustified property.

rightJustified
bool rightJustified [@property setter]

Set rightJustified property.

submenu
gtk.menu.Menu submenu [@property getter]

Get submenu property.

submenu
gtk.menu.Menu submenu [@property setter]

Set submenu property.

useUnderline
bool useUnderline [@property getter]

Get useUnderline property.

useUnderline
bool useUnderline [@property setter]

Set useUnderline property.

newWithLabel
gtk.menu_item.MenuItem newWithLabel(string label)

Creates a new #GtkMenuItem whose child is a #GtkLabel.

newWithMnemonic
gtk.menu_item.MenuItem newWithMnemonic(string label)

Creates a new #GtkMenuItem containing a label.

activate
void activate()

Emits the #GtkMenuItem::activate signal on the given item

deselect
void deselect()

Emits the #GtkMenuItem::deselect signal on the given item.

getAccelPath
string getAccelPath()

Retrieve the accelerator path that was previously set on menu_item.

getLabel
string getLabel()

Sets text on the menu_item label

getReserveIndicator
bool getReserveIndicator()

Returns whether the menu_item reserves space for the submenu indicator, regardless if it has a submenu or not.

getRightJustified
bool getRightJustified()

Gets whether the menu item appears justified at the right side of the menu bar.

getSubmenu
gtk.widget.Widget getSubmenu()

Gets the submenu underneath this menu item, if any. See gtk.menu_item.MenuItem.setSubmenu.

getUseUnderline
bool getUseUnderline()

Checks if an underline in the text indicates the next character should be used for the mnemonic accelerator key.

select
void select()

Emits the #GtkMenuItem::select signal on the given item.

setAccelPath
void setAccelPath(string accelPath)

Set the accelerator path on menu_item, through which runtime changes of the menu item’s accelerator caused by the user can be identified and saved to persistent storage (see gtk.accel_map.AccelMap.save on this). To set up a default accelerator for this menu item, call gtk.accel_map.AccelMap.addEntry with the same accel_path. See also gtk.accel_map.AccelMap.addEntry on the specifics of accelerator paths, and gtk.menu.Menu.setAccelPath for a more convenient variant of this function.

setLabel
void setLabel(string label)

Sets text on the menu_item label

setReserveIndicator
void setReserveIndicator(bool reserve)

Sets whether the menu_item should reserve space for the submenu indicator, regardless if it actually has a submenu or not.

setRightJustified
void setRightJustified(bool rightJustified)

Sets whether the menu item appears justified at the right side of a menu bar. This was traditionally done for “Help” menu items, but is now considered a bad idea. (If the widget layout is reversed for a right-to-left language like Hebrew or Arabic, right-justified-menu-items appear at the left.)

setSubmenu
void setSubmenu(gtk.menu.Menu submenu)

Sets or replaces the menu item’s submenu, or removes it when a null submenu is passed.

setUseUnderline
void setUseUnderline(bool setting)

If true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.

toggleSizeAllocate
void toggleSizeAllocate(int allocation)

Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.

toggleSizeRequest
void toggleSizeRequest(int requisition)

Emits the #GtkMenuItem::toggle-size-request signal on the given item.

connectActivate
gulong connectActivate(T callback, Flag!"After" after)

Connect to Activate signal.

connectActivateItem
gulong connectActivateItem(T callback, Flag!"After" after)

Connect to ActivateItem signal.

connectDeselect
gulong connectDeselect(T callback, Flag!"After" after)

Connect to Deselect signal.

connectSelect
gulong connectSelect(T callback, Flag!"After" after)

Connect to Select signal.

connectToggleSizeAllocate
gulong connectToggleSizeAllocate(T callback, Flag!"After" after)

Connect to ToggleSizeAllocate signal.

connectToggleSizeRequest
gulong connectToggleSizeRequest(T callback, Flag!"After" after)

Connect to ToggleSizeRequest signal.