Toggle.st
changeset 22 ac872628ef2d
parent 7 15a9291b9bd0
child 38 4b9b70b2cc87
--- a/Toggle.st	Sat Jan 08 18:27:56 1994 +0100
+++ b/Toggle.st	Sat Jan 08 18:30:02 1994 +0100
@@ -11,7 +11,7 @@
 "
 
 Button subclass:#Toggle
-       instanceVariableNames:''
+       instanceVariableNames:'showLamp lampColor lampWidth lampHeight'
        classVariableNames:''
        poolDictionaries:''
        category:'Views-Interactors'
@@ -22,13 +22,81 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
 
+$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.5 1994-01-08 17:30:02 claus Exp $
+written spring/summer 89 by claus
+'!
+
+!Toggle class methodsFor:'documentation'!
+
+documentation
+"
 this button changes state whenever pressed and stays pressed until pressed
 again. All the main action is in Button, Toggle just redefines buttonpress/
 release behavior.
+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).
 
-$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.4 1993-12-11 01:50:32 claus Exp $
-written spring/summer 89 by claus
-'!
+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
+"
+! !
+
+!Toggle methodsFor:'initialization'!
+
+initStyle
+    super initStyle.
+
+    showLamp := resources name:'SHOW_LAMP' default:false.
+    showLamp ifTrue:[
+        onLevel := offLevel.
+        lampColor := resources name:'LAMP_COLOR'
+                            default:(Color red:100 green:100 blue:0). "yellow"
+        lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded.
+        lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded.
+
+        "dont know, if I like this ..."
+        "
+        activeBgColor := bgColor
+        "
+    ].
+! !
+
+!Toggle methodsFor:'accessing'!
+
+lampColor:aColor
+    "change the color of the toggle-lamp"
+
+    lampColor := aColor.
+    (shown and:[showLamp and:[pressed]]) ifTrue:[
+        self redraw
+    ]
+! !
+
+
+!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
+    ]
+! !
 
 !Toggle methodsFor:'changing state'!
 
@@ -42,7 +110,9 @@
     ] ifFalse:[
         self level:offLevel.
     ].
-    self redraw
+    shown ifTrue:[
+        self redraw
+    ]
 !
 
 toggle
@@ -76,3 +146,26 @@
     ].
     "ignore"
 ! !
+
+!Toggle methodsFor:'redrawing'!
+
+drawWith:fg and:bg
+    "redraw myself with fg/bg. Use super to draw the label, 
+     drawing of the lamp here."
+
+    |x y|
+
+    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.
+        pressed ifTrue:[
+            self paint:lampColor.
+        ] ifFalse:[
+            self paint:bgColor.
+        ].
+        self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4
+    ]
+! !