HorizontalPanelView.st
author claus
Wed, 13 Oct 1993 03:49:56 +0100
changeset 5 7b4fb1b170e5
parent 3 9d7eefb5e69f
child 38 4b9b70b2cc87
permissions -rw-r--r--
(none)

"
 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:#HorizontalPanelView
       instanceVariableNames:''
       classVariableNames:''
       poolDictionaries:''
       category:'Views-Layout'
!

HorizontalPanelView comment:'

COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

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

$Header: /cvs/stx/stx/libwidg/HorizontalPanelView.st,v 1.3 1993-10-13 02:47:53 claus Exp $

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
    ]
! !