#include <gtk/gtk.h>

GtkApplication *app = NULL;

static void on_app_activate(GApplication* app, void* user_data)
{
  /* Do nothing. */
}

static gboolean on_delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   user_data )
{
   g_assert(app);
   
  /* If you return FALSE in the "delete-event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   */
     
  /* Hide the window. This does not make g_application_run() return, 
   * though gtk_application_add_window()'s is documentation vaguely suggests 
   * that it will. */
  gtk_widget_hide (widget);
    
  /* This makes g_application_run() return: */
  /* gtk_application_remove_window (app, GTK_WINDOW(widget)); */

  return TRUE;
}


int
main (int argc, char **argv)
{
  /* Let us instaniate a GtkWindow before calling g_application_run(). */
  gtk_init (&argc, &argv);
  
  app = gtk_application_new ("org.murrayc.test.gtkapplication",
    G_APPLICATION_HANDLES_OPEN);
  
  /* Avoid the warning about not connecting to the activate signal. */
  g_signal_connect (app, "activate",
		      G_CALLBACK (on_app_activate),
		      NULL);
		      
	GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (window, "delete-event",
		      G_CALLBACK (on_delete_event), NULL);
	gtk_widget_show (window);
	gtk_application_add_window (app, GTK_WINDOW (window));

  const int status = g_application_run (G_APPLICATION (app), argc, argv);
  
  //We never get this far because g_application_run() does not really respond 
  //to the window closing.
  gtk_widget_destroy (window);
  g_object_unref (app);

  return status;
}
