HorizontalScrollBar.st
author claus
Sat, 11 Dec 1993 02:51:34 +0100
changeset 7 15a9291b9bd0
parent 5 7b4fb1b170e5
child 21 9ef599238fea
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.
"

ScrollBar subclass:#HorizontalScrollBar
       instanceVariableNames:''
       classVariableNames:'defaultScrollRightForm
                           defaultScrollLeftForm'
       poolDictionaries:''
       category:'Views-Interactors'
!

HorizontalScrollBar comment:'

COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

this class implements horizontal scrollbars with scroller and
2 step-scroll buttons. when moved or stepped, it perform a
predefined action.

$Header: /cvs/stx/stx/libwidg/HorizontalScrollBar.st,v 1.4 1993-12-11 01:44:42 claus Exp $

written spring/summer 89 by claus
'!

!HorizontalScrollBar class methodsFor:'defaults'!

scrollLeftButtonForm:style
    defaultScrollLeftForm isNil ifTrue:[
        defaultScrollLeftForm := Form fromFile:(self classResources at:'SCROLL_LEFT_BUTTON_FORM_FILE'
                                                     default:(style == #mswindows
                                                                 ifTrue:['ScrollLt_win.xbm']
                                                                 ifFalse:['ScrollLt.xbm']))
                                    resolution:100
    ].
    defaultScrollLeftForm isNil ifTrue:[
        defaultScrollLeftForm :=
            Form width:16 height:16 fromArray:#(2r00000000 2r00000000
                                                2r00000001 2r10000000
                                                2r00000010 2r10000000
                                                2r00000100 2r10000000
                                                2r00001000 2r11111110
                                                2r00010000 2r00000010
                                                2r00100000 2r00000010
                                                2r01000000 2r00000010
                                                2r01000000 2r00000010
                                                2r00100000 2r00000010
                                                2r00010000 2r00000010
                                                2r00001000 2r11111110
                                                2r00000100 2r10000000
                                                2r00000010 2r10000000
                                                2r00000001 2r10000000
                                                2r00000000 2r00000000)
    ].
    ^ defaultScrollLeftForm
!

scrollRightButtonForm:style
    defaultScrollRightForm isNil ifTrue:[
        defaultScrollRightForm := Form fromFile:(self classResources at:'SCROLL_RIGHT_BUTTON_FORM_FILE'
                                                      default:(style == #mswindows
                                                                 ifTrue:['ScrollRt_win.xbm']
                                                                 ifFalse:['ScrollRt.xbm']))
                                     resolution:100
    ].
    defaultScrollRightForm isNil ifTrue:[
        defaultScrollRightForm :=
            Form width:16 height:16 fromArray:#(2r00000000 2r00000000
                                                2r00000001 2r10000000
                                                2r00000001 2r01000000
                                                2r00000001 2r00100000
                                                2r01111111 2r00010000
                                                2r01000000 2r00001000
                                                2r01000000 2r00000100
                                                2r01000000 2r00000010
                                                2r01000000 2r00000010
                                                2r01000000 2r00000100
                                                2r01000000 2r00001000
                                                2r01111111 2r00010000
                                                2r00000001 2r00100000
                                                2r00000001 2r01000000
                                                2r00000001 2r10000000
                                                2r00000000 2r00000000)
    ].
    ^ defaultScrollRightForm
! !

!HorizontalScrollBar methodsFor:'initialization'!

initialize
    |bwn sep h w leftForm rightForm c|

    super initialize.

    "compute my extent from sub-components"
    leftForm  := self class scrollLeftButtonForm:style.
    rightForm := self class scrollRightButtonForm:style.
    w := leftForm width + rightForm width 
         + (1 "self defaultBorderWidth" * 2) 
         + (HorizontalScroller defaultExtent x).
    h := (leftForm height) max:(rightForm height).
    self is3D ifTrue:[
        h := h + 4.
        w := w + 4
    ].
    self extent:w @ h.

    bwn := borderWidth negated + margin.
    self is3D ifTrue:[
        sep := 1
    ] ifFalse:[
        sep := 0
    ].

    "poor design - destroy and re-create thumgs"
    button1 destroy.
    button2 destroy.
    thumb destroy.

    button1 := ArrowButton leftIn:self.
    button1 name:'LeftButton'.
    button1 borderWidth:borderWidth.
    button1 autoRepeat.

    thumb := HorizontalScroller in:self.
    style ~~ #next ifTrue:[
        thumb borderWidth:borderWidth.
    ].

    button2 := ArrowButton rightIn:self.
    button2 name:'RightButton'.
    button2 borderWidth:borderWidth.
    button2 autoRepeat.

    ((style == #iris) and:[Display hasGreyscales])ifTrue:[
        "have to change some of Buttons defaults"
        c := (Color grey:25) on:device.
        button1 offLevel:2.
        button2 offLevel:2.
        button1 foregroundColor:c.
        button1 activeForegroundColor:c.
        button1 enteredForegroundColor:c.
        button2 foregroundColor:c.
        button2 activeForegroundColor:c.
        button2 enteredForegroundColor:c.
    ].

    (layout == #bottom) ifTrue:[
        "buttons at left"
        button1 origin:(bwn @ bwn).
        button1 viewGravity:#West.
        button2 origin:(button1 width @ bwn).
        button2 viewGravity:#West.
        thumb origin:((button1 width + borderWidth + button2 width + sep + sep) @ bwn).
        thumb viewGravity:#West
    ] ifFalse:[
        (layout == #top) ifTrue:[
            "buttons at right"
            button1 viewGravity:#West.
            button2 viewGravity:#West.
            thumb origin:(bwn @ bwn).
            thumb viewGravity:#West
        ] ifFalse:[
            button1 origin:(bwn @ bwn).
            button1 viewGravity:#West.
            button2 viewGravity:#West.
            thumb origin:((button1 width + sep) @ bwn).
            thumb viewGravity:#West
        ]
    ]
! !

!HorizontalScrollBar methodsFor:'accessing'!

scrollLeftAction:aBlock
    button1 action:aBlock
!

scrollRightAction:aBlock
    button2 action:aBlock
! !

!HorizontalScrollBar methodsFor:'events'!

sizeChanged:how
    |leftWidth rightWidth thumbWidth leftAndRightWidth bwn sep sep2 
     thumbHeight h|

    button1 isNil ifTrue:[^ self].
    button2 isNil ifTrue:[^ self].
    thumb isNil ifTrue:[^ self].

    leftWidth := button1 width + borderWidth.
    rightWidth := button2 width + borderWidth.
    leftAndRightWidth := leftWidth + rightWidth.
    bwn := borderWidth negated + margin.
    self is3D ifTrue:[
        sep := 1
    ] ifFalse:[
        sep := 0
    ].

    thumbWidth := width - leftAndRightWidth - borderWidth - (sep * 3).
"
    ((layout ~~ #top) and:[layout ~~ #bottom]) ifTrue:[
        thumbWidth := thumbWidth - borderWidth
    ].
"
    layout == #around ifTrue:[
        thumbWidth := thumbWidth + borderWidth
    ].

    "if I become too small, hide buttons"

    (width < leftAndRightWidth) ifTrue:[
        button1 shown ifTrue:[
            button1 hidden.
            button2 hidden.
            thumb hidden
        ]
    ] ifFalse:[
        shown ifTrue:[
            button1 shown ifFalse:[
                button1 show.
                button2 show.
                thumb show
            ]
        ]
    ].

    (thumbWidth < 10) ifTrue:[
        thumb shown ifTrue:[
            thumb hidden
        ]
    ] ifFalse:[
        thumb shown ifFalse:[
            button1 shown ifTrue:[
                thumb show
            ]
        ]
    ].

    "height of buttons is always my width"

    h := height - (margin * 2).

    (h ~~ button1 height) ifTrue:[
        button1 height:height.
        button2 height:height
    ].

    thumbHeight := h.
    style == #next ifTrue:[
        thumbHeight := thumbHeight - (thumb borderWidth * 2).
        thumbWidth := thumbWidth - 1
    ].


    (layout == #bottom) ifTrue:[
        "buttons at left"
        thumb extent:(thumbWidth @ thumbHeight).
        ^ self
    ].

    sep2 := sep * 2.
    (layout == #top) ifTrue:[
        "buttons at right"
        (how == #smaller) ifTrue:[
            thumb extent:(thumbWidth @ thumbHeight).
            button1 origin:((thumbWidth + sep2) @ bwn).
            button2 origin:((thumbWidth + sep2 + leftWidth) @ bwn)
        ] ifFalse:[
            button1 origin:((thumbWidth + sep2) @ bwn).
            button2 origin:((thumbWidth + sep2 + leftWidth) @ bwn).
            thumb extent:(thumbWidth @ thumbHeight)
        ].
        ^ self
    ].
    "button around thumb"

    button1 origin:(bwn @ bwn).
    button2 origin:((leftWidth + thumbWidth + sep2) @ bwn).
    thumb extent:((thumbWidth + margin) @ thumbHeight).
    thumb origin:((leftWidth - borderWidth + sep) @ bwn)
! !