CheckToggle.st
author ca
Wed, 12 Feb 1997 02:08:25 +0100
changeset 1001 eb7a8b2f4347
parent 967 6739eb5496da
child 1023 74e837fb617e
permissions -rw-r--r--
checkin from browser

"
 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:#CheckToggle
	instanceVariableNames:''
	classVariableNames:'DefaultCheckForm DefaultCheckColor DefaultActiveFGColor
		DefaultActiveBGColor DefaultFGColor DefaultBGColor
		DefaultActiveLevel DefaultPassiveLevel DefaultBorderWidth'
	poolDictionaries:''
	category:'Views-Interactors'
!

!CheckToggle 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
"
    CheckButtons are like Toggles in toggling their state when pressed.
    However, they show an ok-marker if on; nothing if off.
    CheckButtons are mostly used as part of a checkBox (since normally,
    you want to have some label along the check) and often grouped for
    many-in-many or one-in-many setups.

    [StyleSheet values:]

        checkToggleCheckColor   <Color>         color to draw check-image with.
                                                defaults to value of #buttonActiveForegroundColor.

        checkToggleActiveBackgroundColor        background color to draw active checki-image with
        checkToggleForegroundColor              foreground color to use if off
        checkToggleBackgroundColor              background color to use if off

        checkToggleBitmapFile   <String>        name of bitmap file for check-image

        checkToggleStyle        <Symbol>        default checkForm style.
                                                used if above is nil or file not readable
                                                can be #cross or #check; defaults to #check 

        checkToggleAvtiveLevel  <Number>        active level - defaults to value of #buttonPassiveLevel
        checkTogglePassiveLevel <Number>        active level - defaults to value of #buttonPassiveLevel
        checkToggleBorderWidth  <Number>        borderWidth - defaults buttons default

    (if not set in the styleSheet, Toggle values are taken)

    See examples.

    [author:]
        Claus Gittinger

    [see also:]
        CheckBox RadioButton RadioButtonGroup Toggle Button
        Dialog
        ValueHolder TriggerValue
        Block
"
!

examples 
"
    checkToggle alone:
                                                                        [exBegin]
        |top check|

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

        check := CheckToggle in:top.
        check origin:10@10.

        top open
                                                                        [exEnd]


    give it an action:
                                                                        [exBegin]
        |top check|

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

        check := CheckToggle in:top.
        check origin:10@10.
        check action:[:value | Transcript showCR:'changed to: ' , value printString].

        top open
                                                                        [exEnd]


    give it a model:
                                                                        [exBegin]
        |top check model|

        model := false asValue.

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

        check := CheckToggle in:top.
        check origin:10@10.
        check model:model.

        top openModal.

        Transcript showCR:'value after closing box: ' , model value printString
                                                                        [exEnd]


    multiple checks on a single model (with different change selectors):
    (using a checkBox here, for the demonstration ...)
    (this is a typical many-in-many setup)
                                                                        [exBegin]
        |top model panel ext1 ext2
         readFlag writeFlag executeFlag|

        readFlag := writeFlag := true.
        executeFlag := false.

        model := Plug new.
        model respondTo:#read with:[readFlag].
        model respondTo:#write with:[writeFlag].
        model respondTo:#execute with:[executeFlag].
        model respondTo:#read: with:[:val | readFlag := val].
        model respondTo:#write: with:[:val | writeFlag := val].
        model respondTo:#execute: with:[:val | executeFlag := val].

        top := StandardSystemView new.
        top extent:200@200.
        top label:'File permissions:'.

        panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
        panel horizontalLayout:#leftSpace.

        #(read write execute) do:[:sym |
            |check|

            check := CheckBox in:panel.
            check label:sym.
            check model:model; aspect:sym; changeMessage:(sym , ':') asSymbol.
        ].

        top openModal.

        Transcript showCR:'settings after closing box:'.
        Transcript showCR:'  read -> ' , readFlag printString.
        Transcript showCR:'  write -> ' , writeFlag printString.
        Transcript showCR:'  execute -> ' , executeFlag printString.
                                                                        [exEnd]


    checkToggles in a group - now, they have RadioButton behavior.
    (this is a typical one-in-many setup)
                                                                        [exBegin]
        |top panel check1 check2 check3 grp|

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

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

        check1 := CheckToggle in:panel.
        check2 := CheckToggle in:panel.
        check3 := CheckToggle in:panel.

        grp := RadioButtonGroup new.
        grp add:check1.
        grp add:check2.
        grp add:check3.

        top open
                                                                        [exEnd]


     Channel operation 
     -----------------

       enabling other toggles via a toggle
                                                                        [exBegin]
        |top panel t enableChannel|

        top := StandardSystemView new.
        top extent:(400 @ 200).

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

        enableChannel := false asValue.

        1 to:10 do:[:i |
            t := CheckToggle in:panel.
            t enableChannel:enableChannel.
        ].

        t := Toggle in:panel.
        t activeLogo:'enabled'; passiveLogo:'disabled'.
        t pressChannel:enableChannel.

        top open
                                                                        [exEnd]
"
! !

!CheckToggle class methodsFor:'defaults'!

checkFormOn:aDevice
    "return the form used when checkToggle is turned on.
     Provided as public entry, to allow other views
     to share the same check-image."

    ^ DefaultCheckForm on:aDevice.
!

updateStyleCache
    "extract values from the styleSheet and cache them in class variables"

    <resource: #style (#checkToggleCheckColor
                       #checkToggleBackgroundColor #checkToggleForegroundColor
                       #checkToggleActiveBackgroundColor #checkToggleActiveForegroundColor
                       #checkToggleActiveLevel #checkTogglePassiveLevel
                       #checkToggleBorderWidth 
                       #checkToggleBitmapFile
                       #checkToggleStyle)>

    |checkFileName bits checkStyle|

    DefaultCheckColor := StyleSheet colorAt:'checkToggleCheckColor'.
    DefaultFGColor := StyleSheet colorAt:'checkToggleForegroundColor'.
    DefaultBGColor := StyleSheet colorAt:'checkToggleBackgroundColor'.
    DefaultActiveFGColor := StyleSheet colorAt:'checkToggleActiveForegroundColor'.
    DefaultActiveBGColor := StyleSheet colorAt:'checkToggleActiveBackgroundColor'.
    DefaultActiveLevel := StyleSheet at:'checkToggleActiveLevel'.
    DefaultPassiveLevel := StyleSheet at:'checkTogglePassiveLevel'.
    DefaultBorderWidth := StyleSheet at:'checkToggleBorderWidth'.

    DefaultCheckForm := nil.
    checkFileName := StyleSheet at:'checkToggleBitmapFile' default:'CheckOn.xbm'.
    checkFileName notNil ifTrue:[
        DefaultCheckForm := Image fromFile:checkFileName 
                                resolution:100
                                        on:Display.
        DefaultCheckForm isNil ifTrue:[
            DefaultCheckForm := Image fromFile:'bitmaps/' , checkFileName 
                                    resolution:100
                                            on:Display.
        ]
    ].
    DefaultCheckForm isNil ifTrue:[
        checkStyle := StyleSheet at:'checkToggleStyle' default:#check.
        checkStyle == #cross ifTrue:[
            bits := #[2r10000000 2r00000001
                      2r01000000 2r00000010
                      2r00100000 2r00000100
                      2r00010000 2r00001000
                      2r00001000 2r00010000
                      2r00000100 2r00100000
                      2r00000010 2r01000000
                      2r00000001 2r10000000
                      2r00000001 2r10000000
                      2r00000010 2r01000000
                      2r00000100 2r00100000
                      2r00001000 2r00010000
                      2r00010000 2r00001000
                      2r00100000 2r00000100
                      2r01000000 2r00000010
                      2r10000000 2r00000001]

        ] ifFalse:[
            checkStyle == #fatcross ifTrue:[
                bits := #[2r11000000 2r00000011
                          2r11100000 2r00000111
                          2r01110000 2r00001110
                          2r00111000 2r00011100
                          2r00011100 2r00111000
                          2r00001110 2r01110000
                          2r00000111 2r11100000
                          2r00000011 2r11000000
                          2r00000011 2r11000000
                          2r00000111 2r11100000
                          2r00001110 2r01110000
                          2r00011100 2r00111000
                          2r00111000 2r00011100
                          2r01110000 2r00001110
                          2r11100000 2r00000111
                          2r11000000 2r00000011]
            ] ifFalse:[
                bits := #[2r00000000 2r00000000
                          2r00000000 2r00000010
                          2r00000000 2r00000010
                          2r00000000 2r00000100
                          2r00000000 2r00000100
                          2r00000000 2r00001000
                          2r00000000 2r00001000
                          2r00000000 2r00010000
                          2r01000000 2r00110000
                          2r00100000 2r01100000
                          2r00011000 2r01100000
                          2r00001110 2r11000000
                          2r00000111 2r11000000
                          2r00000011 2r10000000
                          2r00000001 2r10000000
                          2r00000000 2r00000000]
            ]
        ].
        DefaultCheckForm := Form width:16 height:16 fromArray:bits on:Display                                           
    ]

    "
     self updateStyleCache
    "
    "Modified: 1.1.1970 / 14:06:21 / cg"
! !

!CheckToggle methodsFor:'initialization'!

initStyle
    "setup viewStyle specifics"

    super initStyle.

    onLevel := offLevel.
    DefaultActiveLevel notNil ifTrue:[onLevel := DefaultActiveLevel].
    DefaultPassiveLevel notNil ifTrue:[offLevel := DefaultPassiveLevel].

    activeLogo := DefaultCheckForm on:device.
    passiveLogo := nil.

    DefaultActiveFGColor notNil ifTrue:[self activeForegroundColor:DefaultActiveFGColor].
    DefaultCheckColor notNil ifTrue:[self activeForegroundColor:DefaultCheckColor].
    DefaultActiveBGColor notNil ifTrue:[self activeBackgroundColor:DefaultActiveBGColor].
    DefaultFGColor notNil ifTrue:[self foregroundColor:DefaultFGColor].
    DefaultBGColor notNil ifTrue:[self backgroundColor:DefaultBGColor].

    showLamp := false.

    DefaultBorderWidth notNil ifTrue:[self borderWidth:DefaultBorderWidth].

    offLevel ~~ level ifTrue:[self level:offLevel].

    "Modified: 22.1.1997 / 11:57:05 / cg"
!

initialize
    super initialize.
    passiveLogo := nil.
    self form:activeLogo "/ to let me compute some defaultExtent
! !

!CheckToggle methodsFor:'redrawing'!

drawWith:fg and:bg
    controller pressed ifTrue:[
	logo := activeLogo.
    ] ifFalse:[
	logo := passiveLogo.
    ].
    super drawWith:fg and:bg

    "Modified: 22.9.1995 / 15:45:02 / claus"
! !

!CheckToggle class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg/CheckToggle.st,v 1.30 1997-02-12 01:08:25 ca Exp $'
! !