ButtonController.st
changeset 125 3ffa271732f7
parent 121 4e63bbdb266a
child 126 40228f4fd66b
--- a/ButtonController.st	Mon May 08 17:19:27 1995 +0200
+++ b/ButtonController.st	Tue May 09 03:57:16 1995 +0200
@@ -14,8 +14,8 @@
 
 Controller subclass:#ButtonController
 	 instanceVariableNames:'enabled pressed active entered isTriggerOnDown autoRepeat
-                repeatBlock initialDelay repeatDelay pressActionBlock
-                releaseActionBlock isToggle'
+		repeatBlock initialDelay repeatDelay pressActionBlock
+		releaseActionBlock isToggle'
 	 classVariableNames:''
 	 poolDictionaries:''
 	 category:'Interface-Support'
@@ -39,7 +39,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.7 1995-05-06 14:16:24 claus Exp $
+$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.8 1995-05-09 01:54:58 claus Exp $
 "
 !
 
@@ -48,20 +48,36 @@
     ButtonControllers are used with buttons and handle all user interaction.
     These are automatically created when a Button is created, therefore no manual
     action is required for creation.
+    In normal applications, you dont have to care for the controller; access to the
+    controllers behavior is also possible via messages to the button.
+    (setting actions, controlling autorepeat etc.)
 
     Instance variables:
 
       enabled                 <Boolean>       pressing is allowed (default: true)
+
       pressed                 <Boolean>       true if currently pressed (read-only)
+
       entered                 <Boolean>       true if the cursor is currently in this view
+
       isTriggerOnDown         <Boolean>       controls if the action should be executed on
 					      press or on release (default: on release).
+
+      isToggle                <Boolean>       controls if the button should show toggle
+					      behavior (as opposed to one-shot behavior)
+
       pressActionBlock        <Block>         block to evaluate when pressed (default: noop)
+
       releaseActionBlock      <Block>         block to evaluate when released (default: noop)
+
       autoRepeat              <Boolean>       auto-repeats when pressed long enough (default: false)
+
       initialDelay            <Number>        seconds till first auto-repeat (default: 0.2)
+
       repeatDelay             <Number>        seconds of repeat intervall (default: 0.025)
+
       repeatBlock             <Block>         block evaluated for auto-repeat (internal)
+
       active                  <Boolean>       true during action evaluation (internal)
 "
 ! !
@@ -91,8 +107,9 @@
 !
 
 active
-    "return true, if I am active; that is currently performing my
-     action."
+    "return true, if I am active; 
+     that is: currently performing my action.
+     This query can be used to avoid multiple redraws."
 
     ^ active
 !
@@ -213,9 +230,9 @@
     "toggle and perform the action"
 
     enabled ifTrue:[
-        self toggleNoAction.
-        self performAction.
-        view changed:#toggle with:pressed
+	self toggleNoAction.
+	self performAction.
+	view changed:#toggle with:pressed
     ]
 !
 
@@ -248,37 +265,33 @@
     |sym action|
 
     (button == 1 or:[button == #select]) ifFalse:[
-        ^ super buttonPress:button x:x y:y
+	^ super buttonPress:button x:x y:y
     ].
 
     enabled ifTrue:[
-        isToggle ifTrue:[
-            self toggle.
-            ^ self
-        ].
+	isToggle ifTrue:[
+	    self toggle.
+	    ^ self
+	].
 
-        pressed ifFalse:[
-            pressed := true.
-            view showActive.
+	pressed ifFalse:[
+	    pressed := true.
+	    view showActive.
 
-            (pressActionBlock notNil or:[model notNil]) ifTrue:[
-                "
-                 force output - so that button is drawn correctly in case
-                 of any long-computation (at high priority)
-                "
-                view device synchronizeOutput.
-            ].
+	    (pressActionBlock notNil or:[model notNil]) ifTrue:[
+		"
+		 force output - so that button is drawn correctly in case
+		 of any long-computation (at high priority)
+		"
+		view device synchronizeOutput.
+	    ].
 
-            active := true.
-
-            self performAction.
+	    self performAction.
 
-            active := false.
-
-            autoRepeat ifTrue:[
-                Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
-            ]
-        ]
+	    autoRepeat ifTrue:[
+		Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
+	    ]
+	]
     ]
 !
 
@@ -288,73 +301,69 @@
     |sym|
 
     (button == 1 or:[button == #select]) ifFalse:[
-        ^ super buttonRelease:button x:x y:y
+	^ super buttonRelease:button x:x y:y
     ].
 
     isToggle ifTrue:[
-        ^ self
+	^ self
     ].
 
     pressed ifTrue:[
-        autoRepeat ifTrue:[
-            Processor removeTimedBlock:repeatBlock
-        ].
-        pressed := false.
-        view showPassive.
+	autoRepeat ifTrue:[
+	    Processor removeTimedBlock:repeatBlock
+	].
+	pressed := false.
+	view showPassive.
 
-        enabled ifTrue:[
-            "
-             only perform action if released within myself
-            "
-            ((x >= 0) 
-            and:[x <= view width
-            and:[y >= 0
-            and:[y <= view height]]]) ifTrue:[
-                (releaseActionBlock notNil or:[model notNil]) ifTrue:[
-                    "
-                     force output - so that button is drawn correctly in case
-                     of any long-computation (at high priority)
-                    "
-                    view device synchronizeOutput.
-                ].
+	enabled ifTrue:[
+	    "
+	     only perform action if released within myself
+	    "
+	    ((x >= 0) 
+	    and:[x <= view width
+	    and:[y >= 0
+	    and:[y <= view height]]]) ifTrue:[
+		(releaseActionBlock notNil or:[model notNil]) ifTrue:[
+		    "
+		     force output - so that button is drawn correctly in case
+		     of any long-computation (at high priority)
+		    "
+		    view device synchronizeOutput.
+		].
 
-                active := true.
-
-                self performAction.
-
-                active := false.
-            ]
-        ]
+		self performAction.
+	    ]
+	]
     ]
 !
 
 pointerEnter:state x:x y:y
-    "redraw with enteredColors if they differ from the normal colors"
+    "mouse pointer entered my view.
+     Redraw with enteredColors if they differ from the normal colors"
 
     entered := true.
-    pressed ifTrue:[
-	"
-	 reentered after a leave with mouse-button down;
-	 restart autorepeating and/or if I am a button with
-	 isTriggerOnDown, show active again.
-	"
-	enabled ifTrue:[
+    enabled ifTrue:[
+	pressed ifTrue:[
+	    "
+	     reentered after a leave with mouse-button down;
+	     restart autorepeating and/or if I am a button with
+	     triggerOnDown, show active again.
+	    "
 	    autoRepeat ifTrue:[
 		Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
 	    ].
 	    isTriggerOnDown ifFalse:[
 		view showActive.
 	    ]
-	]
-    ] ifFalse:[
-	enabled ifTrue:[
+	] ifFalse:[
 	    view redraw
 	]
     ]
 !
 
 pointerLeave:state
-    "redraw with normal colors if they differ from enteredColors"
+    "mouse pointer left my view.
+     Redraw with normal colors if they differ from enteredColors"
 
     entered := false.
     pressed ifTrue:[
@@ -379,20 +388,36 @@
 performAction
     |action|
 
+    "
+     ST/X style actionBlock evaluation ...
+    "
     pressed ifTrue:[
-        action := pressActionBlock
+	action := pressActionBlock
     ] ifFalse:[
-        action := releaseActionBlock
+	action := releaseActionBlock
     ].
-    action notNil ifTrue:[action value].
+    action notNil ifTrue:[
+	active := true.
+	action numArgs == 0 ifTrue:[
+	    action value
+	] ifFalse:[
+	    action value:pressed
+	].
+	active := false.
+    ].
 
+    "
+     ST-80 style model notification ...
+    "
     (isToggle
     or:[(isTriggerOnDown and:[pressed])
     or:[isTriggerOnDown not and:[pressed not]]]) ifTrue:[
-        "the ST-80 way of doing things"
-        view notNil ifTrue:[
-            view sendChangeMessageWith:pressed.
-        ].
+	"the ST-80 way of doing things"
+	view notNil ifTrue:[
+	    active := true.
+	    view sendChangeMessageWith:pressed.
+	    active := false.
+	].
     ].
 !
 
@@ -401,13 +426,11 @@
 !
 
 keyPress:key x:x y:y
-    "only trigger, if I am the focusView of my group"
-
-    |group|
+    "trigger on Return and space, if I am the focusView of my group
+     (i.e. if I got an explicit focus)"
 
-    ((group := view windowGroup) notNil 
-    and:[group focusView == view]) ifTrue:[
-	(key == #Return or:[key == Character space]) ifTrue:[
+    (key == #Return or:[key == Character space]) ifTrue:[
+	view hasFocus ifTrue:[
 	    "just simulate a buttonPress/release here."
 	    self buttonPress:1 x:0 y:0.
 	    self buttonRelease:1 x:0 y:0.