#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix@uunet.uu.net if you want that tool.
# If this archive is complete, you will see the following message at the end:
#		"End of shell archive."
# Contents:  dir.tk
# Wrapped by khattra@phecda on Wed Mar 10 17:08:54 1993
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'dir.tk' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'dir.tk'\"
else
echo shar: Extracting \"'dir.tk'\" \(7667 characters\)
sed "s/^X//" >'dir.tk' <<'END_OF_FILE'
X#!/usr/local/bin/wish -f
X#
X# simple directory browser
X#
X
X
X# need this for mkDialog
Xset auto_path "$tk_library/demos $auto_path"
X
X
X# NLIST - number of listboxes displayed
X# INIT_DIR - initial directory
X# .hscroll - horizontal scrollbar
X# .vscroll - array of vertical scrollbars attached to listboxes in .list
X# .list - array of listboxes
X# lists - array of directory contents
X# firsts - array of first file displayed in each dir
X# selecteds - array of selected file in each dir
X# first - directory currently displayed in first listbox
X
Xglobal NLIST INIT_DIR .hscroll .vscroll .list lists firsts selecteds first
Xset NLIST 4
X
X
X# create horizontal scrollbar
Xscrollbar .hscroll -orient horizontal -command hscroll_callback
X.hscroll set 1 $NLIST 0 [expr $NLIST-1] 
Xpack append . .hscroll {top fillx}
X
X
X# create quit button
Xbutton .quit -text "quit" -command "destroy ." -width 5
Xpack append . .quit {bot pady 20}
X
X
X# create vertical scrollbars and listboxes
Xfor {set i 0} {$i < $NLIST} {incr i} {
X   scrollbar .vscroll($i) -command ".list($i) yview"
X   listbox .list($i) -yscroll ".vscroll($i) set" -relief sunken -geom 20x20 -exportselection false
X   pack append . .list($i) {left filly} .vscroll($i) {left filly}
X}
X
X
X# horizontal scrollbar callback fn
X# XXX - something's wrong here - if the scrollbar is moved rapidly
X#       then the "firsts" array gets screwed up; if you fix the problem
X#       please send me email at khattra@cs.sfu.ca
X
Xproc hscroll_callback {index} {
X
X   global NLIST lists first selecteds firsts
X
X   # don't scroll past last full screen
X   if {$index < 0 || [expr $index+$NLIST] > [array size lists]} return 
X   if {$index == $first} return 
X
X   # update firsts array
X   set tmp [array size firsts]
X   for {set i 0} {$i < $NLIST && [expr $i+$first] < $tmp} {incr i} {
X      set firsts([expr $i+$first]) [lindex [.vscroll($i) get] 2]
X   }
X
X   # tell scrollbar to move
X   set tmp [.hscroll get]
X   .hscroll set \
X      [lindex $tmp 0] \
X      [lindex $tmp 1] \
X      $index \
X      [expr $index+$NLIST-1]
X
X   # update listboxes
X   for {set i $index; set j 0} {$i < [expr $index+$NLIST]} {incr i; incr j} {
X
X      # delete listbox contents
X      .list($j) delete 0 end
X
X      # insert new listbox contents
X      foreach k $lists($i) {
X	 .list($j) insert end $k
X      }
X
X      # set selected item
X      if {$selecteds($i) >= 0} {
X         .list($j) select from $selecteds($i)
X      }
X
X      # set top listbox item
X      if {$i >= $first && $i < [expr $first+$NLIST]} {
X	 set k [lindex [.vscroll([expr $i-$first]) get] 2]
X      } else {
X	 set k $firsts($i)
X      }
X      .list($j) yview $k
X   }
X
X   # reset first
X   set first $index
X}
X
X
X# called when user clicks in a listbox
Xproc click {num} {
X
X   global NLIST INIT_DIR selecteds lists first firsts
X
X   # if no selection in this listbox then return
X   if {![string compare "" [.list($num) curselection]]} return
X
X   # clear proceeding listboxes
X   for {set i [expr $num+1]} {$i < $NLIST} {incr i} {
X      .list($i) delete 0 end
X   }
X
X   # clear proceeding lists
X   set tmp [expr $first+$num+1]
X   set size [array size lists]
X   for {set i $tmp} {$i < $size} {incr i} {
X
X      # delete directory list
X      lreplace lists($i) 0 end
X
X      # disassociate index $i from the all the arrays
X      unset lists($i)
X      unset firsts($i)
X      unset selecteds($i)
X   }
X
X   # set new selected item
X   set selecteds([expr $first+$num]) [.list($num) curselection]
X
X   # construct full path name
X   set name $INIT_DIR
X   set tmp [expr $first+$num]
X   for {set i 0} {$i <= $tmp} {incr i} {
X      set name $name[lindex $lists($i) $selecteds($i)]
X   }
X
X   # if file is a directory and is readable, then read it
X   if {[file isdirectory $name]} {
X
X      # if directory is not readable, then let user know
X      if {![file readable $name]} {
X	 # use mkDialog from library/demos directory
X	 set arg1 [list -text "Can't read directory \"$name\"" -aspect 250 -justify left]
X	 mkDialog .modal $arg1 {OK {}}
X	 wm geometry .modal +300+300
X	 tkwait visibility .modal
X	 grab .modal
X	 tkwait window .modal
X	 focus .
X
X      # else read it
X      } else {
X         # set watch cursor
X         set push_flag 0
X         .list($num) configure -cursor watch
X         update idletasks
X
X         # see if we need to scroll everything left
X         if {$num == [expr $NLIST-1]} {
X            push
X	    incr num -1
X	    set push_flag 1 
X         }
X
X         set tmp [expr $first+$num+1]
X         set tmp2 [expr $num+1]
X         foreach i [exec ls $name] {
X            if [file isdirectory $name/$i] {append i "/"}
X	    lappend lists($tmp) $i
X	    .list($tmp2) insert end $i
X         }
X         if {[array size lists] > [array size firsts]} {
X            set firsts($tmp) -1
X            set selecteds($tmp) -1
X         }
X
X         # unset watch cursor
X         if {$push_flag} {incr num 1}
X         .list($num) configure -cursor ""
X      }
X   }
X
X   # see if we need to scroll right
X   # (XXX - this cd be optimized by merging with the code above)
X
X   if {[expr $first+$NLIST] > [array size lists] && \
X       $first > 0} {
X
X       # clear listboxes
X       for {set i 0} {$i < $NLIST && [expr $i+$first] < [array size lists]} {incr i} {
X
X	  # update firsts array
X	  set firsts([expr $i+$first]) [lindex [.vscroll($i) get] 2]
X
X	  # delete listbox contents
X	  .list($i) delete 0 end
X       }
X
X       set first [expr [array size lists]-$NLIST]
X       if {$first < 0} {set first 0}
X
X       # fill listboxes
X       for {set i 0} {$i < $NLIST && [expr $i+$first] < [array size lists]} {incr i} {
X
X	  foreach j $lists([expr $i+$first]) {
X	     .list($i) insert end $j
X	  }
X
X	  # set selected item
X	  if {$selecteds([expr $i+$first]) >= 0} {
X	     .list($i) select from $selecteds([expr $i+$first])
X	  }
X
X	  # set first item displayed
X	  .list($i) yview $firsts([expr $i+$first])
X       }
X   }
X
X   # update horizontal scrollbar
X   set tmp [.hscroll get]
X   .hscroll set \
X      [array size lists] \
X      $NLIST \
X      $first \
X      [expr $first+$NLIST-1]
X}
X
X
X# called to scroll listbox contents
Xproc push {} {
X
X   global firsts NLIST lists first selecteds
X
X   # remember first row displayed in listbox 0
X   set firsts($first) [lindex [.vscroll(0) get] 2]
X
X   # shift listbox contents to the left
X   for {set i 0} {$i < [expr $NLIST-1]} {incr i} {
X
X      # delete listbox items
X      .list($i) delete 0 end
X
X      # set new listbox items
X      set tmp [expr $i+$first+1]
X      foreach j $lists($tmp) {
X	 .list($i) insert end $j
X      }
X
X      # set selected item
X      .list($i) select from $selecteds($tmp)
X
X      # set top listbox item
X      set tmp [.vscroll([expr $i+1]) get]
X      .list($i) yview [lindex $tmp 2]
X
X      # need this - don't remove !!
X      .vscroll($i) set \
X	 [lindex $tmp 0] \
X	 [lindex $tmp 1] \
X	 [lindex $tmp 2] \
X	 [lindex $tmp 3]
X   }
X
X   # empty last listbox
X   .list([expr $NLIST-1]) delete 0 end
X
X   # update first
X   incr first
X}
X
X
X# cd to initial directory
Xif {$argc > 0} {set INIT_DIR [lindex $argv 0]} else {set INIT_DIR "."}
Xset INIT_DIR [string trimright $INIT_DIR /]/
Xcd $INIT_DIR
X
X
X# read and display initial directory
Xforeach i [exec ls $INIT_DIR] {
X   if [file isdirectory $i] {append i "/"}
X   lappend lists(0) $i
X   .list(0) insert end $i
X}
Xset firsts(0) -1
Xset selecteds(0) -1 
Xset first 0
X
X
X# do default bindings
X
X# easy quit option
Xbind . <q> {destroy .}
X
X
X# can only select single listbox item at a time
Xtk_listboxSingleSelect Listbox
X
X
X# no motion sickness please
Xbind Listbox <B1-Motion> ""
Xbind Listbox <Shift-B1-Motion> ""
X
X
X# set Button-1 binding for each listbox
Xfor {set i 0} {$i < $NLIST} {incr i} {
X   bind .list($i) <1> {%W select from [%W nearest %y]}
X   bind .list($i) <1> +click\ $i
X}

END_OF_FILE
echo shar: NEWLINE appended to \"'dir.tk'\"
if test 7668 -ne `wc -c <'dir.tk'`; then
    echo shar: \"'dir.tk'\" unpacked with wrong size!
fi
chmod +x 'dir.tk'
# end of 'dir.tk'
fi
echo shar: End of shell archive.
exit 0
