/*
 * Auth: Eric Harlow
 * File: filesel.c
 */

#include <gtk/gtk.h>
#include <desktop.h>
#include <dialog.h>

#include <locale.h>
#include <libintl.h>
#define _(STRING) gettext(STRING)

GtkWidget *fileopenw = NULL;

typedef struct {

    void (*func)();
    GtkWidget *filesel;

} typFileSelectionData;

/*
 * --- Filename, remember it.
 */
static   char        *sFilename = NULL; 

char *GetExistingFile ()
{
    return (sFilename);
}
    
/*
 * FileOk
 *
 * The "Ok" button has been clicked 
 * Call the function (func) to do what is needed
 * to the file.
 */
void FileOk (GtkWidget *w, gpointer data)
{
    char *sTempFile;
    typFileSelectionData *localdata;
    GtkWidget *filew;
 
    localdata = (typFileSelectionData *) data;
    filew = localdata->filesel;

    /* --- Which file? --- */
    sTempFile = gtk_file_selection_get_filename (GTK_FILE_SELECTION (filew));

    /* --- Free old memory --- */
    if (sFilename) g_free (sFilename);

    /* --- Duplicate the string --- */
    sFilename = g_strdup (sTempFile); 

    /* --- Call the function that does the work. --- */
    (*(localdata->func)) (sFilename);

    /* --- Close the dialog --- */
    gtk_widget_destroy (filew);
}

/*
 * destroy
 *
 * Function to handle the destroying of the dialog.  We must
 * release the focus that we grabbed.
 */
static void destroy (GtkWidget *widget, gpointer *data)
{
    /* --- Remove the focus. --- */
    gtk_grab_remove (widget);

    g_free (data);
}

/*
 * GetFilename
 *
 * Show a dialog with a title and if "Ok" is selected
 * call the function with the name of the file.
 */
void GetFilename (char *sTitle, void (*callback) (char *))
{
    //GtkWidget *fileopenw = NULL;
    typFileSelectionData *data;

    /* --- Create a new file selection widget --- */
    fileopenw = FileSelectionDialog(sTitle);
    gtk_window_set_policy( GTK_WINDOW(fileopenw), TRUE, TRUE, TRUE );
     
    data = g_malloc (sizeof (typFileSelectionData));
    data->func = callback;
    data->filesel = fileopenw;

    gtk_signal_connect (GTK_OBJECT (fileopenw), "destroy",
            (GtkSignalFunc) destroy, data);

    /* --- Connect the "ok" button --- */
    gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (fileopenw)->ok_button),
            "clicked", (GtkSignalFunc) FileOk, data);
    
    /* --- Connect the cancel button --- */
    gtk_signal_connect_object (
             GTK_OBJECT (GTK_FILE_SELECTION (fileopenw)->cancel_button),
             "clicked", (GtkSignalFunc) gtk_widget_destroy, 
             (gpointer) fileopenw);
    
    if (sFilename) {

        /* --- Set the default filename --- */
        gtk_file_selection_set_filename (GTK_FILE_SELECTION (fileopenw), 
                                         sFilename);
    }
    
    /* --- Show the dialog --- */
    gtk_widget_show (fileopenw);

    /* --- Grab the focus. --- */
    gtk_grab_add (fileopenw);
}

void
notepad_resize_opendialog( int width, int height ) {
	if ( fileopenw ) gtk_widget_set_usize( fileopenw, width, height );
	return;
}
