UIObjectView.st
changeset 128 9779b7459a1c
parent 123 421d412e797b
child 131 715b3dbba87d
--- a/UIObjectView.st	Mon May 26 13:06:42 1997 +0200
+++ b/UIObjectView.st	Mon May 26 13:08:24 1997 +0200
@@ -21,6 +21,19 @@
 	privateIn:UIObjectView::UndoHistory
 !
 
+!UIObjectView class methodsFor:'documentation'!
+
+documentation
+"
+    buildIn view used by the UIPainter; from this view, the layout of the
+    new application derives from.
+
+    [see also:]
+        UIBuilder
+        UIPainterView
+"
+
+! !
 
 !UIObjectView class methodsFor:'conversion'!
 
@@ -233,16 +246,14 @@
     "
     (aState == enableChannel value) ifFalse:[
         aState ifFalse:[
-            saveSelection := selection copy
-        ].
-        enableChannel value:aState.
-
-        aState ifFalse:[
+            saveSelection := self selection copy.
             self unselect.
-            inputView unrealize
+            enableChannel value:aState.
+            inputView unrealize.
         ] ifTrue:[
             inputView raise.
             inputView realize.
+            enableChannel value:aState.
             self select:saveSelection.
         ]
     ]
@@ -298,9 +309,7 @@
     "handle an expose event from device; redraw selection
     "
     super exposeX:x y:y width:w height:h.
-"/    selectionHiddenLevel == 0 ifTrue:[
-        self selectionDo:[:v | self showSelected:v]
-"/    ]
+    self selectionDo:[:v | self showSelected:v]
 
 !
 
@@ -315,27 +324,14 @@
 
     key == #Copy  ifTrue:[ ^ self copySelection].
     key == #Paste ifTrue:[ ^ self pasteBuffer].
-
-    super keyPress:key x:x y:y
-
-
 !
 
 processEvent:anEvent
     "catch expose events for components, and redraw its handles after
      the redraw when this happens
     "
-    |view|
-
-    selection notNil ifTrue:[
-        anEvent type == #damage ifTrue:[
-            view := anEvent view.
-            (selection == view
-            or:[selection isCollection
-                and:[selection includes:view]]) ifTrue:[
-                    self showSelected:view
-            ]
-        ]
+    (anEvent type == #damage and:[self isSelected:(anEvent view)]) ifTrue:[
+        self showSelected:(anEvent view)
     ].
     ^ false.
 
@@ -630,19 +626,13 @@
     "cleanup after object(s) move
     "
     movedObject notNil ifTrue:[
-        movedObject do:[:aView|
-            self invertOutlineOf:aView
-        ].
-
-        movedObject do:[:aView|
-            self showSelected:aView
+        movedObject do:[:aView|self invertOutlineOf:aView].
+
+        movedObject size == 1 ifTrue:[
+            movedObject := movedObject first
         ].
-        movedObject size == 1 ifTrue:[
-            selection := movedObject at:1
-        ] ifFalse:[
-            selection := movedObject
-        ].
-
+
+        self setSelection:movedObject withRedraw:true.
         movedObject := nil.
         self setDefaultActions.
         self changed:#layout.
@@ -672,14 +662,13 @@
 startObjectMoveAt:aPoint
     "start object(s) move at a point
     "
-    self startObjectMove:selection at:aPoint.
-
-    selection isCollection ifTrue:[
-        movedObject := selection
-    ] ifFalse:[
-        movedObject := Array with:selection
+    self startObjectMove:(self selection) at:aPoint.
+    movedObject := self selection.
+
+    movedObject isCollection ifFalse:[
+        movedObject := Array with:movedObject
     ].
-    super unselect.
+    self setSelection:nil withRedraw:true.
 
     moveDelta := movedObject collect:[:aView|
         aPoint - aView computeOrigin
@@ -738,13 +727,11 @@
         ].
 
         (self canMove:aView) ifFalse:[
-            super unselect.
-          ^ self select:aView
+            ^ self select:aView
         ]
     ].
 
     (self isSelected:aView) ifFalse:[
-        super unselect.
         self select:aView.
     ].
 
@@ -817,7 +804,7 @@
     self invertOutlineOf:object.
     self setDefaultActions.
     self elementChangedSize:object.
-    super select:object.
+    self setSelection:object withRedraw:true.
     self changed:#layout.
 !
 
@@ -832,7 +819,7 @@
     self transaction:#resize selectionDo:[:aView|
         self undoLayoutView:aView
     ].
-    super unselect.
+    self setSelection:nil withRedraw:true.
 
     motionAction  := [:movePoint | self doDragResize:movePoint].
     releaseAction := [self endResize].
@@ -1213,41 +1200,80 @@
 
 !UIObjectView methodsFor:'selections'!
 
-addToSelection:something
-    "add something to selection
+addToSelection:anObject
+    "add anObject to selection
     "
-    (self canSelect:something) ifTrue:[
-        super addToSelection:something.
-        self selectionChanged.
+    |coll|
+
+    self enabled ifTrue:[
+        self hasSelection ifFalse:[
+             self select:anObject
+        ] ifTrue:[
+            (self isSelected:anObject) ifFalse:[
+                (coll := self selection) isCollection ifFalse:[
+                    coll := OrderedCollection with:coll
+                ].
+                coll add:anObject.
+                self setSelection:coll withRedraw:false.
+                self showSelected:anObject.
+                self selectionChanged.
+            ]
+        ]
     ]
 !
 
+moveableSelection
+    "tests whether all selected objects are moveable and in case
+     of true the selection is returned, otherwise nil
+    "
+    |coll|
+
+    self hasSelection ifTrue:[
+        (self canMove:(coll := self selection)) ifTrue:[
+            ^ coll
+        ]
+    ].
+  ^ nil
+!
+
 numberOfSelections
     "return the number of selected entries
     "
-    |sz|
-
-    selection isNil ifTrue:[^ 0].
-
-    selection isCollection ifTrue:[^ selection size]
-                          ifFalse:[^ 1 ]
+    |coll size|
+
+    coll := self selection.
+    size := coll size.
+
+    (size ~~ 0 or:[coll isNil]) ifTrue:[^ size].
+  ^ 1
 !
 
-removeFromSelection:something
-    "remove something from selection
+removeFromSelection:anObject
+    "remove anObject from selection
     "
-    something notNil ifTrue:[
-        super removeFromSelection:something.
-        self selectionChanged
+    |sel|
+
+    (self isSelected:anObject) ifFalse:[
+        ^ self
     ].
 
+    sel := self selection.
+
+    sel size > 1 ifTrue:[
+        sel := sel copy.
+        sel remove:anObject ifAbsent:nil.
+    ] ifFalse:[
+        sel := nil
+    ].
+    self setSelection:sel withRedraw:true.
+    self selectionChanged.
 !
 
 select:something
     "change selection to something
     "
-    (self canSelect:something) ifTrue:[
-        super select:something.
+    (self enabled and:[something ~= self selection]) ifTrue:[
+        self setSelection:something withRedraw:true.
         self selectionChanged
     ]
 
@@ -1270,20 +1296,45 @@
 
 !
 
+selectionDo:aBlock
+    "apply block to every selected object
+    "
+    self forEach:(self selection) do:aBlock
+
+
+!
+
+setSelection:aNewSelection withRedraw:doRedraw
+    "set a new selection
+    "
+    doRedraw ifTrue:[
+        self hideSelection.
+        selection := aNewSelection.
+        self showSelection
+    ] ifFalse:[
+        selection := aNewSelection
+    ]
+!
+
 showSelection
+    "show the selection - draw handles
+    "
     selectionHiddenLevel == 0 ifTrue:[
-        super showSelection.
+        self selectionDo:[:el| self showSelected:el ]
     ].
 !
 
 singleSelection
     "returns single selection or nil
     "
-    selection isCollection ifFalse:[
-        ^ selection
+    |coll|
+
+    (coll := self selection) isCollection ifFalse:[
+        ^ coll
     ].
-    selection size == 1 ifTrue:[ ^ selection at:1]
-                       ifFalse:[ ^ nil].
+
+    coll size == 1 ifTrue:[ ^ coll first].
+  ^ nil
 !
 
 singleSelectionDo:aBlock
@@ -1299,12 +1350,7 @@
 unselect
     "clear selection
     "
-    selection notNil ifTrue:[
-        super unselect.
-        self selectionChanged
-    ]
-
-
+    self select:nil
 !
 
 withSelectionHiddenDo:aBlock
@@ -1338,13 +1384,13 @@
     "
     |sel|
 
-    selection isNil ifTrue:[
+    self hasSelection ifFalse:[
         aBlock value
-    ] ifFalse:[
-        sel := selection.
-        super unselect.
+    ] ifTrue:[
+        sel := self selection.
+        self setSelection:nil withRedraw:true.
         aBlock value.
-        super select:sel
+        self setSelection:sel withRedraw:true.
     ]
 
 
@@ -1415,10 +1461,10 @@
     "Modified: 8.4.1997 / 01:19:14 / cg"
 !
 
-canSelect:something
-    "returns true if something can be selected and enabled is true
+hasSelection
+    "returns true if any selected object exists
     "
-    ^ (self enabled and:[something ~~ selection])
+    ^ self numberOfSelections ~~ 0
 
 !
 
@@ -1436,6 +1482,16 @@
 
 !
 
+isSelected:anObject
+    "return true, if the argument, anObject is selected
+    "
+    anObject notNil ifTrue:[
+        self selectionDo:[:el| el == anObject ifTrue:[^ true]]
+    ].
+  ^ false
+
+!
+
 isVerticalResizable:aComponent
     "returns true if instance is vertical resizeable
     "
@@ -1467,7 +1523,7 @@
     "opens a transaction and evaluates a block within the transaction; the
      argument to the block is a view from the selection
     "
-    self transaction:aType objects:selection do:aOneArgBlock
+    self transaction:aType objects:(self selection) do:aOneArgBlock
 
 
 !
@@ -1645,9 +1701,9 @@
 moveSelectionDown
     "move selection down
     "
-    |gridY n|
-
-    (self canMove:selection) ifTrue:[
+    |gridY n sel|
+
+    self moveableSelection notNil ifTrue:[
         gridAlign notNil ifTrue:[gridY := gridAlign y].
 
         self withSelectionHiddenDo:[
@@ -1676,7 +1732,7 @@
     "
     |gridX n|
 
-    (self canMove:selection) ifTrue:[
+    self moveableSelection notNil ifTrue:[
         gridAlign notNil ifTrue:[gridX := gridAlign x].
 
         self withSelectionHiddenDo:[
@@ -1700,7 +1756,7 @@
     "
     |gridX n|
 
-    (self canMove:selection) ifTrue:[
+    self moveableSelection notNil ifTrue:[
         gridAlign notNil ifTrue:[gridX := gridAlign x].
 
         self withSelectionHiddenDo:[
@@ -1725,7 +1781,7 @@
     "
     |gridY n|
 
-    (self canMove:selection) ifTrue:[
+    self moveableSelection notNil ifTrue:[
         gridAlign notNil ifTrue:[gridY := gridAlign y].
 
         self withSelectionHiddenDo:[
@@ -1751,12 +1807,12 @@
     "align selection to the bottom of the first object in the selection; in case
      of one selection the object is aligned to the bottom of its superview
     "
-    |bmost delta|
-
-    (self canMove:selection) ifTrue:[
+    |bmost delta sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                bmost := (selection at:1) computeCorner y.
+                bmost := (sel first) computeCorner y.
 
                 self transaction:#alignBottom selectionDo:[:v|
                     (delta := bmost - (v computeCorner y)) ~~ 0 ifTrue:[
@@ -1781,9 +1837,9 @@
     "align selection to the center/horizontal of the first object in the selection; in case
      of one selection the object is aligned to the center/horizontal of its superview
     "
-    |view center|
-
-    (self canMove:selection) ifTrue:[
+    |view center sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             view := self singleSelection.
 
@@ -1791,7 +1847,7 @@
                 view   := view superView.
                 center := view computeExtent
             ] ifFalse:[
-                view   := selection at:1.
+                view   := sel first.
                 center := view computeCorner + view computeOrigin.
             ].
             center := center x // 2.
@@ -1817,9 +1873,9 @@
     "align selection to the center/vertical of the first object in the selection; in case
      of one selection the object is aligned to the center/vertical of its superview
     "
-    |view center|
-
-    (self canMove:selection) ifTrue:[
+    |view center sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             view := self singleSelection.
 
@@ -1827,7 +1883,7 @@
                 view   := view superView.
                 center := view computeExtent
             ] ifFalse:[
-                view   := selection at:1.
+                view   := sel first.
                 center := view computeCorner + view computeOrigin.
             ].
             center := center y // 2.
@@ -1850,12 +1906,12 @@
     "align selection to the left of the first object in the selection; in case
      of one selection the object is aligned to the left of its superview
     "
-    |lmost delta|
-
-    (self canMove:selection) ifTrue:[
+    |lmost delta sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                lmost := (selection at:1) computeOrigin x.
+                lmost := (sel first) computeOrigin x.
 
                 self transaction:#alignLeft selectionDo:[:v|
                     (delta := lmost - (v computeOrigin x)) ~~ 0 ifTrue:[
@@ -1877,13 +1933,13 @@
     "align selection to the left/right of the first object in the selection; in case
      of one selection the object is aligned to the left/right of its superview
     "
-    |lmost rmost|
-
-    (self canMove:selection) ifTrue:[
+    |lmost rmost sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                lmost := (selection at:1) computeOrigin x.
-                rmost := (selection at:1) computeCorner x.
+                lmost := (sel first) computeOrigin x.
+                rmost := (sel first) computeCorner x.
 
                 self transaction:#alignLeftRight selectionDo:[:aView|
                     |layout|
@@ -1918,12 +1974,12 @@
     "align selection to the right of the first object in the selection; in case
      of one selection the object is aligned to the right of its superview
     "
-    |rmost delta|
-
-    (self canMove:selection) ifTrue:[
+    |rmost delta sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                rmost := (selection at:1) computeCorner x.
+                rmost := (sel first) computeCorner x.
 
                 self transaction:#alignRight selectionDo:[:v|
                     (delta := rmost - (v computeCorner x)) ~~ 0 ifTrue:[
@@ -1945,12 +2001,12 @@
     "align selection to the top of the first object in the selection; in case
      of one selection the object is aligned to the top of its superview
     "
-    |tmost delta|
-
-    (self canMove:selection) ifTrue:[
+    |tmost delta sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                tmost := (selection at:1) computeOrigin y.
+                tmost := (sel first) computeOrigin y.
 
                 self transaction:#alignTop selectionDo:[:v|
                     (delta := tmost - (v computeOrigin y)) ~~ 0 ifTrue:[
@@ -1973,13 +2029,13 @@
     "align selection to the top/bottom of the first object in the selection; in case
      of one selection the object is aligned to the top/bottom of its superview
     "
-    |tmost bmost|
-
-    (self canMove:selection) ifTrue:[
+    |tmost bmost sel|
+
+    (sel := self moveableSelection) notNil ifTrue:[
         self withSelectionHiddenDo:[
             self numberOfSelections > 1 ifTrue:[
-                tmost := (selection at:1) computeOrigin y.
-                bmost := (selection at:1) computeCorner y.
+                tmost := (sel first) computeOrigin y.
+                bmost := (sel first) computeCorner y.
 
                 self transaction:#alignTopBottom selectionDo:[:aView|
                     |layout|
@@ -2015,7 +2071,7 @@
     "
     |superview min max delta val|
 
-    (self canMove:selection) ifFalse:[
+    (self moveableSelection) isNil ifTrue:[
         ^ self
     ].
 
@@ -2098,9 +2154,11 @@
 spreadSelectionHor
     "spread multiple selection horizontal
     "
-    |sumWidths min max viewsInOrder topsInOrder count space|
-
-    (self numberOfSelections > 1 and:[self canMove:selection]) ifFalse:[
+    |sumWidths min max viewsInOrder topsInOrder count space sel|
+
+    sel := self moveableSelection.
+
+    (sel notNil and:[self numberOfSelections > 1]) ifFalse:[
         ^ self
     ].
 
@@ -2118,7 +2176,7 @@
             max := max max:(aView right).
             count := count + 1
         ].
-        viewsInOrder := Array withAll:selection.
+        viewsInOrder := Array withAll:sel.
         topsInOrder  := viewsInOrder collect:[:aView | aView left].
         topsInOrder sortWith:viewsInOrder.
 
@@ -2139,9 +2197,11 @@
 spreadSelectionVer
     "spread multiple selection vertical
     "
-    |sumHeights min max viewsInOrder topsInOrder count space|
-
-    (self numberOfSelections > 1 and:[self canMove:selection]) ifFalse:[
+    |sumHeights min max viewsInOrder topsInOrder count space sel|
+
+    sel := self moveableSelection.
+
+    (sel notNil and:[self numberOfSelections > 1]) ifFalse:[
         ^ self
     ].
 
@@ -2159,7 +2219,7 @@
             max   := max max:(aView bottom).
             count := count + 1
         ].
-        viewsInOrder := Array withAll:selection.
+        viewsInOrder := Array withAll:sel.
         topsInOrder  := viewsInOrder collect:[:aView|aView top].
         topsInOrder sortWith:viewsInOrder.