drag & drop mechanism
authorca
Tue, 21 Jan 2003 08:22:36 +0100
changeset 1675 9e7465c57509
parent 1674 ed8aa80a11a9
child 1676 3f488e7cbbae
drag & drop mechanism
UIGalleryView.st
--- a/UIGalleryView.st	Sat Jan 18 14:53:08 2003 +0100
+++ b/UIGalleryView.st	Tue Jan 21 08:22:36 2003 +0100
@@ -22,8 +22,8 @@
 !
 
 View subclass:#Canvas
-	instanceVariableNames:'dragMode clientSpecHolder selection specification lastClickPoint
-		menuSelector uiBuilder buttonPressed'
+	instanceVariableNames:'clientSpecHolder selection specification lastClickPoint
+		menuSelector uiBuilder hiddenCounter'
 	classVariableNames:''
 	poolDictionaries:''
 	privateIn:UIGalleryView
@@ -407,7 +407,7 @@
 
 !UIGalleryView::Canvas methodsFor:'drag & drop'!
 
-startDrag
+startDragFrom:evView
     "start drag at a point
     "
     |spec dragObj offset clickPos|
@@ -419,16 +419,15 @@
     spec := clientSpecHolder value.
     spec isNil ifTrue:[^ self].
 
-    self hideSelection.
+    self selectionHiddenDo:[
+        spec := self buildSpecFrom:spec.
+        spec name:nil.
 
-    dragMode := true.
-    spec := self buildSpecFrom:spec.
-    spec name:nil.
+        dragObj := DropSpec for:selection specification:spec.
+        offset  := clickPos - selection origin.
+    ].
 
-    dragObj := DropSpec for:selection specification:spec.
-    offset  := clickPos - selection origin.
-
-    DragAndDropManager startDrag:dragObj from:self offset:offset.
+    DragAndDropManager startDrag:dragObj from:evView offset:offset.
 ! !
 
 !UIGalleryView::Canvas methodsFor:'event handling'!
@@ -436,15 +435,6 @@
 processEvent:anEvent
     |evView p|
 
-    dragMode ifTrue:[
-        anEvent isButtonReleaseEvent ifTrue:[
-            dragMode := false.
-            lastClickPoint := nil.
-            self invalidateSelection.
-        ].
-        ^ false
-    ].
-
     evView := anEvent view.
     evView isNil ifTrue:[ ^ false ].
 
@@ -454,7 +444,7 @@
 
     anEvent isButtonEvent ifFalse:[
         anEvent isInputEvent ifTrue:[^ true].
-        anEvent isDamage ifTrue:[ self invalidateSelection ].
+        anEvent isDamage ifTrue:[ self redrawSelection ].
         ^ false
     ].
 
@@ -468,7 +458,8 @@
             p := Point x:(anEvent x) y:(anEvent y).
 
             (lastClickPoint dist:p) > 10.0 ifTrue:[
-                self startDrag.
+                self startDragFrom:evView.
+                lastClickPoint := nil.
             ]
         ].
         ^ true
@@ -504,10 +495,8 @@
 
 initialize
     super initialize.
-
-    dragMode         := false.
-    buttonPressed    := false.
     clientSpecHolder := nil asValue.
+    hiddenCounter := 0.
 !
 
 realize
@@ -515,27 +504,6 @@
     self windowGroup addPreEventHook:self.
 ! !
 
-!UIGalleryView::Canvas methodsFor:'private'!
-
-redrawSelection
-    "redraw all items selected
-    "
-    (buttonPressed or:[selection isNil or:[shown not]]) ifTrue:[
-        ^ self
-    ].
-
-    (self sensor hasEvent:#redrawSelection for:self) ifTrue:[^ self].
-
-    (buttonPressed or:[selection isNil ]) ifFalse:[
-        self clippedByChildren:false.
-
-        self handlesOf:selection do:[:aRectangle|
-            self fillRectangle:aRectangle
-        ].
-        self clippedByChildren:true.
-    ].
-! !
-
 !UIGalleryView::Canvas methodsFor:'searching'!
 
 findObjectAt:aPoint
@@ -600,49 +568,16 @@
 
 !
 
-hideSelection
-    "show unselected
+redrawSelection
+    "redraw all items selected
     "
-    |r saveSelect|
-
-    (shown and:[selection notNil]) ifFalse:[
-        ^ self
-    ].
-    saveSelect := selection.
-    selection  := nil.
-
-    self clippedByChildren:false.
-
-    self handlesOf:saveSelect do:[:aRectangle|
-        self clearRectangle:aRectangle
-    ].
-    self clippedByChildren:true.
-
-    r := saveSelect bounds.
+    (shown and:[selection notNil and:[hiddenCounter == 0]]) ifTrue:[
+        self clippedByChildren:false.
 
-    subViews do:[:sv|
-        |absOrg absFrame|
-
-        (sv bounds intersects:r) ifTrue:[
-            sv borderColor:(Color white).           "/ to force a redraw
-            sv borderColor:(Color black).
-
-            sv withAllSubViewsDo:[:v|
-                v realized ifTrue:[
-                    v fill:v viewBackground.
-                    v exposeX:0 y:0 width:v width height:v height.
-                ]
-            ]
-        ]
-    ].
-    selection := saveSelect.
-!
-
-invalidateSelection
-    "show selected
-    "
-    (shown and:[selection notNil and:[buttonPressed not]]) ifTrue:[
-        self sensor pushUserEvent:#redrawSelection for:self withArguments:nil.
+        self handlesOf:selection do:[:aRectangle|
+            self fillRectangle:aRectangle
+        ].
+        self clippedByChildren:true.
     ].
 !
 
@@ -653,14 +588,54 @@
 
     selection == aView ifTrue:[^ self].
 
-    self hideSelection.
-    spec := self findSpecFor:aView.
+    self selectionHiddenDo:[
+        spec := self findSpecFor:aView.
+
+        spec notNil ifTrue:[ selection := aView ]
+                   ifFalse:[ selection := nil ].
+
+        clientSpecHolder value:spec.
+    ].
+!
+
+selectionHiddenDo:aOneArgBlock
+    |r|
+
+    [   hiddenCounter :=  hiddenCounter + 1.
+
+        hiddenCounter == 1 ifTrue:[
+            (shown and:[selection notNil]) ifTrue:[
+                self clippedByChildren:false.
+
+                self handlesOf:selection do:[:aRectangle|
+                    self clearRectangle:aRectangle
+                ].
+                self clippedByChildren:true.
 
-    spec notNil ifTrue:[ selection := aView ]
-               ifFalse:[ selection := nil ].
+                r := selection bounds.
+
+                subViews do:[:sv|
+                    |absOrg absFrame|
+
+                    (sv bounds intersects:r) ifTrue:[
+                        sv borderColor:(Color white).           "/ to force a redraw
+                        sv borderColor:(Color black).
 
-    clientSpecHolder value:spec.
-    self invalidateSelection.    
+                        sv withAllSubViewsDo:[:v|
+                            v realized ifTrue:[
+                                v fill:v viewBackground.
+                                v exposeX:0 y:0 width:v width height:v height.
+                            ]
+                        ]
+                    ]
+                ]
+            ].
+        ].
+        aOneArgBlock value.
+    ] ensure:[
+        hiddenCounter := hiddenCounter - 1.
+        self redrawSelection.
+    ].
 ! !
 
 !UIGalleryView::Canvas::DropSpec class methodsFor:'instance creation'!