RadioButton.st
author Claus Gittinger <cg@exept.de>
Thu, 08 Jul 1999 11:30:35 +0200
changeset 1954 dc1e397855dc
parent 1892 110a6e458b5a
child 1958 9d53df393af1
permissions -rw-r--r--
allow for allViewBackground to be set.

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

Toggle subclass:#RadioButton
	instanceVariableNames:'buttonStyle buttonOnLevel buttonOffLevel buttonOffImage
		buttonOnImage'
	classVariableNames:'DefaultButtonStyle DefaultActiveLevel DefaultPassiveLevel
		MotifCheckBotForm MotifCheckTopForm MotifCheckInnerForm
		Round3DCheckBotForm Round3DCheckTopForm Round3DCheckInnerForm
		RoundHalfLightForm RoundOffForm RoundOnForm'
	poolDictionaries:''
	category:'Views-Interactors'
!

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

documentation
"
    like a Toggle, but do not turn off when pressed again, instead only
    turn off when another RadioButton is pressed (see RadioButtonGroup).

    written fall 91 by claus

    [author:]
        Claus Gittinger
"
!

examples 
"
  See more examples in RadioButtonGroup class>>examples

  example1: one on behavior (using RadioButtons)
                                                                        [exBegin]
    |top panel b group|

    top := StandardSystemView new.
    top extent:200@200.

    panel := HorizontalPanelView
                origin:0.0@0.0
                corner:1.0@1.0
                    in:top.

    group := RadioButtonGroup new.

    b := RadioButton label:'am' in:panel.
    group add:b.

    b := RadioButton label:'fm' in:panel.
    group add:b.

    b := RadioButton label:'off' in:panel.
    group add:b.

    group value:1.
    top open
                                                                        [exEnd]


  example2: zero or one on behavior (using Toggles)
                                                                        [exBegin]
    |top panel b group|

    top := StandardSystemView new.
    top extent:200@200.

    panel := HorizontalPanelView
                origin:0.0@0.0
                corner:1.0@1.0
                    in:top.

    group := RadioButtonGroup new.

    b := Toggle label:'am' in:panel.
    group add:b.

    b := Toggle label:'fm' in:panel.
    group add:b.

    b := Toggle label:'off' in:panel.
    group add:b.

    group value:1.
    top open
                                                                        [exEnd]


  a selectionInListView and a group displaying the same value:
                                                                        [exBegin]
    |top top2 panel b sv group selectionInList|

    top := StandardSystemView extent:200@200.

    panel := HorizontalPanelView
                origin:0.0@0.0 corner:1.0@1.0 in:top.

    group := RadioButtonGroup new.
    selectionInList := SelectionInList new.
    selectionInList list:#('am' 'fm' 'off').
    selectionInList selectionIndexHolder:group.

    b := Toggle label:'am' in:panel.
    group add:b.

    b := Toggle label:'fm' in:panel.
    group add:b.

    b := Toggle label:'off' in:panel.
    group add:b.

    group value:1.
    top open.

    top2 := StandardSystemView extent:200@200.
    sv := SelectionInListView in:top2.
    sv model:selectionInList.
    sv origin:0.0@0.0 corner:1.0@1.0.
    top2 open.

                                                                        [exEnd]
"
! !

!RadioButton class methodsFor:'defaults'!

motifCheckBotForm
    MotifCheckBotForm isNil ifTrue:[
        MotifCheckBotForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00001110
                                            2r01110000 2r00011100
                                            2r00111000 2r00111000
                                            2r00011100 2r01110000
                                            2r00001110 2r11100000
                                            2r00000111 2r11000000
                                            2r00000011 2r10000000
                                            2r00000001 2r00000000
                                           ].
        MotifCheckBotForm := MotifCheckBotForm onDevice:Display.
    ].
    ^ MotifCheckBotForm

    "Created: / 3.11.1997 / 12:06:45 / cg"
    "Modified: / 30.7.1998 / 21:27:53 / cg"
!

motifCheckInnerForm
    MotifCheckInnerForm isNil ifTrue:[
        MotifCheckInnerForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000001 2r00000000
                                            2r00000011 2r10000000
                                            2r00000111 2r11000000
                                            2r00001111 2r11100000
                                            2r00011111 2r11110000
                                            2r00001111 2r11100000
                                            2r00000111 2r11000000
                                            2r00000011 2r10000000
                                            2r00000001 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                           ].
        MotifCheckInnerForm := MotifCheckInnerForm onDevice:Display
    ].
    ^ MotifCheckInnerForm

    "Created: / 3.11.1997 / 12:06:45 / cg"
    "Modified: / 30.7.1998 / 21:28:45 / cg"
!

motifCheckTopForm
    MotifCheckTopForm isNil ifTrue:[
        MotifCheckTopForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000001 2r00000000
                                            2r00000011 2r10000000
                                            2r00000111 2r11000000
                                            2r00001110 2r11100000
                                            2r00011100 2r01110000
                                            2r00111000 2r00111000
                                            2r01110000 2r00011100
                                            2r11100000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                           ].
        MotifCheckTopForm := MotifCheckTopForm onDevice:Display
    ].
    ^ MotifCheckTopForm

    "Created: / 3.11.1997 / 12:06:45 / cg"
    "Modified: / 30.7.1998 / 21:28:45 / cg"
!

round3DCheckBotForm
    Round3DCheckBotForm isNil ifTrue:[
        Round3DCheckBotForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000010
                                            2r00000000 2r00000010
                                            2r00000000 2r00000110
                                            2r00000000 2r00000110
                                            2r00000000 2r00000110
                                            2r00000000 2r00001110
                                            2r01110000 2r00011100
                                            2r01111000 2r00111100
                                            2r00111111 2r11111000
                                            2r00001111 2r11100000
                                           ].
        Round3DCheckBotForm := Round3DCheckBotForm onDevice:Display.
    ].
    ^ Round3DCheckBotForm

    "Created: / 3.11.1997 / 14:29:37 / cg"
    "Modified: / 30.7.1998 / 21:29:57 / cg"
!

round3DCheckInnerForm
    Round3DCheckInnerForm isNil ifTrue:[
        Round3DCheckInnerForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000111 2r11000000
                                            2r00001111 2r11100000
                                            2r00011111 2r11110000
                                            2r00111111 2r11111000
                                            2r00111111 2r11111000
                                            2r00111111 2r11111000
                                            2r00111111 2r11111000
                                            2r00111111 2r11111000
                                            2r00011111 2r11110000
                                            2r00001111 2r11100000
                                            2r00000111 2r11000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                           ].

        Round3DCheckInnerForm := Round3DCheckInnerForm onDevice:Display.
    ].
    ^ Round3DCheckInnerForm

    "Created: / 3.11.1997 / 14:29:45 / cg"
    "Modified: / 30.7.1998 / 21:30:47 / cg"
!

round3DCheckTopForm
    Round3DCheckTopForm isNil ifTrue:[
        Round3DCheckTopForm := Form 
                                width:15 
                                height:15 
                                fromArray:#[2r00000011 2r10000000
                                            2r00011110 2r11110000
                                            2r00111000 2r00011000
                                            2r01110000 2r00001100
                                            2r01100000 2r00001100
                                            2r11000000 2r00000100
                                            2r11000000 2r00000100
                                            2r11000000 2r00000000
                                            2r11000000 2r00000000
                                            2r11000000 2r00000000
                                            2r01100000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                            2r00000000 2r00000000
                                           ].
"/        fromArray:#[2r00000111 2r11000000
"/                    2r00011110 2r11110000
"/                    2r00111000 2r00111000
"/                    2r01110000 2r00011100
"/                    2r01100000 2r00001100
"/                    2r11000000 2r00000100
"/                    2r11000000 2r00000000
"/                    2r11000000 2r00000000
"/                    2r11000000 2r00000000
"/                    2r11000000 2r00000000
"/                    2r01100000 2r00000000
"/                    2r00000000 2r00000000
"/                    2r00000000 2r00000000
"/                    2r00000000 2r00000000
"/                    2r00000000 2r00000000
"/                   ]

        Round3DCheckTopForm := Round3DCheckTopForm onDevice:Display.
    ].
    ^ Round3DCheckTopForm

    "Created: / 3.11.1997 / 14:29:45 / cg"
    "Modified: / 30.7.1998 / 21:30:47 / cg"
!

roundHalfLightForm
    RoundHalfLightForm isNil ifTrue:[
        RoundHalfLightForm := Form
                                width:15 
                                height:15 
                                fromArray:#[2r00001100 2r01100000
                                            2r00110000 2r00010000
                                            2r01100000 2r00001000
                                            2r01000000 2r00000100
                                            2r11000000 2r00000100
                                            2r10000000 2r00000000
                                            2r10000000 2r00000000
                                            2r10000000 2r00000100
                                            2r10000000 2r00000100
                                            2r10000000 2r00000100
                                            2r11000000 2r00001000
                                            2r00110000 2r00010000
                                            2r00011000 2r00110000
                                            2r00000111 2r11000000
                                            2r00000000 2r00000000
                                           ].

"/        fromArray:#[2r00000000 2r00000000
"/                    2r00000011 2r10000000
"/                    2r00001000 2r00100000
"/                    2r00010000 2r00010000
"/                    2r00100000 2r00001000
"/                    2r00000000 2r00000100
"/                    2r01000000 2r00000100
"/                    2r01000000 2r00000100
"/                    2r01000000 2r00000100
"/                    2r00000000 2r00000100
"/                    2r00100000 2r00001000
"/                    2r00010000 2r00010000
"/                    2r00001000 2r00100000
"/                    2r00000011 2r10000000
"/                    2r00000000 2r00000000
"/                   ]

        RoundHalfLightForm := RoundHalfLightForm onDevice:Display.
    ].
    ^ RoundHalfLightForm

    "Created: / 3.11.1997 / 18:26:40 / cg"
    "Modified: / 30.7.1998 / 21:31:55 / cg"
!

roundOffForm
    RoundOffForm isNil ifTrue:[
        RoundOffForm := Form 
            width:16 
            height:16 
            fromArray:#[

"/                        2r00000011 2r10000000
"/                        2r00001111 2r11100000
"/                        2r00111100 2r01111000
"/                        2r00110000 2r00011000
"/                        2r01100000 2r00001100
"/                        2r01100000 2r00001100
"/                        2r11000000 2r00000110
"/                        2r11000000 2r00000110
"/                        2r11000000 2r00000110
"/                        2r01100000 2r00001100
"/                        2r01100000 2r00001100
"/                        2r00110000 2r00011000
"/                        2r00111100 2r01111000
"/                        2r00001111 2r11100000
"/                        2r00000011 2r10000000
"/                        2r00000000 2r00000000

                        2r00000011 2r10000000
                        2r00001100 2r01100000
                        2r00010000 2r00010000
                        2r00100000 2r00001000
                        2r01000000 2r00000100
                        2r01000000 2r00000100
                        2r10000000 2r00000010
                        2r10000000 2r00000010
                        2r10000000 2r00000010
                        2r01000000 2r00000100
                        2r01000000 2r00000100
                        2r00100000 2r00001000
                        2r00010000 2r00010000
                        2r00001100 2r01100000
                        2r00000011 2r10000000
                        2r00000000 2r00000000
                       ].
        RoundOffForm := RoundOffForm onDevice:Display.
    ].
    ^ RoundOffForm

    "Created: / 3.11.1997 / 12:41:13 / cg"
    "Modified: / 30.7.1998 / 21:33:03 / cg"
!

roundOnForm
    RoundOnForm isNil ifTrue:[
        RoundOnForm := Form 
                        width:16 
                        height:16 
                        fromArray:#[

"/                                    2r00000000 2r00000000
"/                                    2r00000000 2r00000000
"/                                    2r00000000 2r00000000
"/                                    2r00000111 2r11000000
"/                                    2r00001111 2r11100000
"/                                    2r00011111 2r11110000
"/                                    2r00011111 2r11110000
"/                                    2r00011111 2r11110000
"/                                    2r00011111 2r11110000
"/                                    2r00011111 2r11110000
"/                                    2r00001111 2r11100000
"/                                    2r00000111 2r11000000
"/                                    2r00000000 2r00000000
"/                                    2r00000000 2r00000000
"/                                    2r00000000 2r00000000
"/                                    2r00000000 2r00000000

                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000011 2r10000000
                                    2r00000111 2r11000000
                                    2r00001111 2r11100000
                                    2r00001111 2r11100000
                                    2r00001111 2r11100000
                                    2r00000111 2r11000000
                                    2r00000011 2r10000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                    2r00000000 2r00000000
                                   ].
        RoundOnForm := RoundOnForm onDevice:Display.
    ].
    ^ RoundOnForm

    "RoundOnForm := nil. self updateStyleCache"
!

updateStyleCache
    <resource: #style (#'radioButton.style' 
                       #'radioButton.activeLevel'
                       #'radioButton.passiveLevel'
                       #'radioButton.activeImageFile'
                       #'radioButton.activeImage'
                       #'radioButton.passiveImageFile'
                       #'radioButton.passiveImage')>

    |l f|

    DefaultButtonStyle := StyleSheet at:#'radioButton.style'.
    l := -1.
    DefaultButtonStyle == #round2D ifTrue:[l := 0].
    DefaultActiveLevel := StyleSheet at:#'radioButton.activeLevel' default:l.
    DefaultButtonStyle == #motif ifTrue:[l := 1].
    DefaultPassiveLevel := StyleSheet at:#'radioButton.passiveLevel' default:l.

    RoundOnForm := RoundOffForm := nil.

    f := StyleSheet at:#'radioButton.activeImageFile'.
    f notNil ifTrue:[
        f := Image fromFile:f.
    ] ifFalse:[
        f := StyleSheet at:#'radioButton.activeImage'.
    ].
    f notNil ifTrue:[
        RoundOnForm := f onDevice:Display
    ].

    f := StyleSheet at:#'radioButton.passiveImageFile'.
    f notNil ifTrue:[
        f := Image fromFile:f.
    ] ifFalse:[
        f := StyleSheet at:#'radioButton.passiveImage'.
    ].
    f notNil ifTrue:[
        RoundOffForm := f onDevice:Display
    ].

    "
     self updateStyleCache
    "

    "Modified: / 28.4.1999 / 13:59:00 / cg"
! !

!RadioButton methodsFor:'accessing'!

allViewBackground:something
    "redefined here"

    self backgroundColor:something.

    (buttonStyle == #motif 
    or:[buttonStyle == #round3D
    or:[buttonStyle == #image]]) ifTrue:[
        enteredBgColor := something.
        activeBgColor := something.
    ]
!

forceRadioButtonStyle
    "force the radioButton to be displayed as round/motif radio button -
     even if the styleSheet defaults differently.
     (as in IRIS style)"

    buttonStyle isNil ifTrue:[
        styleSheet is3D ifTrue:[
            buttonStyle := #round3D.
            activeBgColor := bgColor. 
            activeFgColor := fgColor.
        ] ifFalse:[
            buttonStyle := #round2D.
                activeFgColor := fgColor.
                activeBgColor := bgColor.
        ].
        buttonOnLevel := DefaultActiveLevel.
        buttonOffLevel := DefaultPassiveLevel.
        onLevel := offLevel := 0.
        self level:0
    ]

    "Modified: / 18.6.1998 / 21:19:01 / cg"
    "Created: / 18.6.1998 / 21:23:58 / cg"
! !

!RadioButton methodsFor:'drawing'!

drawToggleImage
    "drawing of the radio image is done here."

    |x y clrTop clrBot imgTop imgBot imgInside imgH imgOn threeD lvl
     isActive|

    buttonStyle isNil ifTrue:[
        ^ super drawToggleImage
    ].

    isActive := controller pressed.

    threeD := true.
    buttonStyle == #motif ifTrue:[
        "/ square rotated by 45 degrees ...
        imgTop := self class motifCheckTopForm.
        imgBot := self class motifCheckBotForm.
        imgInside := self class motifCheckInnerForm.
    ] ifFalse:[
        buttonStyle == #round3D ifTrue:[
            "/ round 3D button ...
            imgTop := self class round3DCheckTopForm.
            imgBot := self class round3DCheckBotForm.
            imgInside := self class round3DCheckInnerForm.
            imgH := self class roundHalfLightForm.
            buttonOnLevel == buttonOffLevel ifTrue:[
                isActive ifTrue:[
                    imgOn := self class roundOnForm.
                ]
            ].
        ] ifFalse:[
            buttonStyle == #image ifTrue:[
                "/ bitmap images ...
                imgTop := buttonOffImage.
                isActive ifTrue:[
                    imgOn := buttonOnImage.
                ].
            ] ifFalse:[    
                "/ round 2D ...
                imgTop := self class roundOffForm.
                isActive ifTrue:[
                    imgOn := self class roundOnForm.
                ].
            ].
            threeD := false.
        ]
    ].

    imgTop notNil ifTrue:[imgTop := imgTop onDevice:device].
    imgBot notNil ifTrue:[imgBot := imgBot onDevice:device].
    imgInside notNil ifTrue:[imgInside := imgInside onDevice:device].
    imgH notNil ifTrue:[imgH := imgH onDevice:device].
    imgOn notNil ifTrue:[imgOn := imgOn onDevice:device].

    x := hSpace + margin.
    y := (height - imgTop height) // 2.

    threeD ifFalse:[
        self paint:fgColor.
        self displayForm:imgTop x:x y:y.
        isActive ifTrue:[
            self paint:(lampColor ? activeFgColor).
            self displayForm:imgOn x:x y:y
        ]
    ] ifTrue:[
        lvl := isActive ifTrue:[buttonOnLevel]
                        ifFalse:[buttonOffLevel].
        lvl < 0 ifTrue:[
            clrTop := shadowColor.
            clrBot := lightColor.
        ] ifFalse:[
            clrTop := lightColor.
            clrBot := shadowColor.
        ].
        self paint:clrTop.
        self displayForm:imgTop x:x y:y.
        self paint:clrBot.
        self displayForm:imgBot x:x y:y.

        (imgH notNil and:[halfShadowColor notNil]) ifTrue:[
            self paint:halfShadowColor.
            self displayForm:imgH x:x y:y
        ].

        imgInside notNil ifTrue:[
            self paint:(View defaultViewBackgroundColor).
            self displayForm:imgInside x:x y:y
        ].
        (isActive and:[imgOn notNil]) ifTrue:[
            self paint:lampColor.
            self displayForm:imgOn x:x y:y
        ]
    ]

    "Created: / 3.11.1997 / 12:16:30 / cg"
    "Modified: / 28.4.1999 / 14:02:00 / cg"
!

drawWin95FocusFrame
    "redefined to draw the focus frame around the string-only"

    |x y m1|

    x := labelOriginX. " margin + 1. "
    y := labelOriginY. " margin + 1. "
    m1 := margin + 1.
    self
        displayDottedRectangleX:x
        y:y
        width:(width - x - m1)
        height:(height - y - m1).

    "Created: / 17.9.1998 / 14:16:46 / cg"
    "Modified: / 29.4.1999 / 21:51:00 / cg"
! !

!RadioButton methodsFor:'initialization'!

defaultControllerClass
    ^ RadioButtonController
!

initStyle
    <resource: #style (#'radioButton.showLamp')>

    buttonStyle isNil ifTrue:[
        buttonStyle := DefaultButtonStyle.
    ].

    super initStyle.

    enteredBgColor := bgColor.

    buttonStyle isNil ifTrue:[
        showLamp := styleSheet at:#'radioButton.showLamp' default:showLamp.
        ^ self
    ].

    adjust := #left.
    showLamp := true.
    lampColor := Color black.
    buttonOnLevel := DefaultActiveLevel.
    buttonOffLevel := DefaultPassiveLevel.
    onLevel := offLevel := 0.

    self level:0.

    (buttonStyle == #motif 
    or:[buttonStyle == #round3D
    or:[buttonStyle == #image]]) ifTrue:[
        activeBgColor := bgColor. 
        activeFgColor := fgColor.
        buttonStyle == #image ifTrue:[
            buttonOnImage := self class roundOnForm onDevice:device.
            buttonOffImage := self class roundOffForm onDevice:device.
        ]
    ] ifFalse:[
        buttonStyle == #round2D ifTrue:[
            activeFgColor := fgColor.
            activeBgColor := bgColor.
        ]
    ]

    "Modified: / 28.4.1999 / 14:05:35 / cg"
! !

!RadioButton methodsFor:'private'!

computeLabelOrigin
    super computeLabelOrigin.
    buttonStyle notNil ifTrue:[    
        labelOriginX := hSpace + 15 + hSpace  
    ]

    "Modified: / 3.11.1997 / 18:17:58 / cg"
!

computeLabelSize
    "compute the extent needed to hold the label plus the lamp"

    super computeLabelSize.
    buttonStyle notNil ifTrue:[
        labelWidth := labelWidth + 15
    ]
!

lampImageHeight
    buttonStyle isNil ifTrue:[^ super lampImageHeight].
    ^ 15

    "Created: / 3.11.1997 / 14:27:27 / cg"
    "Modified: / 3.11.1997 / 18:10:39 / cg"
!

lampImageWidth
    buttonStyle isNil ifTrue:[^ super lampImageWidth].
    ^ 15

    "Created: / 3.11.1997 / 14:27:23 / cg"
    "Modified: / 3.11.1997 / 18:10:50 / cg"
! !

!RadioButton methodsFor:'queries'!

is3D
    buttonStyle == #round2D ifTrue:[^ false.].
    ^ super is3D

    "Created: / 3.11.1997 / 14:39:36 / cg"
! !

!RadioButton class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg/RadioButton.st,v 1.30 1999-07-08 09:29:50 cg Exp $'
! !