#include <gtk/gtk.h>

static GtkWidget *box;

static void
kill (GtkWidget *widget, GtkWidget *target)
{
  gtk_widget_destroy (target);
}

static gboolean
kill_timeout (gpointer data)
{
  GtkWidget *widget = GTK_WIDGET (data);

  gtk_widget_destroy (widget);

  return FALSE;
}

static void
delayed_kill (GtkWidget *widget, GtkWidget *target)
{
  g_timeout_add (2000, kill_timeout, target);
}

static void
add_button_clicked (GtkWidget *widget, GtkWidget *parent)
{
  GtkWidget *child;
  GtkWidget *button;
  gboolean   allow_transient;
  char *level = g_object_get_data (G_OBJECT (parent), "level");
  char *counter = g_object_get_data (G_OBJECT (parent), "counter");
  char *title;
  char *label;

  allow_transient = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (parent), "allow_transient"));

  level = g_strconcat (level, "/", counter, NULL);
  counter[strlen (counter) - 1]++;

  title = g_strconcat (
#ifdef MODAL
		       "Modal ",
#endif
#ifdef TRANSIENT
		       allow_transient ? "Transient " : "",
#endif
		       "Child Window ", level, NULL);

  child = g_object_connect (gtk_widget_new (gtk_window_get_type (),
                                            "type", GTK_WINDOW_TOPLEVEL,
                                            "title", title,
#ifdef MODAL
                                            "modal", TRUE,
#endif
                                            "user_data", NULL,
                                            "destroy-with-parent", TRUE,
					    "default-width", 400,
                                            NULL),
                            "signal::destroy", gtk_widget_destroy, NULL,
                            NULL);

  g_object_set_data (G_OBJECT (child), "counter", g_strdup ("0"));
  g_object_set_data (G_OBJECT (child), "level", level);
  g_object_set_data (G_OBJECT (child), "allow_transient", GINT_TO_POINTER (TRUE));

  g_object_connect (gtk_widget_new (gtk_button_get_type (),
				    "GtkButton::label", "Create "
#ifdef MODAL
							"Modal "
#endif
#ifdef TRANSIENT
							"Transient "
#endif
							"Child",
				    "GtkWidget::parent", child,
				    NULL),
		    "signal::clicked", add_button_clicked, child,
		    NULL);

#ifdef TRANSIENT
  if (allow_transient)
    gtk_window_set_transient_for (GTK_WINDOW (child), GTK_WINDOW (parent));  
#endif
  gtk_widget_show_all (child);

  label = g_strconcat ("Delayed Kill Child ", level, NULL);
  button = g_object_connect (gtk_widget_new (gtk_button_get_type (),
					     "GtkButton::label", label,
					     "GtkWidget::parent", box,
					     NULL),
			     "signal::clicked", delayed_kill, child,
			     NULL);
  gtk_widget_show_all (box);

  g_object_connect (G_OBJECT (child), "signal::destroy", kill, button,
		    NULL);
}

void
create_main_window ()
{
  GtkWidget *mainwindow;

  mainwindow = g_object_connect (gtk_widget_new (gtk_window_get_type (),
						 "type", GTK_WINDOW_TOPLEVEL,
						 "user_data", NULL,
						 "title", "Parent Window",
						 "default-width", 400,
						 NULL),
				 "signal::destroy", gtk_main_quit, NULL,
				 NULL);

  g_object_set_data (G_OBJECT (mainwindow), "counter", g_strdup ("0"));
  g_object_set_data (G_OBJECT (mainwindow), "level", g_strdup (""));
  g_object_set_data (G_OBJECT (mainwindow), "allow_transient", GINT_TO_POINTER (FALSE));

  box = gtk_widget_new (gtk_vbox_get_type (),
			"GtkWidget::parent", mainwindow,
			NULL);

  g_object_connect (gtk_widget_new (gtk_button_get_type (),
				    "GtkButton::label", "Create "
#ifdef MODAL
							"Modal "
#endif
							"Child",
				    "GtkWidget::parent", box,
				    NULL),
		    "signal::clicked", add_button_clicked, mainwindow,
		    NULL);

  gtk_widget_show_all (mainwindow);
}

int main (int argc, char **argv)
{
  gtk_init (&argc, &argv);

  create_main_window ();
  gtk_main ();

  return 0;
}
