RadioButtonGroup.st
author Claus Gittinger <cg@exept.de>
Mon, 23 Oct 1995 21:07:01 +0100
changeset 164 5ab02ff0d9ae
parent 163 18d87bf16389
child 174 d80a6cc3f9b2
permissions -rw-r--r--
.

"
 COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

'From Smalltalk/X, Version:2.10.5 on 14-mar-1995 at 11:06:48 am'!

OrderedCollection subclass:#RadioButtonGroup
	 instanceVariableNames:'valueChannel groupID'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Views-Support'
!

RadioButtonGroup comment:'
COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.15 1995-10-23 20:06:41 cg Exp $
'!

!RadioButtonGroup class methodsFor:'documentation '!

copyright
"
 COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libwidg/RadioButtonGroup.st,v 1.15 1995-10-23 20:06:41 cg Exp $
"
!

documentation
"
    RadioButtonGroups control the interaction between RadioButtons
    turning off other button(s) when one of the group is pressed.
    To group some buttons (and have one-on behavior) use:

	|g|

	g := RadioButtonGroup new.
	...
	b1 := RadioButton label:....
	g add:b1
	...
	b2 := RadioButton label:....
	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.

    The groupID instance variable is not required for normal operation;
    the interface builder uses it to give a button-group some descriptive 
    (unique) name.
"
!

examples 
"
    example (using Toggles for 'at most one-on behavior'):

	|top panel toggle b g|

	top := StandardSystemView new.
	panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
	g := RadioButtonGroup new.
	b := Toggle label:'one' in:panel.
	g add:b.
	b := Toggle label:'two' in:panel.
	g add:b.
	b := Toggle label:'three' in:panel.
	g add:b.
	top extent:(panel preferredExtent).
	top open.

    example (using RadioButtons for 'one-on behavior'):

	|top panel toggle b g|

	top := StandardSystemView new.
	panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
	g := RadioButtonGroup new.
	b := RadioButton label:'one' in:panel.
	g add:b.
	b := RadioButton label:'two' in:panel.
	g add:b.
	b := RadioButton label:'three' in:panel.
	g add:b.
	top extent:(panel preferredExtent).
	top open.

    example (same, with button 'two' initially on):

	|top panel toggle b g|

	top := StandardSystemView new.
	panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
	g := RadioButtonGroup new.
	b := RadioButton label:'one' in:panel.
	g add:b.
	b := RadioButton label:'two' in:panel.
	g add:b.
	b turnOn.
	b := RadioButton label:'three' in:panel.
	g add:b.
	top extent:(panel preferredExtent).
	top open.

    using checkBoxes (these have a label included):

	|top panel toggle b g|

	top := StandardSystemView new.
	panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
	g := RadioButtonGroup new.
	b := CheckBox label:'one' in:panel.
	g add:b.
	b := CheckBox label:'two' in:panel.
	g add:b.
	b := CheckBox label:'three' in:panel.
	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
	] ifFalse:[
	    Transcript showCr:'aborted'
	]
"
! !

!RadioButtonGroup methodsFor:'accessing'!

groupID:aSymbol 
    groupID := aSymbol

    "Modified: 5.9.1995 / 16:04:28 / claus"
!

groupID
    ^ groupID

    "Modified: 5.9.1995 / 16:04:33 / claus"
! !

!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.

    "
     this would work, if dependencies were not weak ...
    "
"/    aRadioButton addDependent: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'!

update:something with:someArgument from:changedButton
    "/ no - it could be a subcomponent
"/    (self includes:changedButton) ifFalse:[^ self].

    "
     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
"/    "
"/    changedButton isOn ifFalse:[
"/        changedButton toggleNoAction.
"/        ^ self
"/    ].
    self elementChanged:(changedButton isOn) from:changedButton
!

elementChanged:aToggle
    "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 |
	(aToggle isSubViewOf:aButton) ifFalse:[
	    aButton isOn ifTrue:[
		aButton turnOff
	    ]
	] ifTrue:[
	    value ifTrue:[newValue := index]   
	]
    ].
    aToggle turnOn.
    valueChannel value:newValue

    "Modified: 21.9.1995 / 13:42:06 / claus"
! !