/* gcc -o window-reparent -g window-reparent.c $(pkg-config --cflags --libs gtk+-3.0) */

#include <gtk/gtk.h>

static GtkWidget *parent_window;
static GtkWidget *parent_box;

static void
button_click_cb (GtkWidget *widget, gpointer data)
{
  GdkWindow *gdkwindow;
  GtkWidget *header_bar;
  GtkWidget *window;

  header_bar = gtk_header_bar_new ();
  gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header_bar), TRUE);
  gtk_header_bar_set_title (GTK_HEADER_BAR (header_bar), "Another window");

  window = g_object_new (GTK_TYPE_WINDOW, NULL);
  gtk_window_set_titlebar (GTK_WINDOW (window), header_bar);
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 100);
  gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
  gtk_window_set_decorated (GTK_WINDOW (window), TRUE);

  gdkwindow = gtk_widget_get_window (parent_box);
  gtk_widget_set_parent_window (window, gdkwindow);

  gtk_widget_show_all (window);
  gtk_box_pack_start (GTK_BOX (parent_box), window, TRUE, TRUE, 0);
}

gint
main (gint argc, gchar *argv[])
{
  GtkWidget *header_bar;
  GtkWidget *button;

  gtk_init (NULL, NULL);

  /* Second window */
  header_bar = gtk_header_bar_new ();
  gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header_bar), TRUE);

  parent_window = g_object_new (GTK_TYPE_WINDOW, NULL);
  gtk_window_set_titlebar (GTK_WINDOW (parent_window), header_bar);
  g_signal_connect (parent_window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (parent_window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  parent_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (parent_window), parent_box);

  button = gtk_button_new_with_label ("Embed a new window!");
  g_signal_connect (button, "clicked", G_CALLBACK (button_click_cb), NULL);
  gtk_box_pack_start (GTK_BOX (parent_box), button, TRUE, TRUE, 0);

  gtk_widget_show_all (parent_window);

  gtk_main ();

  return 0;
}
