examples
authorClaus Gittinger <cg@exept.de>
Tue, 28 May 1996 11:07:59 +0200
changeset 692 65c5499f4cea
parent 691 906e4d67b9cf
child 693 8369529a2ac4
examples
PopUpMenu.st
--- a/PopUpMenu.st	Sun May 26 16:01:49 1996 +0200
+++ b/PopUpMenu.st	Tue May 28 11:07:59 1996 +0200
@@ -36,9 +36,10 @@
 
 documentation
 "
-    This class provides PopUpMenu functionality; Actually, this class
-    only provides the popup and shadow functionality and wraps another
-    view, which is the actual menu-list (usually an instance of MenuView).
+    This class provides PopUpMenu functionality;
+    -> Actually, this class only provides the popup and shadow functionality 
+       and wraps ANOTHER view, which shows the actual menu-list 
+       (usually an instance of MenuView).
 
     PopUpMenus are usually created with a list of labels, selectors and a
     receivier. Once activated, the specified receiver will be sent a
@@ -58,28 +59,86 @@
 
     [author:]
         Claus Gittinger
+
+    [see also:]
+        PullDownMenu MenuView
 "
 !
 
 examples
 "
+  The ST-80 way of opening menus is to startUp a menu,
+  and use the returned value:
+                                                                        [exBegin]
+    |p|
 
-    Examples:
+    p := PopUpMenu
+            labels:#('foo' 'bar' 'baz').
+    Transcript showCR:p startUp
+                                                                        [exEnd]
+  It returns the index of the selected item or 0 if nothing was selected.
+  This opening is done by either the controller or the view (if it has
+  a middleButtonMenu and/or a menuHolder).
+
+  If the numeric index is inconvenient, alternative values may be specified
+  as in (here, nil is returned if nothing was selected):
+                                                                        [exBegin]
+    |p|
+
+    p := PopUpMenu
+            labels:#('foo' 'bar' 'baz')
+            values:#('hello foo' 'hello bar' 'hello baz').
+    Transcript showCR:p startUp
+                                                                        [exEnd]
+  In ST/X, the above is actually done by a mimicri method (#startUp)
+  and menus are typically created in one of the following ways:
+
+  -> With a single actionBlock.
+     This is convenient, if all actions shall perform a similar task, 
+     but require different arguments.
+
+  this is evaluated, passing the selections index or value as argument.
+  The action block is NOT evaluated, if nothing was selected.
+    With index:
                                                                         [exBegin]
-        |p|
-        p := PopUpMenu
-                labels:#('foo'
-                         'bar'
-                         'baz')
-                selectors:#(
-                            #foo
-                            #bar
-                            #baz)
-                receiver:nil.
-        p showAtPointer
+    |p|
+
+    p := PopUpMenu
+            labels:#('foo' 'bar' 'baz').
+    p action:[:item | Transcript showCR:item].
+    p showAtPointer
+                                                                        [exEnd]
+    With individual arguments:
+                                                                        [exBegin]
+    |p|
+
+    p := PopUpMenu
+            labels:#('foo' 'bar' 'baz')
+            args:#('hello foo' 'hello bar' 'hello baz').
+    p action:[:item | Transcript showCR:item].
+    p showAtPointer
+                                                                        [exEnd]
+
+  -> With an explicit receiver and different selectors.
+     This is convenient, if you have to send per-item messages
+     to some object (typically, the receiver is the view or a model)
+                                                                        [exBegin]
+    |p m|
+
+    m := Plug new.
+    m respondTo:#foo with:[Transcript showCR:'foo received'].
+    m respondTo:#bar with:[Transcript showCR:'bar received'].
+    m respondTo:#maz with:[Transcript showCR:'maz received'].
+
+    p := PopUpMenu
+            labels:#('foo' 'bar' 'baz')
+            selectors:#(#foo #bar #baz)
+            receiver:m.
+    p showAtPointer
                                                                         [exEnd]
 
 
+  More examples:
     dynamic with action instead of selector being sent:
                                                                         [exBegin]
         |p|
@@ -336,6 +395,27 @@
     ^ newMenu
 !
 
+labels:labels args:args 
+    "create and return a menu with label-items and args. 
+     The actionBlock has to be defined later"
+
+    ^ self labels:labels selectors:nil accelerators:nil args:args receiver:nil for:nil
+
+    "
+     |m|
+
+     m := PopUpMenu labels:#('foo' 'bar')
+                      args:#('hello world' 'how about this').
+     m action:[:arg |
+        Transcript showCR:arg
+     ].
+
+     m showAtPointer
+    "
+
+    "Modified: 25.5.1996 / 17:01:43 / cg"
+!
+
 labels:labels selector:aSelector args:args receiver:anObject
     "create and return a popup menu with labels as entries.
      Each item will send aSelector with a corresponding argument from the
@@ -620,6 +700,9 @@
     |return rec sel0 sel1 arg|
 
     return := 0.
+    menuView args notNil ifTrue:[
+        return := nil
+    ].
 
     "/
     "/ arrange for the menu to evaluate this action,
@@ -630,57 +713,60 @@
     "/ The old ST/X mechanism may vanish.
     "/
 
-    menuView action:[:menuView :selected |
-        |actionIndex value sel retVal 
-         args selectors checkFlags check|
-
-        retVal := 0.
-
-        args := menuView args.
-        selectors := menuView selectors.
-        checkFlags := menuView checkFlags.
+    menuView action isNil ifTrue:[
+        menuView action:[:menuView :selected |
+            |actionIndex value sel retVal 
+             args selectors checkFlags check|
 
-        args isNil ifTrue:[
-            selectors notNil ifTrue:[
-                sel0 := selectors at:selected.
-            ].
-        ] ifFalse:[
-            actionIndex := args at:selected.
-            actionIndex notNil ifTrue:[
-                actionValues isNil ifTrue:[
-                    selectors notNil ifTrue:[
-                        "/ mhmh an ST/X menu started the ST-80 way
+            retVal := 0.
 
-                        sel1 := selectors at:selected.
-                        arg := actionIndex.
-                    ] ifFalse:[
-                        retVal := actionIndex
-                    ]
-                ] ifFalse:[
-                    retVal := actionValues at:actionIndex.
-                    (retVal isKindOf:PopUpMenu) ifTrue:[
-                        retVal := retVal startUp
-                    ]
-                ]
-            ] ifFalse:[
-                "/ mhmh an ST/X menu started the ST-80 way
+            args := menuView args.
+            selectors := menuView selectors.
+            checkFlags := menuView checkFlags.
 
+            args isNil ifTrue:[
                 selectors notNil ifTrue:[
                     sel0 := selectors at:selected.
                 ].
-            ]
-        ].
+            ] ifFalse:[
+                actionIndex := args at:selected.
+                actionIndex notNil ifTrue:[
+                    actionValues isNil ifTrue:[
+                        selectors notNil ifTrue:[
+                            "/ mhmh an ST/X menu started the ST-80 way
 
-        checkFlags notNil ifTrue:[
-            check := checkFlags at:selected.
-            check notNil ifTrue:[
-                arg := check.
-                sel1 := sel0.
-                sel0 := nil.
-            ]
-        ].
+                            sel1 := selectors at:selected.
+                            arg := actionIndex.
+                        ] ifFalse:[
+                            retVal := actionIndex
+                        ]
+                    ] ifFalse:[
+                        retVal := actionValues at:actionIndex.
+                        (retVal isKindOf:PopUpMenu) ifTrue:[
+                            retVal := retVal startUp
+                        ]
+                    ]
+                ] ifFalse:[
+                    "/ mhmh an ST/X menu started the ST-80 way
 
-        return := retVal
+                    selectors notNil ifTrue:[
+                        sel0 := selectors at:selected.
+                    ].
+                ]
+            ].
+
+            checkFlags notNil ifTrue:[
+                check := checkFlags at:selected.
+                check notNil ifTrue:[
+                    arg := check.
+                    sel1 := sel0.
+                    sel0 := nil.
+                ]
+            ].
+            return := retVal.
+
+            menuView action:nil
+        ]
     ].
 
     aPoint isNil ifTrue:[
@@ -708,7 +794,7 @@
     "
 
     "Created: 10.1.1996 / 20:11:42 / cg"
-    "Modified: 23.4.1996 / 18:58:26 / cg"
+    "Modified: 25.5.1996 / 16:49:43 / cg"
 !
 
 startUpWithHeading:aString
@@ -1175,5 +1261,5 @@
 !PopUpMenu class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg/PopUpMenu.st,v 1.52 1996-05-22 11:25:06 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg/PopUpMenu.st,v 1.53 1996-05-28 09:07:59 cg Exp $'
 ! !