PopUpList.st
changeset 77 565b052f5277
parent 70 14443a9ea4ec
child 95 7535cfca9509
--- a/PopUpList.st	Mon Feb 06 01:52:01 1995 +0100
+++ b/PopUpList.st	Mon Feb 06 01:53:30 1995 +0100
@@ -11,7 +11,7 @@
 "
 
 Button subclass:#PopUpList
-	 instanceVariableNames:'menu menuAction'
+	 instanceVariableNames:'menu menuAction values'
 	 classVariableNames:''
 	 poolDictionaries:''
 	 category:'Views-Interactors'
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.5 1994-11-28 21:05:13 claus Exp $
+$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.6 1995-02-06 00:53:02 claus Exp $
 '!
 
 !PopUpList class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.5 1994-11-28 21:05:13 claus Exp $
+$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.6 1995-02-06 00:53:02 claus Exp $
 "
 !
 
@@ -61,7 +61,6 @@
      |p|
      p := PopUpList label:'healthy fruit'.
      p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
-
      p open
 
 
@@ -81,6 +80,27 @@
      p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
      p selection:'apples'.
      p open
+
+
+    with an action:
+
+     |p|
+     p := PopUpList label:'dummy'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
+     p selection:'apples'.
+     p action:[:what | Transcript showCr:'you selected: ' , what].
+     p open
+
+
+    with values different from the label strings:
+
+     |p|
+     p := PopUpList label:'dummy'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
+     p selection:'apples'.
+     p values:#(10 20 30 40 nil 50).
+     p action:[:what | Transcript show:'you selected: '; showCr:what].
+     p open
 "
 ! !
 
@@ -89,13 +109,13 @@
 drawWith:fgColor and:bgColor
     |mmH mmV mW mH|
 
-    pressed ifTrue:[
+    controller pressed ifTrue:[
 	super drawWith:enteredFgColor and:enteredBgColor
     ] ifFalse:[
 	super drawWith:fgColor and:bgColor.
     ].
-    mmH := (device horizontalPixelPerMillimeter * 1) rounded.
-    mmV := (device verticalPixelPerMillimeter * 1) rounded.
+    mmH := device horizontalPixelPerMillimeter rounded.
+    mmV := device verticalPixelPerMillimeter rounded.
     mW := (device horizontalPixelPerMillimeter * 2.5) rounded.
     mH := (device verticalPixelPerMillimeter * 1.5) rounded.
 
@@ -105,25 +125,28 @@
 
 !PopUpList methodsFor:'event handling'!
 
-buttonPress:button x:x y:y
+popMenu
     |org mv|
 
-    ((button == 1) or:[button == #select]) ifTrue:[
-	menu notNil ifTrue:[
-	    menu font:font.
-"
-	    menu width:self width + (margin * 2).
-"
-	    mv := menu menuView.
-	    mv width:(self width - (2 * menu margin) - (menu borderWidth*2)).
-	    mv level:0; borderWidth:0.
-	    mv fixSize.
-	    org := device translatePoint:0@0 
-				    from:(self id)
-				      to:(DisplayRootView new id).
-        
-	    menu showAt:org "resizing:false"
-	]
+    menu notNil ifTrue:[
+	self turnOff. 
+	menu font:font.
+
+	"
+	 adjust the menus width to my current width
+	"
+	mv := menu menuView.
+	mv width:(self width - (2 * menu margin) - (menu borderWidth*2)).
+	mv level:0; borderWidth:0.
+	mv fixSize.
+	"
+	 the popupMenu wants Display coordinates in its showAt: method
+	"
+	org := device translatePoint:0@0 
+				from:(self id)
+				  to:(DisplayRootView new id).
+
+	menu showAt:org "resizing:false"
     ].
 ! !
 
@@ -131,12 +154,41 @@
 
 initialize
     super initialize.
-    actionWhenPressed := true.
+    controller beTriggerOnDown.
+    controller action:[self popMenu].
     self adjust:#left
 ! !
 
 !PopUpList methodsFor:'accessing'!
 
+model:aModel
+    "set the model - this is forwarded to my menu. 
+     The popuplist itself has no model"
+
+    menu model:aModel
+!
+
+model
+    "return the model - this is forwarded to my menu. 
+     The popuplist itself has no model"
+
+    ^ menu model
+!
+
+change:aSymbol
+    "set the change symbol - this is forwarded to my menu. 
+     The popuplist itself has no model"
+
+    menu change:aSymbol
+!
+
+changeSymbol
+    "return the change symbol - this is forwarded to my menu. 
+     The popuplist itself has no model"
+
+    ^ menu changeSymbol
+!
+
 action:aOneArgBlock
     "set the action to be performed on selection changes;
      the argument, aOneArgBlock will be evaluated with the
@@ -172,6 +224,22 @@
     "
 !
 
+values:aList
+    "set a value list - these are reported via the action or changeSymbol instead of
+     the labe strings."
+
+    values := aList.
+    menu args:(1 to:aList size).
+
+    "
+     |p|
+     p := PopUpList label:'fruit ?'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p values:#(1 2 3 4 'mhmh - good').
+     p open
+    "
+!
+
 selection:indexOrString
     "set (force) a selection - usually done to set
      an initial selection"
@@ -236,13 +304,22 @@
 !PopUpList methodsFor:'user actions'!
 
 select:anEntry
+    |value label|
+
 "/ 'selected:' print. anEntry printNewline.
+    values isNil ifTrue:[
+	label := value := anEntry
+    ] ifFalse:[
+	label := menu labels at:anEntry.
+	value := values at:anEntry
+    ].
+
     menuAction notNil ifTrue:[
-	menuAction value:anEntry.
+	menuAction value:value.
     ].
     self sizeFixed:true.
-    self label:anEntry printString.
+    self label:label printString.
     (model notNil and:[changeSymbol notNil]) ifTrue:[
-	model perform:changeSymbol with:anEntry
+	model perform:changeSymbol with:value 
     ].
 ! !