Toggle.st
changeset 77 565b052f5277
parent 74 9d0d6a11939e
child 86 df2687090a7f
equal deleted inserted replaced
76:81e3409404d2 77:565b052f5277
    19 
    19 
    20 Toggle comment:'
    20 Toggle comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    22 	      All Rights Reserved
    22 	      All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.10 1995-01-26 16:05:02 claus Exp $
    24 $Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.11 1995-02-06 00:53:28 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !Toggle class methodsFor:'documentation'!
    27 !Toggle class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.10 1995-01-26 16:05:02 claus Exp $
    45 $Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.11 1995-02-06 00:53:28 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
   215 "
   215 "
   216 ! !
   216 ! !
   217 
   217 
   218 !Toggle methodsFor:'initialization'!
   218 !Toggle methodsFor:'initialization'!
   219 
   219 
   220 initialize
       
   221     super initialize.
       
   222     actionWhenPressed := true
       
   223 !
       
   224 
       
   225 initStyle
   220 initStyle
   226     super initStyle.
   221     super initStyle.
   227 
   222 
   228     showLamp := StyleSheet at:#toggleShowLamp default:false.
   223     showLamp := StyleSheet at:#toggleShowLamp default:false.
   229     showLamp ifTrue:[
   224     showLamp ifTrue:[
   236     ].
   231     ].
   237 
   232 
   238     lampColor := StyleSheet at:#toggleLampColor default:Color yellow.
   233     lampColor := StyleSheet at:#toggleLampColor default:Color yellow.
   239     lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded.
   234     lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded.
   240     lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded.
   235     lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded.
       
   236 !
       
   237 
       
   238 defaultController
       
   239     ^ ToggleController
   241 ! !
   240 ! !
   242 
   241 
   243 !Toggle methodsFor:'accessing'!
   242 !Toggle methodsFor:'accessing'!
   244 
   243 
   245 showLamp
   244 showLamp
   262 
   261 
   263 lampColor:aColor
   262 lampColor:aColor
   264     "change the color of the toggle-lamp"
   263     "change the color of the toggle-lamp"
   265 
   264 
   266     lampColor := aColor.
   265     lampColor := aColor.
   267     (shown and:[showLamp and:[pressed]]) ifTrue:[
   266     (shown and:[showLamp and:[controller pressed]]) ifTrue:[
   268 	self redraw
   267 	self redraw
   269     ]
   268     ]
   270 ! !
   269 ! !
   271 
   270 
   272 
   271 
   296 
   295 
   297 toggleNoAction
   296 toggleNoAction
   298     "toggle, but do NOT perform any action - can be used to change a toggle
   297     "toggle, but do NOT perform any action - can be used to change a toggle
   299      under program control (i.e. turn one toggle off from another one)"
   298      under program control (i.e. turn one toggle off from another one)"
   300 
   299 
   301     |newLevel|
   300     |newLevel pressed|
   302 
   301 
   303     pressed := pressed not.
   302     pressed := controller pressed not.
       
   303     controller pressed:pressed.
   304     pressed ifTrue:[
   304     pressed ifTrue:[
   305 	newLevel := onLevel.
   305 	newLevel := onLevel.
   306     ] ifFalse:[
   306     ] ifFalse:[
   307 	newLevel := offLevel.
   307 	newLevel := offLevel.
   308     ].
   308     ].
   317 !
   317 !
   318 
   318 
   319 toggle
   319 toggle
   320     "toggle and perform the action"
   320     "toggle and perform the action"
   321 
   321 
   322     |action|
   322     |action pressed|
   323 
   323 
   324     enabled ifTrue:[
   324     controller enabled ifTrue:[
   325 	self toggleNoAction.
   325 	self toggleNoAction.
   326 	pressed ifTrue:[
   326 	(pressed := controller pressed) ifTrue:[
   327 	    action := pressActionBlock
   327 	    action := controller pressAction
   328 	] ifFalse:[
   328 	] ifFalse:[
   329 	    action := releaseActionBlock
   329 	    action := controller releaseAction
   330 	].
   330 	].
   331 	action notNil ifTrue:[action value].
   331 	action notNil ifTrue:[action value].
   332 	model notNil ifTrue:[
   332 	model notNil ifTrue:[
   333 	    model value:pressed.
   333 	    model value:pressed.
   334 	    model changed
   334 	    model changed
   335 	].
   335 	].
   336 	self changed
   336 	self changed
   337     ]
   337     ]
   338 ! !
   338 ! !
   339 
   339 
   340 !Toggle methodsFor:'event handling'!
       
   341 
       
   342 buttonPress:button x:x y:y
       
   343     ((button == 1) or:[button == #select]) ifTrue:[
       
   344 	self toggle
       
   345     ] ifFalse:[
       
   346 	^ super buttonPress:button x:x y:y
       
   347     ].
       
   348 !
       
   349 
       
   350 buttonRelease:button x:x y:y
       
   351     ((button == 1) or:[button == #select]) ifFalse:[
       
   352 	^ super buttonRelease:button x:x y:y
       
   353     ].
       
   354     "ignore"
       
   355 ! !
       
   356 
       
   357 !Toggle methodsFor:'redrawing'!
   340 !Toggle methodsFor:'redrawing'!
   358 
   341 
   359 drawWith:fg and:bg
   342 drawWith:fg and:bg
   360     "redraw myself with fg/bg. Use super to draw the label, 
   343     "redraw myself with fg/bg. Use super to draw the label, 
   361      drawing of the lamp is done here."
   344      drawing of the lamp is done here."
   362 
   345 
   363     |x y|
   346     |x y clr|
   364 
   347 
   365     super drawWith:fg and:bg.   "this draws the text"
   348     super drawWith:fg and:bg.   "this draws the text"
   366 
   349 
   367     showLamp ifTrue:[
   350     showLamp ifTrue:[
   368 	x := hSpace + margin.
   351 	x := hSpace + margin.
   369 	y := (height - lampHeight) // 2.
   352 	y := (height - lampHeight) // 2.
   370 	self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1.
   353 	self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1.
   371 	pressed ifTrue:[
   354 	controller pressed ifTrue:[
   372 	    self paint:lampColor.
   355 	    clr := lampColor.
   373 	] ifFalse:[
   356 	] ifFalse:[
   374 	    self paint:bgColor.
   357 	    clr := bgColor.
   375 	].
   358 	].
       
   359 	self paint:clr.
   376 	self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4
   360 	self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4
   377     ]
   361     ]
   378 ! !
   362 ! !