#
# Modified by Dave Clemans, May 1993, for my environment
#
set tkfsMiscWinNumber 0

#
# A pwd, that strips off nfs /tmp_mnt prefixes
#
proc tkfsPwd {} {
   set dir [pwd]
   if {[string match "/tmp_mnt/*" "$dir"]} {
       return [string range "$dir" 8 end]
   }
   return "$dir"
}

#
# tkfsFileselect.tcl --
# simple file selector.
#
# Mario Jorge Silva			          msilva@cs.Berkeley.EDU
# University of California Berkeley                 Ph:    +1(510)642-8248
# Computer Science Division, 571 Evans Hall         Fax:   +1(510)642-5775
# Berkeley CA 94720                                 
# 
# Layout:
#
#  file:                  +----+
#  ____________________   | OK |
#                         +----+
#
#  +------------------+    Cancel
#  | ..               |S
#  | file1            |c
#  | file2            |r
#  |                  |b
#  | filen            |a
#  |                  |r
#  +------------------+
#  currrent-directory
#
# Copyright 1993 Regents of the University of California
# Permission to use, copy, modify, and distribute this
# software and its documentation for any purpose and without
# fee is hereby granted, provided that this copyright
# notice appears in all copies.  The University of California
# makes no representations about the suitability of this
# software for any purpose.  It is provided "as is" without
# express or implied warranty.
#


# names starting with "tkfsFileselect" are reserved by this module
# no other names used.


# use the "option" command for further configuration

option add *Listbox*font \
    "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile
option add *Entry*font \
    "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile
option add *Label*font \
    "-*-helvetica-medium-r-normal-*-12-*-*-*-p-*-iso8859-1" startupFile


# this is the default proc  called when "OK" is pressed
# to indicate yours, give it as the second arg to "tkfsFileselect"

proc tkfsFileselect.default.cmd {f} {
  puts stderr "selected file $f"
}


# this is the default proc called when error is detected
# indicate your own as an argument to tkfsFileselect

proc tkfsFileselect.default.errorHandler {errorMessage} {
    puts stdout "error: $errorMessage"
    catch { cd ~ }
}

# this is the proc that creates the file selector box

proc tkfsFileselect {
    {whofor .tk.tk}
    {cmd tkfsFileselect.default.cmd} 
    {purpose "Open file:"} 
    {w .fileSelectWindow} 
    {errorHandler tkfsFileselect.default.errorHandler}} {

    global tkfsMiscWinNumber
    set w $w$tkfsMiscWinNumber
    incr tkfsMiscWinNumber

    catch {destroy $w}

    toplevel $w
    grab $w
    wm title $w "Select File"


    # path independent names for the widgets
    global tkfsFileselect

    set tkfsFileselect(entry) $w.file.eframe.entry
    set tkfsFileselect(list) $w.file.sframe.list
    set tkfsFileselect(scroll) $w.file.sframe.scroll
    set tkfsFileselect(ok) $w.bframe.okframe.ok
    set tkfsFileselect(cancel) $w.bframe.cancel
    set tkfsFileselect(dirlabel) $w.file.dirlabel

    # widgets
    frame $w.file -bd 10 
    frame $w.bframe -bd 10
    pack append $w \
        $w.file {left filly} \
        $w.bframe {left expand frame n}

    frame $w.file.eframe
    frame $w.file.sframe
    label $w.file.dirlabel -anchor e -width 24 -text [tkfsPwd] 

    pack append $w.file \
        $w.file.eframe {top frame w} \
	$w.file.sframe {top fillx} \
	$w.file.dirlabel {top frame w}


    label $w.file.eframe.label -anchor w -width 24 -text $purpose
    entry $w.file.eframe.entry -relief sunken 

    pack append $w.file.eframe \
		$w.file.eframe.label {top expand frame w} \
                $w.file.eframe.entry {top fillx frame w} \


    scrollbar $w.file.sframe.yscroll -relief sunken \
	 -command "$w.file.sframe.list yview"
    listbox $w.file.sframe.list -relief sunken \
	-yscroll "$w.file.sframe.yscroll set" 

    pack append $w.file.sframe \
        $w.file.sframe.yscroll {right filly} \
 	$w.file.sframe.list {left expand fill} 

    # buttons
    frame $w.bframe.okframe -borderwidth 2 -relief sunken
 
    button $w.bframe.okframe.ok -text OK -relief raised -padx 10 \
        -command "tkfsFileselect.ok.cmd $w $cmd $errorHandler $whofor"

    button $w.bframe.cancel -text cancel -relief raised -padx 10 \
        -command "tkfsFileselect.cancel.cmd $w"
    pack append $w.bframe.okframe $w.bframe.okframe.ok {padx 10 pady 10}

    pack append $w.bframe $w.bframe.okframe {expand padx 20 pady 20}\
                          $w.bframe.cancel {top}

    # Fill the listbox with a list of all the files in the directory (run
    # the "/bin/ls" command to get that information).
 
    foreach i [exec /bin/ls -a [pwd]] {
        if {[string compare $i "."] != 0} {
            $tkfsFileselect(list) insert end $i
        }
    }

   # Set up bindings for the browser.
    bind $tkfsFileselect(entry) <Return> {eval $tkfsFileselect(ok) invoke}
    bind $tkfsFileselect(entry) <Control-c> {eval $tkfsFileselect(cancel) invoke}

    bind $w <Control-c> {eval $tkfsFileselect(cancel) invoke}
    bind $w <Return> {eval $tkfsFileselect(ok) invoke}


    tk_listboxSingleSelect $tkfsFileselect(list)


    bind $tkfsFileselect(list) <Button-1> {
        # puts stderr "button 1 release"
        %W select from [%W nearest %y]
	$tkfsFileselect(entry) delete 0 end
	$tkfsFileselect(entry) insert 0 [%W get [%W nearest %y]]
    }

    bind $tkfsFileselect(list) <Key> {
        %W select from [%W nearest %y]
        $tkfsFileselect(entry) delete 0 end
	$tkfsFileselect(entry) insert 0 [%W get [%W nearest %y]]
    }

    bind $tkfsFileselect(list) <Double-ButtonPress-1> {
        # puts stderr "double button 1"
        %W select from [%W nearest %y]
	$tkfsFileselect(entry) delete 0 end
	$tkfsFileselect(entry) insert 0 [%W get [%W nearest %y]]
	$tkfsFileselect(ok) invoke
    }

    bind $tkfsFileselect(list) <Return> {
        %W select from [%W nearest %y]
	$tkfsFileselect(entry) delete 0 end
	$tkfsFileselect(entry) insert 0 [%W get [%W nearest %y]]
	$tkfsFileselect(ok) invoke
    }

    # set kbd focus to entry widget

    focus $tkfsFileselect(entry)

}


# auxiliary button procedures

proc tkfsFileselect.cancel.cmd {w} {
    # puts stderr "Cancel"
    destroy $w
}

proc tkfsFileselect.ok.cmd {w cmd errorHandler whofor} {
    global tkfsFileselect
    set selected [$tkfsFileselect(entry) get]

    # some nasty file names may cause "file isdirectory" to return an error
    set sts [catch { 
	file isdirectory $selected
    }  errorMessage ]

    if { $sts != 0 } then {
	$errorHandler $errorMessage
	destroy $w
	return

    }

    if {[file isdirectory $selected] != 0} {
	cd $selected
	set dir [pwd]
	$tkfsFileselect(dirlabel) configure -text $dir
	$tkfsFileselect(entry) delete 0 end
	$tkfsFileselect(list) delete 0 end
	foreach i [exec /bin/ls -a $dir] {
	    if {[string compare $i "."] != 0} {
		$tkfsFileselect(list) insert end $i
	    }
	}
	return
    }

    destroy $w
    $cmd $whofor $selected
}
