Public Member Functions | Static Public Member Functions

# wxMenuItem Class Reference
[Menus]

`#include <wx/menuitem.h>`

Inheritance diagram for wxMenuItem:
[legend]

## Detailed Description

A menu item represents an item in a menu.

Note that you usually don't have to deal with it directly as wxMenu methods usually construct an object of this class for you.

Also please note that the methods related to fonts and bitmaps are currently only implemented for Windows, Mac and GTK+.

## Events emitted by this class

The following event handler macros redirect the events to member function handlers 'func' with prototypes like:

void handlerFuncName(wxCommandEvent& event) or void handlerFuncName(wxMenuEvent& event)

Event macros for events emitted by this class:

- EVT_MENU(id, func):
  Process a `wxEVT_COMMAND_MENU_SELECTED` command, which is generated by a menu item. This type of event is sent as wxCommandEvent.
- EVT_MENU_RANGE(id1, id2, func):
  Process a `wxEVT_COMMAND_MENU_RANGE` command, which is generated by a range of menu items. This type of event is sent as wxCommandEvent.
- EVT_MENU_OPEN(func):
  A menu is about to be opened. On Windows, this is only sent once for each navigation of the menubar (up until all menus have closed). This type of event is sent as wxMenuEvent.
- EVT_MENU_CLOSE(func):
  A menu has been just closed. This type of event is sent as wxMenuEvent.
- EVT_MENU_HIGHLIGHT(id, func):
  The menu item with the specified id has been highlighted: used to show help prompts in the status bar by wxFrame This type of event is sent as wxMenuEvent.
- EVT_MENU_HIGHLIGHT_ALL(func):
  A menu item has been highlighted, i.e. the currently selected menu item has changed. This type of event is sent as wxMenuEvent.

##

Library: wxCore
Category: Menus

See also:
  wxMenuBar, wxMenu

## Public Member Functions
| wxMenuItem (wxMenu *parentMenu=NULL, int id=wxID_SEPARATOR, const wxString &text=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL, wxMenu *subMenu=NULL)
| Constructs a wxMenuItem object.
virtual | ~wxMenuItem ()
| Destructor.
virtual void | Check (bool check=true)
| Checks or unchecks the menu item.
virtual void | Enable (bool enable=true)
| Enables or disables the menu item.
Getters
wxColour | GetBackgroundColour () const
| Returns the background colour associated with the menu item.
virtual const wxBitmap & | GetBitmap () const
| Returns the checked or unchecked bitmap.
wxFont | GetFont () const
| Returns the font associated with the menu item.
const wxString & | GetHelp () const
| Returns the help string associated with the menu item.
int | GetId () const
| Returns the menu item identifier.
virtual wxString | GetItemLabel () const
| Returns the text associated with the menu item including any accelerator characters that were passed to the constructor or SetItemLabel().
virtual wxString | GetItemLabelText () const
| Returns the text associated with the menu item, without any accelerator characters.
wxItemKind | GetKind () const
| Returns the item kind, one of `wxITEM_SEPARATOR`, `wxITEM_NORMAL`, `wxITEM_CHECK` or `wxITEM_RADIO`.
wxString | GetLabel () const
| Returns the text associated with the menu item without any accelerator characters it might contain.
int | GetMarginWidth () const
| Gets the width of the menu item checkmark bitmap.
wxMenu * | GetMenu () const
| Returns the menu this menu item is in, or NULL if this menu item is not attached.
wxString | GetName () const
| Returns the text associated with the menu item.
wxMenu * | GetSubMenu () const
| Returns the submenu associated with the menu item, or NULL if there isn't one.
const wxString & | GetText () const
| Returns the text associated with the menu item, such as it was passed to the wxMenuItem constructor, i.e.
wxColour | GetTextColour () const
| Returns the text colour associated with the menu item.
Checkers
bool | IsCheckable () const
| Returns true if the item is checkable.
virtual bool | IsChecked () const
| Returns true if the item is checked.
virtual bool | IsEnabled () const
| Returns true if the item is enabled.
bool | IsSeparator () const
| Returns true if the item is a separator.
bool | IsSubMenu () const
| Returns true if the item is a submenu.
Setters
void | SetBackgroundColour (const wxColour &colour) const
| Sets the background colour associated with the menu item.
virtual void | SetBitmap (const wxBitmap &bmp, bool checked=true)
| Sets the bitmap for the menu item.
void | SetBitmaps (const wxBitmap &checked, const wxBitmap &unchecked=wxNullBitmap)
| Sets the checked/unchecked bitmaps for the menu item.
void | SetFont (const wxFont &font)
| Sets the font associated with the menu item.
void | SetHelp (const wxString &helpString)
| Sets the help string.
virtual void | SetItemLabel (const wxString &label)
| Sets the label associated with the menu item.
void | SetMarginWidth (int width) const
| Sets the width of the menu item checkmark bitmap.
void | SetMenu (wxMenu *menu)
| Sets the parent menu which will contain this menu item.
void | SetSubMenu (wxMenu *menu)
| Sets the submenu of this menu item.
virtual void | SetText (const wxString &text)
| Sets the text associated with the menu item.
void | SetTextColour (const wxColour &colour)
| Sets the text colour associated with the menu item.
## Static Public Member Functions
static wxString | GetLabelFromText (const wxString &text)
static wxString | GetLabelText (const wxString &text)
| Strips all accelerator characters and mnemonics from the given text.

List of all members.

## Constructor & Destructor Documentation

wxMenuItem::wxMenuItem | ( | wxMenu * | parentMenu = `NULL`,
|  | int | id = `wxID_SEPARATOR`,
|  | const wxString & | text = `wxEmptyString`,
|  | const wxString & | helpString = `wxEmptyString`,
|  | wxItemKind | kind = `wxITEM_NORMAL`,
|  | wxMenu * | subMenu = `NULL` |
| ) |  |  |

Constructs a wxMenuItem object.

Menu items can be standard, or "stock menu items", or custom. For the standard menu items (such as commands to open a file, exit the program and so on, see Stock items for the full list) it is enough to specify just the stock ID and leave text and helpString empty. Some platforms (currently wxGTK only, and see the remark in SetBitmap() documentation) will also show standard bitmaps for stock menu items.

Leaving at least text empty for the stock menu items is actually strongly recommended as they will have appearance and keyboard interface (including standard accelerators) familiar to the user.

For the custom (non-stock) menu items, text must be specified and while helpString may be left empty, it's recommended to pass the item description (which is automatically shown by the library in the status bar when the menu item is selected) in this parameter.

Finally note that you can e.g. use a stock menu label without using its stock help string:

            // use all stock properties:
            helpMenu->Append(wxID_ABOUT);

            // use the stock label and the stock accelerator but not the stock help string:
            helpMenu->Append(wxID_ABOUT, "", "My custom help string");

            // use all stock properties except for the bitmap:
            wxMenuItem *mymenu = new wxMenuItem(helpMenu, wxID_ABOUT);
            mymenu->SetBitmap(wxArtProvider::GetBitmap(wxART_WARNING));
            helpMenu->Append(mymenu);

that is, stock properties are set independently one from the other.

Parameters:

  | parentMenu | Menu that the menu item belongs to. Can be NULL if the item is going to be added to the menu later.
  | id | Identifier for this menu item. May be `wxID_SEPARATOR`, in which case the given kind is ignored and taken to be `wxITEM_SEPARATOR` instead.
  | text | Text for the menu item, as shown on the menu. See SetItemLabel() for more info.
  | helpString | Optional help string that will be shown on the status bar.
  | kind | May be `wxITEM_SEPARATOR`, `wxITEM_NORMAL`, `wxITEM_CHECK` or `wxITEM_RADIO`.
  | subMenu | If non-NULL, indicates that the menu item is a submenu.

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

Destructor.

## Member Function Documentation

virtual void wxMenuItem::Check | ( | bool | check = `true` | ) | `[virtual]`

Checks or unchecks the menu item.

Note that this only works when the item is already appended to a menu.

virtual void wxMenuItem::Enable | ( | bool | enable = `true` | ) | `[virtual]`

Enables or disables the menu item.

wxColour wxMenuItem::GetBackgroundColour | ( |  | ) | const

Returns the background colour associated with the menu item.

Availability: only available for the wxMSW port.

virtual const wxBitmap& wxMenuItem::GetBitmap | ( |  | ) | const`[virtual]`

Returns the checked or unchecked bitmap.

Availability: only available for the wxMSW port.

wxFont wxMenuItem::GetFont | ( |  | ) | const

Returns the font associated with the menu item.

Availability: only available for the wxMSW port.

const wxString& wxMenuItem::GetHelp | ( |  | ) | const

Returns the help string associated with the menu item.

int wxMenuItem::GetId | ( |  | ) | const

Returns the menu item identifier.

virtual wxString wxMenuItem::GetItemLabel | ( |  | ) | const`[virtual]`

Returns the text associated with the menu item including any accelerator characters that were passed to the constructor or SetItemLabel().

See also:
  GetItemLabelText(), GetLabelText()

virtual wxString wxMenuItem::GetItemLabelText | ( |  | ) | const`[virtual]`

Returns the text associated with the menu item, without any accelerator characters.

See also:
  GetItemLabel(), GetLabelText()

wxItemKind wxMenuItem::GetKind | ( |  | ) | const

Returns the item kind, one of `wxITEM_SEPARATOR`, `wxITEM_NORMAL`, `wxITEM_CHECK` or `wxITEM_RADIO`.

wxString wxMenuItem::GetLabel | ( |  | ) | const

Returns the text associated with the menu item without any accelerator characters it might contain.

Deprecated:
  This function is deprecated in favour of GetItemLabelText().

See also:
  GetItemLabelText()

static wxString wxMenuItem::GetLabelFromText | ( | const wxString & | text | ) | `[static]`

Deprecated:
  This function is deprecated; please use GetLabelText() instead.

See also:
  GetLabelText()

static wxString wxMenuItem::GetLabelText | ( | const wxString & | text | ) | `[static]`

Strips all accelerator characters and mnemonics from the given text.

For example:

              wxMenuItem::GetLabelfromText("&Hello\tCtrl-h");

will return just `"Hello"`.

See also:
  GetItemLabelText(), GetItemLabel()

int wxMenuItem::GetMarginWidth | ( |  | ) | const

Gets the width of the menu item checkmark bitmap.

Availability: only available for the wxMSW port.

wxMenu* wxMenuItem::GetMenu | ( |  | ) | const

Returns the menu this menu item is in, or NULL if this menu item is not attached.

wxString wxMenuItem::GetName | ( |  | ) | const

Returns the text associated with the menu item.

Deprecated:
  This function is deprecated. Please use GetItemLabel() or GetItemLabelText() instead.

See also:
  GetItemLabel(), GetItemLabelText()

wxMenu* wxMenuItem::GetSubMenu | ( |  | ) | const

Returns the submenu associated with the menu item, or NULL if there isn't one.

const wxString& wxMenuItem::GetText | ( |  | ) | const

Returns the text associated with the menu item, such as it was passed to the wxMenuItem constructor, i.e.

with any accelerator characters it may contain.

Deprecated:
  This function is deprecated in favour of GetItemLabel().

See also:
  GetLabelFromText()

wxColour wxMenuItem::GetTextColour | ( |  | ) | const

Returns the text colour associated with the menu item.

Availability: only available for the wxMSW port.

bool wxMenuItem::IsCheckable | ( |  | ) | const

Returns true if the item is checkable.

virtual bool wxMenuItem::IsChecked | ( |  | ) | const`[virtual]`

Returns true if the item is checked.

virtual bool wxMenuItem::IsEnabled | ( |  | ) | const`[virtual]`

Returns true if the item is enabled.

bool wxMenuItem::IsSeparator | ( |  | ) | const

Returns true if the item is a separator.

bool wxMenuItem::IsSubMenu | ( |  | ) | const

Returns true if the item is a submenu.

void wxMenuItem::SetBackgroundColour | ( | const wxColour & | colour | ) | const

Sets the background colour associated with the menu item.

Availability: only available for the wxMSW port.

virtual void wxMenuItem::SetBitmap | ( | const wxBitmap & | bmp,
|  | bool | checked = `true` |
| ) |  |  | `[virtual]`

Sets the bitmap for the menu item.

It is equivalent to wxMenuItem::SetBitmaps(bmp, wxNullBitmap) if checked is true (default value) or SetBitmaps(wxNullBitmap, bmp) otherwise.

Notice that GTK+ uses a global setting called `gtk-menu-images` to determine if the images should be shown in the menus at all. If it is off (which is the case in e.g. Gnome 2.28 by default), no images will be shown, consistently with the native behaviour.

Availability: only available for the wxMSW, wxOSX, wxGTK ports.

void wxMenuItem::SetBitmaps | ( | const wxBitmap & | checked,
|  | const wxBitmap & | unchecked = `wxNullBitmap` |
| ) |  |  |

Sets the checked/unchecked bitmaps for the menu item.

The first bitmap is also used as the single bitmap for uncheckable menu items.

Availability: only available for the wxMSW port.

void wxMenuItem::SetFont | ( | const wxFont & | font | ) |

Sets the font associated with the menu item.

Availability: only available for the wxMSW port.

void wxMenuItem::SetHelp | ( | const wxString & | helpString | ) |

Sets the help string.

virtual void wxMenuItem::SetItemLabel | ( | const wxString & | label | ) | `[virtual]`

Sets the label associated with the menu item.

Note that if the ID of this menu item corresponds to a stock ID, then it is not necessary to specify a label: wxWidgets will automatically use the stock item label associated with that ID. See the constructor for more info.

The label string for the normal menu items (not separators) may include the accelerator which can be used to activate the menu item from keyboard. An accelerator key can be specified using the ampersand `&` character. In order to embed an ampersand character in the menu item text, the ampersand must be doubled.

Optionally you can specify also an accelerator string appending a tab character `\t` followed by a valid key combination (e.g. `CTRL+V`). Its general syntax is any combination of `"CTRL"`, `"ALT"` and `"SHIFT"` strings (case doesn't matter) separated by either `'-'` or `'+'` characters and followed by the accelerator itself. The accelerator may be any alphanumeric character, any function key (from F1 to F12) or one of the special characters listed in the table below (again, case doesn't matter):

- `DEL` or `DELETE:` Delete key
- `INS` or `INSERT:` Insert key
- `ENTER` or `RETURN:` Enter key
- `PGUP:` PageUp key
- `PGDN:` PageDown key
- `LEFT:` Left cursor arrow key
- `RIGHT:` Right cursor arrow key
- `UP:` Up cursor arrow key
- `DOWN:` Down cursor arrow key
- `HOME:` Home key
- `END:` End key
- `SPACE:` Space
- `TAB:` Tab key
- `ESC` or `ESCAPE:` Escape key (Windows only)

Examples:

            m_pMyMenuItem->SetItemLabel("My &item\tCTRL+I");
            m_pMyMenuItem2->SetItemLabel("Clean && build\tF7");
            m_pMyMenuItem3->SetItemLabel("Simple item");
            m_pMyMenuItem4->SetItemLabel("Item with &accelerator");

See also:
  GetItemLabel(), GetItemLabelText()

void wxMenuItem::SetMarginWidth | ( | int | width | ) | const

Sets the width of the menu item checkmark bitmap.

Availability: only available for the wxMSW port.

void wxMenuItem::SetMenu | ( | wxMenu * | menu | ) |

Sets the parent menu which will contain this menu item.

void wxMenuItem::SetSubMenu | ( | wxMenu * | menu | ) |

Sets the submenu of this menu item.

virtual void wxMenuItem::SetText | ( | const wxString & | text | ) | `[virtual]`

Sets the text associated with the menu item.

Deprecated:
  This function is deprecated in favour of SetItemLabel().

See also:
  SetItemLabel().

void wxMenuItem::SetTextColour | ( | const wxColour & | colour | ) |

Sets the text colour associated with the menu item.

Availability: only available for the wxMSW port.

|  | [ top ]