SelectionInListView.st
changeset 2138 a634610a37de
parent 2136 eb816448daec
child 2139 a7444d2da356
--- a/SelectionInListView.st	Fri Feb 11 15:57:10 2000 +0100
+++ b/SelectionInListView.st	Fri Feb 11 17:21:45 2000 +0100
@@ -19,7 +19,8 @@
 		returnKeyActionStyle toggleSelect strikeOut iSearchString items
 		doubleClickMsg hilightStyle clickPosition allowDrag
 		dragObjectConverter dragIsActive endDragAction dropTarget
-		dropSource visualBlock selectedVisualBlock'
+		dropSource visualBlock selectedVisualBlock
+		selectionChangeConditionBlock'
 	classVariableNames:'RightArrowShadowForm RightArrowLightForm RightArrowForm
 		SmallRightArrowShadowForm SmallRightArrowLightForm
 		DefaultForegroundColor DefaultBackgroundColor
@@ -122,7 +123,17 @@
 
         halfIntensityColor      <Color>         foreground for disabled items
 
-        selectConditionBlock    <Block>         if non-nil, this nlock can decide if selection is ok
+        selectConditionBlock    <Block>         if non-nil, this block can decide if selection is ok.
+                                                its invoked with the itemNr of the
+                                                'to-be-selected' item.
+                                                If the block returns true, the item is selected
+                                                (or added to the selection); if false is returned,
+                                                no action is taken.
+        selectionChangeConditionBlock
+                                <Block>         much like above, but invoked without argument,
+                                                on any change of the selection (i.e. also when items
+                                                are deselected).
+                                                Can return false to suppress change.
 
         doubleClickActionBlock  <Block>         action to perform on double-click
                                                 (1-arg blocks gets selectionIndex or selectionValue
@@ -1122,13 +1133,23 @@
 
 selectConditionBlock:aBlock
     "set the conditionBlock; this block is evaluated before a selection
-     change is performed; the change will not be done, if the evaluation
-     returns false. For example, this allows confirmation queries in
-     the SystemBrowser"
+     is made or added to the selection (i.e. not on removes);
+     The block gets the 'about-to-be-selected' items itemNr as argument.
+     The change will not be done, if the evaluation returns false. 
+     For example, this allows confirmation queries in the SystemBrowser"
 
     selectConditionBlock := aBlock
 !
 
+selectionChangeConditionBlock:aBlock
+    "set the selectionChange-conditionBlock; this block is evaluated before 
+     any selection change is performed.
+     The change will not be done, if the block returns false. 
+     For example, this allows confirmation queries in the SystemBrowser"
+
+    selectionChangeConditionBlock := aBlock
+!
+
 useIndex
     "set/clear the useIndex flag. If set, both actionBlock and change-messages
      are passed the index(indices) of the selection as argument. 
@@ -1611,6 +1632,11 @@
 
     (self isValidSelection:aNumber) ifFalse:[^ self].
 
+    "/ any change allowed at all ?
+    (selectionChangeConditionBlock notNil 
+     and:[(selectionChangeConditionBlock value) not]) ifTrue:[^ self].
+
+    "/ this item selectable ?
     (selectConditionBlock notNil 
      and:[(selectConditionBlock value:aNumber) not]) ifTrue:[^ self].
 
@@ -2087,12 +2113,13 @@
 "/           ^ self buttonPress:button x:x y:y
 "/        ].
         enabled ifTrue:[
+            "/ any change allowed at all ?
+            (selectionChangeConditionBlock notNil 
+             and:[(selectionChangeConditionBlock value) not]) ifTrue:[^ self].
+
             listLineNr := self visibleLineToListLine:(self visibleLineOfY:y).
             listLineNr notNil ifTrue:[
                 (self lineIsEnabled:listLineNr) ifFalse:[^ self].
-
-                (selectConditionBlock notNil 
-                 and:[(selectConditionBlock value:listLineNr) not]) ifTrue:[^ self].
             ].
             oldSelection := selection copy.
             listLineNr notNil ifTrue: [
@@ -2103,6 +2130,10 @@
                         self addToSelection:listLineNr
                     ]
                 ] ifFalse:[
+                    "/ this item selectable ?
+                    (selectConditionBlock notNil 
+                     and:[(selectConditionBlock value:listLineNr) not]) ifTrue:[^ self].
+
                     self selectWithoutScroll:listLineNr
                 ]
             ].
@@ -2305,24 +2336,30 @@
         ^ self
     ].
 
-    (selectConditionBlock isNil or:[selectConditionBlock value:index]) ifTrue:[
-        keyActionStyle notNil ifTrue:[
-            keyActionStyle == #pass ifTrue:[
-                ^ super keyPress:key x:x y:y
-            ].
-            oldSelection := selection.
-            (multipleSelectOk and:[self sensor shiftDown]) ifTrue:[
-                (self isInSelection:index) ifTrue:[
-                    self removeFromSelection:index
-                ] ifFalse:[
-                    self addToSelection:index
-                ]
+    "/ any change allowed at all ?
+    (selectionChangeConditionBlock notNil 
+     and:[(selectionChangeConditionBlock value) not]) ifTrue:[^ self].
+
+    keyActionStyle notNil ifTrue:[
+        keyActionStyle == #pass ifTrue:[
+            ^ super keyPress:key x:x y:y
+        ].
+        oldSelection := selection.
+        (multipleSelectOk and:[self sensor shiftDown]) ifTrue:[
+            (self isInSelection:index) ifTrue:[
+                self removeFromSelection:index
             ] ifFalse:[
-                self selection:index.
-        
-                keyActionStyle == #selectAndDoubleClick ifTrue:[
-                    self doubleClicked
-                ]
+                self addToSelection:index
+            ]
+        ] ifFalse:[
+            "/ this item selectable ?
+            (selectConditionBlock notNil 
+            and:[(selectConditionBlock value:index) not]) ifTrue:[^ self].
+
+            self selection:index.
+    
+            keyActionStyle == #selectAndDoubleClick ifTrue:[
+                self doubleClicked
             ]
         ]
     ].
@@ -2886,6 +2923,10 @@
 selectOrToggleAtX:x y:y
     |oldSelection listLineNr|
 
+    "/ any change allowed at all ?
+    (selectionChangeConditionBlock notNil 
+     and:[(selectionChangeConditionBlock value) not]) ifTrue:[^ self].
+
     listLineNr := self visibleLineToListLine:(self visibleLineOfY:y).
     listLineNr notNil ifTrue:[
         (toggleSelect 
@@ -2895,14 +2936,14 @@
         ] ifFalse:[
             (self lineIsEnabled:listLineNr) ifFalse:[^ self].
 
-            (selectConditionBlock notNil 
-             and:[(selectConditionBlock value:listLineNr) not]) ifTrue:[^ self].
-
+            oldSelection := selection copy.
             (toggleSelect and:[multipleSelectOk]) ifTrue:[
-                oldSelection := selection copy.
                 self addToSelection:listLineNr
             ] ifFalse:[
-                oldSelection := selection copy.
+                "/ this item selectable ?
+                (selectConditionBlock notNil 
+                 and:[(selectConditionBlock value:listLineNr) not]) ifTrue:[^ self].
+
                 self selectWithoutScroll:listLineNr.
             ].
         ].
@@ -3659,5 +3700,5 @@
 !SelectionInListView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg/SelectionInListView.st,v 1.160 2000-02-11 13:31:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg/SelectionInListView.st,v 1.161 2000-02-11 16:21:45 cg Exp $'
 ! !