#include <gtk/gtk.h>

//#define USE_STYLE_CLASS 1


#define CLASS "dummy"
#define CSS "."CLASS",#"CLASS"{ color: red; }"


static void
toggle_style_by_name (GtkWidget    *widget,
                      const gchar  *name)
{
#ifdef USE_STYLE_CLASS /* works */
  GtkStyleContext *ctx = gtk_widget_get_style_context (widget);

  if (gtk_style_context_has_class (ctx, name))
    gtk_style_context_remove_class (ctx, name);
  else
    gtk_style_context_add_class (ctx, name);

  g_debug ("widget class = %s",
           gtk_style_context_has_class (ctx, name) ? name : NULL);
#else /* broken */
  if (g_strcmp0 (gtk_widget_get_name (widget), name) == 0)
    gtk_widget_set_name (widget, NULL);
  else
    gtk_widget_set_name (widget, name);
  
  g_debug ("widget name = %s", gtk_widget_get_name (widget));
#endif
}

static gboolean
add_remove_style (gpointer widget)
{
  toggle_style_by_name (widget, CLASS);

  return G_SOURCE_CONTINUE;
}

int
main (int     argc,
      char  **argv)
{
  GtkWidget      *window;
  gchar          *text;
  GtkWidget      *label;
  GtkCssProvider *provider;
  GError         *error = NULL;
  
  gtk_init (&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  
  text = g_strdup_printf ("Hello from GTK %d.%d.%d!",
                          gtk_get_major_version (),
                          gtk_get_minor_version (),
                          gtk_get_micro_version ());
  label = gtk_label_new (text);
  g_free (text);
  gtk_container_add (GTK_CONTAINER (window), label);
  toggle_style_by_name (label, CLASS);

  gtk_widget_show_all (window);
  
  provider = gtk_css_provider_new ();
  if (! gtk_css_provider_load_from_data (provider, CSS, sizeof (CSS), &error))
    {
      g_error ("Failed to load CSS style: %s", error->message);
      g_error_free (error);
    }
  else
    {
      gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (window),
                                                 GTK_STYLE_PROVIDER (provider),
                                                 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    }
  
  g_timeout_add_seconds (1, add_remove_style, label);
  
  gtk_main ();
  
  return 0;
}
