VariablePanelController.st
changeset 2159 2864c0de7f33
parent 2005 9b1ff373a8ad
child 2163 38b6b9fe6796
--- a/VariablePanelController.st	Wed Feb 23 11:29:02 2000 +0100
+++ b/VariablePanelController.st	Wed Feb 23 18:23:15 2000 +0100
@@ -11,7 +11,7 @@
 "
 
 Controller subclass:#VariablePanelController
-	instanceVariableNames:'movedHandle prevPos startPos clickPos'
+	instanceVariableNames:'movedHandle prevPos clickPos clickOffset'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Interface-Support-Controllers'
@@ -83,8 +83,7 @@
     "mouse-button was moved while pressed;
      clear prev handleBar and draw handle bar at new position" 
 
-    |pos limitMin limitMax subViews barHeight
-     oldHx oldHy newHx newHy opaque|
+    |pos oldHx oldHy newHx newHy opaque|
 
     movedHandle isNil ifTrue: [^ self].          "should not happen"
 
@@ -93,147 +92,107 @@
 
 "/    view buttonMotionEventPending ifTrue:[^ self].
 
-    subViews := view subViews.
-    barHeight := view barHeight.
-
-    view orientation ~~ #vertical ifTrue:[
-        pos := bx - startPos.
-        "
-         the two lines below will not allow resizing down to zero
-         (so that some is always visible)
-        "
-"/        limitMin := barHeight // 2.
-"/        limitMax := view width - barHeight.
-        "
-         these allow resizing to zero - which is better ?
-        "
-        limitMin := 0.
-        limitMax := view innerWidth.
-
-        movedHandle > 1 ifTrue:[
-            limitMin := (subViews at:movedHandle) origin x + (barHeight // 2)
-        ].
-        movedHandle < (subViews size - 1) ifTrue:[
-            limitMax := (subViews at:(movedHandle + 2)) origin x - barHeight
-        ].
-    ] ifFalse:[
-        pos := by - startPos.
-        "
-         the two lines below will not allow resizing down to zero
-         (so that some is always visible)
-        "
-"/        limitMin := barHeight // 2.
-"/        limitMax := view height - barHeight.
-        "
-         these allow resizing to zero - which is better ?
-        "
-        limitMin := 0.
-        limitMax := view innerHeight.
-
-        movedHandle > 1 ifTrue:[
-            limitMin := (subViews at:movedHandle) origin y + (barHeight // 2)
-        ].
-        movedHandle < (subViews size - 1) ifTrue:[
-            limitMax := (subViews at:(movedHandle + 2)) origin y - barHeight
-        ].
-    ].
-
-    limitMax := limitMax - barHeight.
-    (pos < limitMin) ifTrue:[ "check against view limits"
-        pos := limitMin
-    ] ifFalse:[
-        (pos > limitMax) ifTrue:[
-            pos := limitMax
-        ]
-    ].
-
+    pos := self checkedHandleMovementX:bx y:by.
     prevPos == pos ifTrue:[^ self].
 
-    view orientation ~~ #vertical ifTrue:[
-        oldHx := prevPos. 
-        newHx := pos.
-        oldHy := newHy := 0.
-    ] ifFalse:[
-        oldHy := prevPos.
-        newHy := pos.
-        oldHx := newHx := 0.
-    ].
-
     opaque := UserPreferences current opaqueVariablePanelResizing.
     opaque ~~ true ifTrue:[
+        view orientation ~~ #vertical ifTrue:[
+            oldHx := prevPos. 
+            newHx := pos.
+            oldHy := newHy := 0.
+        ] ifFalse:[
+            oldHy := prevPos.
+            newHy := pos.
+            oldHx := newHx := 0.
+        ].
+
         view invertHandleBarAtX:oldHx y:oldHy. 
         view invertHandleBarAtX:newHx y:newHy.
     ].
     prevPos := pos.
 
     opaque == true ifTrue:[
-        self doResizeForX:bx y:by.
+        self doResizeForPosition:prevPos.
     ]
 
-    "Modified: / 13.11.1998 / 15:13:47 / cg"
+    "Modified: / 23.2.2000 / 18:04:43 / cg"
 !
 
 buttonPress:button x:bx y:by
     "button was pressed - if it hits a handle, start move"
 
-    |handle barHeight group isHorizontal|
+    |handle barHeight halfBarHeight group isHorizontal 
+     theNearestHandle hX hY smallestClickOffsetSoFar|
 
     ((button == 1) or:[button == #select]) ifTrue:[
-        handle := 1.
+
         barHeight := view barHeight.
+        halfBarHeight := (barHeight // 2) max:1.
 
         isHorizontal := view orientation ~~ #vertical.
 
         "
          search the handle, invert the first time
         "
+        handle := 1.
         view handleOriginsWithIndexDo:[:hPoint :hIndex |
-            |hx hy thatsTheHandle|
-
-            thatsTheHandle := false.
+            |hx hy dist|
 
             isHorizontal ifTrue:[
                 hx := hPoint x.
                 hy := 0.
-                (bx between:(hx - barHeight) and:(hx + barHeight)) ifTrue:[
-                    prevPos := hx.
-                    startPos := bx - hx.
-                    thatsTheHandle := true.
+                (bx between:(hx - 1 "- barHeight") and:(hx + barHeight)) ifTrue:[
+                    dist := bx - hx.
+                    (smallestClickOffsetSoFar isNil 
+                    or:[dist abs < smallestClickOffsetSoFar abs]) ifTrue:[
+                        smallestClickOffsetSoFar := dist.
+                        theNearestHandle := handle.
+                    ].
                 ].
             ] ifFalse:[
                 hx := 0.
                 hy := hPoint y.
-                (by between:(hy - barHeight) and:(hy + barHeight)) ifTrue:[
-                    prevPos := hy.
-                    startPos := by - hy.
-                    thatsTheHandle := true.
+                (by between:(hy - 1 "- barHeight") and:(hy + barHeight)) ifTrue:[
+                    dist := by - hy.
+                    (smallestClickOffsetSoFar isNil 
+                    or:[dist abs < smallestClickOffsetSoFar abs]) ifTrue:[
+                        smallestClickOffsetSoFar := dist.
+                        theNearestHandle := handle.
+                    ].
+                ].
+            ].
+            handle := handle + 1
+        ].
+
+        theNearestHandle notNil ifTrue:[
+            movedHandle := theNearestHandle.
+            clickOffset := smallestClickOffsetSoFar.
+            isHorizontal ifTrue:[
+                prevPos := hX.
+            ] ifFalse:[
+                prevPos := hY.
+            ].
+
+            UserPreferences current opaqueVariablePanelResizing == true ifTrue:[
+                view grabPointerWithCursor:view cursor.
+            ] ifFalse:[
+                view invertHandleBarAtX:hX y:hY.
+                (group := view windowGroup) notNil ifTrue:[
+                    group showCursor:view cursor
                 ].
             ].
 
-            thatsTheHandle ifTrue:[
-                movedHandle := handle.
+            clickPos := bx @ by.
+            ^ self
+        ].
 
-                UserPreferences current opaqueVariablePanelResizing == true ifTrue:[
-                    view grabPointerWithCursor:view cursor.
-                ] ifFalse:[
-                    view invertHandleBarAtX:hx y:hy.
-                    (group := view windowGroup) notNil ifTrue:[
-                        group showCursor:view cursor
-                    ].
-                ].
-
-                clickPos := bx @ by.
-                ^ self
-            ].
-
-            handle := handle + 1
-        ].
         movedHandle := nil
     ] ifFalse:[
         super buttonPress:button x:bx y:by
     ]
 
-    "Modified: / 13.11.1998 / 15:18:10 / cg"
+    "Modified: / 23.2.2000 / 18:04:32 / cg"
 !
 
 buttonRelease:button x:bx y:by
@@ -251,43 +210,107 @@
             ]
         ].
 
-        self doResizeForX:bx y:by.
+        self doResizeForPosition:prevPos.
         movedHandle := nil.
 
     ] ifFalse:[
         super buttonRelease:button x:bx y:by
     ]
 
-    "Modified: / 11.3.1999 / 16:31:29 / cg"
+    "Modified: / 23.2.2000 / 13:32:00 / cg"
 !
 
-doResizeForX:bx y:by
-    "end bar-move"
+checkedHandleMovementX:bx y:by
+    |subViews barHeight halfBarHeight pos limitMin limitMax|
+
+    subViews := view subViews.
+    barHeight := view barHeight.
+    halfBarHeight := (barHeight // 2) max:1.
+
+    view orientation ~~ #vertical ifTrue:[
+        pos := bx - clickOffset.
+        "
+         the two lines below will not allow resizing down to zero
+         (so that some is always visible)
+        "
+"/        limitMin := barHeight // 2.
+"/        limitMax := view width - barHeight.
+        "
+         these allow resizing to zero - which is better ?
+        "
+        limitMin := 0 "- halfBarHeight + 1" + view margin.
+        limitMax := view width - view margin "innerWidth" - barHeight.
+
+        movedHandle > 1 ifTrue:[
+            limitMin := (subViews at:movedHandle) origin x "+ halfBarHeight"
+        ].
+        movedHandle < (subViews size - 1) ifTrue:[
+            limitMax := (subViews at:(movedHandle + 2)) origin x - barHeight - halfBarHeight - 1
+        ].
+    ] ifFalse:[
+        pos := by - clickOffset.
+        "
+         the two lines below will not allow resizing down to zero
+         (so that some is always visible)
+        "
+"/        limitMin := barHeight // 2.
+"/        limitMax := view height - barHeight.
+        "
+         these allow resizing to zero - which is better ?
+        "
+        limitMin := 0 "- halfBarHeight + 1" + view margin.
+        limitMax := view height - view margin "innerHeight" - barHeight.
+
+        movedHandle > 1 ifTrue:[
+            limitMin := (subViews at:movedHandle) origin y "+ halfBarHeight"
+        ].
+        movedHandle < (subViews size - 1) ifTrue:[
+            limitMax := (subViews at:(movedHandle + 2)) origin y - barHeight - halfBarHeight - 1
+        ].
+    ].
+
+    (pos < limitMin) ifTrue:[ "check against limits"
+        pos := limitMin
+    ] ifFalse:[
+        (pos > limitMax) ifTrue:[
+            pos := limitMax
+        ]
+    ].
+    ^ pos
+
+    "Created: / 23.2.2000 / 12:43:07 / cg"
+    "Modified: / 23.2.2000 / 18:30:43 / cg"
+!
+
+doResizeForPosition:newPos
+    "perform the resize"
 
     |aboveView belowView aboveIndex belowIndex 
-     newPos oldPos group subViews 
+     newRelPos oldRelPos subViews 
      relCornerAbove fromIndex toIndex
-     hX hY isHorizontal newCorner newOrigin|
+     hX hY isHorizontal newCorner newOrigin halfBarHeight|
 
     isHorizontal := view orientation ~~ #vertical.
 
     "undo the last invert"
 
-    isHorizontal ifTrue:[
-        hX := prevPos. hY := 0. 
-    ] ifFalse:[
-        hX := 0. hY := prevPos. 
-    ].
     UserPreferences current opaqueVariablePanelResizing ~~ true ifTrue:[
+        isHorizontal ifTrue:[
+            hX := prevPos. hY := 0. 
+        ] ifFalse:[
+            hX := 0. hY := prevPos. 
+        ].
         view invertHandleBarAtX:hX y:hY. 
     ].
 
     "/ any change ?
-    ((isHorizontal and:[bx == clickPos x])
-    or:[isHorizontal not and:[by == clickPos y]]) ifTrue:[
+    ((isHorizontal and:[newPos == clickPos x])
+    or:[isHorizontal not and:[newPos == clickPos y]]) ifTrue:[
         ^ self.
     ].
 
+    halfBarHeight := (view barHeight // 2) max:1.
+
     "compute the new relative heights"
 
     aboveIndex := movedHandle.
@@ -299,22 +322,22 @@
 
     relCornerAbove := aboveView relativeCorner.
     isHorizontal ifTrue:[
-        oldPos := relCornerAbove x.
-        newPos := (prevPos + startPos / view width) asFloat.
-        newCorner := newPos @ relCornerAbove y.
-        newOrigin := newPos @ belowView relativeOrigin y.
+        oldRelPos := relCornerAbove x.
+        newRelPos := (newPos + halfBarHeight / view width) asFloat.
+        newCorner := newRelPos @ relCornerAbove y.
+        newOrigin := newRelPos @ belowView relativeOrigin y.
     ] ifFalse:[
-        oldPos := relCornerAbove y.
-        newPos := (prevPos + startPos / view height) asFloat.
-        newCorner := relCornerAbove x @ newPos.
-        newOrigin := belowView relativeOrigin x @ newPos.
+        oldRelPos := relCornerAbove y.
+        newRelPos := (newPos + halfBarHeight / view height) asFloat.
+        newCorner := relCornerAbove x @ newRelPos.
+        newOrigin := belowView relativeOrigin x @ newRelPos.
     ].
     aboveView relativeCorner:newCorner.
     belowView relativeOrigin:newOrigin.
 
     view lockRedraw.
 
-    oldPos > newPos ifTrue:[
+    oldRelPos > newRelPos ifTrue:[
         fromIndex := aboveIndex. toIndex := belowIndex.
     ] ifFalse:[
         fromIndex := belowIndex. toIndex := aboveIndex.
@@ -323,7 +346,8 @@
     view redrawHandlesFrom:aboveIndex to:belowIndex.
     view unlockRedraw.
 
-    "Modified: / 11.3.1999 / 16:31:29 / cg"
+    "Created: / 23.2.2000 / 13:31:06 / cg"
+    "Modified: / 23.2.2000 / 17:44:35 / cg"
 !
 
 pointerEnter:state x:x y:y
@@ -338,5 +362,5 @@
 !VariablePanelController class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg/VariablePanelController.st,v 1.23 1999-09-06 12:57:31 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg/VariablePanelController.st,v 1.24 2000-02-23 17:23:15 cg Exp $'
 ! !