#!/usr/bin/env wish

# ====================================================================
# Program:     File Broswer
# Description: A Simple File Broswer for Tk
# Author:      Jim Weirich
# Date:        22/Dec/97
# ====================================================================


# ====================================================================
# Function Declarations
# ====================================================================

# FillListbox --------------------------------------------------------
# Fill the listbox with the files in the current directory.
  
proc FillListbox {} {
     .lb delete 0 end
    foreach f [glob .* *] {
        if [file isdirectory $f] {
	    set f "$f/"
	}
        .lb insert end $f;
    }
}

# ChangeDirectory ----------------------------------------------------
# Change to the given directory and update the display.

proc ChangeDirectory {dir} {
    cd $dir
    .labcwd configure -text [pwd]
    FillListbox
}


# HandleListBoxDClick ------------------------------------------------
# Handle the double click action in a list box.  

proc HandleListBoxDClick {} {
    set index [lindex [.lb curselection] 0]
    set filename [.lb get $index]
    if [file isdirectory $filename] {
        ChangeDirectory $filename     
    }
}

# ====================================================================
# Window Declarations
# ====================================================================

# Create a window title

wm title . "File Browser"

# The top most frame of the browser displays  the current directory of
# the file browser. 

frame .labelframe
pack .labelframe -expand no -fill x

label .lab1 -text "Directory:"
label .labcwd -text "" \
    -relief groove
pack .lab1 -in .labelframe \
    -side left -expand no -fill x
pack .labcwd -in .labelframe \
    -side left -expand yes -fill x -anchor w

# The next frame contains the file list box and associated scroll
# bars. 

frame .listframe
pack .listframe -expand yes -fill both

listbox .lb \
    -width 25 \
    -xscrollcommand ".xsb set" \
    -yscrollcommand ".ysb set"

scrollbar .ysb \
    -command ".lb yview" \
    -orient v

scrollbar .xsb \
    -command ".lb xview" \
    -orient h 

pack .xsb -in .listframe -side bottom  -fill x    -expand no
pack .ysb -in .listframe -side right   -fill y    -expand no
pack .lb  -in .listframe -side top     -fill both -expand yes


# The final frame contains the exit button.

frame .buttonframe
pack .buttonframe -fill x

button .done -text Exit -command exit
pack .done -in .buttonframe \
    -side top -fill x


# Finally, bind the double click action on the listbox to the proper
# TCL function.

bind .lb  HandleListBoxDClick



# ====================================================================
# Main Program
# ====================================================================

ChangeDirectory $env(HOME)