Class: Wx::MenuItem
Overview
A menu item represents an item in a menu.
Note that you usually don’t have to deal with it directly as Menu methods usually construct an object of this class for you.
Events emitted by this class
The following event-handler methods redirect the events to member method or handler blocks for MenuEvent events. Event handler methods for events emitted by this class:
-
EvtHandler#evt_menu(id, meth = nil, &block): Process a EVT_MENU command, which is generated by a menu item. This type of event is sent as CommandEvent.
-
EvtHandler#evt_menu_range(id1, id2, meth = nil, &block): Process a EVT_MENU command, which is generated by a range of menu items. This type of event is sent as CommandEvent.
-
EvtHandler#evt_menu_open(meth = nil, &block): 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 MenuEvent.
-
EvtHandler#evt_menu_close(meth = nil, &block): A menu has been just closed. This type of event is sent as MenuEvent.
-
EvtHandler#evt_menu_highlight(id, meth = nil, &block): The menu item with the specified id has been highlighted. If the id is StandardID::ID_NONE, highlighting has been removed from the previously highlighted menu item and there is no highlighted item any more. This is used by Frame to show help prompts in the status bar. This type of event is sent as MenuEvent.
-
EvtHandler#evt_menu_highlight_all(meth = nil, &block): A menu item has been highlighted, i.e. the currently selected menu item has changed. This type of event is sent as MenuEvent.
Customizing Menu Items Appearance
This class provides several functions for customizing the menu items appearance that are available only in WXMSW port. These functions should not be used in portable programs and, moreover, have some known limitations even in WXMSW: using them switches the menu item in a “owner drawn” mode, where it is painted by wxWidgets itself and not the system, which may result in a slightly different appearance for it. Owner drawn items are also incompatible with dark mode support and using them makes the entire menu containing them to use standard light mode colours even in dark mode.
Category: Menus
Class Method Summary collapse
-
.get_label_text(text) ⇒ String
Strips all accelerator characters and mnemonics from the given text.
Instance Method Summary collapse
-
#add_extra_accel(accel) ⇒ void
Add an extra accelerator for this menu item.
-
#check(check = true) ⇒ void
Checks or unchecks the menu item.
-
#clear_extra_accels ⇒ void
Clear the extra accelerators list.
-
#enable(enable = true) ⇒ void
Enables or disables the menu item.
-
#get_accel ⇒ Wx::AcceleratorEntry
(also: #accel)
Get our accelerator or nil (caller must delete the pointer).
-
#get_background_colour ⇒ Wx::Colour
(also: #background_colour)
Returns the background colour associated with the menu item.
-
#get_bitmap ⇒ Wx::Bitmap
(also: #bitmap)
Returns the item bitmap.
-
#get_bitmap_bundle ⇒ Wx::BitmapBundle
(also: #bitmap_bundle)
Returns the bitmap bundle containing the bitmap used for this item.
-
#get_disabled_bitmap ⇒ Wx::Bitmap
(also: #disabled_bitmap)
Returns the bitmap used for disabled items.
-
#get_font ⇒ Wx::Font
(also: #font)
Returns the font associated with the menu item.
-
#get_help ⇒ Wx::String
(also: #help)
Returns the help string associated with the menu item.
-
#get_id ⇒ Integer
(also: #id)
Returns the menu item identifier.
-
#get_item_label ⇒ String
(also: #item_label)
Returns the text associated with the menu item including any accelerator characters that were passed to the constructor or #set_item_label.
-
#get_item_label_text ⇒ String
(also: #item_label_text)
Returns the text associated with the menu item, without any accelerator characters.
-
#get_kind ⇒ Wx::ItemKind
(also: #kind)
Returns the item kind, one of ItemKind::ITEM_SEPARATOR, ItemKind::ITEM_NORMAL, ItemKind::ITEM_CHECK or ItemKind::ITEM_RADIO.
-
#get_margin_width ⇒ Integer
(also: #margin_width)
Gets the width of the menu item checkmark bitmap.
-
#get_menu ⇒ Wx::Menu
(also: #menu)
Returns the menu this menu item is in, or nil if this menu item is not attached.
-
#get_sub_menu ⇒ Wx::Menu
(also: #sub_menu)
Returns the submenu associated with the menu item, or nil if there isn’t one.
-
#get_text_colour ⇒ Wx::Colour
(also: #text_colour)
Returns the text colour associated with the menu item.
-
#initialize(parentMenu = nil, id = Wx::StandardID::ID_SEPARATOR, text = (''), helpString = (''), kind = Wx::ItemKind::ITEM_NORMAL, subMenu = nil) ⇒ Wx::MenuItem
constructor
Constructs a MenuItem object.
-
#is_check ⇒ Boolean
(also: #check?)
Returns true if the item is a check item.
-
#is_checkable ⇒ Boolean
(also: #checkable?)
Returns true if the item is checkable.
-
#is_checked ⇒ Boolean
(also: #checked?)
Returns true if the item is checked.
-
#is_enabled ⇒ Boolean
(also: #enabled?)
Returns true if the item is enabled.
-
#is_radio ⇒ Boolean
(also: #radio?)
Returns true if the item is a radio button.
-
#is_separator ⇒ Boolean
(also: #separator?)
Returns true if the item is a separator.
-
#is_sub_menu ⇒ Boolean
(also: #sub_menu?)
Returns true if the item is a submenu.
-
#set_accel(accel) ⇒ void
(also: #accel=)
Set the accel for this item - this may also be done indirectly with set_text.
-
#set_background_colour(colour) ⇒ void
Sets the background colour associated with the menu item.
- #set_bitmap(*args) ⇒ Object (also: #bitmap=)
-
#set_bitmaps(checked, unchecked = Wx::NULL_BITMAP) ⇒ void
Sets the checked/unchecked bitmaps for the menu item.
-
#set_disabled_bitmap(disabled) ⇒ void
Sets the to be used for disabled menu items.
-
#set_font(font) ⇒ void
Sets the font associated with the menu item.
-
#set_help(helpString) ⇒ void
(also: #help=)
Sets the help string.
-
#set_item_label(label) ⇒ void
(also: #item_label=)
Sets the label associated with the menu item.
-
#set_margin_width(width) ⇒ void
Sets the width of the menu item checkmark bitmap.
-
#set_menu(menu) ⇒ void
(also: #menu=)
Sets the parent menu which will contain this menu item.
-
#set_sub_menu(menu) ⇒ void
(also: #sub_menu=)
Sets the submenu of this menu item.
-
#set_text_colour(colour) ⇒ void
Sets the text colour associated with the menu item.
Methods inherited from Object
#clone, #dup, #is_same_as, #un_share
Constructor Details
#initialize(parentMenu = nil, id = Wx::StandardID::ID_SEPARATOR, text = (''), helpString = (''), kind = Wx::ItemKind::ITEM_NORMAL, subMenu = nil) ⇒ Wx::MenuItem
Constructs a Wx::MenuItem 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 #set_bitmap 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(Wx::ID_ABOUT, "", "My custom help string")
# use all stock properties except for the bitmap:
= Wx::MenuItem.new(helpMenu, Wx::ID_ABOUT)
.set_bitmap(Wx::ArtProvider.get_bitmap(Wx::ART_WARNING))
helpMenu.append()
that is, stock properties are set independently one from the other.
433 |
# File 'lib/wx/doc/gen/menu_item.rb', line 433 def initialize(parentMenu=nil, id=Wx::StandardID::ID_SEPARATOR, text=(''), helpString=(''), kind=Wx::ItemKind::ITEM_NORMAL, subMenu=nil) end |
Class Method Details
Instance Method Details
#add_extra_accel(accel) ⇒ void
This method returns an undefined value.
Add an extra accelerator for this menu item.
Additional accelerators are not shown in the item’s label, but still will trigger the menu command when pressed. They can be useful to let multiple keys be used as accelerators for the same command, e.g. KeyCode::K_ADD and KeyCode::K_NUMPAD_ADD. Availability: only available for the WXMSW, WXGTK ports.
397 |
# File 'lib/wx/doc/gen/menu_item.rb', line 397 def add_extra_accel(accel) end |
#check(check = true) ⇒ void
This method returns an undefined value.
Checks or unchecks the menu item.
Note that this only works when the item is already appended to a menu.
440 |
# File 'lib/wx/doc/gen/menu_item.rb', line 440 def check(check=true) end |
#clear_extra_accels ⇒ void
This method returns an undefined value.
Clear the extra accelerators list.
This doesn’t affect the main item accelerator (if any).
403 |
# File 'lib/wx/doc/gen/menu_item.rb', line 403 def clear_extra_accels; end |
#enable(enable = true) ⇒ void
This method returns an undefined value.
Enables or disables the menu item.
445 |
# File 'lib/wx/doc/gen/menu_item.rb', line 445 def enable(enable=true) end |
#get_accel ⇒ Wx::AcceleratorEntry Also known as: accel
Get our accelerator or nil (caller must delete the pointer)
142 |
# File 'lib/wx/doc/gen/menu_item.rb', line 142 def get_accel; end |
#get_background_colour ⇒ Wx::Colour Also known as: background_colour
Returns the background colour associated with the menu item.
Availability: only available for the WXMSW port.
46 |
# File 'lib/wx/doc/gen/menu_item.rb', line 46 def get_background_colour; end |
#get_bitmap ⇒ Wx::Bitmap Also known as: bitmap
Returns the item bitmap.
This method exists only for compatibility, please use #get_bitmap_bundle in the new code.
53 |
# File 'lib/wx/doc/gen/menu_item.rb', line 53 def get_bitmap; end |
#get_bitmap_bundle ⇒ Wx::BitmapBundle Also known as: bitmap_bundle
Returns the bitmap bundle containing the bitmap used for this item.
The returned bundle is invalid, i.e. empty, if no bitmap is associated with the item.
61 |
# File 'lib/wx/doc/gen/menu_item.rb', line 61 def get_bitmap_bundle; end |
#get_disabled_bitmap ⇒ Wx::Bitmap Also known as: disabled_bitmap
Returns the bitmap used for disabled items.
Availability: only available for the WXMSW port.
69 |
# File 'lib/wx/doc/gen/menu_item.rb', line 69 def get_disabled_bitmap; end |
#get_font ⇒ Wx::Font Also known as: font
Returns the font associated with the menu item.
Availability: only available for the WXMSW port.
77 |
# File 'lib/wx/doc/gen/menu_item.rb', line 77 def get_font; end |
#get_help ⇒ Wx::String Also known as: help
Returns the help string associated with the menu item.
82 |
# File 'lib/wx/doc/gen/menu_item.rb', line 82 def get_help; end |
#get_id ⇒ Integer Also known as: id
Returns the menu item identifier.
87 |
# File 'lib/wx/doc/gen/menu_item.rb', line 87 def get_id; end |
#get_item_label ⇒ String Also known as: item_label
Returns the text associated with the menu item including any accelerator characters that were passed to the constructor or #set_item_label.
96 |
# File 'lib/wx/doc/gen/menu_item.rb', line 96 def get_item_label; end |
#get_item_label_text ⇒ String Also known as: item_label_text
Returns the text associated with the menu item, without any accelerator characters.
105 |
# File 'lib/wx/doc/gen/menu_item.rb', line 105 def get_item_label_text; end |
#get_kind ⇒ Wx::ItemKind Also known as: kind
Returns the item kind, one of ItemKind::ITEM_SEPARATOR, ItemKind::ITEM_NORMAL, ItemKind::ITEM_CHECK or ItemKind::ITEM_RADIO.
110 |
# File 'lib/wx/doc/gen/menu_item.rb', line 110 def get_kind; end |
#get_margin_width ⇒ Integer Also known as: margin_width
Gets the width of the menu item checkmark bitmap.
Availability: only available for the WXMSW port.
118 |
# File 'lib/wx/doc/gen/menu_item.rb', line 118 def get_margin_width; end |
#get_menu ⇒ Wx::Menu Also known as:
Returns the menu this menu item is in, or nil if this menu item is not attached.
123 |
# File 'lib/wx/doc/gen/menu_item.rb', line 123 def ; end |
#get_sub_menu ⇒ Wx::Menu Also known as:
Returns the submenu associated with the menu item, or nil if there isn’t one.
128 |
# File 'lib/wx/doc/gen/menu_item.rb', line 128 def ; end |
#get_text_colour ⇒ Wx::Colour Also known as: text_colour
Returns the text colour associated with the menu item.
Availability: only available for the WXMSW port.
136 |
# File 'lib/wx/doc/gen/menu_item.rb', line 136 def get_text_colour; end |
#is_check ⇒ Boolean Also known as: check?
Returns true if the item is a check item.
Unlike #is_checkable this doesn’t return true for the radio buttons.
149 |
# File 'lib/wx/doc/gen/menu_item.rb', line 149 def is_check; end |
#is_checkable ⇒ Boolean Also known as: checkable?
Returns true if the item is checkable.
Notice that the radio buttons are considered to be checkable as well, so this method returns true for them too. Use #is_check if you want to test for the check items only.
156 |
# File 'lib/wx/doc/gen/menu_item.rb', line 156 def is_checkable; end |
#is_checked ⇒ Boolean Also known as: checked?
Returns true if the item is checked.
161 |
# File 'lib/wx/doc/gen/menu_item.rb', line 161 def is_checked; end |
#is_enabled ⇒ Boolean Also known as: enabled?
Returns true if the item is enabled.
166 |
# File 'lib/wx/doc/gen/menu_item.rb', line 166 def is_enabled; end |
#is_radio ⇒ Boolean Also known as: radio?
Returns true if the item is a radio button.
171 |
# File 'lib/wx/doc/gen/menu_item.rb', line 171 def is_radio; end |
#is_separator ⇒ Boolean Also known as: separator?
Returns true if the item is a separator.
176 |
# File 'lib/wx/doc/gen/menu_item.rb', line 176 def is_separator; end |
#is_sub_menu ⇒ Boolean Also known as:
Returns true if the item is a submenu.
181 |
# File 'lib/wx/doc/gen/menu_item.rb', line 181 def ; end |
#set_accel(accel) ⇒ void Also known as: accel=
This method returns an undefined value.
Set the accel for this item - this may also be done indirectly with set_text
386 |
# File 'lib/wx/doc/gen/menu_item.rb', line 386 def set_accel(accel) end |
#set_background_colour(colour) ⇒ void
This method returns an undefined value.
Sets the background colour associated with the menu item.
Please note that this function only exists in WXMSW and works by making the menu item “owner-drawn” there, please see the note about such items in this class description.
190 |
# File 'lib/wx/doc/gen/menu_item.rb', line 190 def set_background_colour(colour) end |
#set_bitmap(bmp) ⇒ void #set_bitmap(bmp, checked) ⇒ void Also known as: bitmap=
208 |
# File 'lib/wx/doc/gen/menu_item.rb', line 208 def set_bitmap(*args) end |
#set_bitmaps(checked, unchecked = Wx::NULL_BITMAP) ⇒ void
This method returns an undefined value.
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.
219 |
# File 'lib/wx/doc/gen/menu_item.rb', line 219 def set_bitmaps(checked, unchecked=Wx::NULL_BITMAP) end |
#set_disabled_bitmap(disabled) ⇒ void
This method returns an undefined value.
Sets the to be used for disabled menu items.
Availability: only available for the WXMSW port.
227 |
# File 'lib/wx/doc/gen/menu_item.rb', line 227 def set_disabled_bitmap(disabled) end |
#set_font(font) ⇒ void
This method returns an undefined value.
Sets the font associated with the menu item.
Please note that this function only exists in WXMSW and works by making the menu item “owner-drawn” there, please see the note about such items in this class description.
235 |
# File 'lib/wx/doc/gen/menu_item.rb', line 235 def set_font(font) end |
#set_help(helpString) ⇒ void Also known as: help=
This method returns an undefined value.
Sets the help string.
240 |
# File 'lib/wx/doc/gen/menu_item.rb', line 240 def set_help(helpString) end |
#set_item_label(label) ⇒ void Also known as: item_label=
This method returns an undefined value.
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"
, "RAWCTRL"
, "ALT"
and "SHIFT"
strings (case doesn’t matter) separated by either '-'
or '+'
characters and followed by the accelerator itself. Note that the displayed accelerator can differ from the string specified here though, e.g. Ctrl+X
could be actually shown on the screen when Ctrl-X
is used. Notice that CTRL corresponds to the “Ctrl” key on most platforms but not under macOS where it is mapped to “Cmd” key on Mac keyboard. Usually this is exactly what you want in portable code but if you really need to use the (rarely used for this purpose) “Ctrl” key even under Mac, you may use RAWCTRL to prevent this mapping. Under the other platforms RAWCTRL is the same as plain CTRL. The accelerator may be any alphanumeric character, any function key (from F1 to F12), any numpad digit key using KP_ prefix (i.e. from KP_0 to KP_9) or one of the special strings listed below (again, case doesn’t matter) corresponding to the specified key code:
-
Del or Delete: KeyCode::K_DELETE
-
Back: KeyCode::K_BACK
-
Ins or Insert: KeyCode::K_INSERT
-
Enter or Return: KeyCode::K_RETURN
-
PgUp or PageUp: KeyCode::K_PAGEUP
-
PgDn or PageDown: KeyCode::K_PAGEDOWN
-
Left: KeyCode::K_LEFT
-
Right: KeyCode::K_RIGHT
-
Up: KeyCode::K_UP
-
Down: KeyCode::K_DOWN
-
Home: KeyCode::K_HOME
-
End: KeyCode::K_END
-
Space: KeyCode::K_SPACE
-
Tab: KeyCode::K_TAB
-
Esc or Escape: KeyCode::K_ESCAPE
-
Cancel: KeyCode::K_CANCEL
-
Clear: KeyCode::K_CLEAR
-
Menu: KeyCode::K_MENU
-
Pause: KeyCode::K_PAUSE
-
Capital: KeyCode::K_CAPITAL
-
Select: KeyCode::K_SELECT
-
Print: KeyCode::K_PRINT
-
Execute: KeyCode::K_EXECUTE
-
Snapshot: KeyCode::K_SNAPSHOT
-
Help: KeyCode::K_HELP
-
Add: KeyCode::K_ADD
-
Separator: KeyCode::K_SEPARATOR
-
Subtract: KeyCode::K_SUBTRACT
-
Decimal: KeyCode::K_DECIMAL
-
Divide: KeyCode::K_DIVIDE
-
Num_lock: KeyCode::K_NUMLOCK
-
Scroll_lock: KeyCode::K_SCROLL
-
KP_Space: KeyCode::K_NUMPAD_SPACE
-
KP_Tab: KeyCode::K_NUMPAD_TAB
-
KP_Enter: KeyCode::K_NUMPAD_ENTER
-
KP_Home: KeyCode::K_NUMPAD_HOME
-
KP_Left: KeyCode::K_NUMPAD_LEFT
-
KP_Up: KeyCode::K_NUMPAD_UP
-
KP_Right: KeyCode::K_NUMPAD_RIGHT
-
KP_Down: KeyCode::K_NUMPAD_DOWN
-
KP_PageUp: KeyCode::K_NUMPAD_PAGEUP
-
KP_PageDown: KeyCode::K_NUMPAD_PAGEDOWN
-
KP_Prior: KeyCode::K_NUMPAD_PAGEUP
-
KP_Next: KeyCode::K_NUMPAD_PAGEDOWN
-
KP_End: KeyCode::K_NUMPAD_END
-
KP_Begin: KeyCode::K_NUMPAD_BEGIN
-
KP_Insert: KeyCode::K_NUMPAD_INSERT
-
KP_Delete: KeyCode::K_NUMPAD_DELETE
-
KP_Equal: KeyCode::K_NUMPAD_EQUAL
-
KP_Multiply: KeyCode::K_NUMPAD_MULTIPLY
-
KP_Add: KeyCode::K_NUMPAD_ADD
-
KP_Separator: KeyCode::K_NUMPAD_SEPARATOR
-
KP_Subtract: KeyCode::K_NUMPAD_SUBTRACT
-
KP_Decimal: KeyCode::K_NUMPAD_DECIMAL
-
KP_Divide: KeyCode::K_NUMPAD_DIVIDE
-
Windows_Left: KeyCode::K_WINDOWS_LEFT
-
Windows_Right: KeyCode::K_WINDOWS_RIGHT
-
Windows_Menu: KeyCode::K_WINDOWS_MENU
-
Command: KeyCode::K_COMMAND
Examples:
@my_menu_item.set_item_label("My &item\tCTRL+I")
@my_menu_item2.set_item_label("Clean && build\tF7")
@my_menu_item3.set_item_label("Simple item")
@my_menu_item4.set_item_label("Item with &accelerator")
In WXGTK using "SHIFT"
with non-alphabetic characters currently doesn't work, even in combination with other modifiers, due to GTK+ limitation. E.g. Shift+Ctrl+A
works but Shift+Ctrl+1
or Shift+/
do not, so avoid using accelerators of this form in portable code.
In GTk, the left/right/up/down arrow keys do not work as accelerator keys for a menu item unless a modifier key is used. Additionally, the following keycodes are not supported as menu accelerator keys: - KeyCode::K_COMMAND/KeyCode::K_CONTROL - KeyCode::K_SHIFT - KeyCode::K_ALT - KeyCode::K_SCROLL - KeyCode::K_CAPITAL - KeyCode::K_NUMLOCK - KeyCode::K_NUMPAD_TAB - KeyCode::K_TAB - KeyCode::K_WINDOWS_LEFT - KeyCode::K_WINDOWS_RIGHT - KeyCode::K_ADD - KeyCode::K_SEPARATOR - KeyCode::K_SUBTRACT - KeyCode::K_DECIMAL - KeyCode::K_DIVIDE - KeyCode::K_SNAPSHOT
352 |
# File 'lib/wx/doc/gen/menu_item.rb', line 352 def set_item_label(label) end |
#set_margin_width(width) ⇒ void
This method returns an undefined value.
Sets the width of the menu item checkmark bitmap.
Availability: only available for the WXMSW port.
361 |
# File 'lib/wx/doc/gen/menu_item.rb', line 361 def set_margin_width(width) end |
#set_menu(menu) ⇒ void Also known as:
This method returns an undefined value.
Sets the parent menu which will contain this menu item.
366 |
# File 'lib/wx/doc/gen/menu_item.rb', line 366 def () end |
#set_sub_menu(menu) ⇒ void Also known as:
This method returns an undefined value.
Sets the submenu of this menu item.
372 |
# File 'lib/wx/doc/gen/menu_item.rb', line 372 def () end |
#set_text_colour(colour) ⇒ void
This method returns an undefined value.
Sets the text colour associated with the menu item.
Please note that this function only exists in WXMSW and works by making the menu item “owner-drawn” there, please see the note about such items in this class description.
381 |
# File 'lib/wx/doc/gen/menu_item.rb', line 381 def set_text_colour(colour) end |