summaryrefslogtreecommitdiff
path: root/gtk/gtkcssprovider.c
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2012-09-17 22:10:59 -0400
committerMatthias Clasen <mclasen@redhat.com>2012-09-17 22:31:25 -0400
commitab3d6a0b0a7a48914a30187c12c8219a8bcc3295 (patch)
tree15d8de3097f9aef3947e60d4930abbf51aa9055e /gtk/gtkcssprovider.c
parent9bd037aafe1d1bfd410314a2ff3703efbdd0fe08 (diff)
downloadgtk+-ab3d6a0b0a7a48914a30187c12c8219a8bcc3295.tar.gz
Revert "Bind the themes to the livecycle of the screen"
This reverts commit 1f5dea9eba4de5a54e9370fe8e4b90e6c0cec200, since it was causeing noticable behaviour changes. Previously, GTK_DATA_PREFIX=/ ./gtk3-demo would start gtk3-demo with the Raleigh theme. With that change, it was starting with no theme at all (i.e. all black).
Diffstat (limited to 'gtk/gtkcssprovider.c')
-rw-r--r--gtk/gtkcssprovider.c309
1 files changed, 122 insertions, 187 deletions
diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c
index ea56a1b5b3..42d8eb5883 100644
--- a/gtk/gtkcssprovider.c
+++ b/gtk/gtkcssprovider.c
@@ -2690,99 +2690,154 @@ _gtk_css_provider_load_from_resource (GtkCssProvider *css_provider,
return result;
}
-static char *
-_find_theme_path (const gchar *name,
- const gchar *variant)
+/**
+ * gtk_css_provider_get_default:
+ *
+ * Returns the provider containing the style settings used as a
+ * fallback for all widgets.
+ *
+ * Returns: (transfer none): The provider used for fallback styling.
+ * This memory is owned by GTK+, and you must not free it.
+ **/
+GtkCssProvider *
+gtk_css_provider_get_default (void)
{
- gchar *subpath;
- gchar *path = NULL;
+ static GtkCssProvider *provider;
- 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))
+ if (G_UNLIKELY (!provider))
{
- g_free (path);
- path = NULL;
+ provider = gtk_css_provider_new ();
}
- /* Next look in the user's home directory
- */
- if (!path)
+ return provider;
+}
+
+gchar *
+_gtk_css_provider_get_theme_dir (void)
+{
+ const gchar *var;
+ gchar *path;
+
+ var = g_getenv ("GTK_DATA_PREFIX");
+
+ if (var)
+ path = g_build_filename (var, "share", "themes", NULL);
+ else
+ path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
+
+ return path;
+}
+
+/**
+ * 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 = (gchar *)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)
{
- const gchar *home_dir;
+ gchar *resource_path = NULL;
- home_dir = g_get_home_dir ();
- if (home_dir)
- {
- path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
+ 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_file_test (path, G_FILE_TEST_EXISTS))
+ if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
+ {
+ provider = gtk_css_provider_new ();
+ if (!_gtk_css_provider_load_from_resource (provider, resource_path))
{
- g_free (path);
- path = NULL;
+ g_object_unref (provider);
+ provider = NULL;
}
}
+ g_free (resource_path);
}
- if (!path)
+ if (!provider)
{
- gchar *theme_dir;
+ gchar *subpath, *path = NULL;
- theme_dir = _gtk_css_provider_get_theme_dir ();
- path = g_build_filename (theme_dir, name, subpath, NULL);
- g_free (theme_dir);
+ 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;
}
- }
- g_free (subpath);
-
- return path;
-}
+ /* Next look in the user's home directory
+ */
+ if (!path)
+ {
+ const gchar *home_dir;
-static gboolean
-_provider_load (GtkCssProvider *provider,
- const gchar *name,
- const gchar *variant)
-{
- gchar *resource_path;
- gboolean loaded = FALSE;
+ home_dir = g_get_home_dir ();
+ if (home_dir)
+ {
+ path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
- g_assert (provider != NULL);
+ if (!g_file_test (path, G_FILE_TEST_EXISTS))
+ {
+ g_free (path);
+ path = NULL;
+ }
+ }
+ }
- 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 (!path)
+ {
+ gchar *theme_dir;
- if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
- {
- loaded = _gtk_css_provider_load_from_resource (provider, resource_path);
- }
- g_free (resource_path);
+ theme_dir = _gtk_css_provider_get_theme_dir ();
+ path = g_build_filename (theme_dir, name, subpath, NULL);
+ g_free (theme_dir);
- if (!loaded)
- {
- char *path;
+ if (!g_file_test (path, G_FILE_TEST_EXISTS))
+ {
+ g_free (path);
+ path = NULL;
+ }
+ }
- path = _find_theme_path (name, variant);
+ g_free (subpath);
if (path)
{
- char *dir;
- char *resource_file;
+ 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);
@@ -2791,154 +2846,34 @@ _provider_load (GtkCssProvider *provider,
if (resource != NULL)
g_resources_register (resource);
- loaded = gtk_css_provider_load_from_path (provider, path, NULL);
- if (!loaded)
+ 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);
}
+ g_free (path);
g_free (dir);
}
- g_free (path);
- }
-
- return loaded;
-}
-
-static void
-destroy_theme_cache (GHashTable *themes)
-{
- g_hash_table_destroy (themes);
-}
-
-/*
- * _gtk_css_provider_get_named_for_screen:
- * @screen: a #GdkScreen.
- * @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_for_screen (GdkScreen *screen,
- const gchar *name,
- const gchar *variant)
-{
- GtkCssProvider *provider;
- GHashTable *themes;
- gchar *key;
-
- g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
-
- themes = g_object_get_data (G_OBJECT (screen), "gtk-themes");
- if (G_UNLIKELY (!themes))
- {
- themes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
- g_object_set_data_full (G_OBJECT (screen),
- I_("gtk-themes"),
- themes,
- (GDestroyNotify)destroy_theme_cache);
}
- if (name == NULL)
- key = g_strdup ("");
- else if (variant == NULL)
- key = g_strdup (name);
- else
- key = g_strconcat (name, "-", variant, NULL);
-
- provider = g_hash_table_lookup (themes, key);
-
- if (!provider)
- {
- gboolean save = TRUE;
-
- provider = gtk_css_provider_new ();
-
- if (name != NULL)
- save = _provider_load (provider, name, variant);
-
- if (save)
- g_hash_table_insert (themes, g_strdup (key), provider);
- }
-
- g_free (key);
+ if (key != name)
+ g_free (key);
return provider;
}
-/**
- * gtk_css_provider_get_default:
- *
- * Returns the provider containing the style settings used as a
- * fallback for all widgets.
- *
- * Returns: (transfer none): The provider used for fallback styling.
- * This memory is owned by GTK+, and you must not free it.
- **/
-GtkCssProvider *
-gtk_css_provider_get_default (void)
-{
- GdkScreen *screen = gdk_screen_get_default ();
-
- if (screen)
- return _gtk_css_provider_get_named_for_screen (screen, NULL, NULL);
- else
- return NULL;
-}
-
-/**
- * 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)
-{
- GdkScreen *screen = gdk_screen_get_default ();
-
- if (screen)
- return _gtk_css_provider_get_named_for_screen (screen, name, variant);
- else
- return NULL;
-}
-
-gchar *
-_gtk_css_provider_get_theme_dir (void)
-{
- const gchar *var;
- gchar *path;
-
- var = g_getenv ("GTK_DATA_PREFIX");
-
- if (var)
- path = g_build_filename (var, "share", "themes", NULL);
- else
- path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
-
- return path;
-}
-
static int
compare_properties (gconstpointer a, gconstpointer b, gpointer style)
{