wxMenu — wx v2.4.3

Search documentation of wx

# View Source wxMenu (wx v2.4.3)

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 and will be inserted into the system menu (following the appropriate macOS interface guideline).

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 `isChecked/2` 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/4`) 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

Event handlers for the commands generated by the menu items can be connected directly to the menu object itself using `wxEvtHandler::Bind()` (not implemented in wx). If this menu is a submenu of another one, the events from its items can also be processed in the parent menu and so on, recursively.

If the menu is part of a menu bar, then events can also be handled in `wxMenuBar` object.

Finally, menu events can also be handled in the associated window, which is either the `wxFrame` associated with the menu bar this menu belongs to or the window for which `wxWindow:popupMenu/4` was called for the popup menus.

See overview_events_bind for how to bind event handlers to the various objects.

See:

- `wxMenuBar`

- `wxWindow:popupMenu/4`

- Overview events

This class is derived, and can use functions, from:

- `wxEvtHandler`

wxWidgets docs: wxMenu

# Summary

## Types

wxMenu()

## Functions

append(This, MenuItem)

Adds a menu item object.

append(This, Id, Item)

Equivalent to `append(This, Id, Item, [])`.

append/4

Adds a menu item.

append/5

Adds a submenu.

appendCheckItem(This, Id, Item)

Equivalent to `appendCheckItem(This, Id, Item, [])`.

appendCheckItem/4

Adds a checkable item to the end of the menu.

appendRadioItem(This, Id, Item)

Equivalent to `appendRadioItem(This, Id, Item, [])`.

appendRadioItem/4

Adds a radio item to the end of the menu.

appendSeparator(This)

Adds a separator to the end of the menu.

break(This)

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

check(This, Id, Check)

Checks or unchecks the menu item.

'Destroy'/2

Deletes the menu item from the menu.

delete/2

Deletes the menu item from the menu.

destroy(This)

Destroys the object

enable(This, Id, Enable)

Enables or disables (greys out) a menu item.

findItem/2

Finds the menu id for a menu item string.

findItemByPosition(This, Position)

Returns the `wxMenuItem` given a position in the menu.

getHelpString(This, Id)

Returns the help string associated with a menu item.

getLabel(This, Id)

Returns a menu item label.

getMenuItemCount(This)

Returns the number of items in the menu.

getMenuItems(This)
getTitle(This)

Returns the title of the menu.

insert/3

Inserts the given `item` before the position `pos`.

insert/4

Inserts the given `item` before the position `pos`.

insert(This, Pos, Id, Text, Submenu)

Equivalent to `insert(This, Pos, Id, Text, Submenu, [])`.

insert/6

Inserts the given `submenu` before the position `pos`.

insertCheckItem(This, Pos, Id, Item)

Equivalent to `insertCheckItem(This, Pos, Id, Item, [])`.

insertCheckItem/5

Inserts a checkable item at the given position.

insertRadioItem(This, Pos, Id, Item)

Equivalent to `insertRadioItem(This, Pos, Id, Item, [])`.

insertRadioItem/5

Inserts a radio item at the given position.

insertSeparator(This, Pos)

Inserts a separator at the given position.

isChecked(This, Id)

Determines whether a menu item is checked.

isEnabled(This, Id)

Determines whether a menu item is enabled.

new()

Constructs a `wxMenu` object.

new(Options)

Constructs a `wxMenu` object.

new(Title, Options)

Constructs a `wxMenu` object with a title.

prepend/2

Inserts the given `item` at position 0, i.e. before all the other existing items.

prepend/3

Inserts the given `item` at position 0, i.e. before all the other existing items.

prepend(This, Id, Text, Submenu)

Equivalent to `prepend(This, Id, Text, Submenu, [])`.

prepend/5

Inserts the given `submenu` at position 0.

prependCheckItem(This, Id, Item)

Equivalent to `prependCheckItem(This, Id, Item, [])`.

prependCheckItem/4

Inserts a checkable item at position 0.

prependRadioItem(This, Id, Item)

Equivalent to `prependRadioItem(This, Id, Item, [])`.

prependRadioItem/4

Inserts a radio item at position 0.

prependSeparator(This)

Inserts a separator at position 0.

remove/2

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

setHelpString(This, Id, HelpString)

Sets an item's help string.

setLabel(This, Id, Label)

Sets the label of a menu item.

setTitle(This, Title)

Sets the title of the menu.

# Types

Link to this type

# wxMenu()

View Source

    -type wxMenu() :: wx:wx_object().

# Functions

Link to this function

# append(This, MenuItem)

View Source

    -spec append(This, MenuItem) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), MenuItem :: wxMenuItem:wxMenuItem().

Adds a menu item object.

This is the most generic variant of `append/5` 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.

Remark: See the remarks for the other `append/5` overloads.

See:

- `appendSeparator/1`

- `appendCheckItem/4`

- `appendRadioItem/4`

- `insert/6`

- `setLabel/3`

- `getHelpString/2`

- `setHelpString/3`

- `wxMenuItem`

Link to this function

# append(This, Id, Item)

View Source

    -spec append(This, Id, Item) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata().

Equivalent to `append(This, Id, Item, [])`.

Link to this function

# append/4

View Source

    -spec append(This, Id, Item, SubMenu) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata(), SubMenu :: wxMenu();
                (This, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                    when
                        This :: wxMenu(),
                        Id :: integer(),
                        Item :: unicode:chardata(),
                        Option :: {help, unicode:chardata()} | {kind, wx:wx_enum()}.

Adds a menu item.

Example: or even better for stock menu items (see `wxMenuItem:new/1`):

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

See:

- `appendSeparator/1`

- `appendCheckItem/4`

- `appendRadioItem/4`

- `insert/6`

- `setLabel/3`

- `getHelpString/2`

- `setHelpString/3`

- `wxMenuItem`

Link to this function

# append/5

View Source

    -spec append(This, Id, Item, SubMenu, [Option]) -> wxMenuItem:wxMenuItem()
                    when
                        This :: wxMenu(),
                        Id :: integer(),
                        Item :: unicode:chardata(),
                        SubMenu :: wxMenu(),
                        Option :: {help, unicode:chardata()}.

Adds a submenu.

Deprecated:

This function is deprecated, use `AppendSubMenu()` (not implemented in wx) instead.

See:

- `appendSeparator/1`

- `appendCheckItem/4`

- `appendRadioItem/4`

- `insert/6`

- `setLabel/3`

- `getHelpString/2`

- `setHelpString/3`

- `wxMenuItem`

Link to this function

# appendCheckItem(This, Id, Item)

View Source

    -spec appendCheckItem(This, Id, Item) -> wxMenuItem:wxMenuItem()
                             when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata().

Equivalent to `appendCheckItem(This, Id, Item, [])`.

Link to this function

# appendCheckItem/4

View Source

    -spec appendCheckItem(This, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Id :: integer(),
                                 Item :: unicode:chardata(),
                                 Option :: {help, unicode:chardata()}.

Adds a checkable item to the end of the menu.

See:

- `append/5`

- `insertCheckItem/5`

Link to this function

# appendRadioItem(This, Id, Item)

View Source

    -spec appendRadioItem(This, Id, Item) -> wxMenuItem:wxMenuItem()
                             when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata().

Equivalent to `appendRadioItem(This, Id, Item, [])`.

Link to this function

# appendRadioItem/4

View Source

    -spec appendRadioItem(This, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Id :: integer(),
                                 Item :: unicode:chardata(),
                                 Option :: {help, unicode:chardata()}.

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:

- `append/5`

- `insertRadioItem/5`

Link to this function

# appendSeparator(This)

View Source

    -spec appendSeparator(This) -> wxMenuItem:wxMenuItem() when This :: wxMenu().

Adds a separator to the end of the menu.

See:

- `append/5`

- `insertSeparator/2`

Link to this function

# break(This)

View Source

    -spec break(This) -> ok when This :: wxMenu().

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

This function only actually inserts a break in wxMSW and does nothing under the other platforms.

Link to this function

# check(This, Id, Check)

View Source

    -spec check(This, Id, Check) -> ok when This :: wxMenu(), Id :: integer(), Check :: boolean().

Checks or unchecks the menu item.

See: `isChecked/2`

Link to this function

# 'Destroy'/2

View Source

    -spec 'Destroy'(This, Id) -> boolean() when This :: wxMenu(), Id :: integer();
                   (This, Item) -> boolean() when This :: wxMenu(), Item :: wxMenuItem:wxMenuItem().

Deletes the menu item from the menu.

If the item is a submenu, it will be deleted. Use `remove/2` if you want to keep the submenu (for example, to reuse it later).

See:

- `findItem/2`

- `delete/2`

- `remove/2`

Link to this function

# delete/2

View Source

    -spec delete(This, Id) -> boolean() when This :: wxMenu(), Id :: integer();
                (This, Item) -> boolean() when This :: wxMenu(), Item :: wxMenuItem:wxMenuItem().

Deletes the menu item from the menu.

If the item is a submenu, it will `not` be deleted. Use `'Destroy'/2` if you want to delete a submenu.

See:

- `findItem/2`

- `'Destroy'/2`

- `remove/2`

Link to this function

# destroy(This)

View Source

    -spec destroy(This :: wxMenu()) -> ok.

Destroys the object

Link to this function

# enable(This, Id, Enable)

View Source

    -spec enable(This, Id, Enable) -> ok when This :: wxMenu(), Id :: integer(), Enable :: boolean().

Enables or disables (greys out) a menu item.

See: `isEnabled/2`

Link to this function

# findItem/2

View Source

    -spec findItem(This, Id) -> wxMenuItem:wxMenuItem() when This :: wxMenu(), Id :: integer();
                  (This, ItemString) -> integer() when This :: wxMenu(), ItemString :: unicode:chardata().

Finds the menu id for a menu item string.

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

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

Link to this function

# findItemByPosition(This, Position)

View Source

    -spec findItemByPosition(This, Position) -> wxMenuItem:wxMenuItem()
                                when This :: wxMenu(), Position :: integer().

Returns the `wxMenuItem` given a position in the menu.

Link to this function

# getHelpString(This, Id)

View Source

    -spec getHelpString(This, Id) -> unicode:charlist() when This :: wxMenu(), Id :: integer().

Returns the help string associated with a menu item.

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

See:

- `setHelpString/3`

- `append/5`

Link to this function

# getLabel(This, Id)

View Source

    -spec getLabel(This, Id) -> unicode:charlist() when This :: wxMenu(), Id :: integer().

Returns a menu item label.

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

See: `setLabel/3`

Link to this function

# getMenuItemCount(This)

View Source

    -spec getMenuItemCount(This) -> integer() when This :: wxMenu().

Returns the number of items in the menu.

Link to this function

# getMenuItems(This)

View Source

    -spec getMenuItems(This) -> [wxMenuItem:wxMenuItem()] when This :: wxMenu().

Link to this function

# getTitle(This)

View Source

    -spec getTitle(This) -> unicode:charlist() when This :: wxMenu().

Returns the title of the menu.

See: `setTitle/2`

Link to this function

# insert/3

View Source

    -spec insert(This, Pos, Id) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), Pos :: integer(), Id :: integer();
                (This, Pos, MenuItem) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), Pos :: integer(), MenuItem :: wxMenuItem:wxMenuItem().

Inserts the given `item` before the position `pos`.

Inserting the item at position `getMenuItemCount/1` is the same as appending it.

See:

- `append/5`

- `prepend/5`

Link to this function

# insert/4

View Source

    -spec insert(This, Pos, Id, [Option]) -> wxMenuItem:wxMenuItem()
                    when
                        This :: wxMenu(),
                        Pos :: integer(),
                        Id :: integer(),
                        Option ::
                            {text, unicode:chardata()} | {help, unicode:chardata()} | {kind, wx:wx_enum()}.

Inserts the given `item` before the position `pos`.

Inserting the item at position `getMenuItemCount/1` is the same as appending it.

See:

- `append/5`

- `prepend/5`

Link to this function

# insert(This, Pos, Id, Text, Submenu)

View Source

    -spec insert(This, Pos, Id, Text, Submenu) -> wxMenuItem:wxMenuItem()
                    when
                        This :: wxMenu(),
                        Pos :: integer(),
                        Id :: integer(),
                        Text :: unicode:chardata(),
                        Submenu :: wxMenu().

Equivalent to `insert(This, Pos, Id, Text, Submenu, [])`.

Link to this function

# insert/6

View Source

    -spec insert(This, Pos, Id, Text, Submenu, [Option]) -> wxMenuItem:wxMenuItem()
                    when
                        This :: wxMenu(),
                        Pos :: integer(),
                        Id :: integer(),
                        Text :: unicode:chardata(),
                        Submenu :: wxMenu(),
                        Option :: {help, unicode:chardata()}.

Inserts the given `submenu` before the position `pos`.

`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.

See: `prepend/5`

Link to this function

# insertCheckItem(This, Pos, Id, Item)

View Source

    -spec insertCheckItem(This, Pos, Id, Item) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Pos :: integer(),
                                 Id :: integer(),
                                 Item :: unicode:chardata().

Equivalent to `insertCheckItem(This, Pos, Id, Item, [])`.

Link to this function

# insertCheckItem/5

View Source

    -spec insertCheckItem(This, Pos, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Pos :: integer(),
                                 Id :: integer(),
                                 Item :: unicode:chardata(),
                                 Option :: {help, unicode:chardata()}.

Inserts a checkable item at the given position.

See:

- `insert/6`

- `appendCheckItem/4`

Link to this function

# insertRadioItem(This, Pos, Id, Item)

View Source

    -spec insertRadioItem(This, Pos, Id, Item) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Pos :: integer(),
                                 Id :: integer(),
                                 Item :: unicode:chardata().

Equivalent to `insertRadioItem(This, Pos, Id, Item, [])`.

Link to this function

# insertRadioItem/5

View Source

    -spec insertRadioItem(This, Pos, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                             when
                                 This :: wxMenu(),
                                 Pos :: integer(),
                                 Id :: integer(),
                                 Item :: unicode:chardata(),
                                 Option :: {help, unicode:chardata()}.

Inserts a radio item at the given position.

See:

- `insert/6`

- `appendRadioItem/4`

Link to this function

# insertSeparator(This, Pos)

View Source

    -spec insertSeparator(This, Pos) -> wxMenuItem:wxMenuItem() when This :: wxMenu(), Pos :: integer().

Inserts a separator at the given position.

See:

- `insert/6`

- `appendSeparator/1`

Link to this function

# isChecked(This, Id)

View Source

    -spec isChecked(This, Id) -> boolean() when This :: wxMenu(), Id :: integer().

Determines whether a menu item is checked.

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

See: `check/3`

Link to this function

# isEnabled(This, Id)

View Source

    -spec isEnabled(This, Id) -> boolean() when This :: wxMenu(), Id :: integer().

Determines whether a menu item is enabled.

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

See: `enable/3`

Link to this function

# new()

View Source

    -spec new() -> wxMenu().

Constructs a `wxMenu` object.

Link to this function

# new(Options)

View Source

    -spec new([Option]) -> wxMenu() when Option :: {style, integer()}.

Constructs a `wxMenu` object.

Link to this function

# new(Title, Options)

View Source

    -spec new(Title, [Option]) -> wxMenu() when Title :: unicode:chardata(), Option :: {style, integer()}.

Constructs a `wxMenu` object with a title.

Link to this function

# prepend/2

View Source

    -spec prepend(This, Id) -> wxMenuItem:wxMenuItem() when This :: wxMenu(), Id :: integer();
                 (This, Item) -> wxMenuItem:wxMenuItem()
                     when This :: wxMenu(), Item :: wxMenuItem:wxMenuItem().

Inserts the given `item` at position 0, i.e. before all the other existing items.

See:

- `append/5`

- `insert/6`

Link to this function

# prepend/3

View Source

    -spec prepend(This, Id, [Option]) -> wxMenuItem:wxMenuItem()
                     when
                         This :: wxMenu(),
                         Id :: integer(),
                         Option ::
                             {text, unicode:chardata()} | {help, unicode:chardata()} | {kind, wx:wx_enum()}.

Inserts the given `item` at position 0, i.e. before all the other existing items.

See:

- `append/5`

- `insert/6`

Link to this function

# prepend(This, Id, Text, Submenu)

View Source

    -spec prepend(This, Id, Text, Submenu) -> wxMenuItem:wxMenuItem()
                     when This :: wxMenu(), Id :: integer(), Text :: unicode:chardata(), Submenu :: wxMenu().

Equivalent to `prepend(This, Id, Text, Submenu, [])`.

Link to this function

# prepend/5

View Source

    -spec prepend(This, Id, Text, Submenu, [Option]) -> wxMenuItem:wxMenuItem()
                     when
                         This :: wxMenu(),
                         Id :: integer(),
                         Text :: unicode:chardata(),
                         Submenu :: wxMenu(),
                         Option :: {help, unicode:chardata()}.

Inserts the given `submenu` at position 0.

See: `insert/6`

Link to this function

# prependCheckItem(This, Id, Item)

View Source

    -spec prependCheckItem(This, Id, Item) -> wxMenuItem:wxMenuItem()
                              when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata().

Equivalent to `prependCheckItem(This, Id, Item, [])`.

Link to this function

# prependCheckItem/4

View Source

    -spec prependCheckItem(This, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                              when
                                  This :: wxMenu(),
                                  Id :: integer(),
                                  Item :: unicode:chardata(),
                                  Option :: {help, unicode:chardata()}.

Inserts a checkable item at position 0.

See:

- `prepend/5`

- `appendCheckItem/4`

Link to this function

# prependRadioItem(This, Id, Item)

View Source

    -spec prependRadioItem(This, Id, Item) -> wxMenuItem:wxMenuItem()
                              when This :: wxMenu(), Id :: integer(), Item :: unicode:chardata().

Equivalent to `prependRadioItem(This, Id, Item, [])`.

Link to this function

# prependRadioItem/4

View Source

    -spec prependRadioItem(This, Id, Item, [Option]) -> wxMenuItem:wxMenuItem()
                              when
                                  This :: wxMenu(),
                                  Id :: integer(),
                                  Item :: unicode:chardata(),
                                  Option :: {help, unicode:chardata()}.

Inserts a radio item at position 0.

See:

- `prepend/5`

- `appendRadioItem/4`

Link to this function

# prependSeparator(This)

View Source

    -spec prependSeparator(This) -> wxMenuItem:wxMenuItem() when This :: wxMenu().

Inserts a separator at position 0.

See:

- `prepend/5`

- `appendSeparator/1`

Link to this function

# remove/2

View Source

    -spec remove(This, Id) -> wxMenuItem:wxMenuItem() when This :: wxMenu(), Id :: integer();
                (This, Item) -> wxMenuItem:wxMenuItem()
                    when This :: wxMenu(), Item :: wxMenuItem:wxMenuItem().

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).

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

Link to this function

# setHelpString(This, Id, HelpString)

View Source

    -spec setHelpString(This, Id, HelpString) -> ok
                           when This :: wxMenu(), Id :: integer(), HelpString :: unicode:chardata().

Sets an item's help string.

See: `getHelpString/2`

Link to this function

# setLabel(This, Id, Label)

View Source

    -spec setLabel(This, Id, Label) -> ok
                      when This :: wxMenu(), Id :: integer(), Label :: unicode:chardata().

Sets the label of a menu item.

See:

- `append/5`

- `getLabel/2`

Link to this function

# setTitle(This, Title)

View Source

    -spec setTitle(This, Title) -> ok when This :: wxMenu(), Title :: unicode:chardata().

Sets the title of the menu.

Remark: 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/3`.

See: `getTitle/1`