#!/usr/bin/env python
#
# $Id$
#

from Tkinter import *
import os,stat


# ====================================================================
# Program:     File Broswer
# Description: A Simple File Broswer for Tk
# Author:      Jim Weirich - (ported to python by Monty Stein 1/1/98)
# Date:        22/Dec/97
# ====================================================================


class Filebrowse(Frame):
  def __init__(self,startdirectory,parent=None):
    Frame.__init__(self,parent)
    
    # The top most frame of the browser displays  the current directory of
    # the file browser. 
    
    self.labelframe=Frame(parent)
    Label(self.labelframe,text="Directory:").pack(side=LEFT,expand=0,fill=X)
    self.labcwd=Label(self.labelframe,text="",relief=GROOVE)
    self.labcwd.pack(side=LEFT,expand=1,fill=X,anchor=W)
    self.labelframe.pack(expand=0,fill=X)

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

    self.listframe=Frame(parent)

    self.lb=Listbox(self.listframe,width=25)
    self.ysb=Scrollbar(self.listframe,command=self.lb.yview,orient=VERTICAL)
    self.xsb=Scrollbar(self.listframe,command=self.lb.xview,orient=HORIZONTAL)
    self.lb.configure(xscrollcommand=self.xsb.set,yscrollcommand=self.ysb.set)

    self.xsb.pack(side=BOTTOM,fill=X,expand=0)
    self.ysb.pack(side=RIGHT,fill=Y,expand=0)
    self.lb.pack(side=TOP,fill=BOTH,expand=1)
    self.listframe.pack(expand=1,fill=BOTH)
    
    # The final frame contains the exit button.
    self.buttonframe=Frame(parent)
    Button(self.buttonframe,text="Exit",command=self.quit).pack(side=TOP,fill=X)
    self.buttonframe.pack(fill=X)

    # Finally, bind the double click action on the listbox to the proper
    # TCL function.
    self.lb.bind('',self.HandleListBoxDClick)
    self.ChangeDirectory(startdirectory)

  def FillListbox(self):
    """
      FillListbox -
         Fill the listbox with the files in the current directory.
    """
    self.lb.delete(0,END)
    self.lb.insert(END,"./","../")  # these are not returned by listdir
    files=os.listdir(".")
    files.sort()
    for f in files:
      filestat=os.stat(f)
      if stat.S_ISDIR(filestat[0]):
        f=f+"/"
      self.lb.insert(END,f)


  def ChangeDirectory(self,dir):
    """
      ChangeDirectory -
         Change to the given directory and update the display.
    """
    try:
      os.chdir(dir)
    except os.error:
      return(-1)
    self.labcwd.configure(text=os.getcwd())
    self.FillListbox()

  def HandleListBoxDClick(self,widget):
    """
      HandleListBoxDClick -
         Handle the double click action in a list box.  
    """
    index=widget.widget.nearest(widget.y)
    filename=widget.widget.get(index)
    filestat=os.stat(filename)
    if stat.S_ISDIR(filestat[0]):
      self.ChangeDirectory(filename)



# ====================================================================
# Main Program
# ====================================================================
if __name__ == '__main__':
  main=Filebrowse(os.environ['HOME'])
  main.master.title("File Browser")
  try:
    main.mainloop()
  except KeyboardInterrupt:
    main.quit()