AJB - I put this together because Im tired of having to setup the scrollbars, and I dont always want to load packages that have alot of dependencies. More and more I am interested in packaging snit with all of my apps, so here is a wrapper for any scrollable widget that will take care of all of your scrollbar woes. PS, not tested on Mac. Sorry
########################################## # # snitscrollwindow.tcl # # Package to provide a wrapper around any scrollable widget # i.e. - text, listbox, canvas # # The scrollbars should have all of the proper bindings # The scrollbars will auto hide/appear as needed # # Options: # -windowtype -- defaults to canvas, but can be any scrollable widget # this option can only be set at creation time # -- all other options are passed to the internal widget itself # -- scrollbar options can be configured using the xscroll/yscroll methods # # Methods: # xscroll -- calling xscroll will cause all remaining args to be sent to the x-scrollbar # example $win xscroll configure -width 12 # -- all of the usual default snit methods configure, cget, etc # # Results: # calling snit::widget with the path of an empty container widget will provide a -windowtype with # scrollbars that appear and disappear as needed, and that have all of the correct bindings package provide snitScrollWindow 0.1 package require Tk package require snit snit::widget snitScrollWindow { option -windowtype -default canvas -validatemethod IsScrollableWidget -readonly yes delegate option * to mainWindow delegate method * to mainWindow variable mainWindow variable scrollGrid -array {} constructor {args} { catch {$self configurelist $args} set widget [$self cget -windowtype] set mainWindow [$widget $win.main] $self configure -yscrollcommand [mymethod ScrollHandle $win.y] -xscrollcommand [mymethod ScrollHandle $win.x] scrollbar $win.y -orient vertical -command [list $self yview] -width 12 scrollbar $win.x -orient horizontal -command [list $self xview] -width 12 grid $mainWindow -row 0 -column 0 -sticky nesw grid $win.y -row 0 -column 1 -sticky ns grid $win.x -row 1 -column 0 -sticky ew $self configurelist $args set scrollGrid($win.y) [grid info $win.y] set scrollGrid($win.x) [grid info $win.x] grid columnconfigure $win 0 -weight 1 grid rowconfigure $win 0 -weight 1 if {$widget eq "canvas"} {bind $mainWindow <Expose> {%W configure -scrollregion [%W bbox all]}} bind $mainWindow <Button-4> [list $self yview scroll -1 units] bind $mainWindow <Button-5> [list $self yview scroll 1 units] bind $mainWindow <Shift-Button-4> [list $self xview scroll -1 units] bind $mainWindow <Shift-Button-5> [list $self xview scroll 1 units] bind $mainWindow <Button> [mymethod HorizScroll %b] bind $mainWindow <MouseWheel> {%W yview scroll [expr {int(pow(%D/-120,3))}] units} bind $mainWindow <Shift-MouseWheel> {%W xview scroll [expr {int(pow(%D/-120,3))}] units} } method xscroll {args} {eval {$win.x} $args} method yscroll {args} {eval {$win.y} $args} method HorizScroll {btn} { if {$btn == 6} { $mainWindow xview scroll -1 units } elseif {$btn == 7} { $mainWindow xview scroll 1 units } } method ScrollHandle {w args} { eval {$w set} $args if {[lindex $args end-1] <= 0 && [lindex $args end] >= 1} { grid forget $w } else { eval {grid $w} $scrollGrid($w) } } method IsScrollableWidget {opt widget args} { if {[catch {$widget $win.temp -yscrollcommand {}}]} {error "$widget is not a scrollable widget"} destroy $win.temp } }