Programmer Reference : Common Widgets : Menus : Creating a secondary menu using simple menu protocol
Creating a secondary menu using simple menu protocol
The following code creates a window with a menu bar containing the menu and secondary menu illustrated at right. The menu bar contains one item, File, that drops down a menu containing two items, Open and Close. If Close is selected, the fileMenu:clientData:callData: method is invoked. If Open is selected, a secondary menu containing three items is dropped down. Choosing one of the three items invokes the openMenu:clientData:callData: method.

sp007120
| shell main menuBar fileMenu openMenu |
shell := CwTopLevelShell
createApplicationShell: 'shell'
argBlock: [:w |
w title: 'Secondary Menu Example'].
main := shell
createMainWindow: 'main'
argBlock: nil.
main manageChild.
menuBar := main
createSimpleMenuBar: 'bar'
argBlock: [:w |
w
"File defaults to a cascade button because it is in a menu bar."
buttons: #('File');
buttonMnemonics: #($F)].
menuBar manageChild.
fileMenu := menuBar
createSimplePulldownMenu: 'file'
argBlock: [:w |
w
buttons: #('Open' 'separator' 'Close');
"Open must be explicitly created as a cascade button, because
buttons in a menu default to push buttons."
buttonType: (Array
with: XmCASCADEBUTTON
with: XmSEPARATOR
with: XmPUSHBUTTON);
buttonMnemonics: (Array
with: $O
with: 0 asCharacter
with: $C);
postFromButton: 0].
fileMenu
addCallback: XmNsimpleCallback
receiver: self
selector: #fileMenu:clientData:callData:
clientData: nil.
openMenu := fileMenu
"The secondary pull-down menu is created as a child of fileMenu."
createSimplePulldownMenu: 'open'
argBlock: [:w |
w
buttons: #('Read only' 'Write only' 'Read write');
"The secondary menu is activated when the 0th item from fileMenu is selected
(Open)."
postFromButton: 0].
openMenu
addCallback: XmNsimpleCallback
receiver: self
selector: #openMenu:clientData:callData:
clientData: nil.
main
setAreas: menuBar
horizontalScrollbar: nil
verticalScrollbar: nil
workRegion: nil.
shell realizeWidget
The simpleCallback callback used by the code is shown below.
fileMenu: widget clientData: clientData callData: callData
"Execute the desired operation."
self perform: (#(#doNothing #doClose) at: clientData + 1).
openMenu: widget clientData: clientData callData: callData
"Execute the desired operation."
self
perform: (#(#doOpenReadOnly #doOpenWriteOnly #doOpenReadWrite)
at: clientData + 1).
Last modified date: 04/18/2020