The class menu
creates a menu for an interface when specified as part of the menu bar (or as a submenu of a menu on the menu bar).
A menu has a title, and has items appearing in it, where an item can be either a menu item, a menu component or another menu.
The simplest way of providing items to a menu is to pass them using the item
slot, but if you need to compute the items dynamically you should provide an items-function
. This function gets called with the menu's interface, and it should return a list of menu items for the new menu. If an item is not of type menu-object
, then it gets converted to a menu-object
with the item as its data. This function is called before the popup-callback
and the enabled-function
which means that they can affect the new items.
To specify a mnemonic in the menu title, you can use the initarg :mnemonic
. The value can be:
The index of the mnemonic in the title.
The mnemonic in the title.
A character is chosen from a list of common mnemonics, or the :default
behaviour is followed. This is the default.
A mnemonic is chosen using some rules.
The title has no mnemonic.
An alternate way to specify a mnemonic in the menu title is to use the :mnemonic-title
initarg (rather than :title
). The value should be a string with the mnemonic character preceded by the mnemonic escape. The mnemonic escape defaults to #\&
(it can be changed via the :mnemonic-escape
initarg). To include the mnemonic escape as a normal character in the menu title, precede it with the mnemonic escape.
When debugging a menu, it may be useful to pop up a window containing a menu with the minimum of fuss. The function contain
will do just that for you.
(capi:contain (make-instance 'capi:menu
:title "Test"
:items '(:red :green :blue)))
(capi:contain (make-instance
'capi:menu :title "Test"
:items '(:red :green :blue)
:print-function
'string-capitalize))
(capi:contain (make-instance
'capi:menu
:title "Test"
:items '(:red :green :blue)
:print-function 'string-capitalize
:callback #'(lambda (data interface)
(capi:display-message
"Pressed ~S" data))))
Here is an example showing how to add submenus to a menu:
(setq submenu (make-instance 'capi:menu
:title "Submenu..."
:items '(1 2 3)))
(capi:contain (make-instance
'capi:menu
:title "Test"
:items (list submenu)))
Here is an example showing how to use the items-function
:
(capi:contain (make-instance
'capi:menu
:title "Test"
:items-function #'(lambda (interface)
(loop for i below 8
collect (random 10)
))))
Finally, some examples showing how to specify a mnemonic in a menu title:
(capi:contain (make-instance
'capi:menu
:title "Mnemonic Title"
:mnemonic 1
:items '(1 2 3)))
(capi:contain (make-instance
'capi:menu
:mnemonic-title "M&nemonic Title"
:items '(1 2 3)))
(capi:contain (make-instance
'capi:menu
:mnemonic-title "M&e && You"
:items '("Me" "You")))