Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Popup Menus
Last updated at 8:41 pm UTC on 20 March 2014
Question: Martin Kuball June 13, 2004 I would like to use some popup menus (like the world menu) in a small application I'm developing. I asked myself what class should I use? I browsed the available classes and found a lot of candidates, like MenuMorph, PopUpMorph, CustumMenu, etc. Now it would be really nice if somebody can tell me about the pros and cons of each class - or just recommend one.

Answer: From Tim Rowledge relating to Squeak 4.5 - see MenuMorph
Answer: Boris Gaertner There is no best choice - every kind of a menu has its special features. Try all these examples in a Morphic project. [Pretty simple is PopUpMenu]:
 (PopUpMenu
  labelArray: #('Ada' 'Basic' 'Cobol' 'Java' 'Smalltalk')
  lines: #(3))
  startUp 
This is the simplist type of a menu. The method startUp answers the number of the selected item or 0 if no item was selected. PopUpMenus can live in the image for years and can be used again and again.

SelectionMenu is a bit more sophisticated:
 (SelectionMenu
  labelList: #('Ada' 'Basic' 'Cobol' 'Java' 'Smalltalk')
  lines: #(3)
  selections: #(#useAda #useBasic #useCobol #useJava #useSmalltalk))
  startUp 
The method startUp answers the selector that is associated with the selected item or nil when no item is selected. This is very convenient when you want to send the selector to an instance that understands it. Like PopUpMenus, SelectionMenus can live in the image forever.

The CustomMenu is quite different: In a Custom menu, a menu item has not only an item name and a method selector, but also a message receiver. This gives you the possibility of providing different receivers for the selectable messages. These receivers are often short-living, and as a consequence CustomMenu is an object for single use and disposal. Message receivers are often morphs and it is therefore difficult to give an example. To see some infrequently used possibilities, you can try this:
| menu |
menu := CustomMenu new.
menu add: 'red rectangle'
          target: Display
          selector: #fill:fillColor:
          argumentList: (Array with: (10@10 extent: 100@100)
                               with: Color red);
     add: 'green rectangle'
          target: Display
          selector: #fill:fillColor:
          argumentList: (Array with: (10@10 extent: 100@100)
                               with: Color green);
     addLine;
     add: 'browser'
          target: Browser
          selector: #fullOnClass:
          argument: nil;
     add: 'browser on Pen'
          target: Browser
          selector: #fullOnClass:
          argument: Pen;
     add: 'browser on Pen class'
          target: Browser
          selector: #fullOnClass:
          argument: Pen class;
     add: 'browser on Collection'
          target: Browser
          selector: #fullOnClass:
          argument: Collection;
     addLine;
     add: 'inspect' target: Inspector
          selector: #openOn:withEvalPane:
          argumentList: (Array with: menu
                               with: false).
 menu  invokeOn: nil
[In this example, t]he Display has responsibility for implementing the action for the first and the second menu option. The implementers of the other options are various classes. In your application, most menu items will possibly have the target "self".

As to PopUpMorph, MenuMorph, MenuItemMorph, MenuLineMorph, I think these are implementation details that are unimportant for application development.

Response: Thanks for the explanation. MenuMorph is used directly in many places. PopUpMorph and company seem to have their roots in MVC compatibility. But as their interface is much simpler than MenuMorph's interface, they sure have their use in a pure Morphic world.