/*  $Id$ */

/*  ----------------------------------------------------------------------

    gtail -- A "tail -f" for GTK+.
    Copyright (c) 1999 Colin Marquardt <colin.marquardt@gmx.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    ---------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "filesel.h"

static   GtkWidget     *FileSelWidget;
static   char        *sFilename = NULL; 
    
/* 
 * Get the selected filename and print it to the console 
 */
void file_ok_sel (GtkWidget *w, GtkFileSelection *fs) {
  char *sTempFile;
  
  /* --- Get the name --- */
  sTempFile = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
  
  /* --- Allocate space and save it. --- */
  sFilename = malloc (sizeof (char) * (strlen (sTempFile) + 1));
  strcpy (sFilename, sTempFile);
  
  /* --- Destroy the file selection --- */
  gtk_widget_destroy (FileSelWidget);
}


void file_cancel_sel (GtkWidget *w, GtkFileSelection *fs) {
  
  /* --- Destroy the file selection --- */
  gtk_widget_destroy (FileSelWidget);
}


/*
 * DestroyDialog
 *
 * Destroy the dialog (obvious, eh?) but also remove the
 * grab and close the modal. 
 */
int DestroyDialog (GtkWidget *widget, gpointer *data) {
  gtk_grab_remove (widget);
  gtk_main_quit ();
  return (FALSE);
}


/*
 * GetFilename 
 */
char *GetFilename (char *sTitle) {
 
    /* --- If the value is saved from last time we used the dialog,
     * --- free the memory
     */
    if (sFilename) {
        free (sFilename);
        sFilename = NULL;
    }

    
    /* --- Create a new file selection widget --- */
    FileSelWidget = gtk_file_selection_new (sTitle);

    /* --- If it's destroyed --- */
    gtk_signal_connect (GTK_OBJECT (FileSelWidget), "destroy",
            (GtkSignalFunc) DestroyDialog, &FileSelWidget);

    /* --- Connect the ok_button to file_ok_sel function  --- */
    gtk_signal_connect (
            GTK_OBJECT (GTK_FILE_SELECTION (FileSelWidget)->ok_button),
            "clicked", (GtkSignalFunc) file_ok_sel, FileSelWidget );
    
    /* --- Connect the cancel_button to destroy the widget  --- */
    gtk_signal_connect (
             GTK_OBJECT (GTK_FILE_SELECTION (FileSelWidget)->cancel_button),
             "clicked", (GtkSignalFunc) file_cancel_sel, FileSelWidget);
    
    /* --- Lets set the filename, as if this were a save 
     *     dialog, and we are giving a default filename 
     */
    gtk_file_selection_set_filename (GTK_FILE_SELECTION(FileSelWidget), "/var/log/messages");
    
    /* --- Of course, we show it --- */
    gtk_widget_show (FileSelWidget);

    /* --- Make sure we keep the focus --- */
    gtk_grab_add (FileSelWidget);

    gtk_main ();

    return (sFilename);
}
