#include <gtk/gtk.h>
#include <clutter-gtk/clutter-gtk.h>
#include <clutter-gst/clutter-gst.h>

#define USE_HEDER_BAR TRUE

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  gint i;

  window = gtk_application_window_new (app);
  gtk_widget_set_size_request (window, 640, 480);
  if (USE_HEDER_BAR == FALSE)
    gtk_window_set_title (GTK_WINDOW (window), "Hello GTK Clutter world!");
  else {
    GtkWidget *header_bar;

    header_bar = gtk_header_bar_new ();
    gtk_header_bar_set_title (GTK_HEADER_BAR (header_bar), "Hello GTK Clutter world!");
    gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header_bar), TRUE);
    gtk_window_set_titlebar (GTK_WINDOW (window), header_bar);
  }

  grid = gtk_grid_new ();
  gtk_grid_set_row_spacing (GTK_GRID (grid), 18);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 18);
  gtk_container_add (GTK_CONTAINER (window), grid);

  for (i = 0; i < 4; i++) {
    GtkWidget *clutter_em;

    clutter_em = gtk_clutter_embed_new ();
    gtk_grid_attach (GTK_GRID (grid), clutter_em, i % 2, i /2, 1, 1);
  }

  gtk_widget_show_all (window);
}

int
main (int argc, char **argv)
{
  GtkApplication *app;
  int status;

  if (USE_HEDER_BAR) {
    /* Interchange the inits, and I have no clue why...
       if you don't do it, you get a great core dump! */

    /* If you comment this init, all goes fine */
    if (clutter_gst_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
      g_error ("Failed to initialize GST Clutter.\n");
    if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
      g_error ("Cannot init GTK Clutter\n");
  } else {
    if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
      g_error ("Cannot init GTK Clutter\n");
    if (clutter_gst_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
      g_error ("Failed to initialize GST Clutter.\n");
  }

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
  return status;
}
