HorizontalPanelView.st
author claus
Fri, 16 Jul 1993 11:44:44 +0200
changeset 0 e6a541c1c0eb
child 3 9d7eefb5e69f
permissions -rw-r--r--
Initial revision

"
 COPYRIGHT (c) 1989-92 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:#HorizontalPanelView
       instanceVariableNames:''
       classVariableNames:''
       poolDictionaries:''
       category:'Views-Layout'
!

HorizontalPanelView comment:'

COPYRIGHT (c) 1989-92 by Claus Gittinger
              All Rights Reserved

a View for childViews oriented horizontal
all real work is done in PanelView - just redefine layout

%W% %E%

written spring/summer 89 by claus
'!

!HorizontalPanelView methodsFor:'queries'!

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

    |sumOfWidths maxHeight|

    subViews isNil ifTrue:[^ horizontalSpace @ verticalSpace].

    "compute net height needed"

    sumOfWidths := 0.
    maxHeight := 0.

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

    ^ sumOfWidths @ maxHeight
! !

!HorizontalPanelView methodsFor:'layout'!

setChildPositions
    "(re)compute position of every child whenever childs are added or
     my size has changed"

    |xpos ypos space sumOfChildWidths numChilds l|

    subViews isNil ifTrue:[^ self].

    space := horizontalSpace.

    "compute net width needed"

    sumOfChildWidths := 0.
    numChilds := subViews size.
    subViews do:[:child |
        sumOfChildWidths := sumOfChildWidths + child widthIncludingBorder.
    ].

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

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

    (sumOfChildWidths >= width) ifTrue:[
        xpos := 0.
        space := 0
    ] ifFalse: [
        (l == #right) ifTrue:[
            xpos := width - (horizontalSpace * numChilds)
                          - sumOfChildWidths.
            borderWidth == 0 ifTrue:[
                xpos := xpos + horizontalSpace 
            ].
        ] ifFalse:[
            (l == #spread) ifTrue:[
                space := (width - sumOfChildWidths) // (numChilds + 1).
                xpos := space.
                (space == 0) ifTrue:[
                    xpos := (width - sumOfChildWidths) // 2
                ]
            ] ifFalse:[
                (l == #center) ifTrue:[
                    xpos := (width - (sumOfChildWidths
                                      + ((numChilds - 1) * space))) // 2
                ] ifFalse:[
                    borderWidth == 0 ifTrue:[
                        xpos := 0 
                    ] ifFalse:[
                        xpos := horizontalSpace
                    ]
                ]
            ]
        ]
    ].

    "now set positions"

    subViews do:[:child |
        ypos := (height - child heightIncludingBorder) // 2.
        (ypos < 0) ifTrue:[ypos := 0].

        child origin:(xpos @ ypos).
        xpos := xpos + (child widthIncludingBorder) + space
    ]
! !