RadioButtonGroup.st
changeset 131 208fa92f434d
parent 130 338e856bddc9
child 155 d6f3836d2b51
--- a/RadioButtonGroup.st	Tue Jun 06 06:16:07 1995 +0200
+++ b/RadioButtonGroup.st	Tue Jun 27 04:24:41 1995 +0200
@@ -13,7 +13,7 @@
 'From Smalltalk/X, Version:2.10.5 on 14-mar-1995 at 11:06:48 am'!
 
 OrderedCollection subclass:#RadioButtonGroup
-	 instanceVariableNames:''
+	 instanceVariableNames:'valueChannel'
 	 classVariableNames:''
 	 poolDictionaries:''
 	 category:'Views-Support'
@@ -23,7 +23,7 @@
 COPYRIGHT (c) 1991 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.11 1995-06-06 04:14:48 claus Exp $
+$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.12 1995-06-27 02:23:53 claus Exp $
 '!
 
 !RadioButtonGroup class methodsFor:'documentation '!
@@ -44,7 +44,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.11 1995-06-06 04:14:48 claus Exp $
+$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.12 1995-06-27 02:23:53 claus Exp $
 "
 !
 
@@ -65,6 +65,18 @@
 	g add:b2
 	...
 
+    A radioButtonGroup installs itself as the model of its toggles or
+    radioButtons. Therefore, you can no longer operate the individual
+    elements on models.
+    Instead, the group now provides a valueHolder which takes as value 
+    the index of the selected toggle. You can set/get this valueHolder
+    via the valueChannel:/valueChannel messages.
+    See the last example for how this is done.
+"
+!
+
+examples 
+"
     example (using Toggles for 'at most one-on behavior'):
 
 	|top panel toggle b g|
@@ -113,16 +125,64 @@
 	g add:b.
 	top extent:(panel preferredExtent).
 	top open.
+
+    example (operating on a model)
+
+	|selected dialog group|
+
+	selected := 2 asValue.
+	dialog := Dialog new.
+	group := RadioButtonGroup new.
+	group add:(dialog addCheckBox:'one' on:nil) toggleView.
+	group add:(dialog addCheckBox:'two' on:nil) toggleView.
+	group add:(dialog addCheckBox:'three' on:nil) toggleView.
+	group valueChannel:selected.
+	dialog addAbortButton; addOkButton.
+	dialog open.
+	dialog accepted ifTrue:[
+	    Transcript showCr:'you selected: ' , selected value printString
+	]
+
 "
 ! !
 
+!RadioButtonGroup methodsFor:'accessing-mvc'!
+
+valueChannel
+    ^ valueChannel
+!
+
+valueChannel:aValueHolder
+    |oldValue newValue|
+
+    valueChannel notNil ifTrue:[oldValue := valueChannel value].
+    valueChannel := aValueHolder.
+    (newValue := valueChannel value) ~~ oldValue ifTrue:[
+	(self at:newValue) turnOn
+    ]
+! !
+
 !RadioButtonGroup methodsFor:'adding / removing'!
 
 add:aRadioButton
     super add:aRadioButton.
-    aRadioButton model:self; changeMessage:#elementChanged:from:.
+
+    "
+     this would work, if dependencies were not weak ...
+    "
 "/    aRadioButton addDependent:self.
-    (aRadioButton respondsTo:#group) ifTrue:[aRadioButton group:self]
+
+
+    "
+     therefore, we have to install ourself as model of the buttons/toggles
+     creating a valueChannel, if there is none yet.
+    "
+    valueChannel isNil ifTrue:[
+	valueChannel := 0 asValue
+    ].
+    aRadioButton isOn ifTrue:[valueChannel value:self size].
+    aRadioButton model:self; aspectMessage:nil; changeMessage:#elementChanged:from:.
+
 ! !
 
 !RadioButtonGroup methodsFor:'update'!
@@ -130,7 +190,9 @@
 update:something with:someArgument from:changedButton
     (self includes:changedButton) ifFalse:[^ self].
 
-    "a RadioButton in this group has changed - notify the others"
+    "
+     a RadioButton in this group has changed - notify the others
+    "
 
 "/    "in case we have a toggle in the group, 
 "/     and it has been turned off - turn it on again
@@ -139,32 +201,31 @@
 "/        changedButton toggleNoAction.
 "/        ^ self
 "/    ].
-    self do:[:aButton |
-	(aButton == changedButton) ifFalse:[
-	    aButton isOn ifTrue:[
-		aButton turnOff
-	    ]
-	]
-    ]
+    self elementChanged:(changedButton isOn) from:changedButton
 !
 
 elementChanged:aToggle
-    self do:[:aButton |
+    "historic compatibility leftover - will vanish"
+
+    ^ self elementChanged:true from:aToggle
+!
+
+elementChanged:value from:aToggle
+    "some element changed its value - find it, turn the others off
+     and update my value"
+
+    |newValue|
+
+    self keysAndValuesDo:[:index :aButton |
 	(aButton == aToggle) ifFalse:[
 	    aButton isOn ifTrue:[
 		aButton turnOff
 	    ]
+	] ifTrue:[
+	    value ifTrue:[newValue := index]   
 	]
-    ]
-!
+    ].
+    valueChannel value:newValue
 
-elementChanged:newValue from:aToggle
-    self do:[:aButton |
-	(aButton == aToggle) ifFalse:[
-	    aButton isOn ifTrue:[
-		aButton turnOff
-	    ]
-	]
-    ]
 ! !