/* Test case, bug 756042 - GtkAssistant: Segfault after g_object_run_dispose(page)
 *
 * Most of the code has been copied from gtk+/tests/testassistant.c.
 */

#include <gtk/gtk.h>

static GtkWidget*
get_test_page (const gchar *text)
{
  return gtk_label_new (text);
}

static void
cancel_callback (GtkWidget *widget)
{
  g_print ("cancel\n");

  gtk_widget_hide (widget);
}

static void
close_callback (GtkWidget *widget)
{
  g_print ("close\n");

  gtk_widget_hide (widget);
}

static void
apply_callback (GtkWidget *widget)
{
  g_print ("apply\n");
}

static gboolean
progress_timeout (GtkWidget *assistant)
{
  GtkWidget *progress;
  gint current_page;
  gdouble value;

  current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (assistant));
  progress = gtk_assistant_get_nth_page (GTK_ASSISTANT (assistant), current_page);

  value  = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress));
  value += 0.1;
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), value);

  if (value >= 1.0)
    {
      gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), progress, TRUE);
      return FALSE;
    }

  return TRUE;
}

static void
prepare_callback (GtkWidget *widget, GtkWidget *page)
{
  if (GTK_IS_LABEL (page))
    g_print ("prepare: %s\n", gtk_label_get_text (GTK_LABEL (page)));
  else if (gtk_assistant_get_page_type (GTK_ASSISTANT (widget), page) == GTK_ASSISTANT_PAGE_PROGRESS)
    {
      gtk_assistant_set_page_complete (GTK_ASSISTANT (widget), page, FALSE);
      gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (page), 0.0);
      gdk_threads_add_timeout (300, (GSourceFunc) progress_timeout, widget);
    }
  else
    g_print ("prepare: %d\n", gtk_assistant_get_current_page (GTK_ASSISTANT (widget)));
}

static GtkWidget *assistant = NULL;
static GtkWidget *page1 = NULL;

static void
create_simple_assistant (GtkWidget *widget)
{
  if (!assistant)
    {
      GtkWidget *page;

      assistant = gtk_assistant_new ();
      gtk_window_set_default_size (GTK_WINDOW (assistant), 400, 300);

      g_signal_connect (G_OBJECT (assistant), "cancel",
			G_CALLBACK (cancel_callback), NULL);
      g_signal_connect (G_OBJECT (assistant), "close",
			G_CALLBACK (close_callback), NULL);
      g_signal_connect (G_OBJECT (assistant), "apply",
			G_CALLBACK (apply_callback), NULL);
      g_signal_connect (G_OBJECT (assistant), "prepare",
			G_CALLBACK (prepare_callback), NULL);

      page = get_test_page ("Page 1");
      page1 = page;
      gtk_widget_show (page);
      gtk_assistant_append_page (GTK_ASSISTANT (assistant), page);
      gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), page, "Page 1");
      gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), page, TRUE);

      page = get_test_page ("Page 2");
      gtk_widget_show (page);
      gtk_assistant_append_page (GTK_ASSISTANT (assistant), page);
      gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), page, "Page 2");
      gtk_assistant_set_page_type  (GTK_ASSISTANT (assistant), page, GTK_ASSISTANT_PAGE_CONFIRM);
      gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), page, TRUE);
    }

  if (!gtk_widget_get_visible (assistant))
    gtk_widget_show (assistant);
  else
    {
      gtk_widget_destroy (assistant);
      assistant = NULL;
    }
}

static void
delete_page_1 (GtkWidget *widget)
{
  if (page1)
    g_object_run_dispose (G_OBJECT (page1));
  page1 = NULL;
}

struct {
  gchar *text;
  void  (*func) (GtkWidget *widget);
} buttons[] =
  {
    { "simple assistant", create_simple_assistant },
    { "delete page 1",    delete_page_1 },
  };

int
main (int argc, gchar *argv[])
{
  GtkWidget *window, *box, *button;
  gint i;

  gtk_init (&argc, &argv);

  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  g_signal_connect (G_OBJECT (window), "destroy",
		    G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (G_OBJECT (window), "delete-event",
		    G_CALLBACK (gtk_false), NULL);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  gtk_container_add (GTK_CONTAINER (window), box);

  for (i = 0; i < G_N_ELEMENTS (buttons); i++)
    {
      button = gtk_button_new_with_label (buttons[i].text);

      if (buttons[i].func)
	g_signal_connect (G_OBJECT (button), "clicked",
			  G_CALLBACK (buttons[i].func), NULL);

      gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
    }

  gtk_widget_show_all (window);
  gtk_main ();

  if (assistant)
    gtk_widget_destroy (assistant);

  return 0;
}
