Commit 905ba2f1 authored by Carlos Garcia Campos's avatar Carlos Garcia Campos Committed by Carlos Garnacho

GtkIconFactory: add gtk_icon_set_render_icon_pixbuf()

and deprecate gtk_icon_set_render_icon().
parent b75972d0
...@@ -6087,6 +6087,7 @@ gtk_icon_set_new ...@@ -6087,6 +6087,7 @@ gtk_icon_set_new
gtk_icon_set_new_from_pixbuf gtk_icon_set_new_from_pixbuf
gtk_icon_set_ref gtk_icon_set_ref
gtk_icon_set_render_icon gtk_icon_set_render_icon
gtk_icon_set_render_icon_pixbuf
gtk_icon_set_unref gtk_icon_set_unref
gtk_icon_size_lookup gtk_icon_size_lookup
gtk_icon_size_lookup_for_settings gtk_icon_size_lookup_for_settings
......
...@@ -1045,6 +1045,7 @@ gtk_icon_set_new ...@@ -1045,6 +1045,7 @@ gtk_icon_set_new
gtk_icon_set_new_from_pixbuf gtk_icon_set_new_from_pixbuf
gtk_icon_set_ref gtk_icon_set_ref
gtk_icon_set_render_icon gtk_icon_set_render_icon
gtk_icon_set_render_icon_pixbuf
gtk_icon_set_unref gtk_icon_set_unref
gtk_icon_size_from_name gtk_icon_size_from_name
gtk_icon_size_get_name gtk_icon_size_get_name
......
...@@ -1083,12 +1083,12 @@ gtk_icon_size_get_name (GtkIconSize size) ...@@ -1083,12 +1083,12 @@ gtk_icon_size_get_name (GtkIconSize size)
static GdkPixbuf *find_in_cache (GtkIconSet *icon_set, static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
GtkStyle *style, GtkStyleContext *style_context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size); GtkIconSize size);
static void add_to_cache (GtkIconSet *icon_set, static void add_to_cache (GtkIconSet *icon_set,
GtkStyle *style, GtkStyleContext *style_context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size, GtkIconSize size,
...@@ -1102,9 +1102,9 @@ static void clear_cache (GtkIconSet *icon_set, ...@@ -1102,9 +1102,9 @@ static void clear_cache (GtkIconSet *icon_set,
static GSList* copy_cache (GtkIconSet *icon_set, static GSList* copy_cache (GtkIconSet *icon_set,
GtkIconSet *copy_recipient); GtkIconSet *copy_recipient);
static void attach_to_style (GtkIconSet *icon_set, static void attach_to_style (GtkIconSet *icon_set,
GtkStyle *style); GtkStyleContext *style_context);
static void detach_from_style (GtkIconSet *icon_set, static void detach_from_style (GtkIconSet *icon_set,
GtkStyle *style); GtkStyleContext *style_context);
static void style_dnotify (gpointer data); static void style_dnotify (gpointer data);
struct _GtkIconSet struct _GtkIconSet
...@@ -1369,12 +1369,8 @@ ensure_filename_pixbuf (GtkIconSet *icon_set, ...@@ -1369,12 +1369,8 @@ ensure_filename_pixbuf (GtkIconSet *icon_set,
static GdkPixbuf * static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource *icon_source, render_icon_name_pixbuf (GtkIconSource *icon_source,
GtkStyle *style, GtkStyleContext *context,
GtkTextDirection direction, GtkIconSize size)
GtkStateType state,
GtkIconSize size,
GtkWidget *widget,
const char *detail)
{ {
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
GdkPixbuf *tmp_pixbuf; GdkPixbuf *tmp_pixbuf;
...@@ -1386,17 +1382,7 @@ render_icon_name_pixbuf (GtkIconSource *icon_source, ...@@ -1386,17 +1382,7 @@ render_icon_name_pixbuf (GtkIconSource *icon_source,
gint *sizes, *s, dist; gint *sizes, *s, dist;
GError *error = NULL; GError *error = NULL;
if (widget && gtk_widget_has_screen (widget)) screen = gtk_style_context_get_screen (context);
screen = gtk_widget_get_screen (widget);
else if (style && style->visual)
screen = gdk_visual_get_screen (style->visual);
else
{
screen = gdk_screen_get_default ();
GTK_NOTE (MULTIHEAD,
g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
}
icon_theme = gtk_icon_theme_get_for_screen (screen); icon_theme = gtk_icon_theme_get_for_screen (screen);
settings = gtk_settings_get_for_screen (screen); settings = gtk_settings_get_for_screen (screen);
...@@ -1487,9 +1473,7 @@ render_icon_name_pixbuf (GtkIconSource *icon_source, ...@@ -1487,9 +1473,7 @@ render_icon_name_pixbuf (GtkIconSource *icon_source,
tmp_source.type = GTK_ICON_SOURCE_PIXBUF; tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
tmp_source.source.pixbuf = tmp_pixbuf; tmp_source.source.pixbuf = tmp_pixbuf;
pixbuf = gtk_style_render_icon (style, &tmp_source, pixbuf = gtk_render_icon_pixbuf (context, &tmp_source, -1);
direction, state, -1,
widget, detail);
if (!pixbuf) if (!pixbuf)
g_warning ("Failed to render icon"); g_warning ("Failed to render icon");
...@@ -1501,12 +1485,10 @@ render_icon_name_pixbuf (GtkIconSource *icon_source, ...@@ -1501,12 +1485,10 @@ render_icon_name_pixbuf (GtkIconSource *icon_source,
static GdkPixbuf * static GdkPixbuf *
find_and_render_icon_source (GtkIconSet *icon_set, find_and_render_icon_source (GtkIconSet *icon_set,
GtkStyle *style, GtkStyleContext *context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size, GtkIconSize size)
GtkWidget *widget,
const char *detail)
{ {
GSList *failed = NULL; GSList *failed = NULL;
GdkPixbuf *pixbuf = NULL; GdkPixbuf *pixbuf = NULL;
...@@ -1535,9 +1517,7 @@ find_and_render_icon_source (GtkIconSet *icon_set, ...@@ -1535,9 +1517,7 @@ find_and_render_icon_source (GtkIconSet *icon_set,
break; break;
/* Fall through */ /* Fall through */
case GTK_ICON_SOURCE_PIXBUF: case GTK_ICON_SOURCE_PIXBUF:
pixbuf = gtk_style_render_icon (style, source, pixbuf = gtk_render_icon_pixbuf (context, source, size);
direction, state, size,
widget, detail);
if (!pixbuf) if (!pixbuf)
{ {
g_warning ("Failed to render icon"); g_warning ("Failed to render icon");
...@@ -1546,9 +1526,7 @@ find_and_render_icon_source (GtkIconSet *icon_set, ...@@ -1546,9 +1526,7 @@ find_and_render_icon_source (GtkIconSet *icon_set,
break; break;
case GTK_ICON_SOURCE_ICON_NAME: case GTK_ICON_SOURCE_ICON_NAME:
case GTK_ICON_SOURCE_STATIC_ICON_NAME: case GTK_ICON_SOURCE_STATIC_ICON_NAME:
pixbuf = render_icon_name_pixbuf (source, style, pixbuf = render_icon_name_pixbuf (source, context, size);
direction, state, size,
widget, detail);
if (!pixbuf) if (!pixbuf)
failed = g_slist_prepend (failed, source); failed = g_slist_prepend (failed, source);
break; break;
...@@ -1565,12 +1543,10 @@ find_and_render_icon_source (GtkIconSet *icon_set, ...@@ -1565,12 +1543,10 @@ find_and_render_icon_source (GtkIconSet *icon_set,
extern GtkIconCache *_builtin_cache; extern GtkIconCache *_builtin_cache;
static GdkPixbuf* static GdkPixbuf*
render_fallback_image (GtkStyle *style, render_fallback_image (GtkStyleContext *context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size, GtkIconSize size)
GtkWidget *widget,
const char *detail)
{ {
/* This icon can be used for any direction/state/size */ /* This icon can be used for any direction/state/size */
static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE); static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
...@@ -1590,13 +1566,69 @@ render_fallback_image (GtkStyle *style, ...@@ -1590,13 +1566,69 @@ render_fallback_image (GtkStyle *style,
g_object_unref (pixbuf); g_object_unref (pixbuf);
} }
return gtk_style_render_icon (style, return gtk_render_icon_pixbuf (context, &fallback_source, size);
&fallback_source, }
direction,
state, /**
size, * gtk_icon_set_render_icon_pixbuf:
widget, * @icon_set: a #GtkIconSet
detail); * @context: a #GtkStyleContext
* @size: (type int): icon size. A size of (GtkIconSize)-1
* means render at the size of the source and don't scale.
*
* Renders an icon using gtk_render_icon_pixbuf(). In most cases,
* gtk_widget_render_icon() is better, since it automatically provides
* most of the arguments from the current widget settings. This
* function never returns %NULL; if the icon can't be rendered
* (perhaps because an image file fails to load), a default "missing
* image" icon will be returned instead.
*
* Return value: (transfer full): a #GdkPixbuf to be displayed
*
* Since: 3.0
*/
GdkPixbuf *
gtk_icon_set_render_icon_pixbuf (GtkIconSet *icon_set,
GtkStyleContext *context,
GtkIconSize size)
{
GdkPixbuf *icon = NULL;
GtkStateFlags flags = 0;
GtkStateType state;
GtkTextDirection direction;
g_return_val_if_fail (icon_set != NULL, NULL);
g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
flags = gtk_style_context_get_state (context);
if (flags & GTK_STATE_FLAG_INSENSITIVE)
state = GTK_STATE_INSENSITIVE;
else if (flags & GTK_STATE_FLAG_PRELIGHT)
state = GTK_STATE_PRELIGHT;
else
state = GTK_STATE_NORMAL;
direction = gtk_style_context_get_direction (context);
if (icon_set->sources)
{
icon = find_in_cache (icon_set, context, direction, state, size);
if (icon)
{
g_object_ref (icon);
return icon;
}
}
if (icon_set->sources)
icon = find_and_render_icon_source (icon_set, context, direction, state, size);
if (icon == NULL)
icon = render_fallback_image (context, direction, state, size);
add_to_cache (icon_set, context, direction, state, size, icon);
return icon;
} }
/** /**
...@@ -1622,6 +1654,8 @@ render_fallback_image (GtkStyle *style, ...@@ -1622,6 +1654,8 @@ render_fallback_image (GtkStyle *style,
* image" icon will be returned instead. * image" icon will be returned instead.
* *
* Return value: (transfer full): a #GdkPixbuf to be displayed * Return value: (transfer full): a #GdkPixbuf to be displayed
*
* Deprecated: 3.0: Use gtk_icon_set_render_icon_pixbuf() instead
*/ */
GdkPixbuf* GdkPixbuf*
gtk_icon_set_render_icon (GtkIconSet *icon_set, gtk_icon_set_render_icon (GtkIconSet *icon_set,
...@@ -1633,34 +1667,47 @@ gtk_icon_set_render_icon (GtkIconSet *icon_set, ...@@ -1633,34 +1667,47 @@ gtk_icon_set_render_icon (GtkIconSet *icon_set,
const char *detail) const char *detail)
{ {
GdkPixbuf *icon; GdkPixbuf *icon;
GtkStyleContext *context = NULL;
GtkStateFlags flags = 0;
g_return_val_if_fail (icon_set != NULL, NULL); g_return_val_if_fail (icon_set != NULL, NULL);
g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL); g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
if (icon_set->sources == NULL) if (style && gtk_style_has_context (style))
return render_fallback_image (style, direction, state, size, widget, detail);
if (detail == NULL)
{ {
icon = find_in_cache (icon_set, style, direction, g_object_get (style, "context", &context, NULL);
state, size); /* g_object_get returns a refed object */
if (context)
if (icon) g_object_unref (context);
{ }
g_object_ref (icon); else if (widget)
return icon; {
} context = gtk_widget_get_style_context (widget);
} }
if (!context)
return render_fallback_image (context, direction, state, size);
icon = find_and_render_icon_source (icon_set, style, direction, state, size, gtk_style_context_save (context);
widget, detail);
if (icon == NULL) switch (state)
icon = render_fallback_image (style, direction, state, size, widget, detail); {
case GTK_STATE_PRELIGHT:
flags |= GTK_STATE_FLAG_PRELIGHT;
break;
case GTK_STATE_INSENSITIVE:
flags |= GTK_STATE_FLAG_INSENSITIVE;
break;
default:
break;
}
gtk_style_context_set_state (context, flags);
gtk_style_context_set_direction (context, direction);
if (detail == NULL) icon = gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
add_to_cache (icon_set, style, direction, state, size, icon);
gtk_style_context_restore (context);
return icon; return icon;
} }
...@@ -2376,7 +2423,7 @@ struct _CachedIcon ...@@ -2376,7 +2423,7 @@ struct _CachedIcon
/* These must all match to use the cached pixbuf. /* These must all match to use the cached pixbuf.
* If any don't match, we must re-render the pixbuf. * If any don't match, we must re-render the pixbuf.
*/ */
GtkStyle *style; GtkStyleContext *style;
GtkTextDirection direction; GtkTextDirection direction;
GtkStateType state; GtkStateType state;
GtkIconSize size; GtkIconSize size;
...@@ -2398,16 +2445,14 @@ static void ...@@ -2398,16 +2445,14 @@ static void
cached_icon_free (CachedIcon *icon) cached_icon_free (CachedIcon *icon)
{ {
g_object_unref (icon->pixbuf); g_object_unref (icon->pixbuf);
g_object_unref (icon->style);
if (icon->style)
g_object_unref (icon->style);
g_free (icon); g_free (icon);
} }
static GdkPixbuf * static GdkPixbuf *
find_in_cache (GtkIconSet *icon_set, find_in_cache (GtkIconSet *icon_set,
GtkStyle *style, GtkStyleContext *style_context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size) GtkIconSize size)
...@@ -2423,7 +2468,7 @@ find_in_cache (GtkIconSet *icon_set, ...@@ -2423,7 +2468,7 @@ find_in_cache (GtkIconSet *icon_set,
{ {
CachedIcon *icon = tmp_list->data; CachedIcon *icon = tmp_list->data;
if (icon->style == style && if (icon->style == style_context &&
icon->direction == direction && icon->direction == direction &&
icon->state == state && icon->state == state &&
(size == (GtkIconSize)-1 || icon->size == size)) (size == (GtkIconSize)-1 || icon->size == size))
...@@ -2448,7 +2493,7 @@ find_in_cache (GtkIconSet *icon_set, ...@@ -2448,7 +2493,7 @@ find_in_cache (GtkIconSet *icon_set,
static void static void
add_to_cache (GtkIconSet *icon_set, add_to_cache (GtkIconSet *icon_set,
GtkStyle *style, GtkStyleContext *style_context,
GtkTextDirection direction, GtkTextDirection direction,
GtkStateType state, GtkStateType state,
GtkIconSize size, GtkIconSize size,
...@@ -2460,26 +2505,16 @@ add_to_cache (GtkIconSet *icon_set, ...@@ -2460,26 +2505,16 @@ add_to_cache (GtkIconSet *icon_set,
g_object_ref (pixbuf); g_object_ref (pixbuf);
/* We have to ref the style, since if the style was finalized
* its address could be reused by another style, creating a
* really weird bug
*/
if (style)
g_object_ref (style);
icon = g_new (CachedIcon, 1); icon = g_new (CachedIcon, 1);
icon_set->cache = g_slist_prepend (icon_set->cache, icon); icon_set->cache = g_slist_prepend (icon_set->cache, icon);
icon_set->cache_size++; icon_set->cache_size++;
icon->style = style; icon->style = g_object_ref (style_context);
icon->direction = direction; icon->direction = direction;
icon->state = state; icon->state = state;
icon->size = size; icon->size = size;
icon->pixbuf = pixbuf; icon->pixbuf = pixbuf;
attach_to_style (icon_set, icon->style);
if (icon->style)
attach_to_style (icon_set, icon->style);
if (icon_set->cache_size >= NUM_CACHED_ICONS) if (icon_set->cache_size >= NUM_CACHED_ICONS)
{ {
...@@ -2512,7 +2547,7 @@ clear_cache (GtkIconSet *icon_set, ...@@ -2512,7 +2547,7 @@ clear_cache (GtkIconSet *icon_set,
gboolean style_detach) gboolean style_detach)
{ {
GSList *cache, *tmp_list; GSList *cache, *tmp_list;
GtkStyle *last_style = NULL; GtkStyleContext *last_style = NULL;
cache = icon_set->cache; cache = icon_set->cache;
icon_set->cache = NULL; icon_set->cache = NULL;
...@@ -2561,11 +2596,8 @@ copy_cache (GtkIconSet *icon_set, ...@@ -2561,11 +2596,8 @@ copy_cache (GtkIconSet *icon_set,
*icon_copy = *icon; *icon_copy = *icon;
if (icon_copy->style) attach_to_style (copy_recipient, icon_copy->style);
{ g_object_ref (icon_copy->style);
attach_to_style (copy_recipient, icon_copy->style);
g_object_ref (icon_copy->style);
}
g_object_ref (icon_copy->pixbuf); g_object_ref (icon_copy->pixbuf);
...@@ -2580,18 +2612,18 @@ copy_cache (GtkIconSet *icon_set, ...@@ -2580,18 +2612,18 @@ copy_cache (GtkIconSet *icon_set,
} }
static void static void
attach_to_style (GtkIconSet *icon_set, attach_to_style (GtkIconSet *icon_set,
GtkStyle *style) GtkStyleContext *style_context)
{ {
GHashTable *table; GHashTable *table;
table = g_object_get_qdata (G_OBJECT (style), table = g_object_get_qdata (G_OBJECT (style_context),
g_quark_try_string ("gtk-style-icon-sets")); g_quark_try_string ("gtk-style-icon-sets"));
if (table == NULL) if (table == NULL)
{ {
table = g_hash_table_new (NULL, NULL); table = g_hash_table_new (NULL, NULL);
g_object_set_qdata_full (G_OBJECT (style), g_object_set_qdata_full (G_OBJECT (style_context),
g_quark_from_static_string ("gtk-style-icon-sets"), g_quark_from_static_string ("gtk-style-icon-sets"),
table, table,
style_dnotify); style_dnotify);
...@@ -2601,12 +2633,12 @@ attach_to_style (GtkIconSet *icon_set, ...@@ -2601,12 +2633,12 @@ attach_to_style (GtkIconSet *icon_set,
} }
static void static void
detach_from_style (GtkIconSet *icon_set, detach_from_style (GtkIconSet *icon_set,
GtkStyle *style) GtkStyleContext *style_context)
{ {
GHashTable *table; GHashTable *table;
table = g_object_get_qdata (G_OBJECT (style), table = g_object_get_qdata (G_OBJECT (style_context),
g_quark_try_string ("gtk-style-icon-sets")); g_quark_try_string ("gtk-style-icon-sets"));
if (table != NULL) if (table != NULL)
......
...@@ -132,7 +132,6 @@ GdkPixbuf* gtk_icon_set_render_icon (GtkIconSet *icon_set, ...@@ -132,7 +132,6 @@ GdkPixbuf* gtk_icon_set_render_icon (GtkIconSet *icon_set,
GtkWidget *widget, GtkWidget *widget,
const char *detail); const char *detail);
void gtk_icon_set_add_source (GtkIconSet *icon_set, void gtk_icon_set_add_source (GtkIconSet *icon_set,
const GtkIconSource *source); const GtkIconSource *source);
......
...@@ -399,6 +399,9 @@ void gtk_style_context_get_style (GtkStyleContext *context, ...@@ -399,6 +399,9 @@ void gtk_style_context_get_style (GtkStyleContext *context,
GtkIconSet * gtk_style_context_lookup_icon_set (GtkStyleContext *context, GtkIconSet * gtk_style_context_lookup_icon_set (GtkStyleContext *context,
const gchar *stock_id); const gchar *stock_id);
GdkPixbuf * gtk_icon_set_render_icon_pixbuf (GtkIconSet *icon_set,
GtkStyleContext *context,
GtkIconSize size);
void gtk_style_context_set_screen (GtkStyleContext *context, void gtk_style_context_set_screen (GtkStyleContext *context,
GdkScreen *screen); GdkScreen *screen);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment