PopUpList.st
changeset 119 59758ff5b841
parent 105 3d064ba4a0cc
child 120 710d41f17b68
--- a/PopUpList.st	Wed May 03 02:30:14 1995 +0200
+++ b/PopUpList.st	Wed May 03 02:39:07 1995 +0200
@@ -13,7 +13,7 @@
 'From Smalltalk/X, Version:2.10.5 on 14-mar-1995 at 11:16:49 am'!
 
 Button subclass:#PopUpList
-	 instanceVariableNames:'menu menuAction values'
+	 instanceVariableNames:'menu menuAction values useIndex listMsg'
 	 classVariableNames:''
 	 poolDictionaries:''
 	 category:'Views-Interactors'
@@ -23,7 +23,7 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.8 1995-03-18 05:15:33 claus Exp $
+$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.9 1995-05-03 00:36:50 claus Exp $
 '!
 
 !PopUpList class methodsFor:'documentation'!
@@ -44,7 +44,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.8 1995-03-18 05:15:33 claus Exp $
+$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.9 1995-05-03 00:36:50 claus Exp $
 "
 !
 
@@ -62,7 +62,7 @@
 
      |p|
      p := PopUpList label:'healthy fruit'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
      p open
 
 
@@ -70,7 +70,7 @@
 
      |p|
      p := PopUpList label:'dummy'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
      p selection:'apples'.
      p open
 
@@ -78,8 +78,8 @@
     with separating lines:
 
      |p|
-     p := PopUpList label:'dummy'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
+     p := PopUpList label:'fruit'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
      p selection:'apples'.
      p open
 
@@ -88,21 +88,63 @@
 
      |p|
      p := PopUpList label:'dummy'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
      p selection:'apples'.
      p action:[:what | Transcript showCr:'you selected: ' , what].
      p open
 
 
+    sometimes, you may like the index instead of the value:
+    (notice, that the separating line counts, so you have take care ...)
+
+     |p|
+     p := PopUpList label:'dummy'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
+     p selection:'apples'.
+     p action:[:what | Transcript show:'you selected: '; showCr:what].
+     p useIndex:true.
+     p open
+
+
+    since the list is actually a popupMenu, you can add double-separators:
+
+     |p|
+     p := PopUpList label:'dummy'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' 
+	      '=' 
+	      'margaritas' 'pina colada'
+	      '=' 
+	      'smalltalk' 'c++' 'eiffel').
+     p values:#(apples bananas grape lemon 
+		nil 
+		'mhmh - so good' 'makes headache'
+		nil
+		'great' 'another headache' 'no bad').
+     p selection:'apples'.
+     p action:[:what | Transcript show:'you selected: '; showCr:what].
+     p open
+
+
     with values different from the label strings:
 
      |p|
      p := PopUpList label:'dummy'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
      p selection:'apples'.
      p values:#(10 20 30 40 nil 50).
      p action:[:what | Transcript show:'you selected: '; showCr:what].
      p open
+
+    with a model:
+
+     |p model|
+
+     model := SelectionInList with:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
+
+     p := PopUpList label:'healthy fruit'.
+     p model:model.
+     p open.
+     model inspect
 "
 ! !
 
@@ -138,9 +180,10 @@
 	 adjust the menus width to my current width
 	"
 	mv := menu menuView.
+	mv create.      "/ stupid: it resizes itself upon first create
 	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
 	"
@@ -158,11 +201,34 @@
     super initialize.
     controller beTriggerOnDown.
     controller action:[self popMenu].
-    self adjust:#left
+    self adjust:#left.
+    useIndex := false.
+    changeMsg := #list.
+    listMsg := #list.
 ! !
 
 !PopUpList methodsFor:'accessing'!
 
+useIndex:aBoolean 
+    useIndex := aBoolean
+
+    "
+     |p|
+     p := PopUpList label:'fruit ?'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
+     p action:[:val | Transcript showCr:'selected: ' , val printString].   
+     p open.
+    "
+    "
+     |p|
+     p := PopUpList label:'fruit ?'.
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
+     p action:[:val | Transcript showCr:'selected: ' , val printString].   
+     p useIndex:true.
+     p open.
+    "
+!
+
 contents
     ^ self label
 !
@@ -172,28 +238,28 @@
 
 !
 
-model:aModel
+XXmodel:aModel
     "set the model - this is forwarded to my menu. 
      The popuplist itself has no model"
 
     menu model:aModel
 !
 
-model
+XXmodel
     "return the model - this is forwarded to my menu. 
      The popuplist itself has no model"
 
     ^ menu model
 !
 
-change:aSymbol
+XXchange:aSymbol
     "set the change symbol - this is forwarded to my menu. 
      The popuplist itself has no model"
 
     menu change:aSymbol
 !
 
-changeSymbol
+XXchangeSymbol
     "return the change symbol - this is forwarded to my menu. 
      The popuplist itself has no model"
 
@@ -217,12 +283,7 @@
 list:aList
     "set the list - i.e. the values shown in the pop-up list"
 
-    menu := PopUpMenu
-		  labels:aList
-	       selectors:(Array new:(aList size) withAll:#select:)
-		    args:aList
-		receiver:self
-		     for:self.
+    self createMenuFor:aList.
     realized ifTrue:[
 	self computeLabelSize
     ]
@@ -230,7 +291,8 @@
     "
      |p|
      p := PopUpList label:'fruit ?'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
+     p action:[:val | Transcript showCr:'selected: ' , val printString].   
      p open
     "
 !
@@ -245,9 +307,10 @@
     "
      |p|
      p := PopUpList label:'fruit ?'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
      p values:#(1 2 3 4 'mhmh - good').
-     p open
+     p action:[:val | Transcript showCr:'selected: ' , val printString].   
+     p open.
     "
 !
 
@@ -264,13 +327,13 @@
     "
      |p|
      p := PopUpList label:'what fruit ?'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
      p selection:'grape'.
      p open 
 
      |p|
      p := PopUpList label:'what fruit ?'.
-     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
+     p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
      p selection:'blabla'.
      p open
     "
@@ -278,6 +341,23 @@
 
 !PopUpList methodsFor:'private'!
 
+realize
+    super realize.
+    model notNil ifTrue:[
+	self createMenuFor:(model perform:listMsg).
+        
+    ].
+!
+
+createMenuFor:aList
+    menu := PopUpMenu
+		  labels:aList
+	       selectors:#select:
+		    args:(1 to:aList size) 
+		receiver:self
+		     for:self.
+!
+
 computeLabelSize
     "compute the extent needed to hold the label plus the mark"
 
@@ -315,13 +395,17 @@
 !PopUpList methodsFor:'user actions'!
 
 select:anEntry
+    "this is sent from the popupmenu when an entry was selected"
+
     |value label|
 
-"/ 'selected:' print. anEntry printNewline.
+    label := menu labels at:anEntry.
     values isNil ifTrue:[
-	label := value := anEntry
+	value := anEntry.
+	useIndex ifFalse:[
+	    value := menu labels at:anEntry.
+	]
     ] ifFalse:[
-	label := menu labels at:anEntry.
 	value := values at:anEntry
     ].
 
@@ -330,6 +414,8 @@
     ].
     self sizeFixed:true.
     self label:label printString.
+    "
+     tell by model - if any
+    "
     self sendChangeMessageWith:value 
 ! !
-