HPanelV.st
author claus
Sun, 07 Aug 1994 15:23:42 +0200
changeset 38 4b9b70b2cc87
parent 5 7b4fb1b170e5
child 59 450ce95a72a4
permissions -rw-r--r--
2.10.3 pre-final version

"
 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

$Header: /cvs/stx/stx/libwidg/Attic/HPanelV.st,v 1.4 1994-08-07 13:22:03 claus Exp $
'!

!HorizontalPanelView 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/HPanelV.st,v 1.4 1994-08-07 13:22:03 claus Exp $
"
!

documentation
"
    a View which arranges its child-views in a horizontal row.
    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 := 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 := 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 := 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 := 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
"
! !

!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 sumOfWidths numChilds l|

    subViews isNil ifTrue:[^ self].

    space := horizontalSpace.

    "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
    ].

    "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
    ] ifFalse: [
        (l == #right) ifTrue:[
            xpos := width - (space * numChilds) - sumOfWidths.
"
            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.
"
                    borderWidth == 0 ifTrue:[
                        xpos := 0 
                    ].
"
                ]
            ]
        ]
    ].

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