import os, sys
from GUI import \
    Window, Button, CheckBox, application,\
    request_old_file, request_old_files, request_new_file, \
    request_old_directory, request_old_directories, request_new_directory, \
    FileType, DirRef, FileRef
    
last_dir = DirRef(path = os.path.abspath(os.path.dirname(sys.argv[0])))
last_name = ""

print "last_dir =", last_dir ###

class TestWindow(Window):

    file_type = FileType(name = "TIFF Image", suffix = "tiff")

    def __init__(self):
        Window.__init__(self, size = (200, 200))
        self.filt = CheckBox("%ss only" % self.file_type.name)
        #self.multi = CheckBox("Multiple Selection")
        but1 = Button("Old File", action = self.do_old_file)
        but2 = Button("Old Files", action = self.do_old_files)
        but3 = Button("New File", action = self.do_new_file)
        but4 = Button("Old Directory", action = self.do_old_dir)
        but5 = Button("Old Directories", action = self.do_old_dirs)
        but6 = Button("New Directory", action = self.do_new_dir)
        self.place_column([self.filt, but1, but2, but3, but4, but5, but6], left = 20, top = 20)
        self.shrink_wrap(padding = (20, 20))

    def save_result(self, result):
        if isinstance(result, list):
            print "Result:"
            for item in result:
                print "   ", item
        else:
            print "Result =", result
        print
        global last_dir, last_name
        if result:
            if isinstance(result, FileRef):
                last_dir = result.dir
                last_name = result.name
            elif isinstance(result, DirRef):
                last_dir = result
    
    #def multiple(self):
    #	return self.multi.on
    
    def do_old_file(self):
        print "Doing request_old_file"
        if self.filt.on:
            file_types = [self.file_type]
        else:
            file_types = None
        result = request_old_file("Open Dusty Old File:",
            default_dir = last_dir, file_types = file_types)
        self.save_result(result)
    
    def do_old_files(self):
        print "Doing request_old_files"
        if self.filt.on:
            file_types = [self.file_type]
        else:
            file_types = None
        result = request_old_files("Open Dusty Old Files:",
            default_dir = last_dir, file_types = file_types)
        self.save_result(result)
    
    def do_old_dir(self):
        print "Doing request_old_directory"
        result = request_old_directory("Open Mouldy Old Directory:",
            default_dir = last_dir)
        self.save_result(result)
    
    def do_old_dirs(self):
        print "Doing request_old_directories"
        result = request_old_directories("Open Mouldy Old Directory:",
            default_dir = last_dir)
        self.save_result(result)
    
    def do_new_file(self):
        print "Doing request_new_file"
        if self.filt.on:
            file_type = self.file_type
        else:
            file_type = None
        result = request_new_file("Save Shiny New File:",
            default_dir = last_dir, default_name = last_name, file_type = file_type)
        self.save_result(result)

    def do_new_dir(self):
        print "Doing request_new_directory"
        result = request_new_directory("Create Sparkling New Directory:",
            default_dir = last_dir, default_name = last_name)
        self.save_result(result)

def test():
    win = TestWindow()
    win.show()
    application().run()


instructions = """
Buttons should be available for the following dialogs:

  * Old File - select a single existing file
  * Old Files - select multiple existing files
  * New File - specify name and location of a new file
  * Old Directory - select a single existing directory
  * Old Directories - select multiple existing directories
  * New Directory - specify name and location of a new directory

The selected filename or list of filenames should be printed,
or None if the dialog is cancelled.

Checking 'TIFF Images Only' should restrict the files selectable
by Old File and Old Files, and force the file name returned by
New File to have a suffix of '.tiff'.
"""

print instructions
test()
