UIPainterView.st
changeset 2392 59128c4b9cae
parent 2390 07303d4b4e78
child 2395 9f9cf71a311c
--- a/UIPainterView.st	Fri Aug 08 10:16:46 2008 +0200
+++ b/UIPainterView.st	Fri Aug 08 10:53:11 2008 +0200
@@ -1774,63 +1774,73 @@
 !
 
 groupSelectionWithLayout: withLayout
-    | spec rect views box layout specs canvas view oldSelection cS nViews menu item xOffset yOffset |
+    |minViews specs spec menu newView target keepLyt keepPos rectangle|
+
+    self canGroup ifFalse:[^ self ].
+
+    keepLyt := withLayout.
+    keepPos := true.
 
     menu := Menu new.
     menu receiver: self.
-    item := MenuItem label:'Box' value:[spec := ViewSpec new].
-    menu addItem: item.
-    item := MenuItem label:'TBox' value:[spec := TBoxSpec new].
-    menu addItem: item.
-    item := MenuItem label:'FramedBox' value:[spec := FramedBoxSpec new].
-    menu addItem: item.
-    item := MenuItem label:'HorizontalPanel' value:[spec := HorizontalPanelViewSpec new].
-    menu addItem: item.
-    item := MenuItem label:'VerticalPanel' value:[spec := VerticalPanelViewSpec new].
-    menu addItem: item.
-    (MenuPanel menu: menu) startUp.
-    canvas := self painter.
-    cS := canvas getSelectedViewsAndSpecs.
-    cS isNil ifTrue:[^self].
-    views := cS first.
-    specs := cS last.
-    rect := views first frame.
-    2 to: views size do:[:i| rect := rect merge: (views at: i) frame].
-    layout := LayoutFrame leftFraction:0.0 offset: rect origin x
-			 rightFraction:0.0 offset: rect corner x + 1
-			   topFraction:0.0 offset: rect origin y
-			bottomFraction:0.0 offset:rect corner y + 1.
-    oldSelection := canvas selection.
-    canvas select: views first superView.
-    spec layout: layout.
-    spec class == VerticalPanelViewSpec ifTrue:[
-	spec verticalLayout: #topSpace.
-	spec horizontalLayout: #fit.
+    menu addItem:(MenuItem label:'Box' value:[spec := ViewSpec new]).
+    menu addItem:(MenuItem label:'TBox' value:[spec := TBoxSpec new]).
+    menu addItem:(MenuItem label:'FramedBox' value:[spec := FramedBoxSpec new]).
+
+    menu addItem:(MenuItem label:'HorizontalPanel'
+                    value:[
+                        keepLyt := keepPos := false.
+                        spec := HorizontalPanelViewSpec new.
+                        spec verticalLayout: #fit.
+                        spec horizontalLayout: #leftSpace.
+                    ]).
+    menu addItem:(MenuItem label:'VerticalPanel'
+                    value:[
+                        keepLyt := keepPos := false.
+                        spec := VerticalPanelViewSpec new.
+                        spec verticalLayout: #topSpace.
+                        spec horizontalLayout: #fit.
+                    ]).
+
+    menu startUp.
+    spec isNil ifTrue:[^ self].
+
+    minViews := self minClosedViewSetFor:(self selection).
+    minViews size > 1 ifFalse:[^ self].
+
+    specs     := OrderedCollection new.
+    rectangle := minViews first frame copy.
+
+    minViews do:[:eachView|
+        specs add:(self fullSpecWithAbsolutePositionFor:eachView).
+        rectangle := rectangle quickMerge:(eachView frame).
     ].
-    spec class == HorizontalPanelViewSpec ifTrue:[
-	spec verticalLayout: #fit.
-	spec horizontalLayout: #leftSpace.
+    spec layout:rectangle.
+
+    target := self findContainerOfView:(minViews first).
+
+    self withinTransaction:#group objects:(Array with:target) do:[ |widgets|
+        self deleteSelectionBuffered:false.
+
+        spec otherAttributeAt:#uiPainterAttributes put:(Dictionary new
+                at:#origin put:(rectangle origin);
+                at:#extent put:(rectangle extent);
+                at:#absOrigin put:(rectangle origin);
+                yourself).
+
+        newView := self pasteSpecifications:(Array with:spec) 
+                    into:target
+                    keepLayout:true
+                    keepPosition:true
+                    at:#keep.
+
+        widgets := self pasteSpecifications:specs
+                into:newView
+                keepLayout:keepLyt
+                keepPosition:keepPos
+                at:nil.
     ].
-
-    box := self pasteSpecifications:spec keepLayout:true at:nil.
-
-    xOffset := box origin x.
-    yOffset := box origin y.
-    withLayout ifFalse:[
-	1 to: specs size do:[:i|
-	    view := views at: i.
-	    layout :=  LayoutFrame leftFraction:0.0 offset: (view origin x - xOffset)
-				  rightFraction:0.0 offset: (view corner x - xOffset + 1)
-				    topFraction:0.0 offset: (view origin y - yOffset )
-				 bottomFraction:0.0 offset: (view corner y - yOffset + 1).
-	    (specs at: i) layout: layout.
-	].
-    ].
-    canvas selection: oldSelection.
-    canvas deleteSelection.
-    canvas selection:box.
-    nViews := canvas pasteSpecifications:specs keepLayout:true.
-    canvas selection: box.
+    self select:newView.
 !
 
 groupWithLayout
@@ -1847,6 +1857,8 @@
 
     | canvas cS views specs frame view layout superView|
 
+    self canUngroup ifFalse:[^ self ].
+
     canvas := self painter.
     cS := canvas getSelectedViewsAndSpecs.
     cS isNil ifTrue:[^self].
@@ -1858,14 +1870,14 @@
     frame := cS first first frame.
     canvas deleteSelection.
     withLayout ifFalse:[
-	1 to: specs size do:[:i|
-	    view := views at: i.
-	    layout :=  LayoutFrame leftFraction:0.0 offset: (view origin x + frame origin x)
-				  rightFraction:0.0 offset: (view corner x + frame origin x + 1)
-				    topFraction:0.0 offset: (view origin y + frame origin y )
-				 bottomFraction:0.0 offset: (view corner y + frame origin y + 1).
-	    (specs at: i) layout: layout.
-	].
+        1 to: specs size do:[:i|
+            view := views at: i.
+            layout :=  LayoutFrame leftFraction:0.0 offset: (view origin x + frame origin x)
+                                  rightFraction:0.0 offset: (view corner x + frame origin x + 1)
+                                    topFraction:0.0 offset: (view origin y + frame origin y )
+                                 bottomFraction:0.0 offset: (view corner y + frame origin y + 1).
+            (specs at: i) layout: layout.
+        ].
     ].
     canvas selection: superView.
     canvas pasteSpecifications:specs keepLayout:true.
@@ -2256,6 +2268,10 @@
     self selection: sel.
 !
 
+selectedNodes
+    ^ treeView model selectedNodes
+!
+
 updateSelectionFromModel:aSelOrNil
     "update selection from a new selection
     "
@@ -2549,6 +2565,26 @@
 
 !
 
+canGroup
+    "test whether selected elements can be grouped; minimum two elements
+     must be selected and all must have the same parent"
+
+    |selectedNodes parent|
+
+    selectedNodes := self selectedNodes.
+
+    selectedNodes size < 2ifTrue:[ ^ false ].
+
+    parent := selectedNodes first parent.
+    parent isNil ifTrue:[ ^ false ].    "/ test whether not the canvas itself is selected
+
+    selectedNodes do:[:each|
+        each parent ~~ parent ifTrue:[^ false ].
+    ].
+
+    ^true
+!
+
 canKeepLayoutInSelection
     "returns true if layout can be kept during a paste operation
     "
@@ -2576,6 +2612,26 @@
      can be moved or aligned
     "
     ^ self canMove:(self selection)
+!
+
+canUngroup
+    "test whether the selected element can be ungrouped; only one
+     element is selected and has children"
+
+    "/ the #ungroupSelectionWithLayout: dosnot work yet - so disable
+
+"/    |selectedNodes node|
+"/
+"/    selectedNodes := self selectedNodes.
+"/
+"/
+"/    selectedNodes size == 1 ifTrue:[
+"/        node := selectedNodes first.
+"/        node parent isNil ifTrue:[ ^ false ].    "/ test whether not the canvas itself is selected
+"/
+"/        ^ node hasChildren
+"/    ].
+    ^ false
 ! !
 
 !UIPainterView methodsFor:'transaction'!