Toggle.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 1995 19:19:24 +0100
changeset 205 6814c0bf8df8
parent 178 5c28593c2a3b
child 377 ee53da05e265
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1989 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.
"

Button subclass:#Toggle
	 instanceVariableNames:'showLamp lampColor lampWidth lampHeight'
	 classVariableNames:'DefaultShowLamp DefaultLampColor'
	 poolDictionaries:''
	 category:'Views-Interactors'
!

!Toggle class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 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
"
    this button changes state whenever clicked upon and stays down (pressed) 
    until clicked again. All the main action is in Button and the controller
    (ToggleController).

    The toggle may optionally display a little kind-of-lamp (or LED), which
    is turned on when the toggle is pressed. (i.e. as in the Interviews toolkit).

    whenever the Toggle changes its change, it will evaluate one of
    pressAction or releaseAction.

    For ST-80 compatibility, if the model is nonNil, this one gets a new
    value and is sent a changed message. Also, the toggle will follow changes 
    in the model and update its display as appropriate.
    If nonNil, the model is supposed to be a ValueHolder holding true or false.


    instance variables:
	showLamp          <Boolean>     true if a lamp should be displayed
	lampColor         <Color>       color of the lamp
	lampWidth         <Integer>     width of the lamp in pixel
	lampHeight        <Integer>     height of the lamp in pixel

    styleSheet values:
	toggleShowLamp    <Boolean>     if true, a lamp is shown; default:false.
	toggleLampColor   <Color>       lamps color; default:yellow.
"
!

examples
"
    Examples:
    Try all, to see what is possible.

	(notice, that these examples are meant to show what can be done;
	 usually, all style-related stuff is preinitialized - you should not
	 normally play around with onLevel, offLevel, showLamp: etc)


      simple:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'press here' in:v.
	t origin:10 @ 10.
	t action:[:value | Transcript show:'toggle state: '; showCr:value.].
	v open


      separate press/release actions:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'press here' in:v.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'toggle pressed'.].
	t releaseAction:[Transcript showCr:'toggle released'.].
	v open


      changing logo:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'eat me' in:v.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'smaller'. t label:'drink me'].
	t releaseAction:[Transcript showCr:'larger'. t label:'eat me'].
	v open


      changing logo and freezing size (looks better):

	|v t|

	v := StandardSystemView new extent:200@200.
	'create with large logo; freeze; change to small logo'.
	t := Toggle label:'drink me' in:v. 
	t sizeFixed:true.
	t label:'eat me'.

	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'smaller'. t label:'drink me'].
	t releaseAction:[Transcript showCr:'larger'. t label:'eat me'].
	v open


      adding a lamp (in some view styles, this is the default anyway):

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'].
	t releaseAction:[Transcript showCr:'off'. t label:'off'].
	v open


      changing lamps color:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t lampColor:Color red.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'.].
	t releaseAction:[Transcript showCr:'off'. t label:'off'.].
	v open


      changing lamps color & size:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t lampColor:Color red.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'.].
	t releaseAction:[Transcript showCr:'off'. t label:'off'.].
	v open


      lamp only - no '3D going-in' (this is the default with IRIS style)

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t onLevel:(t offLevel).
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'].
	t releaseAction:[Transcript showCr:'off'. t label:'off'].
	v open


      lamp and freezing size of the label (looks better):

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t sizeFixed:true.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'].
	t releaseAction:[Transcript showCr:'off'. t label:'off'].
	v open


      another variation:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t sizeFixed:true.
	t onLevel:(t offLevel).
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t label:'on'].
	t releaseAction:[Transcript showCr:'off'. t label:'off'].
	v open


      and another one:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t showLamp:true.
	t sizeFixed:true.

	t showLamp:false.
	t offLevel:3.
	t onLevel:3.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'. t showLamp:true. t label:'on'].
	t releaseAction:[Transcript showCr:'off'. t showLamp:false. t label:'off'].
	v open


      another font:

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t font:(Font family:'times' face:'bold' style:'roman' size:24).
	t label:'hello'.
	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'.].
	t releaseAction:[Transcript showCr:'off'.].
	v open


      another font (no, I dont know what it means :-):

	|v t|

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'off' in:v.
	t font:(Font family:'k14' face:nil style:nil size:nil).
	t label:(TwoByteString with:(Character value:16r3021)).

	t origin:10 @ 10.
	t pressAction:[Transcript showCr:'on'.].
	t releaseAction:[Transcript showCr:'off'.].
	v open


    using a model (look at 'value' in the inspector):

	|m v t|

	m := ValueHolder newBoolean.
	m inspect.

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'press here to change value' in:v.
	t origin:10 @ 10.
	t model:m.
	v open


    using a model with different changeSelector:

	|m v t|

	m := Plug new.
	m respondTo:#setValue1: with:[:value | Transcript show:'value 1 changed to: '; showCr:value].
	m respondTo:#setValue2: with:[:value | Transcript show:'value 2 changed to: '; showCr:value].

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'press here for value1' in:v.
	t origin:10 @ 10.
	t model:m; change:#setValue1:.
	t := Toggle label:'press here for value2' in:v.
	t origin:10 @ 50.
	t model:m; change:#setValue2:.
	v open


    two toggles on the same model:

	|m v t|

	m := true asValue.

	v := StandardSystemView new extent:200@200.
	t := Toggle label:'press here' in:v.
	t origin:10 @ 10.
	t model:m.

	t := Toggle label:'or here' in:v.
	t origin:10 @ 50.
	t model:m.
	v open
"
! !

!Toggle class methodsFor:'defaults'!

updateStyleCache
    DefaultShowLamp := StyleSheet at:'toggleShowLamp' default:false.
    DefaultLampColor := StyleSheet colorAt:'toggleLampColor' default:Color yellow.
! !

!Toggle methodsFor:'accessing'!

lampColor:aColor
    "change the color of the toggle-lamp"

    lampColor := aColor.
    (shown and:[showLamp and:[controller pressed]]) ifTrue:[
	self redraw
    ]
!

showLamp
    "return true, if I show a lamp"

    ^ showLamp
!

showLamp:aBoolean
    "enable/disable drawing of the lamp"

    showLamp ~~ aBoolean ifTrue:[
	showLamp := aBoolean.
	self computeLabelSize.
	fixSize ifFalse:[
	    self resize
	]
    ]
! !

!Toggle methodsFor:'change & update'!

update:something with:parameter from:changedObject
    something == aspectMsg ifTrue:[
	self getValueFromModel.
	^ self
    ].
    super update:something with:parameter from:changedObject
! !

!Toggle methodsFor:'initialization'!

defaultControllerClass
    ^ ToggleController
!

initStyle
    super initStyle.

    showLamp := DefaultShowLamp.
    showLamp ifTrue:[
	onLevel := offLevel.

	"dont know, if I like this ..."
	"
	activeBgColor := bgColor
	"
    ].

    lampColor := DefaultLampColor on:device.
    lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded.
    lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded.
!

realize
    "/
    "/ get my initial state from the model (if there is one)
    "/
    self getValueFromModel.
    super realize
! !

!Toggle methodsFor:'private'!

computeLabelOrigin
    "compute the origin of the form/text.
     redefined to move label to the right if there is a lamp."

    super computeLabelOrigin.
    showLamp ifTrue:[
	labelOriginX := labelOriginX + hSpace + lampWidth + hSpace.
    ]
!

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

    super computeLabelSize.
    showLamp ifTrue:[
	labelWidth := labelWidth + hSpace + lampWidth + hSpace.
	labelHeight := labelHeight max: lampHeight
    ]
!

getValueFromModel
    |val|

    (model notNil and:[aspectMsg notNil]) ifTrue:[
	aspectMsg numArgs == 0 ifTrue:[
	    val := model perform:aspectMsg
	] ifFalse:[
	    val := model perform:aspectMsg with:self
	].
	val ifTrue:[
	    self turnOn
	] ifFalse:[
	    self turnOff
	].
    ].

    "Created: 14.11.1995 / 21:08:43 / cg"
! !

!Toggle methodsFor:'redrawing'!

drawWith:fg and:bg
    "redraw myself with fg/bg. Use super to draw the label, 
     drawing of the lamp is done here."

    |x y clr|

    super drawWith:fg and:bg.   "this draws the text"

    showLamp ifTrue:[
	x := hSpace + margin.
	y := (height - lampHeight) // 2.
	self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1.
	controller pressed ifTrue:[
	    clr := lampColor.
	] ifFalse:[
	    clr := bgColor.
	].
	self paint:clr.
	self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4
    ]
! !

!Toggle class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.23 1995-11-23 18:17:46 cg Exp $'
! !