HPanelV.st
changeset 59 450ce95a72a4
parent 38 4b9b70b2cc87
child 62 7cc1e330da47
--- a/HPanelV.st	Tue Aug 30 00:54:47 1994 +0200
+++ b/HPanelV.st	Mon Oct 10 04:03:47 1994 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -19,9 +19,9 @@
 
 HorizontalPanelView comment:'
 COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libwidg/Attic/HPanelV.st,v 1.4 1994-08-07 13:22:03 claus Exp $
+$Header: /cvs/stx/stx/libwidg/Attic/HPanelV.st,v 1.5 1994-10-10 03:01:39 claus Exp $
 '!
 
 !HorizontalPanelView class methodsFor:'documentation'!
@@ -29,7 +29,7 @@
 copyright
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libwidg/Attic/HPanelV.st,v 1.4 1994-08-07 13:22:03 claus Exp $
+$Header: /cvs/stx/stx/libwidg/Attic/HPanelV.st,v 1.5 1994-10-10 03:01:39 claus Exp $
 "
 !
 
@@ -54,61 +54,61 @@
 
     example: default layout (centered)
 
-        |v p b1 b2 b3|
+	|v p b1 b2 b3|
 
-        v := StandardSystemView new.
-        p := HorizontalPanelView in:v.
-        p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
-        b1 := Button label:'button1' in:p.
-        b2 := Button label:'button2' in:p.
-        b3 := Button label:'button3' in:p.
-        v extent:300 @ 100.
-        v open
+	v := StandardSystemView new.
+	p := HorizontalPanelView in:v.
+	p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
+	b1 := Button label:'button1' in:p.
+	b2 := Button label:'button2' in:p.
+	b3 := Button label:'button3' in:p.
+	v extent:300 @ 100.
+	v open
 
 
     example: left-layout
 
-        |v p b1 b2 b3|
+	|v p b1 b2 b3|
 
-        v := StandardSystemView new.
-        p := HorizontalPanelView in:v.
-        p layout:#left.
-        p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
-        b1 := Button label:'button1' in:p.
-        b2 := Button label:'button2' in:p.
-        b3 := Button label:'button3' in:p.
-        v extent:300 @ 100.
-        v open
+	v := StandardSystemView new.
+	p := HorizontalPanelView in:v.
+	p layout:#left.
+	p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
+	b1 := Button label:'button1' in:p.
+	b2 := Button label:'button2' in:p.
+	b3 := Button label:'button3' in:p.
+	v extent:300 @ 100.
+	v open
 
 
     example: right-layout
 
-        |v p b1 b2 b3|
+	|v p b1 b2 b3|
 
-        v := StandardSystemView new.
-        p := HorizontalPanelView in:v.
-        p layout:#right.
-        p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
-        b1 := Button label:'button1' in:p.
-        b2 := Button label:'button2' in:p.
-        b3 := Button label:'button3' in:p.
-        v extent:300 @ 100.
-        v open
+	v := StandardSystemView new.
+	p := HorizontalPanelView in:v.
+	p layout:#right.
+	p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
+	b1 := Button label:'button1' in:p.
+	b2 := Button label:'button2' in:p.
+	b3 := Button label:'button3' in:p.
+	v extent:300 @ 100.
+	v open
 
 
     example: spread-layout
 
-        |v p b1 b2 b3|
+	|v p b1 b2 b3|
 
-        v := StandardSystemView new.
-        p := HorizontalPanelView in:v.
-        p layout:#spread.
-        p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
-        b1 := Button label:'button1' in:p.
-        b2 := Button label:'button2' in:p.
-        b3 := Button label:'button3' in:p.
-        v extent:300 @ 100.
-        v open
+	v := StandardSystemView new.
+	p := HorizontalPanelView in:v.
+	p layout:#spread.
+	p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0).
+	b1 := Button label:'button1' in:p.
+	b2 := Button label:'button2' in:p.
+	b3 := Button label:'button3' in:p.
+	v extent:300 @ 100.
+	v open
 "
 ! !
 
@@ -117,7 +117,7 @@
 preferedExtent
     "return a good extent, one that makes subviews fit"
 
-    |sumOfWidths maxHeight|
+    |sumOfWidths maxHeight maxWidth|
 
     subViews isNil ifTrue:[^ horizontalSpace @ verticalSpace].
 
@@ -125,16 +125,25 @@
 
     sumOfWidths := 0.
     maxHeight := 0.
+    maxWidth := 0.
 
     subViews do:[:child |
-        sumOfWidths := sumOfWidths + child widthIncludingBorder.
-        maxHeight := maxHeight max:(child heightIncludingBorder)
+	sumOfWidths := sumOfWidths + child widthIncludingBorder.
+	maxHeight := maxHeight max:(child heightIncludingBorder).
+	maxWidth := maxWidth max:(child widthIncludingBorder).
     ].
     borderWidth ~~ 0 ifTrue:[
-        sumOfWidths := sumOfWidths + (horizontalSpace * 2).
-        maxHeight := maxHeight + (verticalSpace * 2).
+	sumOfWidths := sumOfWidths + (horizontalSpace * 2).
+	maxHeight := maxHeight + (verticalSpace * 2).
     ].
-    sumOfWidths := sumOfWidths + ((subViews size - 1) * horizontalSpace).
+    layout == #fit ifTrue:[
+	sumOfWidths := maxWidth * subViews size.
+	borderWidth ~~ 0 ifTrue:[
+	    sumOfWidths := sumOfWidths + (verticalSpace * 2).
+	]
+    ] ifFalse:[
+	sumOfWidths := sumOfWidths + ((subViews size - 1) * horizontalSpace).
+    ].
 
     ^ sumOfWidths @ maxHeight
 ! !
@@ -145,77 +154,97 @@
     "(re)compute position of every child whenever childs are added or
      my size has changed"
 
-    |xpos ypos space sumOfWidths numChilds l|
+    |xpos ypos space sumOfWidths numChilds l wEach|
 
     subViews isNil ifTrue:[^ self].
 
     space := horizontalSpace.
+    numChilds := subViews size.
+
+    layout == #fit ifTrue:[
+	"
+	 adjust childs extents and set origins.
+	 Be careful to avoid accumulation of rounding errors
+	"
+	wEach := (width - (margin * 2) - (numChilds + 1 * space) + borderWidth) / numChilds.
+	xpos := space + margin - borderWidth.
+	subViews do:[:child |
+	    ypos := (height - child heightIncludingBorder) // 2.
+	    (ypos < 0) ifTrue:[ypos := 0].
+
+	    child origin:(xpos rounded @ ypos)
+		  corner:(xpos + wEach - (child borderWidth)) rounded
+			 @ (ypos + child height).
+	    xpos := xpos + wEach + space
+	].
+	^ self
+    ].
 
     "compute net width needed"
 
     sumOfWidths := subViews inject:0 into:[:sumSoFar :child | sumSoFar + child widthIncludingBorder].
-    numChilds := subViews size.
 
     l := layout.
     ((l == #center) and:[numChilds == 1]) ifTrue:[
-        l := #spread
+	l := #spread
     ].
 
-    "compute position of leftmost subview and space between them;
-     if they do hardly fit, leave no space between them "
-
+    "
+     compute position of leftmost subview and space between them;
+     if they do hardly fit, leave no space between them 
+    "
     (sumOfWidths >= (width - (margin * 2))) ifTrue:[
-        xpos := 0.
-        space := 0
+	xpos := 0.
+	space := 0
     ] ifFalse: [
-        (l == #right) ifTrue:[
-            xpos := width - (space * numChilds) - sumOfWidths.
+	(l == #right) ifTrue:[
+	    xpos := width - (space * numChilds) - sumOfWidths.
 "
-            borderWidth == 0 ifTrue:[
-                xpos := xpos + space 
-            ].
+	    borderWidth == 0 ifTrue:[
+		xpos := xpos + space 
+	    ].
 "
-            xpos < 0 ifTrue:[
-                space := space min:(width - sumOfWidths) // (numChilds + 1).
-                xpos := width - (space * numChilds) - sumOfWidths.
-            ]
-        ] ifFalse:[
-            (l == #spread) ifTrue:[
-                space := (width - sumOfWidths) // (numChilds + 1).
-                xpos := space.
-                (space == 0) ifTrue:[
-                    xpos := (width - sumOfWidths) // 2
-                ]
-            ] ifFalse:[
-                (l == #center) ifTrue:[
-                    xpos := (width - (sumOfWidths
-                                      + ((numChilds - 1) * space))) // 2.
-                    xpos < 0 ifTrue:[
-                        space := (width - sumOfWidths) // (numChilds + 1).
-                        xpos := (width - (sumOfWidths
-                                       + ((numChilds - 1) * space))) // 2.
-                    ]
-                ] ifFalse:[
-                    "left"
-                    space := space min:(width - sumOfWidths) // (numChilds + 1).
-                    xpos := space.
+	    xpos < 0 ifTrue:[
+		space := space min:(width - sumOfWidths) // (numChilds + 1).
+		xpos := width - (space * numChilds) - sumOfWidths.
+	    ]
+	] ifFalse:[
+	    (l == #spread) ifTrue:[
+		space := (width - sumOfWidths) // (numChilds + 1).
+		xpos := space.
+		(space == 0) ifTrue:[
+		    xpos := (width - sumOfWidths) // 2
+		]
+	    ] ifFalse:[
+		(l == #center) ifTrue:[
+		    xpos := (width - (sumOfWidths
+				      + ((numChilds - 1) * space))) // 2.
+		    xpos < 0 ifTrue:[
+			space := (width - sumOfWidths) // (numChilds + 1).
+			xpos := (width - (sumOfWidths
+				       + ((numChilds - 1) * space))) // 2.
+		    ]
+		] ifFalse:[
+		    "left"
+		    space := space min:(width - sumOfWidths) // (numChilds + 1).
+		    xpos := space.
 "
-                    borderWidth == 0 ifTrue:[
-                        xpos := 0 
-                    ].
+		    borderWidth == 0 ifTrue:[
+			xpos := 0 
+		    ].
 "
-                ]
-            ]
-        ]
+		]
+	    ]
+	]
     ].
 
     "now set positions"
 
     subViews do:[:child |
-        ypos := (height - child heightIncludingBorder) // 2.
-        (ypos < 0) ifTrue:[ypos := 0].
+	ypos := (height - child heightIncludingBorder) // 2.
+	(ypos < 0) ifTrue:[ypos := 0].
 
-        child origin:(xpos @ ypos).
-        xpos := xpos + (child widthIncludingBorder) + space
+	child origin:(xpos @ ypos).
+	xpos := xpos + (child widthIncludingBorder) + space
     ]
 ! !