diff options
author | Owen Taylor <otaylor@redhat.com> | 2002-09-24 21:03:58 +0000 |
---|---|---|
committer | Owen Taylor <otaylor@src.gnome.org> | 2002-09-24 21:03:58 +0000 |
commit | 15896b962dfa5c959565ed6be36db6a4f1721273 (patch) | |
tree | 2d1e950f1e05a51d5abd3fc088f75d8aeb86d9cb /gtk/gtkiconfactory.c | |
parent | 73413ba70ec92371772af749f04cd8f9b238e96e (diff) | |
download | gtk+-15896b962dfa5c959565ed6be36db6a4f1721273.tar.gz |
Make icon sizes configurable (#70648, slightly based on a patch from Bill
Tue Sep 24 16:40:14 2002 Owen Taylor <otaylor@redhat.com>
Make icon sizes configurable (#70648, slightly based
on a patch from Bill Haneman, Brian Cameron)
* gtk/gtkiconfactory.[ch]: Add
gtk_icon_size_lookup_for_settings().
* gtk/gtksettings.c: Add gtk-icon-sizes XSETTING
* gtk/gtkiconfactory.c: Make icon sizes per-GtkSettings.
* gtk/gtkstyle.c: Use gtk_icon_size_lookup_for_settings()
* gtk/gtkrc.[ch]: Privately export _gtk_rc_reset_styles()
to force all the widgets to recompute their appearance.
Diffstat (limited to 'gtk/gtkiconfactory.c')
-rw-r--r-- | gtk/gtkiconfactory.c | 425 |
1 files changed, 381 insertions, 44 deletions
diff --git a/gtk/gtkiconfactory.c b/gtk/gtkiconfactory.c index 63a5d1e5d6..3047a688bf 100644 --- a/gtk/gtkiconfactory.c +++ b/gtk/gtkiconfactory.c @@ -24,8 +24,11 @@ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. */ +#include <pango/pango-utils.h> /* For pango_scan_* */ #include "gtkiconfactory.h" #include "stock-icons/gtkstockpixbufs.h" +#include "gtkdebug.h" +#include "gtksettings.h" #include "gtkstock.h" #include "gtkintl.h" #include <stdlib.h> @@ -63,6 +66,10 @@ static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass); static void gtk_icon_factory_finalize (GObject *object); static void get_default_icons (GtkIconFactory *icon_factory); +static GtkIconSize icon_size_register_intern (const gchar *name, + gint width, + gint height); + GType gtk_icon_factory_get_type (void) { @@ -800,7 +807,9 @@ get_default_icons (GtkIconFactory *factory) add_unsized (factory, stock_zoom_out_24, GTK_STOCK_ZOOM_OUT); } -/* Sizes */ +/************************************************************ + * Icon size handling * + ************************************************************/ typedef struct _IconSize IconSize; @@ -821,6 +830,14 @@ struct _IconAlias gint target; }; +typedef struct _SettingsIconSize SettingsIconSize; + +struct _SettingsIconSize +{ + gint width; + gint height; +}; + static GHashTable *icon_aliases = NULL; static IconSize *icon_sizes = NULL; static gint icon_sizes_allocated = 0; @@ -896,13 +913,293 @@ init_icon_sizes (void) } } +static void +free_settings_sizes (gpointer data) +{ + g_array_free (data, TRUE); +} + +static GArray * +get_settings_sizes (GtkSettings *settings, + gboolean *created) +{ + GArray *settings_sizes; + static GQuark sizes_quark = 0; + + if (!sizes_quark) + sizes_quark = g_quark_from_static_string ("gtk-icon-sizes"); + + settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark); + if (!settings_sizes) + { + settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize)); + g_object_set_qdata_full (G_OBJECT (settings), sizes_quark, + settings_sizes, free_settings_sizes); + if (created) + *created = TRUE; + } + + return settings_sizes; +} + +static void +icon_size_set_for_settings (GtkSettings *settings, + const gchar *size_name, + gint width, + gint height) +{ + GtkIconSize size; + GArray *settings_sizes; + SettingsIconSize *settings_size; + + g_return_if_fail (size_name != NULL); + + size = gtk_icon_size_from_name (size_name); + if (size == GTK_ICON_SIZE_INVALID) + /* Reserve a place */ + size = icon_size_register_intern (size_name, -1, -1); + + settings_sizes = get_settings_sizes (settings, NULL); + if (size >= settings_sizes->len) + { + SettingsIconSize unset = { -1, -1 }; + gint i; + + for (i = settings_sizes->len; i <= size; i++) + g_array_append_val (settings_sizes, unset); + } + + settings_size = &g_array_index (settings_sizes, SettingsIconSize, size); + + settings_size->width = width; + settings_size->height = height; +} + +/* Like pango_parse_word, but accept - as well + */ +static gboolean +scan_icon_size_name (const char **pos, GString *out) +{ + const char *p = *pos; + + while (g_ascii_isspace (*p)) + p++; + + if (!((*p >= 'A' && *p <= 'Z') || + (*p >= 'a' && *p <= 'z') || + *p == '_' || *p == '-')) + return FALSE; + + g_string_truncate (out, 0); + g_string_append_c (out, *p); + p++; + + while ((*p >= 'A' && *p <= 'Z') || + (*p >= 'a' && *p <= 'z') || + (*p >= '0' && *p <= '9') || + *p == '_' || *p == '-') + { + g_string_append_c (out, *p); + p++; + } + + *pos = p; + + return TRUE; +} + +static void +icon_size_setting_parse (GtkSettings *settings, + const gchar *icon_size_string) +{ + GString *name_buf = g_string_new (NULL); + const gchar *p = icon_size_string; + + while (pango_skip_space (&p)) + { + gint width, height; + + if (!scan_icon_size_name (&p, name_buf)) + goto err; + + if (!pango_skip_space (&p)) + goto err; + + if (*p != '=') + goto err; + + p++; + + if (!pango_scan_int (&p, &width)) + goto err; + + if (!pango_skip_space (&p)) + goto err; + + if (*p != ',') + goto err; + + p++; + + if (!pango_scan_int (&p, &height)) + goto err; + + if (width > 0 && height > 0) + { + icon_size_set_for_settings (settings, name_buf->str, + width, height); + } + else + { + g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height); + } + + pango_skip_space (&p); + if (*p == '\0') + break; + if (*p == ':') + p++; + else + goto err; + } + + g_string_free (name_buf, TRUE); + return; + + err: + g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string); + g_string_free (name_buf, TRUE); +} + +static void +icon_size_set_all_from_settings (GtkSettings *settings) +{ + GArray *settings_sizes; + gchar *icon_size_string; + + /* Reset old settings */ + settings_sizes = get_settings_sizes (settings, NULL); + g_array_set_size (settings_sizes, 0); + + g_object_get (settings, + "gtk-icon-sizes", &icon_size_string, + NULL); + + if (icon_size_string) + { + icon_size_setting_parse (settings, icon_size_string); + g_free (icon_size_string); + } +} + +static void +icon_size_settings_changed (GtkSettings *settings, + GParamSpec *pspec) +{ + icon_size_set_all_from_settings (settings); + + _gtk_rc_reset_styles (settings); +} + +static void +icon_sizes_init_for_settings (GtkSettings *settings) +{ + g_signal_connect (settings, + "notify::gtk-icon-sizes", + G_CALLBACK (icon_size_settings_changed), + NULL); + + icon_size_set_all_from_settings (settings); +} + +gboolean +icon_size_lookup_intern (GtkSettings *settings, + GtkIconSize size, + gint *widthp, + gint *heightp) +{ + GArray *settings_sizes; + gint width_for_settings = -1; + gint height_for_settings = -1; + + init_icon_sizes (); + + if (size >= icon_sizes_used || icon_sizes[size].width < 0) + return FALSE; + + if (size == GTK_ICON_SIZE_INVALID) + return FALSE; + + if (settings) + { + gboolean initial = FALSE; + + settings_sizes = get_settings_sizes (settings, &initial); + if (initial) + icon_sizes_init_for_settings (settings); + + if (size < settings_sizes->len) + { + SettingsIconSize *settings_size; + + settings_size = &g_array_index (settings_sizes, SettingsIconSize, size); + + width_for_settings = settings_size->width; + height_for_settings = settings_size->height; + } + } + + if (widthp) + *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width; + + if (heightp) + *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height; + + return TRUE; +} + +/** + * gtk_icon_size_lookup_for_settings: + * @GtkSettings: a #GtkSettings object, used to determine + * which set of user preferences to used. + * @size: an icon size + * @width: location to store icon width + * @height: location to store icon height + * + * Obtains the pixel size of a semantic icon size, possibly + * modified by user preferences for a particular #GtkSettings + * Normally @size would be + * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function + * isn't normally needed, gtk_widget_render_icon() is the usual + * way to get an icon for rendering, then just look at the size of + * the rendered pixbuf. The rendered pixbuf may not even correspond to + * the width/height returned by gtk_icon_size_lookup(), because themes + * are free to render the pixbuf however they like, including changing + * the usual size. + * + * Return value: %TRUE if @size was a valid size + **/ +gboolean +gtk_icon_size_lookup_for_settings (GtkSettings *settings, + GtkIconSize size, + gint *widthp, + gint *heightp) +{ + g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE); + + return icon_size_lookup_intern (settings, size, widthp, heightp); +} + /** * gtk_icon_size_lookup: * @size: an icon size * @width: location to store icon width * @height: location to store icon height * - * Obtains the pixel size of a semantic icon size, normally @size would be + * Obtains the pixel size of a semantic icon size, possibly + * modified by user preferences for the default #GtkSettings. + * (See gtk_icon_size_lookup_for_settings().) + * Normally @size would be * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function * isn't normally needed, gtk_widget_render_icon() is the usual * way to get an icon for rendering, then just look at the size of @@ -918,21 +1215,55 @@ gtk_icon_size_lookup (GtkIconSize size, gint *widthp, gint *heightp) { - init_icon_sizes (); + GTK_NOTE (MULTIHEAD, + g_warning ("gtk_icon_size_lookup ()) is not multihead safe")); - if (size >= icon_sizes_used) - return FALSE; + return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (), + size, widthp, heightp); +} - if (size == GTK_ICON_SIZE_INVALID) - return FALSE; +static GtkIconSize +icon_size_register_intern (const gchar *name, + gint width, + gint height) +{ + IconAlias *old_alias; + GtkIconSize size; - if (widthp) - *widthp = icon_sizes[size].width; + init_icon_sizes (); - if (heightp) - *heightp = icon_sizes[size].height; + old_alias = g_hash_table_lookup (icon_aliases, name); + if (old_alias && icon_sizes[old_alias->target].width > 0) + { + g_warning ("Icon size name '%s' already exists", name); + return GTK_ICON_SIZE_INVALID; + } - return TRUE; + if (old_alias) + { + size = old_alias->target; + } + else + { + if (icon_sizes_used == icon_sizes_allocated) + { + icon_sizes_allocated *= 2; + icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated); + } + + size = icon_sizes_used++; + + /* alias to self. */ + gtk_icon_size_register_alias (name, size); + + icon_sizes[size].size = size; + icon_sizes[size].name = g_strdup (name); + } + + icon_sizes[size].width = width; + icon_sizes[size].height = height; + + return size; } /** @@ -956,25 +1287,7 @@ gtk_icon_size_register (const gchar *name, g_return_val_if_fail (width > 0, 0); g_return_val_if_fail (height > 0, 0); - init_icon_sizes (); - - if (icon_sizes_used == icon_sizes_allocated) - { - icon_sizes_allocated *= 2; - icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated); - } - - icon_sizes[icon_sizes_used].size = icon_sizes_used; - icon_sizes[icon_sizes_used].name = g_strdup (name); - icon_sizes[icon_sizes_used].width = width; - icon_sizes[icon_sizes_used].height = height; - - ++icon_sizes_used; - - /* alias to self. */ - gtk_icon_size_register_alias (name, icon_sizes_used - 1); - - return icon_sizes_used - 1; + return icon_size_register_intern (name, width, height); } /** @@ -997,17 +1310,29 @@ gtk_icon_size_register_alias (const gchar *alias, init_icon_sizes (); - if (g_hash_table_lookup (icon_aliases, alias)) - g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias); - - if (!gtk_icon_size_lookup (target, NULL, NULL)) + if (!icon_size_lookup_intern (NULL, target, NULL, NULL)) g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target); - - ia = g_new (IconAlias, 1); - ia->name = g_strdup (alias); - ia->target = target; - g_hash_table_insert (icon_aliases, ia->name, ia); + ia = g_hash_table_lookup (icon_aliases, alias); + if (ia) + { + if (icon_sizes[ia->target].width > 0) + { + g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias); + return; + } + + ia->target = target; + } + + if (!ia) + { + ia = g_new (IconAlias, 1); + ia->name = g_strdup (alias); + ia->target = target; + + g_hash_table_insert (icon_aliases, ia->name, ia); + } } /** @@ -1026,7 +1351,7 @@ gtk_icon_size_from_name (const gchar *name) ia = g_hash_table_lookup (icon_aliases, name); - if (ia) + if (ia && icon_sizes[ia->target].width > 0) return ia->target; else return GTK_ICON_SIZE_INVALID; @@ -1049,6 +1374,8 @@ gtk_icon_size_get_name (GtkIconSize size) return icon_sizes[size].name; } +/************************************************************/ + /* Icon Set */ @@ -1253,17 +1580,27 @@ gtk_icon_set_copy (GtkIconSet *icon_set) return copy; } - static gboolean sizes_equivalent (GtkIconSize lhs, GtkIconSize rhs) { + /* We used to consider sizes equivalent if they were + * the same pixel size, but we don't have the GtkSettings + * here, so we can't do that. Plus, it's not clear that + * it is right... it was just a workaround for the fact + * that we register icons by logical size, not pixel size. + */ +#if 1 + return lhs == rhs; +#else + gint r_w, r_h, l_w, l_h; - gtk_icon_size_lookup (rhs, &r_w, &r_h); - gtk_icon_size_lookup (lhs, &l_w, &l_h); + icon_size_lookup_intern (NULL, rhs, &r_w, &r_h); + icon_size_lookup_intern (NULL, lhs, &l_w, &l_h); return r_w == l_w && r_h == l_h; +#endif } static GtkIconSource* |