#include <gtk/gtk.h>

struct some_data
{
  const gchar *save_directory;
  GtkWidget *main_window;
};

void
clicked (GtkButton *button,
         gpointer   user_data)
{
  struct some_data *data = (struct some_data *) user_data;
  static GtkWidget *dialog = NULL;
  GtkFileChooser *chooser;
  gint res;
  gchar *filename;

  if (g_once_init_enter (&dialog))
  {
    GtkWidget *dlg;
    GtkFileFilter *filt_all, *filt_psyp;

    dlg = gtk_file_chooser_dialog_new ("Save as...",
                                       GTK_WINDOW (data->main_window),
                                       GTK_FILE_CHOOSER_ACTION_SAVE,
                                       "_Cancel",
                                       GTK_RESPONSE_CANCEL,
                                       "_Save",
                                       GTK_RESPONSE_ACCEPT,
                                       NULL);

    g_once_init_leave (&dialog, dlg);
  }

  chooser = GTK_FILE_CHOOSER (dialog);

  filename = g_build_filename (data->save_directory, "Untitled.file", NULL);
  gtk_file_chooser_set_filename (chooser, filename);
  g_free (filename);

  filename = NULL;

  res = gtk_dialog_run (GTK_DIALOG (dialog));

  if (res == GTK_RESPONSE_ACCEPT)
    filename = gtk_file_chooser_get_filename (chooser);

  gtk_widget_hide (GTK_WIDGET (dialog));

  g_free (filename);
}

int main (int argc, char **argv)
{
  GtkWidget *button;
  struct some_data data;

  gtk_init (&argc, &argv);

  data.save_directory = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);

  data.main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  button = GTK_WIDGET (gtk_button_new_with_label ("save"));
  g_signal_connect(G_OBJECT (button), "clicked", G_CALLBACK (clicked), (gpointer) &data);
  gtk_container_add (GTK_CONTAINER (data.main_window), button);

  g_signal_connect(G_OBJECT (data.main_window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show_all (GTK_WIDGET (data.main_window));

  gtk_main ();

  return 0;
}
