/* gcc wl.c -g -O0 -Wno-deprecated-declarations -o wl `pkg-config --cflags --libs gtk+-3.0` && ./wl */

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

static void
create_new_and_close_subwindow_cb (GtkButton *button,
				   gpointer user_data)
{
	GtkWidget *subwindow = user_data;
	GtkWidget *window, *container, *widget;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "sub-sub-window");
	container = gtk_box_new (GTK_ORIENTATION_VERTICAL, TRUE);
	widget = gtk_label_new ("This is a new window, which closed 'sub-window'");
	gtk_container_add (GTK_CONTAINER (container), widget);
	gtk_widget_show_all (container);
	gtk_container_add (GTK_CONTAINER (window), container);

	gtk_widget_show (window);

	gtk_widget_destroy (subwindow);
}

static void
create_new_subwindow_cb (GtkButton *button,
			 gpointer user_data)
{
	GtkWidget *window, *container, *widget;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "sub-window");
	container = gtk_box_new (GTK_ORIENTATION_VERTICAL, TRUE);
	widget = gtk_button_new_with_mnemonic ("Create new and _close this");
	g_signal_connect (widget, "clicked", G_CALLBACK (create_new_and_close_subwindow_cb), window);
	gtk_container_add (GTK_CONTAINER (container), widget);
	gtk_widget_show_all (container);
	gtk_container_add (GTK_CONTAINER (window), container);

	gtk_widget_show (window);
}

int
main (int argc,
      char *argv[])
{
	GMainLoop *loop;
	GtkWidget *window, *container, *widget;

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "main window");
	container = gtk_box_new (GTK_ORIENTATION_VERTICAL, TRUE);

	widget = gtk_button_new_with_mnemonic ("Create _new window");
	g_signal_connect (widget, "clicked", G_CALLBACK (create_new_subwindow_cb), NULL);
	gtk_container_add (GTK_CONTAINER (container), widget);

	widget = gtk_label_new (
		"Steps:\n"
		"1) click 'Create new window' above\n"
		"   - The newly created window is shown above this\n"
		"2) click 'Create new and close this' in the new window\n"
		"   - It will show a new window just with a label and will\n"
		"   close the window from step 1)\n"
		"\n"
		"The window from step 2) has focus, but it's under this\n"
		"window when run in Wayland.");
	gtk_container_add (GTK_CONTAINER (container), widget);

	gtk_widget_show_all (container);
	gtk_container_add (GTK_CONTAINER (window), container);

	loop = g_main_loop_new (NULL, FALSE);
	g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_main_loop_quit), loop);

	gtk_widget_show (window);
	gtk_window_maximize (GTK_WINDOW (window));

	g_main_loop_run (loop);

	g_main_loop_unref (loop);

	return 0;
}
