#include <gtk/gtk.h>

#include <stdio.h>

static GtkWidget *s_window;
static GtkWidget *s_fcd;

static void
choose_load (GtkButton *button)
{
  gtk_window_set_title (GTK_WINDOW (s_fcd), "Choose a file to load");
  gtk_file_chooser_set_action (GTK_FILE_CHOOSER (s_fcd), GTK_FILE_CHOOSER_ACTION_OPEN);
  gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (s_fcd), "/tmp/fakefile.txt");

  int response = gtk_dialog_run (GTK_DIALOG (s_fcd));
  gtk_widget_hide (s_fcd);

  printf("%i\n", response);
}

static void
choose_save (GtkButton *button)
{
  gtk_window_set_title (GTK_WINDOW (s_fcd), "Choose a file to save");
  gtk_file_chooser_set_action (GTK_FILE_CHOOSER (s_fcd), GTK_FILE_CHOOSER_ACTION_SAVE);
  gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (s_fcd), "/tmp/fakefile.txt");

  int response = gtk_dialog_run (GTK_DIALOG (s_fcd));
  gtk_widget_hide (s_fcd);

  printf("%i\n", response);
}

static void
on_application_activate(GApplication *application, void *user_data)
{}

static void
on_application_startup(GApplication *application, void *user_data)
{
  s_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  s_fcd = gtk_file_chooser_dialog_new ("dummy title",
                                       GTK_WINDOW (s_window),
                                       GTK_FILE_CHOOSER_ACTION_SAVE,
                                       "_Cancel",
                                       GTK_RESPONSE_CANCEL,
                                       "_Go",
                                       GTK_RESPONSE_ACCEPT,
                                       NULL);
  gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (s_fcd), TRUE);

  GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
  g_object_set (G_OBJECT (box), "margin", 12, NULL);

  GtkWidget *button_load = gtk_button_new_with_label ("_Load");
  gtk_button_set_use_underline (GTK_BUTTON (button_load), TRUE);
  g_signal_connect (G_OBJECT (button_load), "clicked", G_CALLBACK (&choose_load), NULL);
  gtk_container_add (GTK_CONTAINER (box), button_load);

  GtkWidget *button_save = gtk_button_new_with_label ("_Save");
  gtk_button_set_use_underline (GTK_BUTTON (button_save), TRUE);
  g_signal_connect (G_OBJECT (button_save), "clicked", G_CALLBACK (&choose_save), NULL);
  gtk_container_add (GTK_CONTAINER (box), button_save);

  gtk_container_add (GTK_CONTAINER (s_window), box);
  gtk_widget_show_all (s_window);
  gtk_application_add_window (GTK_APPLICATION (application), GTK_WINDOW (s_window));
}

int
main(int argc, char **argv)
{
  GtkApplication *application = gtk_application_new("org.djb.test", G_APPLICATION_FLAGS_NONE);

  g_signal_connect (G_OBJECT (application), "startup",
                    G_CALLBACK (&on_application_startup), NULL);
  g_signal_connect (G_OBJECT (application), "activate",
                    G_CALLBACK (&on_application_activate), NULL);

  int status = g_application_run (G_APPLICATION (application), argc, argv);
  g_object_unref(application);
  return status;
}
