## wxMenu

A menu is a popup (or pull down) list of items, one of which may be selected before the menu goes away (clicking elsewhere dismisses the menu). Menus may be used to construct either menu bars or popup menus.

A menu item has an integer ID associated with it which can be used to identify the selection, or to change the menu item in some way. A menu item with a special identifier -1 is a separator item and doesn't have an associated command but just makes a separator line appear in the menu.

Menu items may be either normal items, check items or radio items. Normal items don't have any special properties while the check items have a boolean flag associated to them and they show a checkmark in the menu when the flag is set. wxWindows automatically toggles the flag value when the item is clicked and its value may be retrieved using either IsChecked method of wxMenu or wxMenuBar itself or by using wxEvent::IsChecked when you get the menu notification for the item in question.

The radio items are similar to the check items except that all the other items in the same radio group are unchecked when a radio item is checked. The radio group is formed by a contiguous range of radio items, i.e. it starts at the first item of this kind and ends with the first item of a different kind (or the end of the menu). Notice that because the radio groups are defined in terms of the item positions inserting or removing the items in the menu containing the radio items risks to not work correctly. Finally note that the radio items are only supported under Windows and GTK+ currently.

Allocation strategy

All menus except the popup ones must be created on the heap. All menus attached to a menubar or to another menu will be deleted by their parent when it is deleted. As the frame menubar is deleted by the frame itself, it means that normally all menus used are deleted automatically.

Derived from

wxEvtHandler
wxObject

Include files

<wx/menu.h>

Event handling

If the menu is part of a menubar, then wxMenuBar event processing is used.

With a popup menu, there is a variety of ways to handle a menu selection event (wxEVT_COMMAND_MENU_SELECTED).

1. Derive a new class from wxMenu and define event table entries using the EVT_MENU macro.
2. Set a new event handler for wxMenu, using an object whose class has EVT_MENU entries.
3. Provide EVT_MENU handlers in the window which pops up the menu, or in an ancestor of this window.
4. Define a callback of type wxFunction, which you pass to the wxMenu constructor. The callback takes a reference to the menu, and a reference to a wxCommandEvent. This method is deprecated and should not be used in the new code, it is provided for backwards compatibility only.

See also

wxMenuBar, wxWindow::PopupMenu, Event handling overview

Members

wxMenu::wxMenu
wxMenu::~wxMenu
wxMenu::Append
wxMenu::AppendCheckItem
wxMenu::AppendRadioItem
wxMenu::AppendSeparator
wxMenu::Break
wxMenu::Check
wxMenu::Delete
wxMenu::Destroy
wxMenu::Enable
wxMenu::FindItem
wxMenu::FindItemByPosition
wxMenu::GetHelpString
wxMenu::GetLabel
wxMenu::GetMenuItemCount
wxMenu::GetMenuItems
wxMenu::GetTitle
wxMenu::Insert
wxMenu::InsertCheckItem
wxMenu::InsertRadioItem
wxMenu::InsertSeparator
wxMenu::IsChecked
wxMenu::IsEnabled
wxMenu::Prepend
wxMenu::PrependCheckItem
wxMenu::PrependRadioItem
wxMenu::PrependSeparator
wxMenu::Remove
wxMenu::SetHelpString
wxMenu::SetLabel
wxMenu::SetTitle
wxMenu::UpdateUI

### wxMenu::wxMenu

wxMenu(const wxString& title = "", long style = 0)

Constructs a wxMenu object.

Parameters

title

A title for the popup menu: the empty string denotes no title.

style

If set to `wxMENU_TEAROFF`, the menu will be detachable (wxGTK only).

wxMenu(long style)

Constructs a wxMenu object.

Parameters

style

If set to `wxMENU_TEAROFF`, the menu will be detachable (wxGTK only).

### wxMenu::~wxMenu

~wxMenu()

Destructor, destroying the menu.

Note: under Motif, a popup menu must have a valid parent (the window it was last popped up on) when being destroyed. Therefore, make sure you delete or re-use the popup menu before destroying the parent window. Re-use in this context means popping up the menu on a different window from last time, which causes an implicit destruction and recreation of internal data structures.

### wxMenu::Append

void Append(int id, const wxString& item, const wxString& helpString = "", wxItemKind kind = wxITEM_NORMAL)

Adds a string item to the end of the menu.

void Append(int id, const wxString& item, wxMenu *subMenu, const wxString& helpString = "")

Adds a pull-right submenu to the end of the menu. Append the submenu to the parent menu after you have added your menu items, or accelerators may not be registered properly.

void Append(wxMenuItem* menuItem)

Adds a menu item object. This is the most generic variant of Append() method because it may be used for both items (including separators) and submenus and because you can also specify various extra properties of a menu item this way, such as bitmaps and fonts.

Parameters

id

The menu command identifier.

item

The string to appear on the menu item.

menu

Pull-right submenu.

kind

May be `wxITEM_SEPARATOR`, `wxITEM_NORMAL`, `wxITEM_CHECK` or `wxITEM_RADIO`

helpString

An optional help string associated with the item. By default, wxFrame::OnMenuHighlight displays this string in the status line.

menuItem

A menuitem object. It will be owned by the wxMenu object after this function is called, so do not delete it yourself.

Remarks

This command can be used after the menu has been shown, as well as on initial creation of a menu or menubar.

See also

wxMenu::AppendSeparator, wxMenu::AppendCheckItem, wxMenu::AppendRadioItem, wxMenu::Insert, wxMenu::SetLabel, wxMenu::GetHelpString, wxMenu::SetHelpString, wxMenuItem

wxPython note: In place of a single overloaded method name, wxPython implements the following methods:

Append(id, string, helpStr="", checkable=FALSE) |
AppendMenu(id, string, aMenu, helpStr="") |
AppendItem(aMenuItem) |

### wxMenu::AppendCheckItem

void AppendCheckItem(int id, const wxString& item, const wxString& helpString = "")

Adds a checkable item to the end of the menu.

See also

wxMenu::Append, wxMenu::InsertCheckItem

### wxMenu::AppendRadioItem

void AppendRadioItem(int id, const wxString& item, const wxString& helpString = "")

Adds a radio item to the end of the menu. All consequent radio items form a group and when an item in the group is checked, all the others are automatically unchecked.

NB: Currently only implemented under Windows and GTK, use `#if wxHAS_RADIO_MENU_ITEMS` to test for availability of this feature.

See also

wxMenu::Append, wxMenu::InsertRadioItem

### wxMenu::AppendSeparator

void AppendSeparator()

Adds a separator to the end of the menu.

See also

wxMenu::Append, wxMenu::InsertSeparator

### wxMenu::Break

void Break()

Inserts a break in a menu, causing the next appended item to appear in a new column.

### wxMenu::Check

void Check(int id, const bool check)

Checks or unchecks the menu item.

Parameters

id

The menu item identifier.

check

If TRUE, the item will be checked, otherwise it will be unchecked.

See also

wxMenu::IsChecked

### wxMenu::Delete

void Delete(int id)

void Delete(wxMenuItem *item)

Deletes the menu item from the menu. If the item is a submenu, it will not be deleted. Use Destroy if you want to delete a submenu.

Parameters

id

Id of the menu item to be deleted.

item

Menu item to be deleted.

See also

wxMenu::FindItem, wxMenu::Destroy, wxMenu::Remove

### wxMenu::Destroy

void Destroy(int id)

void Destroy(wxMenuItem *item)

Deletes the menu item from the menu. If the item is a submenu, it will be deleted. Use Remove if you want to keep the submenu (for example, to reuse it later).

Parameters

id

Id of the menu item to be deleted.

item

Menu item to be deleted.

See also

wxMenu::FindItem, wxMenu::Deletes, wxMenu::Remove

### wxMenu::Enable

void Enable(int id, const bool enable)

Enables or disables (greys out) a menu item.

Parameters

id

The menu item identifier.

enable

TRUE to enable the menu item, FALSE to disable it.

See also

wxMenu::IsEnabled

### wxMenu::FindItem

int FindItem(const wxString& itemString) const

Finds the menu item id for a menu item string.

wxMenuItem * FindItem(int id, wxMenu **menu = NULL) const

Finds the menu item object associated with the given menu item identifier and, optionally, the (sub)menu it belongs to.

wxPerl note: In wxPerl this method takes just the `id` parameter; in scalar context it returns the associated `Wx::MenuItem`, in list context it returns a 2-element list `( item, submenu )`

Parameters

itemString

Menu item string to find.

id

Menu item identifier.

menu

If the pointer is not NULL, it will be filled with the items parent menu (if the item was found)

Return value

First form: menu item identifier, or wxNOT_FOUND if none is found.

Second form: returns the menu item object, or NULL if it is not found.

Remarks

Any special menu codes are stripped out of source and target strings before matching.

wxPython note: The name of this method in wxPython is `FindItemById` and it does not support the second parameter.

### wxMenu::FindItemByPosition

wxMenuItem* FindItemByPosition(size_t position) const

Finds the menu item for the given position, returning NULL if not found.

### wxMenu::GetHelpString

wxString GetHelpString(int id) const

Returns the help string associated with a menu item.

Parameters

id

The menu item identifier.

Return value

The help string, or the empty string if there is no help string or the item was not found.

See also

wxMenu::SetHelpString, wxMenu::Append

### wxMenu::GetLabel

wxString GetLabel(int id) const

Returns a menu item label.

Parameters

id

The menu item identifier.

Return value

The item label, or the empty string if the item was not found.

See also

wxMenu::SetLabel

### wxMenu::GetMenuItemCount

size_t GetMenuItemCount() const

Returns the number of items in the menu.

### wxMenu::GetMenuItems

wxMenuItemList& GetMenuItems() const

Returns the list of items in the menu. wxMenuItemList is a pseudo-template list class containing wxMenuItem pointers.

### wxMenu::GetTitle

wxString GetTitle() const

Returns the title of the menu.

Remarks

This is relevant only to popup menus, use wxMenuBar::GetLabelTop for the menus in the menubar.

See also

wxMenu::SetTitle

### wxMenu::Insert

bool Insert(size_t pos, wxMenuItem *item)

void Insert(size_t pos, int id, const wxString& item, const wxString& helpString = "", wxItemKind kind = wxITEM_NORMAL)

Inserts the given item before the position pos. Inserting the item at the position GetMenuItemCount is the same as appending it.

See also

wxMenu::Append, wxMenu::Prepend

### wxMenu::InsertCheckItem

void InsertCheckItem(size_t pos, int id, const wxString& item, const wxString& helpString = "")

Inserts a checkable item at the given position.

See also

wxMenu::Insert, wxMenu::AppendCheckItem

### wxMenu::InsertRadioItem

void InsertRadioItem(size_t pos, int id, const wxString& item, const wxString& helpString = "")

Inserts a radio item at the given position.

See also

wxMenu::Insert, wxMenu::AppendRadioItem

### wxMenu::InsertSeparator

void InsertSeparator(size_t pos)

Inserts a separator at the given position.

See also

wxMenu::Insert, wxMenu::AppendSeparator

### wxMenu::IsChecked

bool IsChecked(int id) const

Determines whether a menu item is checked.

Parameters

id

The menu item identifier.

Return value

TRUE if the menu item is checked, FALSE otherwise.

See also

wxMenu::Check

### wxMenu::IsEnabled

bool IsEnabled(int id) const

Determines whether a menu item is enabled.

Parameters

id

The menu item identifier.

Return value

TRUE if the menu item is enabled, FALSE otherwise.

See also

wxMenu::Enable

### wxMenu::Prepend

bool Prepend(size_t pos, wxMenuItem *item)

void Prepend(int id, const wxString& item, const wxString& helpString = "", wxItemKind kind = wxITEM_NORMAL)

Inserts the given item at the position 0.

See also

wxMenu::Append, wxMenu::Inserts

### wxMenu::PrependCheckItem

void PrependCheckItem(int id, const wxString& item, const wxString& helpString = "")

Inserts a checkable item at the position 0.

See also

wxMenu::Prepend, wxMenu::AppendCheckItem

### wxMenu::PrependRadioItem

void PrependRadioItem(int id, const wxString& item, const wxString& helpString = "")

Inserts a radio item at the position 0.

See also

wxMenu::Prepend, wxMenu::AppendRadioItem

### wxMenu::PrependSeparator

void PrependSeparator(size_t pos)

Inserts a separator at the position 0.

See also

wxMenu::Prepend, wxMenu::AppendSeparator

### wxMenu::Remove

wxMenuItem * Remove(int id)

wxMenuItem * Remove(wxMenuItem *item)

Removes the menu item from the menu but doesn't delete the associated C++ object. This allows to reuse the same item later by adding it back to the menu (especially useful with submenus).

Parameters

id

The identifier of the menu item to remove.

item

The menu item to remove.

Return value

The item which was detached from the menu.

### wxMenu::SetHelpString

void SetHelpString(int id, const wxString& helpString)

Sets an item's help string.

Parameters

id

The menu item identifier.

helpString

The help string to set.

See also

wxMenu::GetHelpString

### wxMenu::SetLabel

void SetLabel(int id, const wxString& label)

Sets the label of a menu item.

Parameters

id

The menu item identifier.

label

The menu item label to set.

See also

wxMenu::Append, wxMenu::GetLabel

### wxMenu::SetTitle

void SetTitle(const wxString& title)

Sets the title of the menu.

Parameters

title

The title to set.

Remarks

This is relevant only to popup menus, use wxMenuBar::SetLabelTop for the menus in the menubar.

See also

wxMenu::GetTitle

### wxMenu::UpdateUI

void UpdateUI(wxEvtHandler* source = NULL) const

Sends events to source (or owning window if NULL) to update the menu UI. This is called just before the menu is popped up with wxWindow::PopupMenu, but the application may call it at other times if required.

See also

wxUpdateUIEvent