/* gcc testcss.c `pkg-config --cflags --libs gtk+-3.0` */

#include <gtk/gtk.h>

static void
gtk_css_section_print (const GtkCssSection *section)
{
  GFile *file = gtk_css_section_get_file (section);

  if (file)
    {
      GFileInfo *info;

      info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);

      if (info)
        {
          g_print ("%s", g_file_info_get_display_name (info));
          g_object_unref (info);
        }
      else
        {
          g_print ("<broken file>");
        }
    }
  else
    {
      g_print ("<data>");
    }

  g_print (":%u:%u", 
           gtk_css_section_get_end_line (section) + 1,
           gtk_css_section_get_end_position (section));
}

static void
gtk_style_context_print_interesting_sections (GtkStyleContext *context)
{
  const char *names[] = { "background-color", "background-image", "color" };
  guint i;

  for (i = 0; i < G_N_ELEMENTS (names); i++)
    {
      GtkCssSection *section = gtk_style_context_get_section (context, names[i]);

      g_print ("%s: ", names[i]);
      if (section)
        gtk_css_section_print (section);
      else
        g_print ("---");
      g_print ("\n");
    }
}

int
main (int argc, char **argv)
{
  GtkStyleContext *context;
  GtkWidgetPath *path;

  g_setenv ("GTK_CSS_DEBUG", "1", FALSE);

  gtk_init (&argc, &argv);

  path = gtk_widget_path_new ();
  gtk_widget_path_append_type (path, GTK_TYPE_ENTRY);
  gtk_widget_path_iter_set_object_name (path, -1, "entry");

  context = gtk_style_context_new ();
  gtk_style_context_set_path (context, path);
  gtk_widget_path_free (path);

  gtk_style_context_print_interesting_sections (context);
  g_print ("\n");

  gtk_style_context_save (context);
  gtk_style_context_print_interesting_sections (context);
  g_print ("\n");

  gtk_style_context_restore (context);
  gtk_style_context_print_interesting_sections (context);

  g_object_unref (context);

  return 0;
}
