# wxMenu Class Reference
[Menus]

`#include </home/work/projects/wx/2.9/wx/interface/wx/menu.h>`

Inheritance diagram for wxMenu:
[legend]

## Detailed Description

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 wxID_SEPARATOR is a separator item and doesn't have an associated command but just makes a separator line appear in the menu.

Note:
  Please note that wxID_ABOUT and wxID_EXIT are predefined by wxWidgets and have a special meaning since entries using these IDs will be taken out of the normal menus under MacOS X and will be inserted into the system menu (following the appropriate MacOS X interface guideline). On PalmOS wxID_EXIT is disabled according to Palm OS Companion guidelines.

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. wxWidgets automatically toggles the flag value when the item is clicked and its value may be retrieved using either wxMenu::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.

## Allocation strategy

All menus must be created on the heap because all menus attached to a menubar or to another menu will be deleted by their parent when it is deleted. The only exception to this rule are the popup menus (i.e. menus used with wxWindow::PopupMenu()) as wxWidgets does not destroy them to allow reusing the same menu more than once. But the exception applies only to the menus themselves and not to any submenus of popup menus which are still destroyed by wxWidgets as usual and so must be heap-allocated.

As the frame menubar is deleted by the frame itself, it means that normally all menus used are deleted automatically.

## Event handling

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

With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to handle a menu selection event (`wxEVT_COMMAND_MENU_SELECTED`):

- Provide `EVT_MENU` handlers in the window which pops up the menu, or in an ancestor of that window (the simplest method);
- Derive a new class from wxMenu and define event table entries using the `EVT_MENU` macro;
- Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler, specifying an object whose class has `EVT_MENU` entries;

Note that instead of static `EVT_MENU` macros you can also use dynamic connection; see Dynamic Event Handling.

##

Library: wxCore
Category: Menus

See also:
  wxMenuBar, wxWindow::PopupMenu, Events and Event Handling, wxFileHistory (most recently used files menu)

## Public Member Functions

| wxMenu (long style)
| Constructs a wxMenu object.
| wxMenu (const wxString &title, long style=0)
| Constructs a wxMenu object with a title.
virtual | ~wxMenu ()
| Destructor, destroying the menu.
wxMenuItem * | Append (int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL)
| Adds a menu item.
wxMenuItem * | Append (int id, const wxString &item, wxMenu *subMenu, const wxString &helpString=wxEmptyString)
| Adds a submenu.
wxMenuItem * | Append (wxMenuItem *menuItem)
| Adds a menu item object.
wxMenuItem * | AppendCheckItem (int id, const wxString &item, const wxString &help=wxEmptyString)
| Adds a checkable item to the end of the menu.
wxMenuItem * | AppendRadioItem (int id, const wxString &item, const wxString &help=wxEmptyString)
| Adds a radio item to the end of the menu.
wxMenuItem * | AppendSeparator ()
| Adds a separator to the end of the menu.
wxMenuItem * | AppendSubMenu (wxMenu *submenu, const wxString &text, const wxString &help=wxEmptyString)
| Adds the given submenu to this menu.
virtual void | Break ()
| Inserts a break in a menu, causing the next appended item to appear in a new column.
void | Check (int id, bool check)
| Checks or unchecks the menu item.
bool | Delete (int id)
| Deletes the menu item from the menu.
bool | Delete (wxMenuItem *item)
| Deletes the menu item from the menu.
bool | Destroy (int id)
| Deletes the menu item from the menu.
bool | Destroy (wxMenuItem *item)
| Deletes the menu item from the menu.
void | Enable (int id, bool enable)
| Enables or disables (greys out) a menu item.
wxMenuItem * | FindChildItem (int id, size_t *pos=NULL) const
| Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu.
virtual int | FindItem (const wxString &itemString) const
| Finds the menu 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.
wxMenuItem * | FindItemByPosition (size_t position) const
| Returns the wxMenuItem given a position in the menu.
virtual wxString | GetHelpString (int id) const
| Returns the help string associated with a menu item.
wxString | GetLabel (int id) const
| Returns a menu item label.
wxString | GetLabelText (int id) const
| Returns a menu item label, without any of the original mnemonics and accelerators.
size_t | GetMenuItemCount () const
| Returns the number of items in the menu.
const wxString & | GetTitle () const
| Returns the title of the menu.
wxMenuItem * | Insert (size_t pos, wxMenuItem *item)
| Inserts the given item before the position pos.
wxMenuItem * | Insert (size_t pos, int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL)
| Inserts the given item before the position pos.
wxMenuItem * | InsertCheckItem (size_t pos, int id, const wxString &item, const wxString &helpString=wxEmptyString)
| Inserts a checkable item at the given position.
wxMenuItem * | InsertRadioItem (size_t pos, int id, const wxString &item, const wxString &helpString=wxEmptyString)
| Inserts a radio item at the given position.
wxMenuItem * | InsertSeparator (size_t pos)
| Inserts a separator at the given position.
bool | IsChecked (int id) const
| Determines whether a menu item is checked.
bool | IsEnabled (int id) const
| Determines whether a menu item is enabled.
wxMenuItem * | Prepend (wxMenuItem *item)
| Inserts the given item at position 0, i.e.
wxMenuItem * | Prepend (int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL)
| Inserts the given item at position 0, i.e.
wxMenuItem * | PrependCheckItem (int id, const wxString &item, const wxString &helpString=wxEmptyString)
| Inserts a checkable item at position 0.
wxMenuItem * | PrependRadioItem (int id, const wxString &item, const wxString &helpString=wxEmptyString)
| Inserts a radio item at position 0.
wxMenuItem * | PrependSeparator ()
| Inserts a separator at position 0.
wxMenuItem * | Remove (int id)
| Removes the menu item from the menu but doesn't delete the associated C++ object.
wxMenuItem * | Remove (wxMenuItem *item)
| Removes the menu item from the menu but doesn't delete the associated C++ object.
virtual void | SetHelpString (int id, const wxString &helpString)
| Sets an item's help string.
void | SetLabel (int id, const wxString &label)
| Sets the label of a menu item.
virtual void | SetTitle (const wxString &title)
| Sets the title of the menu.
void | UpdateUI (wxEvtHandler *source=NULL)
| Sends events to source (or owning window if NULL) to update the menu UI.
wxMenuItemList & | GetMenuItems () const
| Returns the list of items in the menu.
const wxMenuItemList & | GetMenuItems () const
| Returns the list of items in the menu.

List of all members.

## Constructor & Destructor Documentation

wxMenu::wxMenu | ( | long | style | ) |

Constructs a wxMenu object.

Parameters:

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

wxMenu::wxMenu | ( | const wxString & | title,
|  | long | style = `0` |
| ) |  |  |

Constructs a wxMenu object with a title.

Parameters:

  | title | Title at the top of the menu (not always supported).
  | style | If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).

virtual wxMenu::~wxMenu | ( |  | ) | `[virtual]`

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.

## Member Function Documentation

wxMenuItem* wxMenu::Append | ( | int | id,
|  | const wxString & | item = `wxEmptyString`,
|  | const wxString & | helpString = `wxEmptyString`,
|  | wxItemKind | kind = `wxITEM_NORMAL` |
| ) |  |  |

Adds a menu item.

Parameters:

  | id | The menu command identifier.
  | item | The string to appear on the menu item. See wxMenuItem::SetItemLabel() for more details.
  | helpString | An optional help string associated with the item. By default, the handler for the `wxEVT_MENU_HIGHLIGHT` event displays this string in the status line.
  | kind | May be `wxITEM_SEPARATOR`, `wxITEM_NORMAL`, `wxITEM_CHECK` or `wxITEM_RADIO`.

Example:

            m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document");

or even better for stock menu items (see wxMenuItem::wxMenuItem):

            m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document");

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:
  AppendSeparator(), AppendCheckItem(), AppendRadioItem(), AppendSubMenu(), Insert(), SetLabel(), GetHelpString(), SetHelpString(), wxMenuItem

wxMenuItem* wxMenu::Append | ( | int | id,
|  | const wxString & | item,
|  | wxMenu * | subMenu,
|  | const wxString & | helpString = `wxEmptyString` |
| ) |  |  |

Adds a submenu.

Deprecated:
  This function is deprecated, use AppendSubMenu() instead.

Parameters:

  | id | The menu command identifier.
  | item | The string to appear on the menu item.
  | subMenu | Pull-right submenu.
  | helpString | An optional help string associated with the item. By default, the handler for the `wxEVT_MENU_HIGHLIGHT` event displays this string in the status line.

See also:
  AppendSeparator(), AppendCheckItem(), AppendRadioItem(), AppendSubMenu(), Insert(), SetLabel(), GetHelpString(), SetHelpString(), wxMenuItem

wxMenuItem* wxMenu::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:

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

Remarks:
  See the remarks for the other Append() overloads.

See also:
  AppendSeparator(), AppendCheckItem(), AppendRadioItem(), AppendSubMenu(), Insert(), SetLabel(), GetHelpString(), SetHelpString(), wxMenuItem

wxMenuItem* wxMenu::AppendCheckItem | ( | int | id,
|  | const wxString & | item,
|  | const wxString & | help = `wxEmptyString` |
| ) |  |  |

Adds a checkable item to the end of the menu.

See also:
  Append(), InsertCheckItem()

wxMenuItem* wxMenu::AppendRadioItem | ( | int | id,
|  | const wxString & | item,
|  | const wxString & | help = `wxEmptyString` |
| ) |  |  |

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.

Note:
  Radio items are not supported under wxMotif.

See also:
  Append(), InsertRadioItem()

wxMenuItem* wxMenu::AppendSeparator | ( |  | ) |

Adds a separator to the end of the menu.

See also:
  Append(), InsertSeparator()

wxMenuItem* wxMenu::AppendSubMenu | ( | wxMenu * | submenu,
|  | const wxString & | text,
|  | const wxString & | help = `wxEmptyString` |
| ) |  |  |

Adds the given submenu to this menu.

text is the text shown in the menu for it and help is the help string shown in the status bar when the submenu item is selected.

virtual void wxMenu::Break | ( |  | ) | `[virtual]`

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

void wxMenu::Check | ( | int | id,
|  | 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:
  IsChecked()

bool wxMenu::Delete | ( | int | id | ) |

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.

See also:
  FindItem(), Destroy(), Remove()

bool wxMenu::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:

  | item | Menu item to be deleted.

See also:
  FindItem(), Destroy(), Remove()

bool wxMenu::Destroy | ( | int | id | ) |

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.

See also:
  FindItem(), Delete(), Remove()

bool wxMenu::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:

  | item | Menu item to be deleted.

See also:
  FindItem(), Delete(), Remove()

void wxMenu::Enable | ( | int | id,
|  | 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:
  IsEnabled()

wxMenuItem* wxMenu::FindChildItem | ( | int | id,
|  | size_t * | pos = `NULL` |
| ) |  |  | const

Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu.

Unlike FindItem(), this function doesn't recurse but only looks at the direct children of this menu.

Parameters:

  | id | The identifier of the menu item to find.
  | pos | If the pointer is not NULL, it is filled with the item's position if it was found or ``(size_t)wxNOT_FOUND otherwise.

Returns:
  Menu item object or NULL if not found.

virtual int wxMenu::FindItem | ( | const wxString & | itemString | ) | const`[virtual]`

Finds the menu id for a menu item string.

Parameters:

  | itemString | Menu item string to find.

Returns:
  Menu item identifier, or wxNOT_FOUND if none is found.

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

wxMenuItem* wxMenu::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.

Parameters:

  | id | Menu item identifier.
  | menu | If the pointer is not NULL, it will be filled with the item's parent menu (if the item was found)

Returns:
  Menu item object or NULL if none is found.

wxMenuItem* wxMenu::FindItemByPosition | ( | size_t | position | ) | const

Returns the wxMenuItem given a position in the menu.

virtual wxString wxMenu::GetHelpString | ( | int | id | ) | const`[virtual]`

Returns the help string associated with a menu item.

Parameters:

  | id | The menu item identifier.

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

See also:
  SetHelpString(), Append()

wxString wxMenu::GetLabel | ( | int | id | ) | const

Returns a menu item label.

Parameters:

  | id | The menu item identifier.

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

See also:
  GetLabelText(), SetLabel()

wxString wxMenu::GetLabelText | ( | int | id | ) | const

Returns a menu item label, without any of the original mnemonics and accelerators.

Parameters:

  | id | The menu item identifier.

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

See also:
  GetLabel(), SetLabel()

size_t wxMenu::GetMenuItemCount | ( |  | ) | const

Returns the number of items in the menu.

wxMenuItemList& wxMenu::GetMenuItems | ( |  | ) | const

Returns the list of items in the menu.

wxMenuItemList is a pseudo-template list class containing wxMenuItem pointers, see wxList.

const wxMenuItemList& wxMenu::GetMenuItems | ( |  | ) | const

Returns the list of items in the menu.

wxMenuItemList is a pseudo-template list class containing wxMenuItem pointers, see wxList.

const wxString& wxMenu::GetTitle | ( |  | ) | const

Returns the title of the menu.

See also:
  SetTitle()

wxMenuItem* wxMenu::Insert | ( | size_t | pos,
|  | int | id,
|  | const wxString & | item = `wxEmptyString`,
|  | const wxString & | helpString = `wxEmptyString`,
|  | wxItemKind | kind = `wxITEM_NORMAL` |
| ) |  |  |

Inserts the given item before the position pos.

Inserting the item at position GetMenuItemCount() is the same as appending it.

See also:
  Append(), Prepend()

wxMenuItem* wxMenu::Insert | ( | size_t | pos,
|  | wxMenuItem * | item |
| ) |  |  |

Inserts the given item before the position pos.

Inserting the item at position GetMenuItemCount() is the same as appending it.

See also:
  Append(), Prepend()

wxMenuItem* wxMenu::InsertCheckItem | ( | size_t | pos,
|  | int | id,
|  | const wxString & | item,
|  | const wxString & | helpString = `wxEmptyString` |
| ) |  |  |

Inserts a checkable item at the given position.

See also:
  Insert(), AppendCheckItem()

wxMenuItem* wxMenu::InsertRadioItem | ( | size_t | pos,
|  | int | id,
|  | const wxString & | item,
|  | const wxString & | helpString = `wxEmptyString` |
| ) |  |  |

Inserts a radio item at the given position.

See also:
  Insert(), AppendRadioItem()

wxMenuItem* wxMenu::InsertSeparator | ( | size_t | pos | ) |

Inserts a separator at the given position.

See also:
  Insert(), AppendSeparator()

bool wxMenu::IsChecked | ( | int | id | ) | const

Determines whether a menu item is checked.

Parameters:

  | id | The menu item identifier.

Returns:
  true if the menu item is checked, false otherwise.

See also:
  Check()

bool wxMenu::IsEnabled | ( | int | id | ) | const

Determines whether a menu item is enabled.

Parameters:

  | id | The menu item identifier.

Returns:
  true if the menu item is enabled, false otherwise.

See also:
  Enable()

wxMenuItem* wxMenu::Prepend | ( | wxMenuItem * | item | ) |

Inserts the given item at position 0, i.e.

before all the other existing items.

See also:
  Append(), Insert()

wxMenuItem* wxMenu::Prepend | ( | int | id,
|  | const wxString & | item = `wxEmptyString`,
|  | const wxString & | helpString = `wxEmptyString`,
|  | wxItemKind | kind = `wxITEM_NORMAL` |
| ) |  |  |

Inserts the given item at position 0, i.e.

before all the other existing items.

See also:
  Append(), Insert()

wxMenuItem* wxMenu::PrependCheckItem | ( | int | id,
|  | const wxString & | item,
|  | const wxString & | helpString = `wxEmptyString` |
| ) |  |  |

Inserts a checkable item at position 0.

See also:
  Prepend(), AppendCheckItem()

wxMenuItem* wxMenu::PrependRadioItem | ( | int | id,
|  | const wxString & | item,
|  | const wxString & | helpString = `wxEmptyString` |
| ) |  |  |

Inserts a radio item at position 0.

See also:
  Prepend(), AppendRadioItem()

wxMenuItem* wxMenu::PrependSeparator | ( |  | ) |

Inserts a separator at position 0.

See also:
  Prepend(), AppendSeparator()

wxMenuItem* wxMenu::Remove | ( | int | id | ) |

Removes the menu item from the menu but doesn't delete the associated C++ object.

This allows you 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.

Returns:
  A pointer to the item which was detached from the menu.

wxMenuItem* wxMenu::Remove | ( | wxMenuItem * | item | ) |

Removes the menu item from the menu but doesn't delete the associated C++ object.

This allows you to reuse the same item later by adding it back to the menu (especially useful with submenus).

Parameters:

  | item | The menu item to remove.

Returns:
  A pointer to the item which was detached from the menu.

virtual void wxMenu::SetHelpString | ( | int | id,
|  | const wxString & | helpString |
| ) |  |  | `[virtual]`

Sets an item's help string.

Parameters:

  | id | The menu item identifier.
  | helpString | The help string to set.

See also:
  GetHelpString()

void wxMenu::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:
  Append(), GetLabel()

virtual void wxMenu::SetTitle | ( | const wxString & | title | ) | `[virtual]`

Sets the title of the menu.

Parameters:

  | title | The title to set.

Remarks:
  Notice that you can only call this method directly for the popup menus, to change the title of a menu that is part of a menu bar you need to use wxMenuBar::SetLabelTop().

See also:
  GetTitle()

void wxMenu::UpdateUI | ( | wxEvtHandler * | source = `NULL` | ) |

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.

|  | [ top ]