|« Main Page | Index|
Table of Contents
Gtk::MenuItem and its derived widgets are the only valid children for menus. Their function is to correctly handle highlighting, alignment, events and submenus. As MenuItem derives from Gtk::Bin, a menu item can hold any valid child widget, although only a few are really useful.
You can create a MenuItem with one of the following constructors:
The 'label' argument is a text string to use for the label. The 'submenu' argument is a popup menu to display when the menu item is activated. If 'use_underline' is true the label string is parsed for an underscore preceding the mnemonic character.
The menu item label can be set at any time with this method:
A popup submenu can be set, retrieved and removed by calling the following methods respectively:
To have a menu item appear justified at the right side of a menu bar call:
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.)
Menu items emit four signals: activate, activate_item, toggle_size_request and toggle_size_allocate. The main signal applications use is the 'activate' signal. You can connect to the activate signal like this:
where the 'activate_handler' has the following prototype:
Most of the time you wont need to connect to the activate signal as this gets done for you when you call one of the Gtk::MenuShell methods: append, prepend or insert and pass it a slot, like this:
You can create a CheckMenuItem with one of the following constructors:
You will need to set and get the active state of the menu item. This can be done with the following methods respectively:
The get_active() method returns true if the check menu item is currently in its active state.
If you want to indicate to the user an intermediate state, neither off nor on, you have to do it manually by calling:
The get_inconsistent() method returns true if the check menu item is currently in an intermediate state.
The 'toggled' signal is emitted when the state of the check box is changed. You can connect to the toggled signal like this:
where 'toggled_handler' has the following prototype:
The 'image' argument is the image widget for the menu item. The 'stock_id' argument is the stock item to use to set the menu item image and text. The 'accel_group' argument is the accelerator group to add the stock item's accelerator to. The other arguments are the same as those for the menu items above:
You can set and retrieve the image for an ImageMenuItem with the following methods respectively:
You can create a RadioMenuItem by calling one of the following constructors:
The first constructor creates an empty radio menu item that starts a new group. The second constructor creates an empty radio menu item that is a member of the specified group. The last constructor creates a radio menu item that is a member of the specified group and displays 'label' as its text. If 'use_underline' is true the label string is parsed for an underscore preceding the mnemonic character.
If the 'group' argument is null a new group will be created for the radio menu item. This should be the case for the first radio menu item created in a group. Then for subsequent radio menu items you pass a pointer to the previous radio menu item created. This allows a chain of radio menu items to be established. The example below should make this clear.
You can set and get the group a radio menu item belongs to by calling the following methods respectively:
The 'group' argument is a pointer to a Group, which is just a typedef for a GSList.
It is also a good idea to explicitly set which radio menu item should be the active menu item for the group:
If 'is_active' is true the menu item will be active and the previously active menu tiem will become inactive.
You can create a separator menu item with the following constructor:
You can create a tearoff menu item with the following constructor:
To determine if a tearoff menu item is in its torn off state call:
The return value is true if the menu item has been torn off.