/* Test GtkDialog
 * Build with gcc `pkg-config --cflags --libs gtk+-3.0` -o gtk-dialog-test gtk-dialog-test.c
 */

#include <gtk/gtk.h>

static gboolean
set_unresizable (gpointer user_data)
{
  gtk_window_set_resizable (GTK_WINDOW (user_data), FALSE);
  return FALSE;
}

static void
run_dialog (GtkWindow *window,
            gboolean resizable,
            gboolean after)
{
  GtkWidget *dialog;

  if (resizable)
    {
      dialog = g_object_new (GTK_TYPE_DIALOG, "default-width", 300, "default-height", 300, NULL);
      gtk_window_set_title (GTK_WINDOW (dialog), "Dialog (resizable)");
    }
  else if (after)
    {
      dialog = g_object_new (GTK_TYPE_DIALOG, "default-width", 300, "default-height", 300, NULL);
      gtk_window_set_title (GTK_WINDOW (dialog), "Dialog (set unresizable after creation)");
      g_idle_add (set_unresizable, dialog);
    }
  else
    {
      dialog = g_object_new (GTK_TYPE_DIALOG, "default-width", 300, "default-height", 300, "resizable", FALSE, NULL);
      gtk_window_set_title (GTK_WINDOW (dialog), "Dialog (set unresizable at creation)");
    }

  gtk_dialog_add_button (GTK_DIALOG (dialog), "Close dialog", GTK_RESPONSE_ACCEPT);
  gtk_window_set_transient_for (GTK_WINDOW (dialog), window);
  g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);

  gtk_widget_show_all (dialog);
}

static void
run_dialog_resizable (GtkWindow *window,
                      GtkWidget *button G_GNUC_UNUSED)
{
  run_dialog (window, TRUE, FALSE);
}

static void
run_dialog_after_creation (GtkWindow *window,
                   GtkWidget *button G_GNUC_UNUSED)
{
  run_dialog (window, FALSE, TRUE);
}

static void
run_dialog_at_creation (GtkWindow *window,
                  GtkWidget *button G_GNUC_UNUSED)
{
  run_dialog (window, FALSE, FALSE);
}

int
main (int   argc,
      char *argv[])
{
  GtkWidget *window, *grid, *button;
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 24);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  grid = gtk_grid_new ();
  gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
  gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
  gtk_container_add (GTK_CONTAINER (window), grid);

  button = gtk_button_new_with_label ("Run dialog (resizable)");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (run_dialog_resizable), window);
  gtk_container_add (GTK_CONTAINER (grid), button);

  button = gtk_button_new_with_label ("Run dialog (set unresizable after creation)");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (run_dialog_after_creation), window);
  gtk_container_add (GTK_CONTAINER (grid), button);

  button = gtk_button_new_with_label ("Run dialog (set unresizable at creation)");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (run_dialog_at_creation), window);
  gtk_container_add (GTK_CONTAINER (grid), button);

  gtk_widget_show_all (window);

  gtk_main ();
  return 0;
}
