#
#   Python GUI - File selection dialogs - Gtk
#

import os
import gtk
from Files import FileRef
from AlertFunctions import confirm
from Applications import application

class _FileDialog(gtk.FileSelection):

    def __init__(self, title = None):
        gtk.FileSelection.__init__(self, title)
        self.ok_button.connect('clicked', self.ok)
        self.cancel_button.connect('clicked', self.cancel)
        self.set_position(gtk.WIN_POS_CENTER)
    
    def present_modally(self):
        self.present()
        self.result = None
        app = application()
        while self.result is None:
            app.handle_next_event()
        return self.result
    
    def dismiss(self, result):
        self.result = result
    
    def ok(self, sender):
        self.dismiss(1)
    
    def cancel(self, sender):
        self.dismiss(0)

#------------------------------------------------------------------

class _SaveFileDialog(_FileDialog):
    
    def ok(self, sender):
        path = self.get_filename()
        if not os.path.exists(path):
            self.dismiss(1)
        else:
            #alert = gtk.MessageDialog(self, gtk.DIALOG_MODAL,
            #	gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL,
            #	"Replace existing '%s'?" % os.path.basename(path))
            #result = alert.run()
            #alert.destroy()
            result = confirm("Replace existing '%s'?" % os.path.basename(path),
                "Cancel", "Replace", cancel = None)
            if result == 0:
                self.dismiss(1)

#------------------------------------------------------------------

def request_old_file(prompt = "Open File", default_dir = None):
    """Present a dialog requesting the name of an existing file.
    Returns a FileRef or None."""
    
    if prompt.endswith(":"):
        prompt = prompt[:-1]
    dlog = _FileDialog(title = prompt)
    if default_dir:
        dlog.set_filename(default_dir.path + "/")
    if dlog.present_modally():
        result = FileRef(path = dlog.get_filename())
    else:
        result = None
    dlog.destroy()
    return result
  
#------------------------------------------------------------------

def request_new_file(prompt = "Save File", default_dir = None,
        default_name = ""):
    """Present a dialog requesting the name of an existing file.
    Returns a FileRef or None."""

    if prompt.endswith(":"):
        prompt = prompt[:-1]
    dlog = _SaveFileDialog(title = prompt)
    if default_dir or default_name:
        if default_dir:
            default_dir_path = default_dir.path
        else:
            default_dir_path = "."
        default_path = default_dir_path + "/" + default_name
        dlog.set_filename(default_path)
    if dlog.present_modally():
        result = FileRef(path = dlog.get_filename())
    else:
        result = None
    dlog.destroy()
    return result

#-----------------------------------------------------------------

#if __name__ == "__main__":
#  result = request_old_file_name()
#  print "Result:", result
