HVScrollableView.st
author Claus Gittinger <cg@exept.de>
Tue, 15 Oct 1996 14:05:48 +0200
changeset 845 ab868eb2c7cd
parent 818 e922d238966c
child 951 f913fa110081
permissions -rw-r--r--
examples added

"
 COPYRIGHT (c) 1991 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.
"

ScrollableView subclass:#HVScrollableView
	instanceVariableNames:'hScrollBar'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Basic'
!

!HVScrollableView class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1991 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.
"
!

documentation
"
    a view containing both horizontal and vertical scrollbars
    and some other (slave-)view.

    Please see the documentation and examples in my superclass, ScrollableView

    [author:]
        Claus Gittinger
"
!

examples
"
    See more examples in ScrollableView - all of those work
    also by replacing ScrollableView with HVScrollableView.


    example (simple scrolled text):
                                                                        [exBegin]
        |top scr txt|

        top := StandardSystemView label:'scroll example'.
        top extent:200@100.

        scr := HVScrollableView for:EditTextView in:top.
        scr origin:0.0@0.0 corner:1.0@1.0.
        txt := scr scrolledView.

        txt list:#('line1'
                   'a long line which may require scrolling.'
                   'line3'
                   'line4'
                   'line5'
                   'line7'
                   'line8'
                   'line9'
                   'line10'
                  ).
        top open
                                                                        [exEnd]
    example (horizontal miniScroller):
                                                                        [exBegin]
        |top scr txt|

        top := StandardSystemView label:'scroll example'.
        top extent:200@100.

        scr := HVScrollableView 
                    for:EditTextView 
                    miniScrollerH:true
                    miniScrollerV:false
                    in:top.
        scr origin:0.0@0.0 corner:1.0@1.0.
        txt := scr scrolledView.

        txt list:#('line1'
                   'a long line which may require scrolling.'
                   'line3'
                   'line4'
                   'line5'
                   'line7'
                   'line8'
                   'line9'
                   'line10'
                  ).
        top open
                                                                        [exEnd]

    example (vertical miniScroller):
                                                                        [exBegin]
        |top scr txt|

        top := StandardSystemView label:'scroll example'.
        top extent:200@100.

        scr := HVScrollableView 
                    for:EditTextView 
                    miniScrollerH:false
                    miniScrollerV:true
                    in:top.
        scr origin:0.0@0.0 corner:1.0@1.0.
        txt := scr scrolledView.

        txt list:#('line1'
                   'a long line which may require scrolling.'
                   'line3'
                   'line4'
                   'line5'
                   'line7'
                   'line8'
                   'line9'
                   'line10'
                  ).
        top open
                                                                        [exEnd]

    example (both scrollers are miniScroller):
                                                                        [exBegin]
        |top scr txt|

        top := StandardSystemView label:'scroll example'.
        top extent:200@100.

        scr := HVScrollableView 
                    for:EditTextView 
                    miniScrollerH:true
                    miniScrollerV:true
                    in:top.
        scr origin:0.0@0.0 corner:1.0@1.0.
        txt := scr scrolledView.

        txt list:#('line1'
                   'a long line which may require scrolling.'
                   'line3'
                   'line4'
                   'line5'
                   'line7'
                   'line8'
                   'line9'
                   'line10'
                  ).
        top open
                                                                        [exEnd]
"

! !

!HVScrollableView methodsFor:'accessing - components'!

horizontalScrollBar
    "return the horizontal scrollbar"

    ^ hScrollBar
!

scrolledView:aView
    "set the scrolled view"

    super scrolledView:aView.

    "redefine subviews size"
    styleSheet is3D ifTrue:[
        scrolledView 
            extent:[(width 
                     - scrollBar width 
                     - (innerMargin * 2))
                    @
                    (height 
                     - hScrollBar height 
                     - (innerMargin * 2))
                    ]
    ] ifFalse:[
        scrolledView
            extent:[(width
                     - scrollBar width
                     - scrollBar borderWidth
                     "- scrolledView borderWidth") 
                    @ 
                    (height
                     - hScrollBar height
                     - hScrollBar borderWidth
                     "- scrolledView borderWidth")
                   ]
    ].
    self setScrollActions.

    "Modified: 1.8.1996 / 12:44:37 / cg"
! !

!HVScrollableView methodsFor:'changes '!

update:something with:argument from:changedObject
    "whenever the scrolledview changes its contents, we have to
     update the scrollers too"

    changedObject == scrolledView ifTrue:[
	something == #sizeOfContents ifTrue:[
	    scrollBar setThumbFor:scrolledView.
	    hScrollBar setThumbFor:scrolledView.
	    ^ self
	].
	something == #originOfContents ifTrue:[
	    lockUpdates ifFalse:[
		scrollBar setThumbOriginFor:scrolledView.
		hScrollBar setThumbOriginFor:scrolledView.
	    ].
	    ^ self
	].
    ].
! !

!HVScrollableView methodsFor:'event processing'!

sizeChanged:how
    |orgY orgX scroll|

    super sizeChanged:how.
    scrolledView notNil ifTrue:[
        scrollBar setThumbFor:scrolledView.
        hScrollBar setThumbFor:scrolledView.

        orgY := scrollBar thumbOrigin.
        orgX := hScrollBar thumbOrigin.
        scroll := false.

        orgY + scrollBar thumbHeight >= 100 ifTrue:[
            scrollBar thumbOrigin:(100 - scrollBar thumbHeight).
            orgY := scrollBar thumbOrigin.
            scroll := true.
        ].
        orgX + hScrollBar thumbHeight >= 100 ifTrue:[
            hScrollBar thumbOrigin:(100 - hScrollBar thumbHeight).
            orgX := hScrollBar thumbOrigin.
            scroll := true.
        ].
        scroll ifTrue:[
            scrolledView scrollToPercent:(orgX@orgY).
        ].
    ].

    "Modified: 8.9.1995 / 12:46:36 / claus"
    "Modified: 5.8.1996 / 12:18:52 / stefan"
! !

!HVScrollableView methodsFor:'initialization'!

initializeFor:aViewClass miniScrollerH:miniH miniScrollerV:miniV
    |negativeOffset halfMargin orgX mrg halfSpacing 
     is3D cls hBorderWidth isST80 isOpenWin extra|

    isST80 := StyleSheet name = #st80.  "leftover - remove it"

    isST80 ifTrue:[
        cls := HorizontalScrollBar
    ] ifFalse:[
        cls := miniH ifTrue:[HorizontalMiniScroller] ifFalse:[HorizontalScrollBar].
    ].

    hScrollBar := cls in:self.

    super 
        initializeFor:aViewClass 
        miniScrollerH:miniH 
        miniScrollerV:miniV.

    negativeOffset := borderWidth negated.
    halfMargin := innerMargin // 2.
    is3D := styleSheet is3D.
    isOpenWin := styleSheet name = #openwin.

    "
     change vertical scrollbars size
    "
    mrg := hScrollBar borderWidth.
    mrg isNil ifTrue:[mrg := 0].
    hBorderWidth := mrg.

    is3D ifTrue:[
        isST80 ifTrue:[
            halfSpacing := 0
        ] ifFalse:[
            mrg := mrg + innerMargin + innerMargin.
            halfSpacing := ViewSpacing // 2.
        ].
    ].

    extra := 0.
    isOpenWin ifTrue:[
        extra := 2.
    ].

    scrollBar extent:[scrollBar width 
                      @ 
                     (height - hScrollBar height - mrg + extra)].

    hScrollBar thumbOrigin:0 thumbHeight:100.

    scrollBarPosition == #left ifTrue:[
        orgX := scrollBar origin x + scrollBar width.
        is3D ifTrue:[
            orgX := orgX + halfSpacing + 1.
            isST80 ifTrue:[
                orgX := orgX - (scrolledView margin)
            ]
        ]
    ] ifFalse:[
        orgX := 0 - hBorderWidth.
"/      isST80 ifTrue:[
"/            orgX := orgX + 1
"/      ]
    ].
    isOpenWin ifTrue:[
        orgX := orgX + 1
    ].

    is3D ifTrue:[
        hScrollBar origin:[(orgX + innerMargin - halfSpacing - hScrollBar margin)
                           @
                           (height - hScrollBar height - halfMargin)
                          ]
                   extent:[(width - scrollBar width - (innerMargin * 2) + extra)
                           @
                           hScrollBar height
                          ]
    ] ifFalse:[
        scrollBarPosition == #left ifTrue:[
            hScrollBar 
                origin:[(orgX + scrollBar borderWidth)
                        @
                        (height - hScrollBar height - hBorderWidth)
                       ]
                extent:[(width - scrollBar width) @ hScrollBar height]
        ] ifFalse:[
            hScrollBar 
                origin:[orgX @ (height - hScrollBar height - hBorderWidth) ]
                extent:[(width - scrollBar width - hBorderWidth) @ hScrollBar height]
        ]
    ].

    scrolledView notNil ifTrue:[
        "redefine subviews size"
        is3D ifTrue:[
            scrolledView 
                extent:[(width - scrollBar width - (innerMargin * 2))
                        @
                        (height - hScrollBar height - (innerMargin * 2)) ]
        ] ifFalse:[
            scrolledView
                extent:[(width - scrollBar width - scrollBar borderWidth) 
                        @ 
                        (height - hScrollBar height - hScrollBar borderWidth)
                       ]
        ].
        self setScrollActions.
    ]

    "Modified: 1.8.1996 / 12:44:41 / cg"
!

realize
    super realize.
    scrolledView notNil ifTrue:[
	hScrollBar setThumbFor:scrolledView
    ]
! !

!HVScrollableView methodsFor:'private'!

setScrollActions
    lockUpdates := false.

    scrollBar scrollAction:[:position |
        lockUpdates := true.
        scrolledView scrollVerticalToPercent:position.
        lockUpdates := false
    ].
    scrollBar scrollUpAction:[scrolledView scrollUp].
    scrollBar scrollDownAction:[scrolledView scrollDown].

    hScrollBar scrollAction:[:position |
        lockUpdates := true.
        scrolledView scrollHorizontalToPercent:position.
        lockUpdates := false
    ].
    hScrollBar scrollLeftAction:[scrolledView scrollLeft].
    hScrollBar scrollRightAction:[scrolledView scrollRight].

    scrolledView addDependent:self.

    "
     pass my keyboard input (and other subviews input) 
     to the scrolled view ...
    "
    self delegate:(KeyboardForwarder toView:scrolledView).

    "Modified: 1.8.1996 / 12:44:16 / cg"
! !

!HVScrollableView methodsFor:'queries'!

isHorizontalScrollable
    "return true, because I am horizontal scrollable"

    ^ true

    "Modified: 5.8.1996 / 12:29:10 / stefan"
!

preferredExtent
    "return the components preferredExtent."

    |pref m2|

    "/ If I have an explicit preferredExtent ..

    preferredExtent notNil ifTrue:[
        ^ preferredExtent
    ].

    scrolledView notNil ifTrue:[ 
        m2 := innerMargin * 2.
        pref := scrolledView preferredExtent.
        ^ (pref x + scrollBar width + m2) 
          @ 
          (pref y + hScrollBar height + m2).
    ].
    ^ super preferredExtent.

    "Modified: 19.7.1996 / 20:44:16 / cg"
!

preferredExtentForLines:numLines cols:numCols
    "return my preferredExtent for given number of lines and cols."

    |pref m2|

    scrolledView notNil ifTrue:[ 
        m2 := innerMargin * 2.
        pref := scrolledView preferredExtentForLines:numLines cols:numCols.
        ^ (pref x + scrollBar width + m2) 
          @ 
          (pref y + hScrollBar height + m2).
    ].
    ^ super preferredExtent.

    "Modified: 23.4.1996 / 00:15:25 / cg"
    "Created: 24.5.1996 / 17:15:43 / cg"
! !

!HVScrollableView class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg/HVScrollableView.st,v 1.22 1996-10-15 12:05:48 cg Exp $'
! !