#include <stdio.h>
#include <gtk/gtk.h>

static void
removed (GtkNotebook *notebook,
  GtkWidget   *child,
  guint        page_num,
  gpointer     user_data)
{
  if (gtk_notebook_get_n_pages (notebook) == 0)
    {
      gtk_widget_destroy (GTK_WIDGET (user_data));
    }
}

void
create_window (const gchar *title) {
  GtkWidget *window, *notebook, *child;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), title);
  gtk_widget_set_size_request (window, 320, 240);

  notebook = gtk_notebook_new ();
  g_signal_connect (notebook, "page-removed",
    G_CALLBACK (removed), window);
  gtk_notebook_set_group (GTK_NOTEBOOK (notebook), "badgers");
  gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
  gtk_container_add (GTK_CONTAINER (window), notebook);

  child = gtk_label_new (title);

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
    child, gtk_label_new (title));
  gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook),
    child, TRUE);

  gtk_widget_show_all (window);
}

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

  create_window("Misc");
  create_window("Misc");

  gtk_main ();

  return 0;
}
