Updated 2012-05-07 01:20:36 by gjs

GJS 2012/5/6 The autoscroll widget is a scrollbar that disappears when not needed. Most of the code came from the autoscroll package with minor changes. One option is added, -auto is set to either true or false.
package provide tkoo::autoscroll 0.1.1

package require tkoo

#Most of the code came from the autoscroll package with slight changes

tkoo::class ::tkoo::autoscroll {
        superclass tkoo::ttk_scrollbar
        variable widCmd pathname options exists grid time
        constructor {wid args} {
                #check to see if the class already exists
                my Exists $wid
                set time 0
                #-auto option
                my Opt add -auto auto Auto true {
                        my variable widCmd pathname options exists
                        if {![string is bool $value]} {error [msgcat::mc "expected boolean value but got \"%s\"" $value]}
                        my set {*}[my get]
                #get options that can only be edited during creation
                set cArgs [list]
                for {set i 0} {$i < [llength $args]} {incr i 2} {
                        set o [lindex $args $i]
                        set v [lindex $args [expr $i + 1]]
                        switch -exact -- $o {
                                -class -
                                -container -
                                -visual {
                                        lappend cArgs $o $v
                #create the scrollbar
                if {![winfo exists $wid]} {
                        ttk::scrollbar $wid {*}$cArgs
                bind $wid <Map> {%W map}
                #default code
                next $wid {*}$args
        method set {min max} {
                if {$min <= 0 && $max >= 1 && [my Opt get -auto]} {
                        #no scrollbar needed
                        if {![info exists grid]} {
                                #prevent flicker effects
                                if {[expr [clock milliseconds] - $time] < 50} {return}
                                #hide the scrollbar
                                switch -exact -- [winfo manager $pathname] {
                                        grid {
                                                lappend grid "[list grid $pathname] [grid info $pathname]"
                                                grid forget $pathname
                                        pack {
                                                foreach x [pack slaves [winfo parent $w]] {
                                                        lappend grid "[list pack $x] [pack info $x]"
                                                pack forget $pathname
                } elseif {[info exists grid]} {
                        #store the time the scroll bar was shown
                        set time [clock milliseconds]
                        #show the scrollbar
                        eval [join $grid \;]
                        unset grid
                return [tailcall $widCmd set $min $max]
        method map {} {
                wm geometry [winfo toplevel $pathname] [wm geometry [winfo toplevel $pathname]]