-static gboolean
-add_color_to_hash (gchar *name,
- GdkColor *color,
- GHashTable *target)
-{
- GdkColor *old;
-
- old = g_hash_table_lookup (target, name);
- if (!old || !gdk_color_equal (old, color))
- {
- g_hash_table_insert (target, g_strdup (name), gdk_color_copy (color));
-
- return TRUE;
- }
-
- return FALSE;
-}
-
-static gboolean
-add_colors_to_hash_from_string (GHashTable *hash,
- const gchar *colors)
-{
- gchar *s, *p, *name;
- GdkColor color;
- gboolean changed = FALSE;
- gchar *copy;
-
- copy = g_strdup (colors);
- s = copy;
- while (s && *s)
- {
- name = s;
- p = strchr (s, ':');
- if (p)
- {
- *p = '\0';
- p++;
- }
- else
- break;
-
- while (*p == ' ')
- p++;
-
- s = p;
- while (*s)
- {
- if (*s == '\n' || *s == ';')
- {
- *s = '\0';
- s++;
- break;
- }
- s++;
- }
-
- if (gdk_color_parse (p, &color))
- changed |= add_color_to_hash (name, &color, hash);
- }
-
- g_free (copy);
-
- return changed;
-}
-
-static gboolean
-update_color_hash (ColorSchemeData *data,
- const gchar *str,
- GtkSettingsSource source)
-{
- gboolean changed = FALSE;
- gint i;
- GHashTable *old_hash;
- GHashTableIter iter;
- gpointer name;
- gpointer color;
-
- if ((str == NULL || *str == '\0') &&
- (data->lastentry[source] == NULL || data->lastentry[source][0] == '\0'))
- return FALSE;
-
- if (str && data->lastentry[source] && strcmp (str, data->lastentry[source]) == 0)
- return FALSE;
-
- /* For the THEME source we merge the values rather than over-writing
- * them, since multiple rc files might define independent sets of colors
- */
- if ((source != GTK_SETTINGS_SOURCE_THEME) &&
- data->tables[source] && g_hash_table_size (data->tables[source]) > 0)
- {
- g_hash_table_unref (data->tables[source]);
- data->tables[source] = NULL;
- changed = TRUE; /* We can't rely on the code below since str might be "" */
- }
-
- if (data->tables[source] == NULL)
- data->tables[source] = g_hash_table_new_full (g_str_hash, g_str_equal,
- g_free,
- (GDestroyNotify) gdk_color_free);
-
- g_free (data->lastentry[source]);
- data->lastentry[source] = g_strdup (str);
-
- changed |= add_colors_to_hash_from_string (data->tables[source], str);
-
- if (!changed)
- return FALSE;
-
- /* Rebuild the merged hash table. */
- if (data->color_hash)
- {
- old_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
- (GDestroyNotify) gdk_color_free);
-
- g_hash_table_iter_init (&iter, data->color_hash);
- while (g_hash_table_iter_next (&iter, &name, &color))
- {
- g_hash_table_insert (old_hash, name, color);
- g_hash_table_iter_steal (&iter);
- }
- }
- else
- {
- old_hash = NULL;
- }
-
- for (i = 0; i <= GTK_SETTINGS_SOURCE_APPLICATION; i++)
- {
- if (data->tables[i])
- g_hash_table_foreach (data->tables[i], (GHFunc) add_color_to_hash,
- data->color_hash);
- }
-
- if (old_hash)
- {
- /* now check if the merged hash has changed */
- changed = FALSE;
- if (g_hash_table_size (old_hash) != g_hash_table_size (data->color_hash))
- changed = TRUE;
- else
- {
- GHashTableIter iter;
- gpointer key, value, new_value;
-
- g_hash_table_iter_init (&iter, old_hash);
- while (g_hash_table_iter_next (&iter, &key, &value))
- {
- new_value = g_hash_table_lookup (data->color_hash, key);
- if (!new_value || !gdk_color_equal (value, new_value))
- {
- changed = TRUE;
- break;
- }
- }
- }
-
- g_hash_table_unref (old_hash);
- }
- else
- changed = TRUE;
-
- return changed;
-}
-
-static void
-merge_color_scheme (GtkSettings *settings,
- const GValue *value,
- GtkSettingsSource source)
-{
- ColorSchemeData *data;
- const gchar *colors;
-
- g_object_freeze_notify (G_OBJECT (settings));
-
- colors = g_value_get_string (value);
-
- settings_update_color_scheme (settings);
-
- data = (ColorSchemeData *) g_object_get_data (G_OBJECT (settings),
- "gtk-color-scheme");
-
- if (update_color_hash (data, colors, source))
- g_object_notify (G_OBJECT (settings), "color-hash");
-
- g_object_thaw_notify (G_OBJECT (settings));
-}
-
-static GHashTable *
-get_color_hash (GtkSettings *settings)
-{
- ColorSchemeData *data;
-
- settings_update_color_scheme (settings);
-
- data = (ColorSchemeData *)g_object_get_data (G_OBJECT (settings),
- "gtk-color-scheme");
-
- return data->color_hash;
-}
-
-static void
-append_color_scheme (gpointer key,
- gpointer value,
- gpointer data)
-{
- gchar *name = (gchar *)key;
- GdkColor *color = (GdkColor *)value;
- GString *string = (GString *)data;
-
- g_string_append_printf (string, "%s: #%04x%04x%04x\n",
- name, color->red, color->green, color->blue);
-}
-
-static gchar *
-get_color_scheme (GtkSettings *settings)
-{
- ColorSchemeData *data;
- GString *string;
-
- settings_update_color_scheme (settings);
-
- data = (ColorSchemeData *) g_object_get_data (G_OBJECT (settings),
- "gtk-color-scheme");
-
- string = g_string_new ("");
-
- g_hash_table_foreach (data->color_hash, append_color_scheme, string);
-
- return g_string_free (string, FALSE);
-}