]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcssprovider.c
docs: Add gtk-doc comment for GtkTreeRowReference
[~andy/gtk] / gtk / gtkcssprovider.c
index 483ea58c80a984e074f8092501716c9eaa0d2474..68bd3e5e8e655e5b8f6186001417033cbabfe01d 100644 (file)
 
 #include "gtkbitmaskprivate.h"
 #include "gtkcssarrayvalueprivate.h"
+#include "gtkcsscolorvalueprivate.h"
 #include "gtkcsskeyframesprivate.h"
 #include "gtkcssparserprivate.h"
 #include "gtkcsssectionprivate.h"
 #include "gtkcssselectorprivate.h"
 #include "gtkcssshorthandpropertyprivate.h"
 #include "gtkcssstylefuncsprivate.h"
-#include "gtksymboliccolor.h"
 #include "gtkstyleprovider.h"
 #include "gtkstylecontextprivate.h"
 #include "gtkstylepropertiesprivate.h"
@@ -1063,34 +1063,13 @@ gtk_css_provider_parsing_error (GtkCssProvider  *provider,
                                      0,
                                      TRUE))
     {
-      GFileInfo *info;
-      GFile *file;
-      const char *path;
+      char *s = _gtk_css_section_to_string (section);
 
-      file = gtk_css_section_get_file (section);
-      if (file)
-        {
-          info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
-
-          if (info)
-            path = g_file_info_get_display_name (info);
-          else
-            path = "<broken file>";
-        }
-      else
-        {
-          info = NULL;
-          path = "<data>";
-        }
-
-      g_warning ("Theme parsing error: %s:%u:%u: %s",
-                 path,
-                 gtk_css_section_get_end_line (section) + 1,
-                 gtk_css_section_get_end_position (section),
+      g_warning ("Theme parsing error: %s: %s",
+                 s,
                  error->message);
 
-      if (info)
-        g_object_unref (info);
+      g_free (s);
     }
 }
 
@@ -1424,75 +1403,12 @@ gtk_css_provider_init (GtkCssProvider *css_provider)
 
   priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  (GDestroyNotify) g_free,
-                                                 (GDestroyNotify) gtk_symbolic_color_unref);
+                                                 (GDestroyNotify) _gtk_css_value_unref);
   priv->keyframes = g_hash_table_new_full (g_str_hash, g_str_equal,
                                            (GDestroyNotify) g_free,
                                            (GDestroyNotify) _gtk_css_value_unref);
 }
 
-static void
-css_provider_dump_symbolic_colors (GtkCssProvider     *css_provider,
-                                   GtkStyleProperties *props)
-{
-  GtkCssProviderPrivate *priv;
-  GHashTableIter iter;
-  gpointer key, value;
-
-  priv = css_provider->priv;
-  g_hash_table_iter_init (&iter, priv->symbolic_colors);
-
-  while (g_hash_table_iter_next (&iter, &key, &value))
-    {
-      const gchar *name;
-      GtkSymbolicColor *color;
-
-      name = key;
-      color = value;
-
-      gtk_style_properties_map_color (props, name, color);
-    }
-}
-
-static GtkStyleProperties *
-gtk_css_provider_get_style (GtkStyleProvider *provider,
-                            GtkWidgetPath    *path)
-{
-  GtkCssMatcher matcher;
-  GtkCssProvider *css_provider;
-  GtkCssProviderPrivate *priv;
-  GtkStyleProperties *props;
-  guint i, j;
-
-  css_provider = GTK_CSS_PROVIDER (provider);
-  priv = css_provider->priv;
-  props = gtk_style_properties_new ();
-
-  css_provider_dump_symbolic_colors (css_provider, props);
-  if (_gtk_css_matcher_init (&matcher, path, 0))
-    {
-      for (i = 0; i < priv->rulesets->len; i++)
-        {
-          GtkCssRuleset *ruleset;
-
-          ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
-
-          if (ruleset->styles == NULL)
-            continue;
-
-          if (!gtk_css_ruleset_matches (ruleset, &matcher))
-            continue;
-
-          for (j = 0; j < ruleset->n_styles; j++)
-            _gtk_style_properties_set_property_by_property (props,
-                                                            GTK_CSS_STYLE_PROPERTY (ruleset->styles[i].property),
-                                                            _gtk_css_selector_get_state_flags (ruleset->selector),
-                                                            ruleset->styles[i].value);
-        }
-    }
-
-  return props;
-}
-
 static gboolean
 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
                                      GtkWidgetPath    *path,
@@ -1560,11 +1476,10 @@ gtk_css_provider_get_style_property (GtkStyleProvider *provider,
 static void
 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
 {
-  iface->get_style = gtk_css_provider_get_style;
   iface->get_style_property = gtk_css_provider_get_style_property;
 }
 
-static GtkSymbolicColor *
+static GtkCssValue *
 gtk_css_style_provider_get_color (GtkStyleProviderPrivate *provider,
                                   const char              *name)
 {
@@ -1589,18 +1504,19 @@ gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
 {
   GtkCssProvider *css_provider;
   GtkCssProviderPrivate *priv;
-  int i;
+  GtkCssRuleset *ruleset;
   guint j;
 
   css_provider = GTK_CSS_PROVIDER (provider);
   priv = css_provider->priv;
 
-  for (i = priv->rulesets->len - 1; i >= 0; i--)
-    {
-      GtkCssRuleset *ruleset;
-
-      ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
+  if (priv->rulesets->len == 0)
+    return;
 
+  for (ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, priv->rulesets->len - 1);
+       ruleset >= &g_array_index (priv->rulesets, GtkCssRuleset, 0);
+       ruleset--)
+    {
       if (ruleset->styles == NULL)
         continue;
 
@@ -1624,6 +1540,9 @@ gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
                                ruleset->styles[j].section,
                                ruleset->styles[j].value);
         }
+
+      if (_gtk_bitmask_is_empty (_gtk_css_lookup_get_missing (lookup)))
+        break;
     }
 }
 
@@ -1826,32 +1745,14 @@ gtk_css_provider_propagate_error (GtkCssProvider  *provider,
                                   GError         **propagate_to)
 {
 
-  GFileInfo *info;
-  GFile *file;
-  const char *path;
-
-  file = gtk_css_section_get_file (section);
-  if (file)
-    {
-      info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
-
-      if (info)
-        path = g_file_info_get_display_name (info);
-      else
-        path = "<broken file>";
-    }
-  else
-    {
-      info = NULL;
-      path = "<unknown>";
-    }
+  char *s;
 
   /* don't fail for deprecations */
   if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
     {
-      g_warning ("Theme parsing error: %s:%u:%u: %s", path,
-                 gtk_css_section_get_end_line (section) + 1,
-                 gtk_css_section_get_end_position (section), error->message);
+      s = _gtk_css_section_to_string (section);
+      g_warning ("Theme parsing error: %s: %s", s, error->message);
+      g_free (s);
       return;
     }
 
@@ -1860,12 +1761,12 @@ gtk_css_provider_propagate_error (GtkCssProvider  *provider,
     return;
 
   *propagate_to = g_error_copy (error);
-  g_prefix_error (propagate_to, "%s:%u:%u: ", path,
-                  gtk_css_section_get_end_line (section) + 1,
-                  gtk_css_section_get_end_position (section));
-
-  if (info)
-    g_object_unref (info);
+  if (section)
+    {
+      s = _gtk_css_section_to_string (section);
+      g_prefix_error (propagate_to, "%s", s);
+      g_free (s);
+    }
 }
 
 static gboolean
@@ -1937,7 +1838,7 @@ parse_import (GtkCssScanner *scanner)
 static gboolean
 parse_color_definition (GtkCssScanner *scanner)
 {
-  GtkCssValue *symbolic;
+  GtkCssValue *color;
   char *name;
 
   gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
@@ -1961,8 +1862,8 @@ parse_color_definition (GtkCssScanner *scanner)
       return TRUE;
     }
 
-  symbolic = _gtk_css_symbolic_value_new (scanner->parser);
-  if (symbolic == NULL)
+  color = _gtk_css_color_value_parse (scanner->parser);
+  if (color == NULL)
     {
       g_free (name);
       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
@@ -1973,7 +1874,7 @@ parse_color_definition (GtkCssScanner *scanner)
   if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
     {
       g_free (name);
-      _gtk_css_value_unref (symbolic);
+      _gtk_css_value_unref (color);
       gtk_css_provider_error_literal (scanner->provider,
                                       scanner,
                                       GTK_CSS_PROVIDER_ERROR,
@@ -1985,7 +1886,7 @@ parse_color_definition (GtkCssScanner *scanner)
       return TRUE;
     }
 
-  g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
+  g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, color);
 
   gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
   return TRUE;
@@ -2567,7 +2468,10 @@ gtk_css_provider_load_internal (GtkCssProvider *css_provider,
  * Loads @data into @css_provider, making it clear any previously loaded
  * information.
  *
- * Returns: %TRUE if the data could be loaded.
+ * Returns: %TRUE. The return value is deprecated and %FALSE will only be
+ *     returned for backwards compatibility reasons if an @error is not 
+ *     %NULL and a loading error occured. To track errors while loading
+ *     CSS, connect to the GtkCssProvider::parsing-error signal.
  **/
 gboolean
 gtk_css_provider_load_from_data (GtkCssProvider  *css_provider,
@@ -2612,7 +2516,10 @@ gtk_css_provider_load_from_data (GtkCssProvider  *css_provider,
  * Loads the data contained in @file into @css_provider, making it
  * clear any previously loaded information.
  *
- * Returns: %TRUE if the data could be loaded.
+ * Returns: %TRUE. The return value is deprecated and %FALSE will only be
+ *     returned for backwards compatibility reasons if an @error is not 
+ *     %NULL and a loading error occured. To track errors while loading
+ *     CSS, connect to the GtkCssProvider::parsing-error signal.
  **/
 gboolean
 gtk_css_provider_load_from_file (GtkCssProvider  *css_provider,
@@ -2642,7 +2549,10 @@ gtk_css_provider_load_from_file (GtkCssProvider  *css_provider,
  * Loads the data contained in @path into @css_provider, making it clear
  * any previously loaded information.
  *
- * Returns: %TRUE if the data could be loaded.
+ * Returns: %TRUE. The return value is deprecated and %FALSE will only be
+ *     returned for backwards compatibility reasons if an @error is not 
+ *     %NULL and a loading error occured. To track errors while loading
+ *     CSS, connect to the GtkCssProvider::parsing-error signal.
  **/
 gboolean
 gtk_css_provider_load_from_path (GtkCssProvider  *css_provider,
@@ -2726,91 +2636,70 @@ _gtk_css_provider_get_theme_dir (void)
 }
 
 /**
- * gtk_css_provider_get_named:
+ * _gtk_css_provider_load_named:
+ * @provider: a #GtkCssProvider
  * @name: A theme name
  * @variant: (allow-none): variant to load, for example, "dark", or
  *     %NULL for the default
  *
- * Loads a theme from the usual theme paths
- *
- * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
- *     This memory is owned by GTK+, and you must not free it.
- */
-GtkCssProvider *
-gtk_css_provider_get_named (const gchar *name,
-                            const gchar *variant)
+ * Loads a theme from the usual theme paths. The actual process of
+ * finding the theme might change between releases, but it is
+ * guaranteed that this function uses the same mechanism to load the
+ * theme than GTK uses for loading its own theme.
+ **/
+void
+_gtk_css_provider_load_named (GtkCssProvider *provider,
+                              const gchar    *name,
+                              const gchar    *variant)
 {
-  static GHashTable *themes = NULL;
-  GtkCssProvider *provider;
-  gchar *key;
-
-  if (variant == NULL)
-    key = (gchar *)name;
-  else
-    key = g_strconcat (name, "-", variant, NULL);
-  if (G_UNLIKELY (!themes))
-    themes = g_hash_table_new (g_str_hash, g_str_equal);
+  gchar *subpath, *path;
+  gchar *resource_path;
 
-  provider = g_hash_table_lookup (themes, key);
+  g_return_if_fail (GTK_IS_CSS_PROVIDER (provider));
+  g_return_if_fail (name != NULL);
 
-  if (!provider)
-    {
-      gchar *resource_path = NULL;
+  gtk_css_provider_reset (provider);
 
-      if (variant)
-        resource_path = g_strdup_printf ("/org/gtk/libgtk/%s-%s.css", name, variant);
-      else
-        resource_path = g_strdup_printf ("/org/gtk/libgtk/%s.css", name);
+  /* try loading the resource for the theme. This is mostly meant for built-in
+   * themes.
+   */
+  if (variant)
+    resource_path = g_strdup_printf ("/org/gtk/libgtk/%s-%s.css", name, variant);
+  else
+    resource_path = g_strdup_printf ("/org/gtk/libgtk/%s.css", name);
 
-      if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
-        {
-          provider = gtk_css_provider_new ();
-          gtk_css_provider_load_from_resource (provider, resource_path);
-        }
+  if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
+    {
+      gtk_css_provider_load_from_resource (provider, resource_path);
       g_free (resource_path);
+      return;
     }
+  g_free (resource_path);
 
-  if (!provider)
-    {
-      gchar *subpath, *path = NULL;
-
-      if (variant)
-        subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
-      else
-        subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
 
-      /* First look in the user's config directory
-       */
-      path = g_build_filename (g_get_user_data_dir (), "themes", name, subpath, NULL);
-      if (!g_file_test (path, G_FILE_TEST_EXISTS))
-        {
-          g_free (path);
-          path = NULL;
-        }
+  /* Next try looking for files in the various theme directories.
+   */
+  if (variant)
+    subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
+  else
+    subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
 
+  /* First look in the user's config directory
+   */
+  path = g_build_filename (g_get_user_data_dir (), "themes", name, subpath, NULL);
+  if (!g_file_test (path, G_FILE_TEST_EXISTS))
+    {
+      g_free (path);
       /* Next look in the user's home directory
        */
-      if (!path)
-        {
-          const gchar *home_dir;
-
-          home_dir = g_get_home_dir ();
-          if (home_dir)
-            {
-              path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
-
-              if (!g_file_test (path, G_FILE_TEST_EXISTS))
-                {
-                  g_free (path);
-                  path = NULL;
-                }
-            }
-        }
-
-      if (!path)
+      path = g_build_filename (g_get_home_dir (), ".themes", name, subpath, NULL);
+      if (!g_file_test (path, G_FILE_TEST_EXISTS))
         {
           gchar *theme_dir;
 
+          g_free (path);
+
+          /* Finally, try in the default theme directory */
           theme_dir = _gtk_css_provider_get_theme_dir ();
           path = g_build_filename (theme_dir, name, subpath, NULL);
           g_free (theme_dir);
@@ -2821,48 +2710,85 @@ gtk_css_provider_get_named (const gchar *name,
               path = NULL;
             }
         }
+    }
 
-      g_free (subpath);
+  g_free (subpath);
 
-      if (path)
-        {
-          char *dir, *resource_file;
-          GResource *resource;
+  if (path)
+    {
+      char *dir, *resource_file;
+      GResource *resource;
 
-          provider = gtk_css_provider_new ();
+      dir = g_path_get_dirname (path);
+      resource_file = g_build_filename (dir, "gtk.gresource", NULL);
+      resource = g_resource_load (resource_file, NULL);
+      g_free (resource_file);
 
-          dir = g_path_get_dirname (path);
-          resource_file = g_build_filename (dir, "gtk.gresource", NULL);
-          resource = g_resource_load (resource_file, NULL);
-          g_free (resource_file);
+      if (resource != NULL)
+        g_resources_register (resource);
 
-          if (resource != NULL)
-            g_resources_register (resource);
+      gtk_css_provider_load_from_path (provider, path, NULL);
 
-          if (!gtk_css_provider_load_from_path (provider, path, NULL))
-            {
-              if (resource != NULL)
-                {
-                  g_resources_unregister (resource);
-                  g_resource_unref (resource);
-                }
-              g_object_unref (provider);
-              provider = NULL;
-            }
-          else
-            {
-              /* Only set this after load success, as load_from_path will clear it */
-              provider->priv->resource = resource;
-              g_hash_table_insert (themes, g_strdup (key), provider);
-            }
+      /* Only set this after load, as load_from_path will clear it */
+      provider->priv->resource = resource;
 
-          g_free (path);
-          g_free (dir);
+      g_free (path);
+      g_free (dir);
+    }
+  else
+    {
+      /* Things failed! Fall back! Fall back! */
+
+      if (variant)
+        {
+          /* If there was a variant, try without */
+          _gtk_css_provider_load_named (provider, name, NULL);
+        }
+      else
+        {
+          /* Worst case, fall back to Raleigh */
+          g_return_if_fail (!g_str_equal (name, "Raleigh")); /* infloop protection */
+          _gtk_css_provider_load_named (provider, "Raleigh", NULL);
         }
     }
+}
 
-  if (key != name)
-    g_free (key);
+/**
+ * gtk_css_provider_get_named:
+ * @name: A theme name
+ * @variant: (allow-none): variant to load, for example, "dark", or
+ *     %NULL for the default
+ *
+ * Loads a theme from the usual theme paths
+ *
+ * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
+ *     This memory is owned by GTK+, and you must not free it.
+ */
+GtkCssProvider *
+gtk_css_provider_get_named (const gchar *name,
+                            const gchar *variant)
+{
+  static GHashTable *themes = NULL;
+  GtkCssProvider *provider;
+  gchar *key;
+
+  if (variant == NULL)
+    key = g_strdup (name);
+  else
+    key = g_strconcat (name, "-", variant, NULL);
+  if (G_UNLIKELY (!themes))
+    themes = g_hash_table_new (g_str_hash, g_str_equal);
+
+  provider = g_hash_table_lookup (themes, key);
+  
+  if (!provider)
+    {
+      provider = gtk_css_provider_new ();
+      _gtk_css_provider_load_named (provider, name, variant);
+      g_hash_table_insert (themes, g_strdup (key), provider);
+    }
+  
+  g_free (key);
 
   return provider;
 }
@@ -2952,7 +2878,6 @@ gtk_css_provider_print_colors (GHashTable *colors,
                                GString    *str)
 {
   GList *keys, *walk;
-  char *s;
 
   keys = g_hash_table_get_keys (colors);
   /* so the output is identical for identical styles */
@@ -2961,14 +2886,12 @@ gtk_css_provider_print_colors (GHashTable *colors,
   for (walk = keys; walk; walk = walk->next)
     {
       const char *name = walk->data;
-      GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
+      GtkCssValue *color = g_hash_table_lookup (colors, (gpointer) name);
 
       g_string_append (str, "@define-color ");
       g_string_append (str, name);
       g_string_append (str, " ");
-      s = gtk_symbolic_color_to_string (symbolic);
-      g_string_append (str, s);
-      g_free (s);
+      _gtk_css_value_print (color, str);
       g_string_append (str, ";\n");
     }