Last updated at 11:27 pm UTC on 31 August 2016
| menu |
menu := MenuMorph new defaultTarget: self.
menu addStayUpItems. "buttons in title bar to keep the menu open even after a selection"
menu addTitle: 'My Label' . "can also include an icon with #addTitle:icon:"
menu add: 'Thing One' help: 'make me do ThingOne' action: #doThingOne. "item with a help balloon; executes self doThingOne if selected"
menu add: 'thing Two' action: [2 zork]. "no help balloon, evaluates the block if selected"
menu add: 'a third' selector: #doThingThree: argument: 4. "since doThingThree requires an augment, supply it. Multiple arguments can be provided by using #add:selector:arguments:"
menu add: 'fourth wotsit' target: self someInsVarOrOther selector: #doThingOne. "note how having a different target would make something different happen even with a repeated selector. You can also use the argument/arguments: as above"
menu addItem:[:menuItem| menuItem target: SomeGlobal; selector: #foo1; contents: 'wobble'; isEnabled: false "some code to explicitly work on a newly created MenuItemMorph that will be added to the menu"].
menu popUpInWorld "fire it up - note that this is NOT a synchronous menu action as with old MVC menus"
There are a lot more options in the 'construction' protocol, including ones to dynamically mark certain entires as enabled or not.
The final example above is 'menu popUpInWorld' which is the most basic way to open the menu. See the 'control' protocol for more ways, including ways to open in response to a particular event, in a particular World or for a particular Hand. Using menus like this relies upon the target being properly set for every item and no return value is provided, unlike the MVC menus.
If you need a modal menu you must open the menu with one of the methods in the 'modal control' protocol and build the menu a little differently. For example -
menu := MenuMorph new.
myList do: [:n | menu add: n target: menu selector: #modalSelection: argument: n]. "note that the target is the menu itself, the selector MUST be #modalSelector: and the argument must be the item you want returned"
(chosenItem := menu invokeModal) ifNil: [^ self].