/**
 * Compile with gcc main.c `pkg-config --cflags --libs gtk+-3.0` -o sample
 */

/* License CC0 in dedication of Public Domain */

#include <gtk/gtk.h>


GtkWidget* button = NULL;
GtkWidget* checkbox = NULL;
GtkWidget* stack = NULL;
GtkWidget* box = NULL;
GtkWidget* switcher = NULL;


gboolean
on_delete (GtkWidget *widget,
           GdkEvent  *event,
           gpointer   user_data)
{
  // this is the gist of the bug. Not removing these cause no problem.
  gtk_container_remove(GTK_CONTAINER(box), switcher);
  gtk_container_remove(GTK_CONTAINER(stack), checkbox);
  gtk_container_remove(GTK_CONTAINER(stack), button);

  gtk_widget_destroy(widget);
  return TRUE;
}


int main (int argc, char *argv[])
{
//  GtkApplication *app = gtk_application_new("org.gtkmm.example", G_APPLICATION_FLAGS_NONE);
  gtk_init (&argc, &argv);

  // BEGIN
  GtkWidget *helloworld = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect (helloworld,
                    "delete-event",
                    G_CALLBACK (on_delete),
                    NULL);
  g_signal_connect (helloworld, "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

  gtk_container_set_border_width(GTK_CONTAINER(helloworld), 10);

  button = gtk_button_new_with_label("Hello World");
  checkbox = gtk_check_button_new_with_label("Good bye");

  box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add(GTK_CONTAINER(helloworld), box);

  switcher = gtk_stack_switcher_new();
  gtk_box_pack_start(GTK_BOX(box), switcher, TRUE, FALSE, 0);

  stack = gtk_stack_new();
  gtk_box_pack_end(GTK_BOX(box), stack, TRUE, FALSE, 0);

  gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(switcher), GTK_STACK(stack));

  gtk_stack_add_titled(GTK_STACK(stack), button, "Hello", "Hello");
  gtk_stack_add_titled(GTK_STACK(stack), checkbox, "bye", "Good bye");

  gtk_widget_show_all(helloworld);
  // END

  //Shows the window and returns when it is closed.
//  gtk_application_add_window(app, GTK_WINDOW(helloworld));

//  return g_application_run(G_APPLICATION(helloworld), argc, argv);
  gtk_main();
  return 0;
}

