wxRuby Documentation Home

# Wx::MenuBar

A menu bar is a series of menus accessible from the top of a frame.

## Derived from

Window

EvtHandler

Object

## Event handling

To respond to a menu selection, provide a handler for EVT_MENU, in the frame
that contains the menu bar. If you have a toolbar which uses the same identifiers
as your EVT_MENU entries, events from the toolbar will also be processed by your
EVT_MENU event handlers.

Tip: under Windows, if you discover that menu shortcuts (for example, Alt-F to show the file menu)
are not working, check any EVT_CHAR events you are handling in child windows.
If you are not calling `event.Skip()` for events that you don’t process in these event handlers,
menu shortcuts may cease to work.

## See also

Menu, Event handling overview

## Methods

- MenuBar.new
- MenuBar#append
- MenuBar#check
- MenuBar#enable
- MenuBar#enable_top
- MenuBar#find_menu
- MenuBar#find_menu_item
- MenuBar#find_item
- MenuBar#get_help_string
- MenuBar#get_label
- MenuBar#get_label_top
- MenuBar#get_menu
- MenuBar#get_menu_count
- MenuBar#insert
- MenuBar#is_checked
- MenuBar#is_enabled
- MenuBar#refresh
- MenuBar#remove
- MenuBar#replace
- MenuBar#set_help_string
- MenuBar#set_label
- MenuBar#set_label_top

### MenuBar.new

MenuBar.new(%(arg-type)Integer% style = 0)

Default constructor.

MenuBar.new(%(arg-type)Integer% n, Menu menus[], String titles[], Integer style = 0)

Construct a menu bar from arrays of menus and titles.

#### Parameters

- n The number of menus.
- menus An array of menus. Do not use this array again – it now belongs to the
  menu bar.
- titles An array of title strings. Deallocate this array after creating the menu bar.
- style If `MB_DOCKABLE` the menu bar can be detached (GTK only).

destructor()

Destructor, destroying the menu bar and removing it from the parent frame (if any).

### MenuBar#append

Boolean append(%(arg-type)Menu% menu, String title)

Adds the item to the end of the menu bar.

#### Parameters

- menu The menu to add. Do not deallocate this menu after calling Append.
- title The title of the menu.

#### Return value

true on success, false if an error occurred.

#### See also

MenuBar#insert

### MenuBar#check

check(%(arg-type)Integer% id, Boolean check)

Checks or unchecks a menu item.

#### Parameters

- id The menu item identifier.
- check If true, checks the menu item, otherwise the item is unchecked.

#### Remarks

Only use this when the menu bar has been associated
with a frame; otherwise, use the Menu equivalent call.

### MenuBar#enable

enable(%(arg-type)Integer% id, Boolean enable)

Enables or disables (greys out) a menu item.

#### Parameters

- id The menu item identifier.
- enable true to enable the item, false to disable it.

#### Remarks

Only use this when the menu bar has been
associated with a frame; otherwise, use the Menu equivalent call.

### MenuBar#enable_top

enable_top(%(arg-type)Integer% pos, Boolean enable)

Enables or disables a whole menu.

#### Parameters

- pos The position of the menu, starting from zero.
- enable true to enable the menu, false to disable it.

#### Remarks

Only use this when the menu bar has been
associated with a frame.

### MenuBar#find_menu

Integer find_menu(%(arg-type)String% title)

Returns the index of the menu with the given title or `NOT_FOUND` if no
such menu exists in this menubar. The title parameter may specify either
the menu title (with accelerator characters, i.e. `"&File"`) or just the
menu label (`"File"`) indifferently.

### MenuBar#find_menu_item

Integer find_menu_item(%(arg-type)String% menuString, String itemString)

Finds the menu item id for a menu name/menu item string pair.

#### Parameters

- menuString Menu title to find.
- itemString Item to find.

#### Return value

The menu item identifier, or `NOT_FOUND` if none was found.

#### Remarks

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

### MenuBar#find_item

MenuItem find_item(%(arg-type)Integer% id, Menu *menu = nil)

Finds the menu item object associated with the given menu item identifier.

#### Parameters

- id Menu item identifier.
- menu If not NULL, menu will get set to the associated menu.

#### Return value

The found menu item object, or NULL if one was not found.

### MenuBar#get_help_string

String get_help_string(%(arg-type)Integer% id)

Gets the help string associated with the menu item identifier.

#### Parameters

- id The menu item identifier.

#### Return value

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

#### See also

MenuBar#set_help_string

### MenuBar#get_label

String get_label(%(arg-type)Integer% id)

Gets the label associated with a menu item.

#### Parameters

- id The menu item identifier.

#### Return value

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

#### Remarks

Use only after the menubar has been associated with a frame.

### MenuBar#get_label_top

String get_label_top(%(arg-type)Integer% pos)

Returns the label of a top-level menu. Note that the returned string does not
include the accelerator characters which could have been specified in the menu
title string during its construction.

#### Parameters

- pos Position of the menu on the menu bar, starting from zero.

#### Return value

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

#### Remarks

Use only after the menubar has been associated with a frame.

#### See also

MenuBar#set_label_top

### MenuBar#get_menu

Menu get_menu(%(arg-type)Integer% menuIndex)

Returns the menu at menuIndex (zero-based).

### MenuBar#get_menu_count

Integer get_menu_count()

Returns the number of menus in this menubar.

### MenuBar#insert

Boolean insert(%(arg-type)Integer% pos, Menu menu, String title)

Inserts the menu at the given position into the menu bar. Inserting menu at
position $0$ will insert it in the very beginning of it, inserting at position
get_menu_count is the same as calling
Append.

#### Parameters

- pos The position of the new menu in the menu bar
- menu The menu to add. MenuBar owns the menu and will free it.
- title The title of the menu.

#### Return value

true on success, false if an error occurred.

#### See also

MenuBar#append

### MenuBar#is_checked

Boolean is_checked(%(arg-type)Integer% id)

Determines whether an item is checked.

#### Parameters

- id The menu item identifier.

#### Return value

true if the item was found and is checked, false otherwise.

### MenuBar#is_enabled

Boolean is_enabled(%(arg-type)Integer% id)

Determines whether an item is enabled.

#### Parameters

- id The menu item identifier.

#### Return value

true if the item was found and is enabled, false otherwise.

### MenuBar#refresh

refresh()

Redraw the menu bar

### MenuBar#remove

Menu remove(%(arg-type)Integer% pos)

Removes the menu from the menu bar and returns the menu object – the caller is
responsible for deleting it. This function may be used together with
MenuBar#insert to change the menubar
dynamically.

#### See also

MenuBar#replace

### MenuBar#replace

Menu replace(%(arg-type)Integer% pos, Menu menu, String title)

Replaces the menu at the given position with another one.

#### Parameters

- pos The position of the new menu in the menu bar
- menu The menu to add.
- title The title of the menu.

#### Return value

The menu which was previously at position pos. The caller is
responsible for deleting it.

#### See also

MenuBar#insert, MenuBar#remove

### MenuBar#set_help_string

set_help_string(%(arg-type)Integer% id, String helpString)

Sets the help string associated with a menu item.

#### Parameters

- id Menu item identifier.
- helpString Help string to associate with the menu item.

#### See also

MenuBar#get_help_string

### MenuBar#set_label

set_label(%(arg-type)Integer% id, String label)

Sets the label of a menu item.

#### Parameters

- id Menu item identifier.
- label Menu item label.

#### Remarks

Use only after the menubar has been associated with a frame.

#### See also

MenuBar#get_label

### MenuBar#set_label_top

set_label_top(%(arg-type)Integer% pos, String label)

Sets the label of a top-level menu.

#### Parameters

- pos The position of a menu on the menu bar, starting from zero.
- label The menu label.

#### Remarks

Use only after the menubar has been associated with a frame.

#### See also

MenuBar#get_label_top

[This page automatically generated from the Textile source at 2023-06-09 00:45:26 +0000]