VPanelV.st
author claus
Mon, 10 Oct 1994 04:03:47 +0100
changeset 59 450ce95a72a4
parent 38 4b9b70b2cc87
child 63 f4eaf04d1eaf
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      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
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

PanelView subclass:#VerticalPanelView
       instanceVariableNames:''
       classVariableNames:''
       poolDictionaries:''
       category:'Views-Layout'
!

VerticalPanelView comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libwidg/Attic/VPanelV.st,v 1.5 1994-10-10 03:03:18 claus Exp $
'!

!VerticalPanelView class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      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
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libwidg/Attic/VPanelV.st,v 1.5 1994-10-10 03:03:18 claus Exp $
"
!

documentation
"
    a View which arranges its child-views in a vertical column.
    All real work is done in PanelView - only the layout computation is
    redefined here.

    example: default layout (centered)

	|v p b1 b2 b3|

	v := StandardSystemView new.
	p := VerticalPanelView 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:100 @ 300.
	v open


    example: left-layout

	|v p b1 b2 b3|

	v := StandardSystemView new.
	p := VerticalPanelView in:v.
	p layout:#top.
	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:100 @ 300.
	v open


    example: right-layout

	|v p b1 b2 b3|

	v := StandardSystemView new.
	p := VerticalPanelView in:v.
	p layout:#bottom.
	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:100 @ 300.
	v open


    example: spread-layout

	|v p b1 b2 b3|

	v := StandardSystemView new.
	p := VerticalPanelView 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:100 @ 300.
	v open
"
! !

!VerticalPanelView methodsFor:'queries'!

preferedExtent
    "return a good extent, one that makes subviews fit"

    |sumOfHeights maxWidth maxHeight|

    subViews isNil ifTrue:[^ horizontalSpace @ verticalSpace].

    "compute net height needed"

    sumOfHeights := 0.
    maxWidth := 0.

    subViews do:[:child |
	sumOfHeights := sumOfHeights + child heightIncludingBorder.
	maxWidth := maxWidth max:(child widthIncludingBorder).
	maxHeight := maxHeight max:(child heightIncludingBorder).
    ].
    borderWidth ~~ 0 ifTrue:[
	sumOfHeights := sumOfHeights + (horizontalSpace * 2).
	maxWidth := maxWidth + (horizontalSpace * 2).
    ].
    sumOfHeights := sumOfHeights + ((subViews size - 1) * verticalSpace).

    ^ maxWidth @ sumOfHeights
! !

!VerticalPanelView methodsFor:'layout'!

setChildPositions
    "(re)compute position of every child"

    |xpos ypos space sumOfHeights numChilds l hEach|

    subViews isNil ifTrue:[^ self].

    space := verticalSpace.

    numChilds := subViews size.
    layout == #fit ifTrue:[
	"
	 adjust childs extents and set origins.
	 Be careful to avoid accumulation of rounding errors
	"
	hEach := (height - (margin * 2) - (numChilds + 1 * space) + borderWidth) / numChilds.
	ypos := space + margin - borderWidth.
	subViews do:[:child |
	    xpos := (width - child widthIncludingBorder) // 2.
	    (xpos < 0) ifTrue:[xpos := 0].

	    child origin:(xpos @ ypos rounded)
		  corner:(xpos + (child width))
			 @ (ypos + hEach - (child borderWidth)) rounded.
	    ypos := ypos + hEach + "(child borderWidth * 2) +" space
	].
	^ self
    ].

    "compute net height needed"

    sumOfHeights := subViews inject:0 into:[:sumSoFar :child | sumSoFar + child heightIncludingBorder].

    l := layout.
    ((l == #center) and:[numChilds == 1]) ifTrue:[
	l := #spread
    ].

    "compute position of topmost subview and space between them;
     if they do hardly fit, leave no space between them "

    (sumOfHeights >= (height - (margin * 2))) ifTrue:[
	ypos := 0.
	space := 0
    ] ifFalse:[
	(l == #bottom) ifTrue:[
	    ypos := height - (space * numChilds) - sumOfHeights.
"
	    borderWidth == 0 ifTrue:[
		ypos := ypos + space 
	    ].
"
	    ypos < 0 ifTrue:[
		space := space min:(height - sumOfHeights) // (numChilds + 1).
		ypos := height - (space * numChilds) - sumOfHeights.
	    ]
	] ifFalse: [
	    (l == #spread) ifTrue:[
		space := (height - sumOfHeights) // (numChilds + 1).
		ypos := space.
		(space == 0) ifTrue:[
		    ypos := (height - sumOfHeights) // 2
		]
	    ] ifFalse: [
		(l == #center) ifTrue:[
		    ypos := (height - (sumOfHeights
				       + ((numChilds - 1) * space))) // 2.
		    ypos < 0 ifTrue:[
			space := (height - sumOfHeights) // (numChilds + 1).
			ypos := (height - (sumOfHeights
				       + ((numChilds - 1) * space))) // 2.
		    ]
		] ifFalse:[
"
		    borderWidth == 0 ifTrue:[
			ypos := 0
		    ] ifFalse:[
			ypos := verticalSpace
		    ].
"
		    space := space min:(height - sumOfHeights) // (numChilds + 1).
		    ypos := space.
		]
	    ]
	]
    ].


    "now set positions"

    subViews do:[:childView |
	xpos := (width - childView widthIncludingBorder) // 2.
	(xpos < 0) ifTrue:[ xpos := 0 ].

	childView origin:(xpos@ypos).
	ypos := ypos + (childView heightIncludingBorder) + space
    ]
! !